<script type="text/javascript">

function validate_admin(){
	
	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($("#admin_fname").val()==''){
	    $("#admin_fname").addClass('error');
	    $("#admin_fname").focus();
	    $("#msg_admin_fname").html('Please enter first name').show();
		return false;
	}else{
		$("#admin_fname").removeClass('error');
	    $("#msg_admin_fname").hide();
	}
   if($("#admin_lname").val()==''){
	    $("#admin_lname").addClass('error');
	    $("#admin_lname").focus();
	    $("#msg_admin_lname").html('Please enter last name').show();
		return false;
	}else{
		$("#admin_lname").removeClass('error');
	    $("#msg_admin_lname").hide();
	}

	if($("#admin_role").val()==''){
	    $("#admin_role").addClass('error');
	    $("#admin_role").focus();
	    $("#msg_admin_role").html('Please select role').show();
		return false;
	}else{
		$("#admin_role").removeClass('error');
	    $("#msg_admin_role").hide();
	}
	
	
	if($("#admin_email_address").val()==''){
	    $("#admin_email_address").addClass('error');
	    $("#admin_email_address").focus();
	    $("#msg_admin_email_address").html('Please enter email address').show();
		return false;
	}else if($("#admin_email_address").val()!='' && !(emailFilter.test($("#admin_email_address").val())) ){
	    $("#admin_email_address").addClass('error');
	    $("#admin_email_address").focus();
	    $("#msg_admin_email_address").html('Please enter valid email address').show();
		return false;
	}else{

		if($("#admin_id").length > 0){
			var admin_id = $("#admin_id").val();
		}else{
			var admin_id = '';
		}
		var email = $("#admin_email_address").val();

		$.ajax({
            url: "adminadd.php",
            data: 'act=validate_email&email='+email+'&admin_id='+admin_id,
            method: 'POST',
            success: function(msg){
              if(msg!=''){
                  $("#admin_email_address").addClass('error');
                  $("#msg_admin_email_address").html(msg).show();
                  return false;
              }else{
                  $("#admin_email_address").removeClass('error');
                  $("#msg_admin_email_address").hide();
              }
            }
          });
	}
	
	<?php if(!isset($_REQUEST['admin_id'])){ ?>

		if($("#admin_password").val()==''){
		    $("#admin_password").addClass('error');
		    $("#admin_password").focus();
		    $("#msg_admin_password").html('Please enter password').show();
			return false;
			
		}else if($("#admin_password").val()!='' && $("#admin_password").val()!=$("#admin_repassword").val()){
		    $("#admin_password").addClass('error');
		    $("#admin_repassword").addClass('error');
		    $("#admin_repassword").focus();
		    $("#msg_admin_repassword").html('Password and confirm password does not matched').show();
			return false;
		}else{
			$("#admin_password").removeClass('error');
			$("#admin_repassword").removeClass('error');
		    $("#msg_admin_password").hide();
		    $("#msg_admin_repassword").hide();
	   }
	<?php } ?>	
}

</script>


<div class="breadcrumb">
    <h1>Administrator</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Admin</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 Admin</div>
                <form action="" method="post" onsubmit="return validate_admin();" enctype="multipart/form-data">

					<?php if(isset($adminDetail->admin_id) && $adminDetail->admin_id!=0) { ?>
						<input type="hidden" id="admin_id" name="admin_id" value="<?php echo $adminDetail->admin_id ?>" />
						<input type="hidden" id="admin_type" name="admin_type" value="<?php echo $adminDetail->admin_type ?>" />
					<?php }else{ ?>
						<input type="hidden" id="admin_type" name="admin_type" value="administrator" />
					<?php } ?>

                    <div class="row">

                        <div class="col-md-6 form-group mb-3">
                            <label for="admin_fname">First name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="admin_fname" name="admin_fname" value="<?php echo $adminDetail->admin_fname; ?>" placeholder="Enter your first name">
                            <div class="invalid-tooltip" id="msg_admin_fname"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="admin_lname">Last name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="admin_lname" name="admin_lname" value="<?php echo $adminDetail->admin_lname; ?>" placeholder="Enter your last name">
                            <div class="invalid-tooltip" id="msg_admin_lname"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="admin_email_address">Email address <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="admin_email_address" name="admin_email_address" value="<?php echo $adminDetail->admin_email_address; ?>" placeholder="Enter your email address">
                            <div class="invalid-tooltip" id="msg_admin_email_address"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="admin_phone">Phone number</label>
                            <input type="text" class="form-control" id="admin_phone" name="admin_phone" value="<?php echo $adminDetail->admin_phone; ?>" placeholder="Enter your phone number">
                            <div class="invalid-tooltip" id="msg_admin_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="admin_role" name="admin_role">
                            	<option value="">Select Role</option>
	                            <?php
	                                  $roleobj = new role;
	                                  $fields = array('role_id,role_name');
	                                  $roleArg = array("fields"=>$fields);
	                                  $roledata = $roleobj->getAllRecords($roleArg);
	                                  foreach ($roledata as $ckey => $role_value) 
	                                    { 
	                                      ?>
	                                        <option value="<?php echo $role_value->role_id; ?>" 
	                                        	<?php echo (isset($adminDetail)&& $adminDetail->admin_type==$role_value->role_id)?"selected='selected'":""  ?> ><?php echo $role_value->role_name; ?></option>
	                              <?php }
	                            ?>
                        	</select>
                        	<div class="invalid-tooltip" id="msg_admin_role"></div>
                        </div>

                        <?php if(!isset($adminDetail->admin_password) || $adminDetail->admin_password==''){ ?>

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

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


                        <?php } ?>


						<?php if($adminDetail->admin_id!=1){ ?>
						
		                <?php $accessArr = array();
							  if(isset($adminDetail->admin_access) && $adminDetail->admin_access!=''){
								$accessArr = explode(',',$adminDetail->admin_access);	
						  } ?>
					
		                <!-- <div class="col-md-12 form-group mb-3">
		                  <label class="control-label">Module permission</label>
		                  <div class="controls">
		                  
		   					<?php if($adminModules && count($adminModules) > 0){ foreach ($adminModules as $key => $val){ ?>
	
			                     <label class="checkbox checkbox-outline-primary inline-checkbox">
		                                <input type="checkbox" name="admin_access[]" id="admin_access_<?php echo $key ?>"  value="<?php echo $val->id ?>" <?php echo ($accessArr && in_array($val->id,$accessArr)) ? "checked='checked'" : '' ?>>
		                                <span><?php echo $val->name; ?></span>
		                                <span class="checkmark"></span>
		                         </label>

			                     
							<?php } } ?>	
							                     
		                   </div>
		               </div> -->
				
					<?php } ?>


                   <?php if(!isset($adminDetail) || $adminDetail->admin_type!='super administrator'){ ?>
                        <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="admin_status" name="admin_status" <?php echo ($adminDetail->admin_status=='1') ? "checked='checked'" : "" ?>>
                                <span class="slider"></span>
                            </label>
			                </div>
	                     </div>
						<?php }  ?>

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