<script type="text/javascript">
/*$(document).ready(function(){
	$("#frm-practice").submit(function(e){
            if(!validate_practice()){
                e.preventDefault();
                 $("#pageloader").fadeIn();
            }
	});
});*/
function validate_practice(){
	var practicename = $("#practice_name");var practicenamemsg = $("#msg_practice_name");
	var practice_xcare_id = $("#xcare_id");var xcare_idmsg = $("#msg_xcare_id");
	var pullguarantors = $("#pullguarantors_name");var pullguarantorsmsg = $("#msg_pullguarantors_name");
	var pullPatients = $("#PullPatients_name");var pullPatientsmsg = $("#msg_PullPatients_name");
    var pullAppointments = $("#PullAppointments_name");var pullAppointmentsmsg = $("#msg_PullAppointments_name");
	
    if(practicename.val()==''){practicename.addClass('error');practicename.focus();practicenamemsg.html('Please enter practice name').show();
            return false;
	}else
    {
        practicename.removeClass('error');
        practicenamemsg.hide();
    }
        
    if(practice_xcare_id.val()=='')
    {
        practice_xcare_id.addClass('error');
        practice_xcare_id.focus();
        xcare_idmsg.html('Please select xcareId').show();
        return false;
	}else
    {
        practice_xcare_id.removeClass('error');
        xcare_idmsg.hide();
    }

    if(pullguarantors.val()=='')
    {
          pullguarantors.addClass('error');
          pullguarantors.focus();
          pullguarantorsmsg.html('Please select guarantor').show();
          return false;
    }else
    {   
        pullguarantors.removeClass('error');
        pullguarantorsmsg.hide();
    }

    if(pullPatients.val()=='')
    {
          pullPatients.addClass('error');
          pullPatients.focus();
          pullPatientsmsg.html('Please select patient').show();
          return false;
    }else
    {   
        pullPatients.removeClass('error');
        pullPatientsmsg.hide();
    }

    

    if(pullAppointments.val()=='')
    {
        pullAppointments.addClass('error');
        pullAppointments.focus();
        pullAppointmentsmsg.html('Please select appoinment').show();
        return false;
    }else
    {   
        pullAppointments.removeClass('error');
        pullAppointmentsmsg.hide();
    }
    return true;
}

</script>
<div class="breadcrumb">
    <h1>Practice</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>/practicelist.php">Practice List</a></li>
    </ul>
</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="separator-breadcrumb border-top"></div>
<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">Practice</div>
                <form action="" id='frm-practice' method="post" enctype="multipart/form-data">
                    <?php if(isset($practiceDetail->practice_id) && $practiceDetail->practice_id!=0) { ?>
                       <input type="hidden" id="practice_id" name="practice_id" value="<?php echo $practiceDetail->practice_id ?>" />
                    <?php } ?>
                    <div class="row">
                    	<div class="col-md-6 form-group mb-3">
                            <label for="practice_name">Name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="practice_name" name="practice_name" value="<?php echo $practiceDetail->practice_name; ?>" placeholder="Enter first name">
                            <div class="invalid-tooltip" id="msg_practice_name"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="practice_xcare_id">XcareId <span class="asterisk">*</span></label>
                            <select class="form-control" id="xcare_id" name="xcare_id">
                            <option value="">Select XcareId</option>
                            <?php
                                $clinicObj =  new  clinic();
                                $fields  = array('c.clinic_id','c.clinic_name');
                                //$where = "clinic_id='".$practiceDetail->practice_xcare_id."'";
                                $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 (isset($practiceDetail) && $practiceDetail->practice_xcare_id == $clinicdata->clinic_id) ? "selected='selected'" : ""; ?>><?php echo $clinicdata->clinic_name; ?></option>
                                <?php } } ?>
                            </select>
                            <div class="invalid-tooltip" id="msg_xcare_id"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="pullguarantors_name">PullGuarantors <span class="asterisk">*</span></label>
                            <select class="form-control" id="pullguarantors_name" name="pullguarantors_name">
                                <option value="">Select PullGuarantors</option>
                                <option value="pending" <?php echo ($practiceDetail->practice_pullGuarantors == 'pending') ? "selected='selected'" : ""  ?> >Pending</option>
                                <option value="running" <?php echo ($practiceDetail->practice_pullGuarantors == 'running') ? "selected='selected'" : ""  ?> >Running</option>
                                <option value="finished" <?php echo ($practiceDetail->practice_pullGuarantors == 'finished') ? "selected='selected'" : ""  ?> >Finished</option>
                                <option value="start" <?php echo ($practiceDetail->practice_pullGuarantors == 'start') ? "selected='selected'" : ""  ?> >Start</option>
                            </select>    
                            <div class="invalid-tooltip" id="msg_pullguarantors_name"></div>
                        </div>
                    

                        <div class="col-md-6 form-group mb-3">
                            <label for="PullPatients_name">PullPatients <span class="asterisk">*</span></label>
                            <select class="form-control" id="PullPatients_name" name="PullPatients_name">
                                <option value="">Select PullPatients</option>
                                <option value="pending" <?php echo ($practiceDetail->practice_pullPatients == 'pending') ? "selected='selected'" : ""  ?> >Pending</option>
                                <option value="running" <?php echo ($practiceDetail->practice_pullPatients == 'running') ? "selected='selected'" : ""  ?> >Running</option>
                                <option value="finished" <?php echo ($practiceDetail->practice_pullPatients == 'finished') ? "selected='selected'" : ""  ?> >Finished</option>
                                <option value="start" <?php echo ($practiceDetail->practice_pullPatients == 'start') ? "selected='selected'" : ""  ?> >Start</option>
                            </select>    
                            <div class="invalid-tooltip" id="msg_PullPatients_name"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="PullPatients_name">PullAppointments <span class="asterisk">*</span></label>
                            <select class="form-control" id="PullAppointments_name" name="PullAppointments_name">
                                <option value="">Select PullAppointments</option>
                                <option value="pending" <?php echo ($practiceDetail->practice_pullAppointments == 'pending') ? "selected='selected'" : ""  ?> >Pending</option>
                                <option value="running" <?php echo ($practiceDetail->practice_pullAppointments == 'running') ? "selected='selected'" : ""  ?> >Running</option>
                                <option value="finished" <?php echo ($practiceDetail->practice_pullAppointments == 'finished') ? "selected='selected'" : ""  ?> >Finished</option>
                                <option value="start" <?php echo ($practiceDetail->practice_pullAppointments == 'start') ? "selected='selected'" : ""  ?> >Start</option>
                            </select>    
                            <div class="invalid-tooltip" id="msg_PullAppointments_name"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="practice_interval">Interval <span>(minutes)</span></label>
                            <input type="text" class="form-control" id="practice_interval" name="practice_interval" value="<?php echo $practiceDetail->practice_interval; ?>" placeholder="Enter Interval">
                            <div class="invalid-tooltip" id="msg_practice_interval"></div>
                        </div>

                        


                        <div class="col-md-6 form-group mb-3" id="div_pms_date">
                          <label for="pms_date">PMS On Board Date</label>
                          <input type="date" class="form-control" name="pms_date" id="pms_date" value="<?php echo $practiceDetail->pmsOnboardDate ?>" />
                          <div class="invalid-tooltip" id="msg_pms_date"></div>
                          <br/>
                        </div>

                        <div class="col-md-12">
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Submit">
                        </div>
                        
                        <div class='loader-bubble loader-bubble-secondary m-10' align="center" style="display: none;"></div>
                        <br/><br/><br/><br/>
                        <div class="col-md-12" id="showmsgdata" align="center"></div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3" style="border-bottom: 2px solid #ddd; padding-bottom: 10px;">Sync Patient fields from PMS</div>
                 <div class="row">
                    <div class="col-md-6 form-group mb-3">
                         <label for="practice_uptodatefields">Select Patient fields to sync</label>
                         <select class="form-control" id="practice_uptodatefields" name="practice_uptodatefields[]" multiple="multiple">
                             <?php 
                             $selected_fields = isset($practiceDetail->practice_uptodatefields) ? explode(',', $practiceDetail->practice_uptodatefields) : array();
                             $pms_fields = array('firstname', 'lastname', 'birthdate','homephone', 'workphone', 'cell', 'email','primary_insurance'); 
                             foreach($pms_fields as $field){
                                 $selected = in_array($field, $selected_fields) ? "selected='selected'" : "";
                                 echo "<option value='$field' $selected>".ucfirst(str_replace('_', ' ', $field))."</option>";
                             }
                             ?>
                         </select>
                    </div>
                     <div class="col-md-6 form-group mb-3" style="display: flex; align-items: center; margin-top: 15px;">
                        <input type="button" class="btn btn-primary" id="save_sync_fields" value="Save">
                         <div id="sync_fields_msg" style="display:inline-block; margin-left:10px;"></div>
                    </div>
                </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="text/javascript">
$(document).ready(function(){

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

    $("#practice_uptodatefields").select2({
        placeholder: "Select fields",
        tags: true
    });

    $("#save_sync_fields").click(function(){
        var practice_id = <?php echo $_REQUEST['practice_id']; ?>;
        var fields = $("#practice_uptodatefields").val();
        
        $.ajax({
            url: "practice_edit.php",
            type: "POST",
            data: {
                act: "update_sync_fields",
                practice_id: practice_id,
                practice_uptodatefields: fields
            },
            success: function(response){
                if(response == 'success'){
                    //$("#sync_fields_msg").html("<span style='color:green'>Saved successfully</span>").show().fadeOut(3000);

                    $("#status_msg").show();
                      $("#status_msg").html("<div class='alert alert-success'><button data-dismiss='alert' class='close' type='button'>×</button> Updated successfully...</div>");
                        //$("#status_msg_td").html(msg);
                        hideMessage();
                } else {
                    $("#sync_fields_msg").html("<span style='color:red'>Failed to save</span>").show();
                }
            }
        });
    });
    
    var practice_id = <?php echo $_REQUEST['practice_id']; ?>;
    
});  
  $( "form" ).on( "submit", function(e) {
      e.preventDefault();
        var practice_name = $("#practice_name").val();
        var practice_xcare_id = $("#xcare_id").val();
        var pullguarantors_name = $("#pullguarantors_name").val();
        var PullPatients_name = $("#PullPatients_name").val();
        var PullAppointments_name = $("#PullAppointments_name").val();
        var practice_interval = $("#practice_interval").val();
        var pms_date = $("#pms_date").val();
        var practice_id = <?php echo $_REQUEST['practice_id']; ?>
        
        if($("#practice_name").val()=='')
        {
            $("#practice_name").addClass('error');
            $("#practice_name").focus();
            $("#msg_practice_name").html('Please enter name').show();
            return false;
        }else if($("#xcare_id").val() == '')
        {
            if($("#practice_name").val()!='')
            {
                $("#practice_name").removeClass('error');
                $("#msg_practice_name").hide();
            }

            $("#xcare_id").addClass('error');
            $("#xcare_id").focus();
            $("#msg_xcare_id").html('Please select xcareId').show();
            return false;
            
        }else if($("#pullguarantors_name").val() == '')
        {
            if($("#practice_name").val()!='')
            {
                $("#xcare_id").removeClass('error');
                $("#msg_xcare_id").hide();
            }

            $("#pullguarantors_name").addClass('error');
            $("#pullguarantors_name").focus();
            $("#msg_pullguarantors_name").html('Please select guarantor').show();
            return false;
            
        }else if($("#PullPatients_name").val() == '')
        {
            if($("#practice_name").val()!='')
            {
                $("#pullguarantors_name").removeClass('error');
                $("#msg_pullguarantors_name").hide();
            }

            $("#PullPatients_name").addClass('error');
            $("#PullPatients_name").focus();
            $("#msg_PullPatients_name").html('Please select patient').show();
            return false;
            
        }else if($("#PullAppointments_name").val() == '')
        {
            if($("#PullPatients_name").val()!='')
            {
                $("#PullPatients_name").removeClass('error');
                $("#msg_PullPatients_name").hide();
            }

            $("#PullAppointments_name").addClass('error');
            $("#PullAppointments_name").focus();
            $("#msg_PullAppointments_name").html('Please select appoinment').show();
            return false;
            
        }
        else
        {
          if($("#PullAppointments_name").val() !== '')
          {
            $("#PullAppointments_name").removeClass('error');
            $("#msg_PullAppointments_name").hide();
          }

          <?php if(isset($_REQUEST['practice_id']))
          { ?>
            if(pullguarantors_name != 'pending' || PullPatients_name != 'pending' || PullAppointments_name != 'pending')
            {
                $(".loader-bubble").show();
                $("#showmsgdata").html("Process is going on"+"<br/>Guarantors: "+pullguarantors_name+"<br/>Patients: "+PullPatients_name+"<br/>Appointments: "+PullAppointments_name);
                $("#submit").prop("disabled", true);
                var intervalID = setInterval(function() {
                    jQuery.ajax({
                        method: "POST",
                        url: "practice_edit.php",
                        data: 'act=getdata&practice_id='+practice_id,
                        dataType:"json",
                        cache: false,
                        success:function(response){
                            //console.log("1");
                            var pullGuarantors = response.practice_pullGuarantors;
                            var pullPatients = response.practice_pullPatients;
                            var pullAppointments = response.practice_pullAppointments;

                            $("#showmsgdata").html("Process is going on"+"<br/>Guarantors: "+pullGuarantors+"<br/>Patients: "+pullPatients+"<br/>Appointments: "+pullAppointments);
                            //console.log("PullAppointments"+pullAppointments);
                            $("#pullguarantors_name").val(pullGuarantors);
                            $("#PullPatients_name").val(pullPatients);
                            $("#PullAppointments_name").val(pullAppointments);

                            if(pullGuarantors == 'finished' && pullPatients == 'pending' && pullAppointments =='pending')
                            {
                                //console.log("guarantor if finished");
                                $(".loader-bubble").hide();
                                $("#showmsgdata").html("Successfully imported data to xcare");
                                $("#submit").prop("disabled", false);
                                $("#pullguarantors_name").val(pullGuarantors);
                                $("#PullPatients_name").val(pullPatients);
                                $("#PullAppointments_name").val(pullAppointments);
                                clearInterval(intervalID);
                            }
                            if((pullGuarantors == 'finished' || pullGuarantors == 'pending') && pullPatients == 'finished' && pullAppointments =='pending')
                            {
                                //console.log("patients if finished");
                                $("#pullguarantors_name").val(pullGuarantors);
                                $("#PullPatients_name").val(pullPatients);
                                $("#PullAppointments_name").val(pullAppointments);

                                $(".loader-bubble").hide();
                                $("#showmsgdata").html("Successfully imported data to xcare");
                                $("#submit").prop("disabled", false);
                                $("#pullguarantors_name").val(pullGuarantors);
                                $("#PullPatients_name").val(pullPatients);
                                $("#PullAppointments_name").val(pullAppointments);
                                clearInterval(intervalID);
                            }
                            if((pullGuarantors == 'finished' || pullGuarantors == 'pending') && (pullPatients == 'finished' || pullPatients == 'pending') && pullAppointments =='finished')
                            {
                                //console.log("Appointments if finished");
                                $("#pullguarantors_name").val(pullGuarantors);
                                $("#PullPatients_name").val(pullPatients);
                                $("#PullAppointments_name").val(pullAppointments);

                                $(".loader-bubble").hide();
                                $("#showmsgdata").html("Successfully imported data to xcare");
                                $("#submit").prop("disabled", false);
                                $("#pullguarantors_name").val(pullGuarantors);
                                $("#PullPatients_name").val(pullPatients);
                                $("#PullAppointments_name").val(pullAppointments);
                                clearInterval(intervalID);
                            }
                            if (pullGuarantors == 'finished' && pullPatients == 'finished' && pullAppointments == 'finished')
                            {
                                
                                $("#pullguarantors_name").val(pullGuarantors);
                                $("#PullPatients_name").val(pullPatients);
                                $("#PullAppointments_name").val(pullAppointments);

                                $(".loader-bubble").hide();
                                $("#showmsgdata").html("Successfully imported data to xcare");
                                $("#submit").prop("disabled", false);
                                $("#pullguarantors_name").val(pullGuarantors);
                                $("#PullPatients_name").val(pullPatients);
                                $("#PullAppointments_name").val(pullAppointments);
                                clearInterval(intervalID);
                            }
                        }
                    });
                }, 30000);
            }

              $.ajax({
                  url: "practice_edit.php",
                  data: 'act=load&practice_name='+practice_name+'&xcare_id='+practice_xcare_id+'&pullguarantors_name='+pullguarantors_name+'&PullPatients_name='+PullPatients_name+'&PullAppointments_name='+PullAppointments_name+'&practice_id='+practice_id+"&practice_interval="+practice_interval+"&pms_date="+pms_date,
                  method: 'POST',
                  success: function(msg){
                      if(msg!='success'){
                          $("#module_name").addClass('error');
                          $("#module_name").focus();
                          $("#msg_module_name").html(msg).show();
                          return false;
                      }else{
                          
                          $("#status_msg").show();
                          $("#status_msg").html("<div class='alert alert-success'><button data-dismiss='alert' class='close' type='button'>×</button> Updated successfully...</div>");
                            //$("#status_msg_td").html(msg);
                            hideMessage();
                      }
                  }
              });
          <?php 
          } ?>
      }

  });
</script>