<?php 
include 'header.php';
include('xcrud/xcrud.php');
$xcrud = Xcrud::get_instance();
echo Xcrud::load_css();
$bdd = new DB();
?>
<div class="content-wrapper">
	<section class="content-header"><div class="container-fluid"></div></section>
	<section class="content">
		<div class="container-fluid">
			<div class="col-lg-12">
            <div class="card">
              <div class="card-body">
				<button class="btn btn-success" id="add_appointment">ADD</button>

<div id="add_appointment_form" style="display:none" class="card mt-3">
  <div class="card-body">
    <form id="appointmentForm">

      <h5>Service Details</h5>

      <div class="row">
        <div class="col-md-4">
          <label>Customer Name</label>
          <input type="text" name="customer_name" class="form-control" required>
        </div>

        <div class="col-md-4">
          <label>Mobile No</label>
          <input type="text" name="mobile_no" class="form-control" required>
        </div>
		
		<div class="col-md-4">
          <label>Chassis No</label>
          <input type="text" name="chassis_no" class="form-control" required>
        </div>
		
		<div class="col-md-4">
          <label>Registration No</label>
          <input type="text" name="registration_no" class="form-control">
        </div>

        <div class="col-md-4">
          <label>Product</label>
          <select name="product_id" class="form-control" required>
            <?php
			$product_rows = $bdd->getAll("SELECT id,name FROM products", []);
            foreach ($product_rows as $row) {
              echo "<option value='{$row['id']}'>{$row['name']}</option>";
            }
            ?>
          </select>
        </div>
      </div>

      <hr>

      <h5>Appointment Details</h5>

      <div class="row">
        <div class="col-md-4">
          <label>Service Type</label>
          <select name="service_type" class="form-control" required>
            <?php
            $service_type_rows = $bdd->getAll("SELECT id,name FROM service_type", []);
            foreach ($service_type_rows as $row) {
              echo "<option value='{$row['id']}'>{$row['name']}</option>";
            }
            ?>
          </select>
        </div>

        <div class="col-md-4">
          <label>Date</label>
          <input type="date" name="service_date" class="form-control" required>
        </div>

        <div class="col-md-4">
          <label>Time Slot</label>
          <select name="time_slot" class="form-control" required>
            <?php
            $time_slot_rows = $bdd->getAll("SELECT id,name FROM time_slot", []);
            foreach ($time_slot_rows as $row) {
              echo "<option value='{$row['id']}'>{$row['name']}</option>";
            }
            ?>
          </select>
        </div>
      </div>

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

      <br>
      <button type="submit" class="btn btn-primary">Save</button>
      <button type="button" class="btn btn-secondary" id="cancel_add">Cancel</button>

    </form>
  </div>
</div>

				
                <?php
				$xcrud->table('appointment');
				$xcrud->join('service_id','service','id');
				
				$xcrud->relation('service.product_id','products','id','name');
				$xcrud->relation('service_type','service_type','id','name');
				$xcrud->relation('user_id','users','id','username');
				$xcrud->relation('time_slot','time_slot','id','name');
				
				$xcrud->columns('service.customer_name,service.mobile_no,service.chassis_no,service.registration_no,service.product_id,service_type,service_date,time_slot,user_id',false);

				//$xcrud->fields('service.customer_name,service.mobile_no,service.chassis_no,service.registration_no,service.product_id,service_type,service_date,time_slot,user_id');
				$xcrud->fields('service.customer_name,service.mobile_no,service.chassis_no,service.registration_no,service.product_id,service_type,service_date,time_slot,user_id');

				
				$xcrud->validation_required("service.customer_name,service.mobile_no");
				
				$xcrud->label(array('service.product_id'=>'Product','user_id'=>'User'));
				
				$xcrud->unset_title();
				$xcrud->unset_add();
				
				if($_SESSION['user_type']!='admin'){
					$xcrud->unset_remove();
				}
				echo $xcrud->render();
				?>
              </div>
            </div>
          </div>
		</div>
	</section>
</div>
<?php
$bdd->disconnect();
include 'footer.php';
echo Xcrud::load_js();
?>
<script>
const loader = document.getElementById("loader-overlay");

$(document).ready(function(){

  $('#add_appointment').on('click', function(){
    $('#add_appointment_form').slideToggle();
  });

  $('#cancel_add').on('click', function(){
    $('#add_appointment_form').slideUp();
  });

  $('#appointmentForm').on('submit', function(e){
    e.preventDefault();
	
	if(loader) loader.style.display = "flex";

    let page = $('.xcrud-pagination li.active a').text();
    page = page ? page : 1;

    $.ajax({
      url: 'add_appointment.php',
      type: 'POST',
      data: $(this).serialize(),
      success: function(res){
        if(res.trim() === 'success'){
          location.href = location.pathname;
        } else {
          alert(res);
        }
      },
		complete: function () {
			if(loader) loader.style.display = "none";
		}
    });
  });

});
</script>