<div class="breadcrumb">
    <h1>PMS Patients</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>PMS Patients list</li>
    </ul>
</div>

<div class="separator-breadcrumb border-top"></div>

<form action="" method="post" id="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">
                        <label for="pmspatient_id">PMS PatientMasterId</label>
                        <input type="text" class="form-control" name="pmspatient_id" id="pmspatient_id" />
                    </div>

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

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

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

                    <div class="col-md-2  mt-3 mt-md-0">
                        <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('c.clinic_id','c.clinic_name');
                                $data    = array('fields' => $fields);
                                $clinicDetails = $clinicObj->getAllRecords($data);
                            ?>
                            
                            <?php if($clinicDetails && count($clinicDetails) > 0){ 
                                  foreach($clinicDetails as $clinicdata){ ?>
                                <option value="<?php echo $clinicdata->clinic_id; ?>"><?php echo $clinicdata->clinic_name; ?></option>
                                <?php } } ?>
                        </select>
                    </div>

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

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

                    <div class="col-md-2  mt-3 mt-md-0">
                        <br/>
                        <label for="pmspatient_gender">Gender</label>
                        <select class="form-control" id="pmspatient_gender" name="pmspatient_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">
                        <br/>
                        <label for="pmspatient_phone">Phone</label>
                        <input type="text" class="form-control" name="pmspatient_phone" id="pmspatient_phone" />
                    </div>


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

                    <div class="col-md-2  mt-3 mt-md-0">
                        <br/>
                        <label for="patient_xcare_id">Patient XcareId</label>
                        <input type="text" class="form-control" name="patient_xcare_id" id="patient_xcare_id" placeholder="IS NULL" />
                    </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="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>

<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="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="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>PatientMaster Id</th>
                                <th>Name</th>
                                <th>SikkaId/OfficeId</th>
                                <th>DOB / Gender/Relationship</th>
                                <th>Email</th>
                                <th>Phone</th>
                                <th>Insurance</th>
                                <th>Details</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: 160px !important;
}
</style>

<script type="application/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");                  
        }
    });

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

     $('#list').DataTable( { 
          "ajax": "pmsPatientlist.php?act=load",
          "processing": true,
          "serverSide": true,
          "pageLength": 25,
                "aaSorting": [],
                "aoColumnDefs": [
                    { 'bSortable': false, 'aTargets': [0] },
                    { 'bSortable': true, 'aTargets': [1] },
                    { 'bSortable': true, 'aTargets': [2] },
                    { 'bSortable': false, 'aTargets': [3] },
                    { 'bSortable': true, 'aTargets': [4] },
                    { 'bSortable': false, 'aTargets': [5] },
                    { 'bSortable': false, 'aTargets': [6] },
                    { 'bSortable': false, 'aTargets': [7] },
                ],
          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 practice_id = $("#practice_id").val();
        var sikka_id = $("#sikka_id").val();
        var email = $("#email").val();
        var fullname = $("#fullname").val();
        var clinic_id = $("#clinic_id").val();
        var guarantor_id = $("#guarantor_id").val();
        var patient_xcare_id = $("#patient_xcare_id").val();
        var pmspatient_gender = $("#pmspatient_gender").val();
        var pmspatient_phone = $("#pmspatient_phone").val();
        var pmspatient_dob = $("#pmspatient_dob").val();
        var pmspatient_id = $("#pmspatient_id").val();
        
        $('#list').DataTable().destroy();
          $('#list').DataTable( { 
            "ajax": "pmsPatientlist.php?act=load&practice_id="+practice_id+"&sikka_id="+sikka_id+"&email="+email+"&fullname="+fullname+"&clinic_id="+clinic_id+"&guarantor_id="+guarantor_id+"&patient_xcare_id="+patient_xcare_id+"&pmspatient_gender="+pmspatient_gender+"&pmspatient_phone="+pmspatient_phone+"&pmspatient_dob="+pmspatient_dob+"&pmspatient_id="+pmspatient_id,
            "processing": true,
            "serverSide": true,
            "pageLength": 25,
                  "aaSorting": [],
                  "aoColumnDefs": [
                        { 'bSortable': false, 'aTargets': [0] },
                        { 'bSortable': true, 'aTargets':  [1] },
                        { 'bSortable': true, 'aTargets':  [2] },
                        { 'bSortable': false, 'aTargets': [3] },
                        { 'bSortable': true, 'aTargets':  [4] },
                        { 'bSortable': false, 'aTargets': [5] },
                        { 'bSortable': false, 'aTargets': [6] },
                        { 'bSortable': false, 'aTargets': [7] },
                  ],
            language: {
                "processing" : "<div class='loader-bubble loader-bubble-primary m-5'></div>",
                "sEmptyTable" :  "No records found."
            }
            
        } );

      });
});

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';
    }

    //console.log(act);return false;     
    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();
        swal("Success!", "Successfully Deleted Data!", "success");
    }, function (dismiss) {
      return false; 
    });
}
</script>