<script type="text/javascript">
    function validate_agency(){
        
        var fname = $("#agencyuser_fname");
        var pnamemsg = $("#msg_agencyuser_fname");

        var lname = $("#agencyuser_lanme");
        var plname = $("#msg_agencyuser_lname");

        var email = $("#agencyuser_email");
        var pemail = $("#msg_agencyuser_email");
        
        if(fname.val()==''){
            fname.addClass('error');
            fname.focus();
            pnamemsg.html('Please enter first name').show();
            return false;
        }else{
            fname.removeClass('error');
            pnamemsg.hide();
        }
        if(lname.val()==''){
            lname.addClass('error');
            lname.focus();
            plname.html('Please enter last name').show();
            return false;
        }else{
            lname.removeClass('error');
            plname.hide();
        }
        if(email.val()==''){
            email.addClass('error');
            email.focus();
            pemail.html('Please enter email').show();
            return false;
        }else{
            email.removeClass('error');
            pemail.hide();
        }
        return true;
    }

</script>
<div class="breadcrumb">
    <h1>Agency User</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Agency User</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 id="results" >
      <div  id="msg_error_name"></div>
  </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 class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">Add New Agency User</div>
                <form action="" id='frm-agency' method="post" enctype="multipart/form-data" onsubmit="return validate_agency();">
                    <?php if(isset($agencyuserDetail->agencyuser_id ) && $agencyuserDetail->agencyuser_id !=0) { ?>
                       <input type="hidden" id="agencyuser_id" name="agencyuser_id" value="<?php echo $agencyuserDetail->agencyuser_id; ?>" />
                    <?php } ?>
                    <div class="row">
                        <div class="col-md-6 form-group mb-3">
                            <label for="agencyuser_role">Agency <span class="asterisk">*</span></label>
                            <select class="form-control" id="agencyuser_agency_id" name="agencyuser_agency_id">
                                <option value="">Select Agency</option>
                                <?php 
                                if($allagency  && count($allagency)>0){
                                    foreach ($allagency as $key => $agency) {?>
                                        <option value="<?php echo $agency->agency_id ?>" <?php echo $agencyuserDetail->agencyuser_agency_id==$agency->agency_id?"selected='selected'":"" ?>><?php echo $agency->agency_name ?></option>
                                    <?php }
                                }
                                ?>
                            </select>
                            <div class="invalid-tooltip" id="msg_agencyuser_agency_id"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="agencyuser_fname">First name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="agencyuser_fname" name="agencyuser_fname" value="<?php echo $agencyuserDetail->agencyuser_fname ?>" placeholder="Enter agency User First name">
                            <div class="invalid-tooltip" id="msg_agencyuser_fname"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="agencyuser_lanme">Last name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="agencyuser_lname" name="agencyuser_lname" value="<?php echo $agencyuserDetail->agencyuser_lname ?>" placeholder="Enter agency User last name">
                            <div class="invalid-tooltip" id="msg_agencyuser_lname"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="agencyuser_email">Email <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="agencyuser_email" name="agencyuser_email" value="<?php echo $agencyuserDetail->agencyuser_email ?>" placeholder="Enter agency User email">
                            <div class="invalid-tooltip" id="msg_agencyuser_email"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="agencyuser_name">Phone <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="agencyuser_phone" name="agencyuser_phone" value="<?php echo $agencyuserDetail->agencyuser_phone ?>" placeholder="Enter agency User phone">
                            <div class="invalid-tooltip" id="msg_agencyuser_phone"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="admin_role">Role <span class="asterisk">*</span></label>
                            <select class="form-control" id="agencyuser_role" name="agencyuser_role">
                                <option value="">Select Role</option>
                                <option value="Standard" <?php echo ($agencyuserDetail->agencyuser_role=='Standard')?"selected='selected'":"" ?> >Standard</option>
                                <option value="Account Owner" <?php echo ($agencyuserDetail->agencyuser_role=='Account Owner')?"selected='selected'":"" ?>>Account Owner</option>
                                <option value="View Only" <?php echo ($agencyuserDetail->agencyuser_role=='View Only')?"selected='selected'":"" ?>>View Only</option>
                            </select>
                            <div class="invalid-tooltip" id="msg_agencyuser_role"></div>
                        </div>
                        
                        <?php if(!isset($agencyuserDetail->agencyuser_password) || $agencyuserDetail->agencyuser_password==''){ ?>

                       <div class="col-md-6 form-group mb-3">
                            <label for="agencyuser_password">Password <span class="asterisk">*</span></label>
                            <input type="password" class="form-control" id="agencyuser_password" name="agencyuser_password" placeholder="Enter your password">
                            <div class="invalid-tooltip" id="msg_agencyuser_password"></div>
                        </div>

                       <div class="col-md-6 form-group mb-3">
                            <label for="agencyuser_repassword">Password <span class="asterisk">*</span></label>
                            <input type="password" class="form-control" id="agencyuser_repassword" name="agencyuser_repassword" placeholder="Enter your password again">
                            <div class="invalid-tooltip" id="msg_agencyuser_repassword"></div>
                        </div>


                        <?php } ?>
                        <div class="col-md-6 form-group mb-3">
                            <label for="admin_role">Clinics <span class="asterisk">*</span></label>
                            <select class="form-control selectclinics" id="agencyuser_clinics" name="agencyuser_clinics[]" multiple="multiple">
                                <option value="">Select clinics</option>
                                <?php 
                                 $clinicobj = new clinic;
                                  $fields = array('clinic_id,clinic_name');
                                  $clinicArg = array("fields"=>$fields);
                                  $clinicdata = $clinicobj->getAllRecords($clinicArg);
                                  foreach ($clinicdata as $ckey => $clinic_value){ 
                                    if(!empty($agencuserClinics)){?>
                                        <option value="<?php echo $clinic_value->clinic_id; ?>" <?php echo in_array($clinic_value->clinic_id, $agencuserClinics) ? "selected='selected'":"" ?>><?php echo $clinic_value->clinic_name; ?></option>
                                   <?php }else{ ?>
                                        <option value="<?php echo $clinic_value->clinic_id; ?>">
                                            <?php echo $clinic_value->clinic_name; ?>
                                        </option>
                                  <?php   }
                                   } ?>
                            </select>
                            <div class="invalid-tooltip" id="msg_agencyuser_clinics"></div>
                        </div>
                        <div class="col-md-12 form-group mb-3">
                            <label class="control-label">Status</label>
                            <div class="controls">

                            <label class="switch switch-primary mr-3">
                                <input type="checkbox" id="agencyuser_status" name="agencyuser_status" <?php echo ($agencyuserDetail->agencyuser_status=='1') ? "checked='checked'" : "" ?>>
                                <span class="slider"></span>
                            </label>
                            </div>
                         </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 src="./assets/js/jquery.multiselect.js"></script>
<link rel="stylesheet" href="./assets/styles/css/jquery.multiselect.css">
<script type="text/javascript">
$(document).ready(function() { 
    $('.selectclinics').multiselect({
        columns: 1,
        placeholder: 'Select clinics',
        search: true,
        selectAll: true
    });
    
});
</script>