<script type="text/javascript">
 
$(document).ready(function(){
    $("#checkall").click(function(){
        if(this.checked){
            $(".check_class").attr("checked",true);					
            $(".check_class").parent().addClass("checked");					
        }else{
            $(".check_class").attr("checked",false);					
            $(".check_class").parent().removeClass("checked");					
        }
    });
    $("#status_msg").hide();
    $("#alert_msg").hide();
});

function delete_entry(patient_id){
  swal({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#0CC27E',
        cancelButtonColor: '#FF586B',
        confirmButtonText: 'Yes, delete it!',
        cancelButtonText: 'No, cancel!',
        confirmButtonClass: 'btn btn-success mr-5',
        cancelButtonClass: 'btn btn-danger',
        buttonsStyling: false
    }).then(function () {
        location.href = 'patientlist.php?act=delete&patient_id='+patient_id;
    }, function (dismiss) {
      return false; 
    });
}
	
function change_status(patient_id,status){

	$(".loading_image").show();
	$.ajax({
	  url: "patientlist.php",
	  data: 'act=change_status&patient_id='+patient_id+'&status='+status,
    dataType: 'json',
    cache: false,
	  success: function(data){
                $("#status_msg").show();
                $("#status_msg").html("<div class='alert alert-success'><button data-dismiss='alert' class='close' type='button'>×</button>Status updated successfully...</div>");
                $("#status_msg_td").html(data.msg);
                hideMessage();
                if(data.pstatus==0){
                    $("#td_status_"+patient_id).html("<a href=\"javascript:void(0)\" onclick=\"change_status('"+patient_id+"','1')\" ><span class=\"badge badge-danger\">Inactive</span></a>");
                }else{
                    $("#td_status_"+patient_id).html("<a href=\"javascript:void(0)\" onclick=\"change_status('"+patient_id+"','0')\" ><span class=\"badge badge-success\">Active</span></a>");
                }
            }
	});
}

function validate_all(act){
    var act = $("#act").val();
    if(act==''){
        $("#alert_msg").show();
        $("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>Please select action to apply on selected records.</div>");
        return false;
    }else if($(".check_class:checked").length == 0){
  	$("#alert_msg").show();
  	$("#alert_msg").html("<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>Please select atleast one checkbox.</div>");
        return false;
    }else{
        $("#alert_msg").hide();
    }
        
  if(act == 'delete_all'){
      var msg = 'delete';
  }else if(act == 'activate_all'){
      var msg = 'activate';
  }else{
      var msg = 'inactivate';
  }
  
    swal({
        title: 'Are you sure to '+act.replace("_"," ")+' ?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#0CC27E',
        cancelButtonColor: '#FF586B',
        confirmButtonText: 'Yes, '+msg+' it!',
        cancelButtonText: 'No, cancel!',
        confirmButtonClass: 'btn btn-success mr-5',
        cancelButtonClass: 'btn btn-danger',
        buttonsStyling: false
    }).then(function () {
        $("#action").val(act);
        $("#listFrm").submit();
    }, function (dismiss) {
      return false; 
    });
}
	
</script>

<?php
$filename=basename($_SERVER['SCRIPT_FILENAME']);
$adminlogObj = new admin_log();

if($filename == 'patientlist.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
}
?>

<div class="breadcrumb">
    <!-- <h1>Patients</h1> -->
    <h1>Patient Sign-in/Guarantor</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <!-- <li>Patient list</li> -->
        <li>Patient Sign-in/Guarantor list</li>
        <li><a href="<?php echo SITE_URL."x-admin/adminloglist.php?module_id=$module_id" ?>" target="_blank">Log</a></li>
    </ul>
</div>
<div class="separator-breadcrumb border-top"></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>

<form action="" method="post" id="searchFrm" name="searchFrm">
<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_patient_id">
                        <!-- <label for="patient_id">Patient Id</label> -->
                        <label for="patient_id">Xcare Patient Id</label>
                        <input type="text" class="form-control" name="patient_id" id="patient_id"  />
                    </div>

                    <div class="col-md-2 mt-3 mt-md-0"  id="div_patient_uniqueid">
                        <label for="patient_uniqueid">External Id</label>
                        <input type="text" class="form-control" name="patient_uniqueid" id="patient_uniqueid"  />
                    </div>
                    
                    <!-- <div class="col-md-2 mt-3 mt-md-0"  id="div_patient_fullname">
                        <label for="patient_fullname">Full Name</label>
                        <input type="text" class="form-control" name="patient_fullname" id="patient_fullname"  />
                    </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_referral_code">
                        <label for="referral_code">Referral Code</label>
                        <input type="text" class="form-control" name="referral_code" id="referral_code"  />
                    </div>

                    <!-- <div class="col-md-2 mt-3 mt-md-0"  id="div_patient_gender">
                        <label for="patient_gender">Gender</label>
                        <select class="form-control" id="patient_gender" name="patient_gender">
                            <option value="">Select Gender</option>
                            <option value="Male">Male</option>
                            <option value="Female">Female</option>
                        </select>
                        
                    </div> -->

                    <div class="col-md-2  mt-3 mt-md-0">
                        <label for="patient_email">Email</label>
                        <input type="text" class="form-control" name="patient_email" id="patient_email" />
                    </div>

                    <div class="col-md-2 mt-3 mt-md-0" id="div_patient_phone" >
                        <br/>
                      <label for="patient_phone">Phone</label>
                      <input type="text" class="form-control" name="patient_phone" id="patient_phone" />
                    </div>


                    <div class="col-md-2  mt-3 mt-md-0" id="div_patient_dob">
                        <br/>
                        <label for="patient_dob">DOB</label>
                        <input type="date" class="form-control" name="patient_dob" id="patient_dob" />
                    </div>

                    <div class="col-md-2 mt-3 mt-md-0">
                        <br/>
                        <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">
                        <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-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 class="col-md-6 pull-right">
      <div class="button-group">
         <!-- <a href="patientadd.php" class="btn btn-dark m-1"><i class="i-Add-User"></i> Add New</a> -->
      </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 class="center"><input type="checkbox" name="checkall" id="checkall"></th>
                                <!-- <th>Unique Id</th> -->
                                <th>Name</th>
                                <!-- <th>Clinic</th> -->
                                <th>Contact</th>
                                <th>Address</th> 
                                <!-- <th>Date</th> -->
                                <!-- <th>Photo</th> -->
                                <!-- <th>Insurance Detail</th> -->
                                <th>Status</th>
                                <!-- <th>Action</th> -->
                            </tr>
                        </thead>
                    </table>
                  </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="application/javascript">
   $(document).ready(function() {
     $('#list').DataTable( { 
          "ajax": "patientlist.php?act=load",
          "processing": true,
          "serverSide": true,
          "pageLength": 25,
                "aaSorting": [],
                "aoColumnDefs": [
                    { 'bSortable': false, 'aTargets': [0,4] }
                ],
          language: {
              "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
              "sEmptyTable" :  "No records found."
          }
          
      } );

      //$( "form" ).on( "submit", function(e) {
        $( "#searchFrm" ).on( "submit", function(e) {
        e.preventDefault();
        
        //var patient_fullname = $("#patient_fullname").val();
        var patient_firstname = $("#patient_firstname").val();
        var patient_lastname = $("#patient_lastname").val();
        var referral_code = $("#referral_code").val();
        //var patient_gender = $("#patient_gender").val();
        var patient_uniqueid = $("#patient_uniqueid").val();
        var patient_phone = $("#patient_phone").val();
        var patient_email = $("#patient_email").val();
        var patient_dob = $("#patient_dob").val();
        var patient_fname = $("#patient_fname").val();
        var patient_lname = $("#patient_lname").val();
        var patient_id = $("#patient_id").val();
        
        $('#list').DataTable().destroy();
          $('#list').DataTable( { 
            "ajax": "patientlist.php?act=load&firstname="+patient_firstname+"&lastname="+patient_lastname+"&referral_code="+referral_code+"&patient_uniqueid="+patient_uniqueid+"&patient_phone="+patient_phone+"&patient_email="+patient_email+"&patient_dob="+patient_dob+"&patient_id="+patient_id,
            "processing": true,
            "serverSide": true,
            "pageLength": 25,
                  "aaSorting": [],
                  "aoColumnDefs": [
                      { 'bSortable': false, 'aTargets': [0,4] }
                  ],
            language: {
                "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
                "sEmptyTable" :  "No records found."
            }
            
        } );

      });
    });
</script>