<?php $clinic_ids =  $_GET['clinic_id'];
 ?>
<div class="breadcrumb">
    <h1>Appointment Log</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Appointment Log list</li>
    </ul>
</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_clinic_id">
                        <label for="clinic_id">ClinicId</label>
                        <select class="form-control" id="clinic_id" name="clinic_id" required="true">
                            
                            <option value="0">Select Clinic</option>
                            <?php
                                 if($_GET['clinic_id'] == '')
                                 {   

                                      $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 } 
                                }else{ 
                                    
                                      $clinicobj = new clinic;
                                      $fields = array('clinic_id,clinic_name');
                                      $where = array("clinic_id="=>$clinic_ids);
                                      $clinicArg = array("fields"=>$fields,"where"=>$where);
                                      $clinicdatas = $clinicobj->fromFields($clinicArg); 
                                      ?>
                                      <option value="<?php echo $clinicdatas->clinic_id; ?>" <?php echo ($clinicdatas->clinic_id==$clinic_ids)?"selected='selected'":""  ?>  ><?php echo $clinicdatas->clinic_name; ?></option>
                                <?php } ?>
                            

                        </select>
                    </div>

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

                    <div class="col-md-2 mt-3 mt-md-0"  id="div_external_appointment_id">
                        <label for="external_appointment_id">External Id</label>
                        <input type="text" class="form-control" name="external_appointment_id" id="external_appointment_id" />
                    </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_search_loglist_date">
                        <label for="search_loglist_date">Log Date</label>
                        <input type="date" class="form-control" name="search_loglist_date" id="search_loglist_date" />
                        <div class="invalid-tooltip" id="msg_search_loglist_date"></div>
                    </div>

                    <div class="col-md-2 mt-3 mt-md-0"  id="div_search_loglist_time">
                        <label for="search_loglist_time">Log Time</label>
                        <input type="time" class="form-control" name="search_loglist_time" id="search_loglist_time" />
                        <div class="invalid-tooltip" id="msg_search_loglist_time"></div>
                    </div>

                    <div class="col-md-2 mt-3 mt-md-0"  id="div_search_loglist_apptdate">
                        <label for="search_loglist_apptdate">Appointment Date</label>
                        <input type="date" class="form-control" name="search_loglist_apptdate" id="search_loglist_apptdate" />
                        <div class="invalid-tooltip" id="msg_search_loglist_apptdate"></div>
                    </div>

                    <div class="col-md-2 mt-3 mt-md-0"  id="div_action">
                        <label for="action">Action</label>
                        <select class="form-control" id="action" name="action" required="true">
                            
                            <option value="0">Select Action</option>
                            <?php
                                 
                                      $logobj = new log;
                                      $fields = array('DISTINCT log_type as action');
                                      $logArg = array("fields"=>$fields);
                                      $logdata = $logobj->getAllRecords($logArg);
                                      foreach ($logdata as $lkey => $log_value) 
                                        { 
                                          ?>
                                            <option value="<?php echo $log_value->action; ?>"><?php echo $log_value->action; ?></option>
                                    <?php }  ?>
                            

                        </select>
                    </div>

                    <div class="col-md-2 mt-3 mt-md-0">
                        <label for="">&nbsp;</label>
                        <button type="submit" name="submit" class="btn btn-primary btn-block">Search</button>
                    </div>
                    <div class="col-md-2 mt-3 mt-md-0">
                        <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="separator-breadcrumb border-top"></div>

<div class="row mb-4">
  <div class="col-md-6">
        <!-- <select id="act" name="act" class="form-control maction">
          <option value="">Multiple action</option>              
          <option value="activate_all">Activate selected</option>
          <option value="inactivate_all">De-activate selected</option>
          <option value="delete_all">Delete selected</option>
        </select>
        <button type="button" class="btn btn-primary m-1" onclick="return validate_all();">Apply</button> -->
  </div> 
</div>     
<div class="widget-body">
    <div class="portlet-body">
                        
    <?php echo (isset($successMsg) && $successMsg!='') ? "<div class='alert alert-success'><button data-dismiss='alert' class='close' type='button'>×</button>".base64_decode($successMsg)."</div>" : '' ; ?>
    <?php echo (isset($failedMsg) && $failedMsg!='') ? "<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>".base64_decode($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>

<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="">
                    <table id="list" class="display table table-striped table-bordered" style="width:100%">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <th>Patient Info/Details</th>
                                <!-- <th>Patient Details</th> -->
                                <th>Action</th>
                                <th>Status</th>
                                <!-- <th>Loaded Date</th> -->
                                <th>Date/Time</th>
                                <th>Remark</th>
                            </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>


<?php if ($_GET['clinic_id'] != '')
{  ?>

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

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

    var cliniId = <?php echo $_GET['clinic_id']; ?>;
    var appId = <?php echo $_GET['app_id']; ?>;
    
    if(cliniId != '')
    {
         $('#list').DataTable( { 
              "ajax": "appointmentloglistdata.php?act=load&clinic_id="+cliniId+"&appId="+appId,
              "processing": true,
              "serverSide": true,
              "pageLength": 25,
                    "aaSorting": [],
                    "aoColumnDefs": [
                        { 'bSortable': false, 'aTargets': [0] }
                    ],
              language: {
                  "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
                        "sEmptyTable" :  "No records found."
              }
              
          });
    }

      $( "form" ).on( "submit", function(e) {
        e.preventDefault();
        var cliniId = $("#clinic_id").val();
        
        var appoinmnetId = $("#appoinmnet_id").val();
        //var patient_fullname = $("#patient_name").val();
        var searchloglistdate = $("#search_loglist_date").val();
        var searchloglisttime = $("#search_loglist_time").val();
        var external_appointment_id = ("#external_appointment_id").val();
        var patient_firstname = $("#patient_firstname").val();
        var patient_lastname = $("#patient_lastname").val();
        var searchloglistapptdate = $("#search_loglist_apptdate").val();
        var action = $("#action").val();

        if(searchloglisttime !== '' && searchloglistdate === '')
        {
            $("#search_loglist_date").addClass('error');
            $("#search_loglist_date").focus();
            $("#msg_search_loglist_date").html("Please Select Date").show();
            return false;
        }else{
            $("#search_loglist_date").removeClass('error');
             $("#msg_search_loglist_date").hide();
        }

        $('#list').DataTable().destroy();
          $('#list').DataTable( { 
            "ajax": "appointmentloglistdata.php?act=load&clinic_id="+cliniId+"&log_appointment_id="+appoinmnetId+"&searchloglistdate="+searchloglistdate+"&searchloglistapptdate="+searchloglistapptdate+"&searchloglisttime="+searchloglisttime+"&external_appointment_id="+external_appointment_id+"&firstname="+patient_firstname+"&action="+action+"&lastname="+patient_lastname,
            "processing": true,
            "serverSide": true,
            "pageLength": 25,
                  "aaSorting": [],
                  "aoColumnDefs": [
                      { 'bSortable': false, 'aTargets': [0] }
                  ],
            language: {
                "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
                "sEmptyTable" :  "No records found."
            }
            
        } );

      });
    });
</script>

<?php }else{  ?>

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

    $("#clinic_id").select2({
          tags: true
      });
     /*$('#list').DataTable( { 
          "ajax": "loglistdata.php?act=load",
          "processing": true,
          "serverSide": true,
          "pageLength": 25,
                "aaSorting": [],
                "aoColumnDefs": [
                    { 'bSortable': false, 'aTargets': [0] }
                ],
          language: {
              "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
                    "sEmptyTable" :  "No records found."
          }
          
      });*/

      $( "form" ).on( "submit", function(e) {
        e.preventDefault();
        var cliniId = $("#clinic_id").val();
        var appoinmnetId = $("#appoinmnet_id").val();
        //var patient_fullname = $("#patient_name").val();
        var searchloglistdate = $("#search_loglist_date").val();
        var searchloglisttime = $("#search_loglist_time").val();
        var patient_firstname = $("#patient_firstname").val();
        var patient_lastname = $("#patient_lastname").val();
        var searchloglistapptdate = $("#search_loglist_apptdate").val();
        var action = $("#action").val();

        if(searchloglisttime !== '' && searchloglistdate === '')
        {
            $("#search_loglist_date").addClass('error');
            $("#search_loglist_date").focus();
            $("#msg_search_loglist_date").html("Please Select Date").show();
            return false;
        }else{
            $("#search_loglist_date").removeClass('error');
             $("#msg_search_loglist_date").hide();
        }

        $('#list').DataTable().destroy();
          $('#list').DataTable( { 
            "ajax": "appointmentloglistdata.php?act=load&clinic_id="+cliniId+"&log_appointment_id="+appoinmnetId+"&searchloglistdate="+searchloglistdate+"&searchloglistapptdate="+searchloglistapptdate+"&action="+action+"&searchloglisttime="+searchloglisttime+"&firstname="+patient_firstname+"&lastname="+patient_lastname,
            "processing": true,
            "serverSide": true,
            "pageLength": 25,
                  "aaSorting": [],
                  "aoColumnDefs": [
                      { 'bSortable': false, 'aTargets': [0] }
                  ],
            language: {
                "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
                "sEmptyTable" :  "No records found."
            }
            
        } );

      });
    });
</script>

<?php } ?>