<script type="text/javascript">
$(document).ready(function(){
	$("#frm-clinicsetting").submit(function(e){
            if(!validate_practice()){
                e.preventDefault();
            }
	});

    var maxField = 30; //Input fields increment limitation
    var addButton = $('.add_button'); //Add button selector
    var wrapper = $('.field_wrapper'); //Input field wrapper
    var fieldHTML = '<div><br/><input type="text" name="field_setting1[]" value="" placeholder="email"/>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="field_setting2[]" value="" placeholder="info@dmo.com" /><a href="javascript:void(0);" class="remove_button"><img src="<?Php echo SITE_URL."x-admin/assets/images/clinic_setting/remove-icon.png" ?>"/></a></div>'; //New input field html 
    var x = 1; //Initial field counter is 1
    //<input type="text" name="field_setting2[]" value=""/>
    //Once add button is clicked
    $(addButton).click(function(){
        //Check maximum number of input fields
        if(x < maxField){ 
            x++; //Increment field counter
            $(wrapper).append(fieldHTML); //Add field html
        }
    });
    
    //Once remove button is clicked
    $(wrapper).on('click', '.remove_button', function(e){
        e.preventDefault();
        $(this).parent('div').remove(); //Remove field html
        x--; //Decrement field counter
    });
});
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>Clinic Setting</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>/cliniclist.php">Clinic List</a></li>
    </ul>
</div>
<div class="separator-breadcrumb border-top"></div>

<form action="" method="post" id="communicationproviderFrm" name="communicationproviderFrm">
<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-3 mt-3 mt-md-0"  id="div_communicationprovider">
                        <label for="communicationprovider ">Communication Provider</label>
                        <select class="form-control" id="communicationprovider" name="communicationprovider">
                            <option value="">Select provider</option>
                            <?php 
                            $communiprovider = json_decode($clinicDetail->clinic_setting);
                                ?>
                            <option value="twilio" <?php echo (isset($communiprovider) && $communiprovider->communicationprovider == "twilio") ? "selected='selected'" : ""; ?>>Twilio</option>
                            <option value="goto" <?php echo (isset($communiprovider) && $communiprovider->communicationprovider == "goto") ? "selected='selected'" : ""; ?>>Goto</option>
                            <option value="mango" <?php echo (isset($communiprovider) && $communiprovider->communicationprovider == "mango") ? "selected='selected'" : ""; ?>>Mango</option>

                        </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">submit</button>
                    </div>
                    
                </div>
            </div>
        </div>
    </div>
</div>
</form>
<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">Clinic Setting</div>
                <form action="" id='frm-clinicsetting' method="post" enctype="multipart/form-data">
                    <?php  if(isset($clinicDetail->clinic_id) && $clinicDetail->clinic_id!=0) { ?>
                       <input type="hidden" id="clinic_id" name="clinic_id" value="<?php echo $clinicDetail->clinic_id ?>" />
                    <?php } ?>
                    <div class="row">
                        <?php
                            if(isset($clinicDetail) && $clinicDetail > 0)
                            {
                                //foreach ($clinicDetail as $ckey => $clinic_value) 
                                //{
                                    if(isset($clinicDetail->clinic_setting) && $clinicDetail->clinic_setting != '')
                                    {
                                        $clinicsetting = json_decode($clinicDetail->clinic_setting,1);

                                        foreach ($clinicsetting as $skey => $settingvalue) 
                                        { if($skey != 'communicationprovider'){?>
                                            <div class="col-md-8 form-group mb-3">
                                                <input type="text"  name="field_setting1[]" value="<?php echo $skey; ?>"/>&nbsp;&nbsp;&nbsp;&nbsp;
                                                <input type="text" name="field_setting2[]" value="<?php echo $settingvalue; ?>"/>
                                                
                                            </div>
                                       <?php } }
                                    }
                                    
                                //}
                                
                            }
                        ?>
                        
                    	<div class="col-md-6 form-group mb-3 field_wrapper">
                            <input type="text" name="field_setting1[]" value="" placeholder="email" />&nbsp;&nbsp;&nbsp;&nbsp;
                            <input type="text" name="field_setting2[]" value="" placeholder="info@dmo.com"/>
                            <a href="javascript:void(0);" class="add_button" title="Add field"><img src=<?Php echo SITE_URL."x-admin/assets/images/clinic_setting/add-icon.png" ?>></a>
                        </div>

                        
                        
                        <div class="col-md-12">
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Submit">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
   
    $( "#communicationproviderFrm" ).on( "submit", function(e) {
            //console.log("provider");return false;
            e.preventDefault();

            var communicationprovider = $("#communicationprovider").val();
            var clinic_id = <?php echo $_REQUEST['clinic_id']; ?>;
            //console.log(communicationprovider+" "+clinic_id);
            /*return false;*/
            $.ajax({
                      url: "clinic_setting.php",
                      data: { act:"communicationprovider",
                            communicationprovider:communicationprovider,clinic_id:clinic_id},
                      method: 'POST',
                      success: function(msg){
                          if(msg!=''){
                              window.location='cliniclist.php';
                              //return false;
                          }
                      }
                  });

    });    
</script>