<?php
 $type =  $_REQUEST['type'];
  if($type == 'requested')
  {
    $appointment_data = 'Requested';
    $dependantId = '0';
  }else if($type == 'scheduled')
  {
    $appointment_data = 'Scheduled';
    $dependantId = '0';
  }else if($type == 'completed')
  {
    $appointment_data = 'Completed';
    $dependantId = '0';
  }else if($type == 'cancelled')
  {
    $appointment_data = 'Cancelled';
    $dependantId = '0';
  }else if($type == 'guestappointment')
  {
    $appointment_data = 'Guest appointment';
    $dependantId = '0';
  }else if($type == 'all_data')
  {
    $appointment_data = 'Appointment';
    $dependantId = '0';
  }else if($type == 'dependantdata')
  {
     $appointment_data = 'Appointment';
     $dependantId = $_REQUEST['dependants_id'];
  }else if($type == 'floating')
  {
    $appointment_data = 'Floating';
    $dependantId = '0';
  }else if($type == 'confirmed')
  {
    $appointment_data = 'Confirmed';
    $dependantId = '0';
  }else{
    $dependantId = '1';
  }
  
?>
<div class="breadcrumb">
    <h1><?php echo $appointment_data; ?></h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li><?php echo $appointment_data; ?> list</li>
    </ul>
</div>
<div class="separator-breadcrumb border-top"></div>

<div class="widget-body">
    <div class="portlet-body">
                        
    <?php echo (isset($_REQUEST['successMsg']) && $_REQUEST['successMsg']!='') ? "<div class='alert alert-success'><button data-dismiss='alert' class='close' type='button'>×</button>".$_REQUEST['successMsg']."</div>" : '' ; ?>
    <?php echo (isset($_REQUEST['failedMsg']) && $_REQUEST['failedMsg']!='') ? "<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>".$_REQUEST['failedMsg']."</div>" : '' ?>
   
    <div id="status_msg" style="display: none;"><div class="alert alert-success"><button data-dismiss='alert' class='close' type='button'>×</button>&nbsp;</div></div>
    <div id="alert_msg" style="display: none;"><div class="alert alert-danger"><button data-dismiss='alert' class='close' type='button'>×</button>&nbsp;</div></div>

    </div>
</div>

<form action="" method="post">
<div class="row">
    <div class="col-lg-12 col-md-12">
        <div class="card mb-5">
            <div class="card-body">
                <div class="row row-xs">
                    
                    <div class="col-md-2 mt-3 mt-md-0"  id="div_appointment_id">
                        <label for="appointment_id ">Appointment Id</label>
                        <input type="text" class="form-control" name="appointment_id" id="appointment_id"  />
                    </div>

                    <div class="col-md-2 mt-3 mt-md-0"  id="div_unique_id">
                        <label for="unique_id ">External Id</label>
                        <input type="text" class="form-control" name="unique_id" id="unique_id"  />
                    </div>

                    <!-- <div class="col-md-2 mt-3 mt-md-0"  id="div_patient_name">
                        <label for="patient_name ">Patient Name</label>
                        <input type="text" class="form-control" name="patient_name" id="patient_name"  />
                    </div> -->

                    <div class="col-md-2 mt-3 mt-md-0"  id="div_patient_firstname">
                        <label for="patient_firstname">First Name</label>
                        <input type="text" class="form-control" name="patient_firstname" id="patient_firstname"  />
                    </div>


                    <div class="col-md-2 mt-3 mt-md-0"  id="div_patient_lastname">
                        <label for="patient_lastname">Last Name</label>
                        <input type="text" class="form-control" name="patient_lastname" id="patient_lastname"  />
                    </div>

                    <div class="col-md-2  mt-3 mt-md-0" id="div_clinic_id">
                        <label for="clinic_id">Clinic</label>
                        <select class="form-control" id="clinic_id" name="clinic_id">
                            <option value="">Select Clinic</option>
                            <?php
                                  $clinicobj = new clinic;
                                  $fields = array('clinic_id,clinic_name');
                                  $clinicArg = array("fields"=>$fields);
                                  $clinicdata = $clinicobj->getAllRecords($clinicArg);
                                  foreach ($clinicdata as $ckey => $clinic_value) 
                                    { 
                                      ?>
                                        <option value="<?php echo $clinic_value->clinic_id; ?>"><?php echo $clinic_value->clinic_name; ?></option>
                              <?php }
                            ?>

                        </select>
                    </div>
                    <?php if($type != 'guestappointment') { ?>
                    <div class="col-md-2  mt-3 mt-md-0" id="div_dependant_id">
                        <label for="dependant_id">Dependant</label>
                        <select class="form-control" id="dependant_id" name="dependant_id">
                            <option value="">Select Dependant</option>
                            <?php
                                  $dependantobj = new dependant;
                                  $fields = array('dependant_id,dependant_fname,dependant_lname');
                                  $dependantArg = array("fields"=>$fields);
                                  $dependantdata = $dependantobj->getAllRecords($dependantArg);
                                  foreach ($dependantdata as $dkey => $dependant_value) 
                                    { 
                                      $dependantfullname = w::d($dependant_value->dependant_fname)." ".w::d($dependant_value->dependant_lname);
                                      ?>
                                        <option value="<?php echo $dependant_value->dependant_id; ?>"><?php echo $dependantfullname; ?></option>
                              <?php }
                            ?>

                        </select>
                    </div>
                    <?php } ?>
                    <div class="col-md-2" id="div_all_email">
                      <?php if($type != 'guestappointment') { ?>
                        <br/>
                      <?php } ?>
                      <label for="all_email">Email</label>
                      <input type="text" class="form-control" id="all_email" name="all_email" >

                    </div>

                    <div class="col-md-2" id="div_all_phone">
                      <?php //if($type != 'guestappointment') { ?>  
                      <br/>
                      <?php //} ?>
                      <label for="all_phone">Phone</label>
                      <input type="text" class="form-control" id="all_phone" name="all_phone" >

                    </div>
                    <?php if($type != 'guestappointment') { ?>
                    <div class="col-md-2" id="div_all_website">
                      <br/>
                      <label for="all_website">Website</label>
                      <input type="text" class="form-control" id="all_website" name="all_website" >
                    </div>
                    <?php } ?>

                    <div class="col-md-2" id="div_st_date">
                      <br/>
                      <label for="start_date">Start Date</label>
                      <input type="date" class="form-control" id="start_date" name="start_date" >

                    </div>

                    <div class="col-md-2" id="div_end_date" >
                      <br/>
                      <label for="end_date">End Date</label>
                      <input type="date" class="form-control" id="end_date" name="end_date" >
                    </div>

                    <div class="col-md-2" id="div_start_time" >
                      <br/>
                      <label for="start_time">Start time</label>
                      <input type="time" class="form-control" id="start_time" name="start_time" >
                    </div>

                    <div class="col-md-2" id="div_end_time" >
                        <br/>
                      <label for="end_time">End time</label>
                      <input type="time" class="form-control" id="end_time" name="end_time" >
                    </div>
                    <?php if($type != 'guestappointment') { ?>
                    <?php if($type == 'all_data'){ ?>
                    <div class="col-md-2  mt-3 mt-md-0">
                        <br/>
                        <label for="appointment_status">Xcare Status</label>
                        <select class="form-control" id="appointment_status" name="appointment_status">
                            <option value="">Select Status</option>
                            
                            <option value="requested">Requested</option>
                            <option value="cancelled">Cancelled</option>
                            <option value="completed">Completed</option>
                            <option value="scheduled">Scheduled</option>
                            <option value="draft">Draft</option>
                          
                        </select>
                    </div>
                  <?php } } ?>

                    <?php if($type != 'guestappointment') { ?>
                    <div class="col-md-2  mt-3 mt-md-0">
                        <br/>
                        <label for="appointment_pmsstatus">PMS Status</label>
                        <select class="form-control" id="appointment_pmsstatus" name="appointment_pmsstatus">
                            <option value="">Select Status</option>
                          
                        </select>
                    </div>
                  <?php }  ?>
                  <?php if($type != 'guestappointment') { ?>
                    <?php if($type != 'floating' && $type != 'confirmed') { ?>
                    <div class="col-md-2  mt-3 mt-md-0">
                        <br/>
                        <label for="internal_status">Internal Status</label>
                        <select class="form-control" id="internal_status" name="internal_status">
                            <option value="">Select Status</option>
                            <option value="confirmed">Confirmed</option>
                            <option value="floating">Floating</option>
                          
                        </select>
                    </div>
                  <?php } } ?>
                    <div class="col-md-2 mt-3 mt-md-0">
                       <br/>
                        <label for="">&nbsp;</label>
                        <button type="submit" name="submit" id="submit" class="btn btn-primary btn-block">Search</button>
                    </div>
                    <div class="col-md-2 mt-3 mt-md-0">
                       <br/>
                        <label for="">&nbsp;</label>
                        <button type="reset" name="reset" class="btn btn-primary btn-block" onClick="window.location.reload()">Reset</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</form>

<div class="row mb-4">
    <div class="col-md-12 mb-4">
        <div class="card text-left">

            <div class="card-body">
                <div class="table-responsive">
                  <form id="listFrm" name="listFrm" action="" method="post">       
                    <input type="hidden" id="action" name="action" value="">
                    <input type="hidden" id="action_type" name="action_type" value="<?php echo $type ?>">
                    <table id="list" class="display table table-striped table-bordered" style="width:100%">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <!-- <th>UniqueId</th> -->
                                <th>Clinic/Provider Detail</th>
                                <!-- <th>Doctor Detail</th> -->
                                <?php if($type == 'guestappointment') { ?>
                                    <th>Patient Detail</th>
                                <?php } ?>
                                <?php if($type != 'guestappointment') { ?>
                                <!-- <th>Patient Detail</th> -->
                                <th>Patient Detail</th>
                                <th>Guarantor Detail</th>
                                <?php } ?>
                                <?php if($type != 'requested') { ?>  <th>Appointment Date</th>  <?php } ?>
                                <!-- <th>AppointmentType Name</th> -->
                                <?php if($type != 'guestappointment') { ?>
                                 <th>Status</th>   
                                <th>Action</th>
                                <?php } ?>
                            </tr>
                        </thead>
                    </table>
                  </form>
                </div>
            </div>
        </div>
    </div>
</div>

<link href="./assets/styles/css/select2.min.css" rel="stylesheet" />
<script src="./assets/js/select2.min.js"></script>

<style type="text/css">
.select2 {
  width: 150px !important;
}
</style>

<script type="application/javascript">
   $(document).ready(function() {

    $("#dependant_id").select2({
        tags: true
    });

    $("#clinic_id").select2({
        tags: true
    });

    $("#clinic_id").on("change",function(){

        var clinic_id = $("#clinic_id").val();
        $.ajax({
                  url: "all_app_data.php?act=getpmsstatus", 
                  type: "POST",
                  data: ({clinic_id: clinic_id}),
                  success: function(data)
                  {  
                    $("#appointment_pmsstatus").html(data);
                  }
             });
    })

   	var type_action = $("#action_type").val();

    if(type_action == 'dependantdata')
    {
        $('#list').DataTable( { 
          "ajax": "all_app_data.php?act=load&app_type="+type_action+"&dependants_id="+<?php echo $dependantId ?>,
          "processing": true,
          "serverSide": true,
          "pageLength": 25,
                "aaSorting": [],
                "aoColumnDefs": [
                    <?php if($type != 'guestappointment') { ?>
                        //4,5,6
                    { 'bSortable': false, 'aTargets': [0] },
                    { 'bSortable': false, 'aTargets': [1] },
                    { 'bSortable': false, 'aTargets': [2] },
                    { 'bSortable': false, 'aTargets': [3] },
                    { 'bSortable': false, 'aTargets': [4] },
                    { 'bSortable': false, 'aTargets': [5] },
                    { 'bSortable': false, 'aTargets': [6] },
                <?php } ?>
                ],
          language: {
              "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
              "sEmptyTable" :  "No records found."
          }
          
      } );
    }
    else{
            
       $('#list').DataTable( { 
          "ajax": "all_app_data.php?act=load&app_type="+type_action,
          "processing": true,
          "serverSide": true,
          "pageLength": 25,
                "aaSorting": [],
                "aoColumnDefs": [
                    <?php if($type != 'guestappointment') { ?>
                        //4,5,6
                    { 'bSortable': false, 'aTargets': [0] },
                    { 'bSortable': false, 'aTargets': [1] },
                    { 'bSortable': false, 'aTargets': [2] },
                    { 'bSortable': false, 'aTargets': [3] },
                    { 'bSortable': false, 'aTargets': [4] },
                    { 'bSortable': false, 'aTargets': [5] },
                   // { 'bSortable': false, 'aTargets': [6] },
                <?php } ?>
                ],
          language: {
              "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
              "sEmptyTable" :  "No records found."
          }
          
      } );
    }
     /*$('#list').DataTable( { 
          //"ajax": "all_app_data.php?act=load&app_type="+type_action,
          ajxcall,
          "processing": true,
          "serverSide": true,
          "pageLength": 25,
                "aaSorting": [],
                "aoColumnDefs": [
                    <?php //if($type != 'guestappointment') { ?>
                        //4,5,6
                    { 'bSortable': false, 'aTargets': [0] }
                <?php //} ?>
                ],
          language: {
              "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
              "sEmptyTable" :  "No records found."
          }
          
      } );*/

        /*var appointment_id1 = $("#appointment_id").val();
        console.log(appointment_id1);
        if(appointment_id1 != '')
        {
            $("#submit").trigger("click");
            //return false;    
        }*/

     if(type_action == 'dependantdata')
     {
            
            $( "form" ).on( "submit", function(e) {
            e.preventDefault();
            
            var unique_id = $("#unique_id").val();
            var clinic_id = $("#clinic_id").val();
            var dependant_id = $("#dependant_id").val();
            var start_date = $("#start_date").val();
            var end_date = $("#end_date").val();
            var start_time = $("#start_time").val();
            var end_time = $("#end_time").val();
            var all_email = $("#all_email").val();
            var all_phone = $("#all_phone").val();
            var all_website = $("#all_website").val();
            var patient_firstname = $("#patient_firstname").val();
            var patient_lastname = $("#patient_lastname").val();
            //var patient_name = $("#patient_name").val();
            //var dependant_name = $("#dependant_name").val();
            var appointment_id = $("#appointment_id").val();
            var internal_status = $("#internal_status").val();

            if(type_action == 'all_data')
            { 
                var appointment_status = $('#appointment_status').val();
            }else{
                var appointment_status = '';
            }
            var appointment_pmsstatus = $("#appointment_pmsstatus").val();

            if(end_date ==='' && start_date !== '')
            {
                $("#alert_msg").show();
                $("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>Please select End Date.</div>");
                 return false;
            }else if( start_date > end_date)
            {
                $("#alert_msg").show();
                $("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>End date should be greater than Start date.</div>");
                 return false;
            }
            else if(end_time ==='' && start_time !== '')
            {
                $("#alert_msg").show();
                $("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>Please select End Time.</div>");
                 return false;
            }else if( start_time > end_time)
            {
                $("#alert_msg").show();
                $("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>End time should be greater than Start time.</div>");
                 return false;
            }

            $('#list').DataTable().destroy();
              $('#list').DataTable( { 
                "ajax": "all_app_data.php?act=load&app_type="+type_action+"&unique_id="+unique_id+"&clinic_id="+clinic_id+"&dependant_id="+dependant_id+"&start_date="+start_date+"&end_date="+end_date+"&start_time="+start_time+"&end_time="+end_time+"&appointment_status="+appointment_status+"&all_email="+all_email+"&all_phone="+all_phone+"&all_website="+all_website+"&firstname="+patient_firstname+"&lastname="+patient_lastname+"&appointment_id="+appointment_id+"&dependants_id="+<?php echo $dependantId ?>+"&appointment_pmsstatus="+appointment_pmsstatus+"&internal_status="+internal_status,
                "processing": true,
                "serverSide": true,
                "pageLength": 25,
                      "aaSorting": [],
                      "aoColumnDefs": [
                          <?php if($type != 'guestappointment') { ?>
                            { 'bSortable': false, 'aTargets': [0] },
                            { 'bSortable': false, 'aTargets': [1] },
                            { 'bSortable': false, 'aTargets': [2] },
                            { 'bSortable': false, 'aTargets': [3] },
                            { 'bSortable': false, 'aTargets': [4] },
                            { 'bSortable': false, 'aTargets': [5] },
                            //{ 'bSortable': false, 'aTargets': [6] },
                      <?php } ?>
                      ],
                language: {
                    "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
                    "sEmptyTable" :  "No records found."
                }
                
            } );
          });
     }else{
            
            
            $( "form" ).on( "submit", function(e) {
            e.preventDefault();
            
            var unique_id = $("#unique_id").val();
            var clinic_id = $("#clinic_id").val();
            var dependant_id = $("#dependant_id").val();
            var start_date = $("#start_date").val();
            var end_date = $("#end_date").val();
            var start_time = $("#start_time").val();
            var end_time = $("#end_time").val();
            var all_email = $("#all_email").val();
            var all_phone = $("#all_phone").val();
            var all_website = $("#all_website").val();
            //var patient_name = $("#patient_name").val();
            var patient_firstname = $("#patient_firstname").val();
            var patient_lastname = $("#patient_lastname").val();
            //var dependant_name = $("#dependant_name").val();
            var appointment_id = $("#appointment_id").val();
            var internal_status = $("#internal_status").val();

            if(type_action == 'all_data')
            { 
                var appointment_status = $('#appointment_status').val();
            }else{
                var appointment_status = '';
            }
            var appointment_pmsstatus = $("#appointment_pmsstatus").val();
            
            if(end_date ==='' && start_date !== '')
            {
                $("#alert_msg").show();
                $("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>Please select End Date.</div>");
                 return false;
            }else if( start_date > end_date)
            {
                $("#alert_msg").show();
                $("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>End date should be greater than Start date.</div>");
                 return false;
            }
            else if(end_time ==='' && start_time !== '')
            {
                $("#alert_msg").show();
                $("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>Please select End Time.</div>");
                 return false;
            }else if( start_time > end_time)
            {
                $("#alert_msg").show();
                $("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>End time should be greater than Start time.</div>");
                 return false;
            }

            $('#list').DataTable().destroy();
              $('#list').DataTable( { 
                "ajax": "all_app_data.php?act=load&app_type="+type_action+"&unique_id="+unique_id+"&clinic_id="+clinic_id+"&dependant_id="+dependant_id+"&start_date="+start_date+"&end_date="+end_date+"&start_time="+start_time+"&end_time="+end_time+"&appointment_status="+appointment_status+"&all_email="+all_email+"&all_phone="+all_phone+"&all_website="+all_website+"&firstname="+patient_firstname+"&lastname="+patient_lastname+"&appointment_id="+appointment_id+"&appointment_pmsstatus="+appointment_pmsstatus+"&internal_status="+internal_status,
                "processing": true,
                "serverSide": true,
                "pageLength": 25,
                      "aaSorting": [],
                      "aoColumnDefs": [
                          <?php if($type != 'guestappointment') { ?>
                            { 'bSortable': false, 'aTargets': [0] },
                            { 'bSortable': false, 'aTargets': [1] },
                            { 'bSortable': false, 'aTargets': [2] },
                            { 'bSortable': false, 'aTargets': [3] },
                            { 'bSortable': false, 'aTargets': [4] },
                            { 'bSortable': false, 'aTargets': [5] },
                            //{ 'bSortable': false, 'aTargets': [6] },
                      <?php } ?>
                      ],
                language: {
                    "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
                    "sEmptyTable" :  "No records found."
                }
                
            } );
          });
     } 
     
    });
</script>