<script type="text/javascript">
$(document).ready(function(){
	/*$("#frm-superuser").submit(function(e){
            if(!validate_suuperuser()){
                e.preventDefault();
            }
	});*/
    $("#superuser_email_address").blur(function(){
            var email = $("#superuser_email_address");
            var emailmsg = $("#msg_superuser_email_address");
            if(email.val()==""){
                email.addClass('error');email.focus();emailmsg.html('Please enter email address').show();
                return false;
            }
            if($("#superuser_id").length > 0){var superuser_id = $("#superuser_id").val();
            }else{var user_id = '';}
            $("#submit").prop('disabled', true);
            $.ajax({
                url: "superuseradd.php",
                data: 'act=validate_email&email='+email.val()+'&superuser_id='+superuser_id,
                method: 'POST',
                success: function(msg){
                    if(msg!=''){
                        email.addClass('error');email.focus();emailmsg.html(msg).show();
                        return false;
                    }else{
                        $("#submit").prop('disabled', false);
                        email.removeClass('error');emailmsg.hide();
                    }
                }
            });
	});
	
});
function validate_suuperuser(){
	var fname = $("#superuser_fname");
    var fnamemsg = $("#msg_superuser_fname");
	var lname = $("#superuser_lname");
    var lnamemsg = $("#msg_superuser_lname");
	var email = $("#superuser_email_address");
    var emailmsg = $("#msg_superuser_email_address");
    
	//var password = $("#superuser_password");var passwordmsg = $("#msg_superuser_password");
    //var rpassword = $("#superuser_rpassword");var rpasswordmsg = $("#msg_superuser_rpassword");
	var emailFilter=/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,4}))$/;

    if(fname.val()==''){
            fname.addClass('error');fname.focus();fnamemsg.html('Please enter first name').show();
            return false;
	}else{fname.removeClass('error');fnamemsg.hide();}
        if(lname.val()==''){ console.log("lname");return false;
            lname.addClass('error');lname.focus();lnamemsg.html('Please enter last name').show();return false;
	}else{lname.removeClass('error');lnamemsg.hide();}
        if(email.val()==''){ console.log("email");return false;
          email.addClass('error');email.focus();emailmsg.html('Please enter email address').show();return false;
	}else if(email.val()!='' && !(emailFilter.test(email.val())) ){
	    email.addClass('error');email.focus();emailmsg.html('Please enter valid email address').show();return false;
	}
    
}

</script>
<div class="breadcrumb">
    <h1>Dashboard Super User</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Dashboard Super User</li>
    </ul>
</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">Add New Dashboard Super User</div>
                <form action="" id='frm-superuser' method="post" enctype="multipart/form-data">
                    <?php if(isset($superuserDetail->superuser_id) && $superuserDetail->superuser_id!=0) { ?>
                       <input type="hidden" id="superuser_id" name="superuser_id" value="<?php echo $superuserDetail->superuser_id ?>" />
                    <?php } ?>
                    <div class="row">
                    	<div class="col-md-6 form-group mb-3">
                            <label for="superuser_fname">First name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="superuser_fname" name="superuser_fname" value="<?php echo $superuserDetail->superuser_fname; ?>" placeholder="Enter first name">
                            <div class="invalid-tooltip" id="msg_superuser_fname"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="superuser_lname">Last name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="superuser_lname" name="superuser_lname" value="<?php echo $superuserDetail->superuser_lname; ?>" placeholder="Enter last name">
                            <div class="invalid-tooltip" id="msg_superuser_lname"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="superuser_email_address">Email address <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="superuser_email_address" name="superuser_email_address" value="<?php echo $superuserDetail->superuser_email_address; ?>" placeholder="Enter email address">
                            <div class="invalid-tooltip" id="msg_superuser_email_address"></div>
                        </div>
                        
                        <?php if(!isset($_REQUEST['superuser_id'])) { ?>
                       <div class="col-md-6 form-group mb-3">
                            <label for="superuser_password">Password <span class="asterisk">*</span></label>
                            <input type="password" class="form-control" id="superuser_password" name="superuser_password" placeholder="Enter your password">
                            <div class="invalid-tooltip" id="msg_superuser_password"></div>
                        </div>

                       <div class="col-md-6 form-group mb-3">
                            <label for="superuser_repassword">Retype Password <span class="asterisk">*</span></label>
                            <input type="password" class="form-control" id="superuser_repassword" name="superuser_repassword" placeholder="Enter your password again">
                            <div class="invalid-tooltip" id="msg_superuser_repassword"></div>
                        </div>
                    <?php } ?>
                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="clinics">Dashboard Access </label>
                            <select class="form-control" id="clinics" multiple name="clinics[]">
                                <!-- <option value="">Select Clinic</option> -->
                                <?php foreach ($allClinic as $key => $value) {?>
                                 <option value="<?php echo $value->clinic_id;?>" ><?php echo $value->clinic_name;?></option> 
                                <?php } ?>

                            </select>

                            
                            <div class="invalid-tooltip" id="msg_clinic"></div>
                        </div>
                        <div class="col-md-3 form-group mb-3">
                            <label class="control-label">Status</label>
                            <div class="controls">

                            <select class="form-control" id="superuser_status" name="superuser_status">
                                <option value="1" <?php echo (isset($superuserDetail) && $superuserDetail->superuser_status == '1') ? "selected='selected'" : ""; ?>>Active</option>
                                <option value="0" <?php echo (isset($superuserDetail) && $superuserDetail->superuser_status == '0') ? "selected='selected'" : ""; ?>>Inactive</option>
                            </select>
                            </div>
                        </div>
                        <div class="col-md-3 form-group mb-3">
                        </div>
                        <?php if($superuserDetail->superuser_id==''){ ?>
                        <div class="col-md-6 form-group mb-3">
                            <label class="control-label">Verified</label>
                            <div class="controls">

                            <label class="switch switch-primary mr-3">
                                <input type="checkbox" id="superuser_verified" name="superuser_verified">
                                <span class="slider"></span>
                            </label>
                            </div>
                        </div>
                        <?php } ?>
                        <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>


<div class='loader-bubble loader-bubble-secondary m-5' align="center" style="display: none;"></div>
<script src="./assets/js/jquery.multiselect.js"></script>
<link rel="stylesheet" href="./assets/styles/css/jquery.multiselect.css">

<script type="text/javascript">
   
    var clinic_data = "<?php echo  $superuserDetail->superuser_clinic;?>";
    if(clinic_data !== '')
    {
        //make array
        var dataarray=clinic_data.split(",");
        $("#clinics").val(dataarray);
        $("#clinics").multiselect("refresh");    
    }


    $('#clinics').multiselect({
        columns: 1,
        placeholder: 'Select clinic',
        search: true,
        selectAll: true
    });


    $( "form" ).on( "submit", function(e) {
      e.preventDefault();

      var superuser_fname = $("#superuser_fname").val();
      var superuser_lname = $("#superuser_lname").val();
      var email = $("#superuser_email_address").val();
      var password = $("#superuser_password").val();
      var rpassword = $("#superuser_repassword").val();
      var clinics = $("#clinics").val();
      var status = $("#superuser_status").val();
      var superuser_verified = $("#superuser_verified").val();
      
      var emailFilter=/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,4}))$/;
      //console.log(product_mode);return false;
      
       if($("#superuser_fname").val()=='')
       {
          $("#superuser_fname").addClass('error');
          $("#superuser_fname").focus();
          $("#msg_superuser_fname").html('Please input first name').show();
          return false;
       }
       else if($("#superuser_lname").val()=='')
       {
          
          if($("#superuser_fname").val()!='')
          {
                $("#superuser_fname").removeClass('error');
                $("#msg_superuser_fname").hide();
          }

          $("#superuser_lname").addClass('error');
          $("#superuser_lname").focus();
          $("#msg_superuser_lname").html('Please input last name').show();
          return false;
       }
       else if($("#superuser_email_address").val()=='')
       {

          if($("#superuser_lname").val()!='')
          {
                $("#superuser_lname").removeClass('error');
                $("#msg_superuser_lname").hide();
          }

          $("#superuser_email_address").addClass('error');
          $("#superuser_email_address").focus();
          $("#msg_superuser_email_address").html('Please input email').show();
          return false;
       }
       else if($("#superuser_email_address").val()!='' && !(emailFilter.test($("#superuser_email_address").val())) )
       {
          
          $("#superuser_email_address").addClass('error');
          $("#product_superuser_email_addressname").focus();
          $("#msg_superuser_email_address").html('Please valid email').show();
          return false;
       }
       <?php if(!isset($_REQUEST['superuser_id']))
       { ?>
           else if($("#superuser_password").val()=='')
           {

              if($("#superuser_email_address").val()!='')
              {
                    $("#superuser_email_address").removeClass('error');
                    $("#msg_superuser_email_address").hide();
              }

              $("#superuser_password").addClass('error');
              $("#superuser_password").focus();
              $("#msg_superuser_password").html('Please input password').show();
              return false;
           }
           else if($("#superuser_repassword").val()=='')
           {

              if($("#superuser_password").val()!='')
              {
                    $("#superuser_password").removeClass('error');
                    $("#msg_superuser_password").hide();
              }

              $("#superuser_repassword").addClass('error');
              $("#superuser_repassword").focus();
              $("#msg_superuser_repassword").html('Please input repeat password').show();
              return false;
           }
           
        <?php } ?>
        else
        {
          <?php if(isset($_REQUEST['superuser_id']))
          { ?>
            
              $.ajax({
                  url: "superuseradd.php",
                  data: 'act=validate_email&email='+email+'&superuser_id='+<?php echo $_REQUEST['superuser_id']; ?>,
                  method: 'POST',
                  success: function(msg){
                      if(msg!=''){
                          email.addClass('error');email.focus();emailmsg.html(msg).show();
                        return false;
                      }else{
                          
                         $(".loader-bubble").show();
                          $.ajax({
                              url: "superuseradd.php",
                              data: { act:"load",
                                    superuser_fname:superuser_fname,superuser_lname:superuser_lname,superuser_email_address:email,clinics:clinics,superuser_status:status,superuser_id:<?php echo $_REQUEST['superuser_id'];?> },
                              method: 'POST',
                              success: function(msg){
                                  if(msg!=''){
                                     $(".loader-bubble").hide();
                                      window.location='superuserlist.php';
                                      return false;
                                  }
                              }
                          });
                      }
                  }
              });
          <?php 
          }else
          {  ?>
            $(".loader-bubble").show();
            $.ajax({      
                    url: "superuseradd.php",
                    data: 
                    { 
                        act:"load",
                        superuser_fname:superuser_fname,superuser_lname:superuser_lname,superuser_email_address:email,password:password,rpassword:rpassword,clinics:clinics,superuser_status:status,superuser_verified:superuser_verified 
                    },
                    method: 'POST',
                    success: function(msg){
                        if(msg!=''){
                            $(".loader-bubble").hide();
                           window.location='superuserlist.php';
                        }
                    }
                });
         <?php }
          ?>
      }

  });

</script>