<?php
date_default_timezone_set('Asia/Colombo');

include '../includes/auth.php';
require_once __DIR__ . '/../config/db.php';
include '../includes/header.php';
include '../includes/navbar.php';
include '../includes/csrf.php';
?>

<div class="container-fluid mt-4">

       <form method="POST" action="save_survey.php">

              <div class="card shadow">

                     <div class="card-header bg-primary text-white">
                     Sabaragamuwa Province School Computer Lab Survey
                     </div>

                     <div class="card-body">

                            <!-- Survey Details -->

                            <div class="row">

                                   <div class="col-md-6">

                                          <label>Date</label>

                                          <input type="text"
                                                 class="form-control"
                                                 value="<?= date('Y-m-d H:i:s') ?>"
                                                 readonly>

                                   </div>

                                   <div class="col-md-6">

                                          <label>Entered By</label>

                                          <input type="text"
                                                 class="form-control"
                                                 value="<?= $_SESSION['fullname'] ?>"
                                                 readonly>

                                   </div>

                                   <input
                                   type="hidden"
                                   name="csrf_token"
                                   value="<?= $_SESSION['csrf_token'] ?>">

                            </div>

                            <hr>

                            <h5>School Details</h5>

                            <div class="row">

                                   <div class="col-md-3">
                                          <label>Census No</label>

                                          <input type="text"
                                                 id="census_no"
                                                 name="census_no"
                                                 class="form-control"
                                                 required>
                                   </div>

                                   <div class="col-md-3">
                                          <label>District</label>

                                          <input type="text"
                                                 id="district"
                                                 name="district"
                                                 class="form-control"
                                                 readonly>
                                   </div>

                                   <div class="col-md-3">
                                          <label>Zone</label>

                                          <input type="text"
                                                 id="zone"
                                                 name="zone_name"
                                                 class="form-control"
                                                 readonly>
                                   </div>

                                   <div class="col-md-3">
                                          <label>Division</label>

                                          <input type="text"
                                                 id="division"
                                                 name="division_name"
                                                 class="form-control"
                                                 readonly>
                                   </div>

                            </div>

                            <div class="row mt-3">

                                   <div class="col-md-6">
                                          <label>School</label>

                                          <input type="text"
                                                 id="school_name"
                                                 name="school_name"
                                                 class="form-control"
                                                 readonly>
                                   </div>

                                   <div class="col-md-3">
                                          <label>National/Provincial</label>

                                          <input type="text"
                                                 id="school_category"
                                                 name="school_category"
                                                 class="form-control"
                                                 readonly>
                                   </div>

                                   <div class="col-md-3">
                                          <label>School Type</label>

                                          <input type="text"
                                                 id="school_type"
                                                 name="school_type"
                                                 class="form-control"
                                                 readonly>
                                   </div>

                            </div>

                            <hr>

                            <h5>Principal Details</h5>

                            <div class="row">

                                   <div class="col-md-4">
                                          <input type="text"
                                          name="principal_name"
                                          placeholder="Principal Name"
                                          class="form-control">
                                   </div>

                                   <div class="col-md-4">
                                          <input type="text"
                                          name="principal_phone"
                                          placeholder="Principal Phone"
                                          class="form-control">
                                   </div>

                                   <div class="col-md-4">
                                          <input type="text"
                                          name="school_phone"
                                          placeholder="School Phone"
                                          class="form-control">
                                   </div>

                            </div>

                            <hr>

                            <h5>IT Teacher Details</h5>

                            <div class="row">

                                   <div class="col-md-6">
                                          <input type="text"
                                          name="it_teacher_name"
                                          placeholder="IT Teacher Name"
                                          class="form-control">
                                   </div>

                                   <div class="col-md-6">
                                          <input type="text"
                                          name="it_teacher_phone"
                                          placeholder="IT Teacher Phone"
                                          class="form-control">
                                   </div>

                            </div>

                            <hr>

                            <h5>Computer Lab Equipment</h5>

                            <div class="table-responsive">

                                   <table class="table table-bordered">

                                          <thead class="table-dark">

                                                 <tr>
                                                        <th>Equipment</th>
                                                        <th>Working</th>
                                                        <th>Not Working</th>
                                                 </tr>

                                          </thead>

                                          <tbody>

                                                 <?php

                                                 $equipment = mysqli_query(
                                                 $conn,
                                                 "SELECT *
                                                 FROM equipment_master
                                                 WHERE status='Active'
                                                 ORDER BY display_order ASC, equipment_name ASC"
                                                 );

                                                 while($row = mysqli_fetch_assoc($equipment))
                                                 {

                                                 $field = strtolower($row['equipment_name']);

                                                 // Convert spaces and special characters to underscores
                                                 $field = preg_replace('/[^a-z0-9]+/', '_', $field);

                                                 ?>

                                                 <tr>

                                                        <td>
                                                               <?= htmlspecialchars($row['equipment_name']) ?>
                                                        </td>

                                                        <td>
                                                               <input
                                                                      type="number"
                                                                      name="working[<?= $row['id'] ?>]"
                                                                      value="0"
                                                                      class="form-control">

                                                        </td>

                                                        <td>
                                                                      <input
                                                                      type="number"
                                                                      name="not_working[<?= $row['id'] ?>]"
                                                                      value="0"
                                                                      class="form-control">

                                                        </td>

                                                 </tr>

                                                 <?php
                                                 }
                                                 ?>

                                          </tbody>

                                   </table>

                            </div>

                            <div class="mt-3">

                                   <button type="reset"
                                          class="btn btn-secondary">
                                          Clear Data
                                   </button>

                                   <button type="submit"
                                          class="btn btn-success">
                                          Submit Survey
                                   </button>

                            </div>

                     </div>

              </div>

       </form>

</div>

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<script>
              $("#census_no").keyup(function(){

              let census_no = $(this).val();

              $.ajax({

              url:"../ajax/get_school.php",

              type:"POST",

              data:{
              census_no:census_no
              },

              success:function(res){

              $("#district").val(res.district);
              $("#zone").val(res.zone_name);
              $("#division").val(res.division_name);

              $("#school_name").val(res.school_name);

              $("#school_category")
              .val(res.school_category);

              $("#school_type")
              .val(res.school_type);

              }

              });

              }); </script>

<?php include '../includes/footer.php'; ?>