<?php   
$pageTitleForAdmin = 'Admin Account';
$mainTitleForAdmin = 'Administrators';
$pageTitle = ucfirst($action).' '.$pageTitleForAdmin; 
$actUrl = 'admin/admin_accounts/sub_admin_manager';
if(!empty($accountId)){
	$actUrl = 'admin/admin_accounts/sub_admin_manager/'.$action.'/'.$accountId;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title><?php echo $pageTitle; ?> - ACP - <?php echo site_name(); ?></title>

<?php $this->load->view('admin/ap_top'); ?>

<script type="text/javascript">

 function doActionFn(action,id){
	 if(action == 'edit' || action == 'view'){
			window.location= '<?php echo base_url('admin/admin_accounts/sub_admin_manager/'); ?>/'+action+'/'+id;
	}else{
		 $('#doAction').val(action);	
		 if(action == 'delete'){
			if (confirm("Are you sure you want to delete?")) {
				$('#listFrm').submit();
			}	
		} else{
			$('#listFrm').submit();
		}
	}
 }
 $(document).ready(function(){
 	 $(document).on('change', "#changeUserPassword", function() {
	 	if($(this).is(':checked') == true){
			$('.changePass').show();	
		}else {
			$('.changePass').hide();	
		}
	});
}); 
 $(document).on('change', "#email", function() { 
	 $errStaus=true;
 	 if($errStaus){
	 	var userEmail = $('#email').val(); 
		var user_id = '<?php echo $accountId ?>'; 
			var baseURL = "<?php echo base_url('admin/ajax/isExistsData'); ?>";
			$.ajax({
				url:baseURL, async: false, type: 'POST',cache: false,
				data: 'userEmail='+userEmail+'&user_id='+user_id,
				success:function (resdata) {
					var resObj = jQuery.parseJSON( resdata );
					if(resObj.email == 'exist') {setMsg('email','Email already being used!');$errStaus=true;}
 					else {
						$('#div_email').removeClass('has-error').addClass('has-success');$('#err_email').html('');	
						$errStaus=false;}
				}
			});
	 }
			 
 	});
 $(document).on('change', "#username", function() { 
	 $errStaus=true;
 	 if($errStaus){
	 	var username = $('#username').val(); 
		var user_id = '<?php echo $accountId ?>'; 
			var baseURL = "<?php echo base_url('admin/ajax/isExistsData'); ?>";
			$.ajax({
				url:baseURL, async: false, type: 'POST',cache: false,
				data: 'username='+username+'&user_id='+user_id,
				success:function (resdata) {
					var resObj = jQuery.parseJSON( resdata );
					if(resObj.username == 'exist') {setMsg('username','Username already being used!');$errStaus=true;}
 					else {
					$('#div_username').removeClass('has-error').addClass('has-success');$('#err_username').html('');	
					$errStaus=false;}
				}
			});
	 }
			 
 	});
 	
    function checkUncheckAll(booVal){
  $('.accessModule').each(function(){
	  $(this).prop('checked',booVal);
  });
}

    function validateAccountForm(){
	clrErr();
	$errStaus = false; 
	var action = '<?php echo $action ?>';
	var passValidation = true;
	if(action == 'edit'){
		passValidation = false;
		if($('#changeUserPassword').length && $('#changeUserPassword').is(':checked') ){
			passValidation = true;
		}
	}
   	if(validateFormFields('name','Please enter the name.',''))$errStaus=true;
	if(validateFormFields('username','Please enter the username.','validUsername'))$errStaus=true;
	if(validateFormFields('email','Please enter the E-mail.','validEmail'))$errStaus=true;
	
	if(passValidation){
		if(validateFormFields('password','Please enter the password.',''))$errStaus=true;
		if(validateFormFields('password2','Please enter confirm  password.',''))$errStaus=true;
	}
	if(($('#password').val() != "" && $('#password2').val() != "") && ($('#password').val() !=  $('#password2').val()) ){
		setMsg('password2','Password mismatching! Please try again.');$errStaus=true;
	}
	if(validateFormFields('usertype','Please select the usertype.',''))$errStaus=true;
	if(validateFormFields('accessPrivilege','Please select atleast one privilege.','checkbox'))$errStaus=true;
	if(validateFormFields('accessModule','Please select atleast one module.','checkbox'))$errStaus=true;
	
	if($errStaus == false){
 		var userEmail = $('#email').val(); 
		var username = $('#username').val(); 
		var user_id = '<?php echo $accountId ?>'; 
		var baseURL = "<?php echo base_url('admin/ajax/isExistsData'); ?>";
		$.ajax({
				url:baseURL, 
				async: false,
				 type: 'POST',
				 cache: false,
					data: 'userEmail='+userEmail+'&user_id='+user_id+'&username='+username,
				success:function (resdata) {
					var resObj = jQuery.parseJSON( resdata );
					if(resObj.email == 'exist') {setMsg('email','Email already being used!');$errStaus=true;}
					else if(resObj.username == 'exist') {setMsg('username','Username already being used!');$errStaus=true;}
				}
			});
	}
	
	if($errStaus) {
		return false;
	} else {
	return true;		
	}
	
}
 
</script>

<style type="text/css">

.table td
{
    white-space: normal;
}
.checkbox-scroll
{
    border: 1px solid #cccccc;
    float: left;
    margin-bottom: 15px;
    max-height: 125px;
    overflow-x: hidden;
    overflow-y: auto;
    width: 100%;
    padding: 10px 0;
}
</style>

</head>

<body>
    
    <div class="wrapper" >
        <?php $this->load->view('admin/navbar'); ?>
            
		<div class="main-panel" style="background-color: #DEE2E6;height: auto;">
		    <div class="content">
				<div class="page-inner">
					<div class="page-header">
						<h4 class="page-title"><?php echo $mainTitleForAdmin ; ?></h4>
						<ul class="breadcrumbs">
							<li class="nav-home">
								<a href="<?php echo base_url('admin/dashboard');?>">
									<i class="flaticon-home"></i>
								</a>
							</li>
							<li class="separator">
								<i class="flaticon-right-arrow"></i>
							</li>
							<li class="nav-item">
								<a href="<?php echo base_url('admin/admin_accounts/my_account');?>"><?php echo $mainTitleForAdmin; ?></a>
							</li>
							<li class="separator">
								<i class="flaticon-right-arrow"></i>
							</li>
							<li class="nav-item">
								<a href="<?php echo base_url('admin/admin_accounts/sub_admins');?>"><?php echo $pageTitleForAdmin; ?> List</a>
							</li>
							<li class="separator">
								<i class="flaticon-right-arrow"></i>
							</li>
							<li class="nav-item">
								<a href="#"><?php echo $pageTitle; ?></a>
							</li>
						</ul>
					</div>
					
					 <?php
                    	if($action == 'edit' || $action == 'add'){
                    	    //add, edit card
                	    ?>
                    	    <div class="row">
						        <div class="col-md-12">
    							<div class="card">
    							    
    								<div class="card-header">
    									<h4 class="card-title">Account Details</h4>
    								</div>
    								
    								<!--notifiactions-->
        							<div class="container" id="">
                                        <?php 
                                 		$errMsg = validation_errors('* ');
                                		if(empty($errMsg)){ $errMsg = $this->session->flashdata('errMsg');  }
                                		$warningMsg = $this->session->flashdata('warningMsg');
                                		$successMsg = $this->session->flashdata('successMsg');
                                		if(isset($errMsg) && !empty($errMsg)){ echo showMessage($errMsg,'error'); }  
                                		if(isset($warningMsg) && !empty($warningMsg)){ echo showMessage($warningMsg,'warning'); }
                                		if(isset($successMsg) && !empty($successMsg)){ echo showMessage($successMsg,'success'); }
                                	?>
                                    </div>
    								
    								<div class="card-body">
    								    
    							        <?php echo form_open_multipart( $actUrl,array('name' => 'accAdminFrm', 'id' => 'accAdminFrm','onSubmit' => 'return validateAccountForm()'));?>
    										        
        										    <div class="form-group form-inline" id="div_name">
        												<label for="inlineinput" class="col-md-2 col-form-label"> Name <span style="color:red;">&nbsp;*</span></label>
        												<div class="col-md-6 p-0" >
        												   <?php   $data = array( 'name' => 'name',
        												            'value' => set_value('name',isset($adminRow->accountFirstname)?$adminRow->accountFirstname:$this->input->post('name')),
        												            'id' => 'name',  'placeholder'   => 'Name', 'class' => 'form-control input-full required', );
                                              						 echo form_input($data);
                                            					?>
        													<small class="form-text text-muted text-danger err_msg" id="err_name"></small>
        												</div>
        											</div>
        											
        											<div class="form-group form-inline" id="div_username">
        												<label for="inlineinput" class="col-md-2 col-form-label"> Username <span style="color:red;">&nbsp;*</span></label>
        												<div class="col-md-6 p-0">
        												    <?php   $data = array('name' => 'username', 'value'=>set_value('username',isset($adminRow->accountUsername)?$adminRow->accountUsername:$this->input->post('username')),
        												            'id' => 'username', 'placeholder'=>'Enter your username here', 'class'=>'form-control input-full required');
                                          						 echo form_input($data);
                                        					?>
    														<small class="form-text text-muted text-danger err_msg" id="err_username"></small>
        												</div>
        											</div>
        											
        											<div class="form-group form-inline" id="div_email">
        												<label for="inlineinput" class="col-md-2 col-form-label">Email Address <span style="color:red;">&nbsp;*</span></label>
        												<div class="col-md-6 p-0">
        												    <?php   $data = array( 'name' => 'email','value' => set_value('email',isset($adminRow->accountEmail)?$adminRow->accountEmail:$this->input->post('email')),
        												            'id' => 'email',  'placeholder'=> 'Enter your Email here', 'class' => 'form-control input-full required');
                                         						 echo form_input($data);
                                        					?>
        													<small class="form-text text-muted text-danger err_msg" id="err_email"></small>
        												</div>
        											</div>
        											
        											<!--<div class="form-group form-inline">-->
        											<!--	<label for="inlineinput" class="col-md-2 col-form-label">Profile Image</label>-->
        											<!--	<div class="col-md-6 p-0">-->
        											<!--		<input type="file" class="form-control input-full"  name="profile_image" >-->
        											<!--	</div>-->
        											<!--</div>-->
        											
    											    <?php
                                        			$showPass = 'display';
                                                	if($action == 'edit')
                                                	{
                                        				$showPass = 'none';
                                                		?>
                                                		<div class="form-check form-inline">
            												<label class="form-check-label col-md-2" for="changeUserPassword">
            												    <?php
                                            		                $changePass = array( 'name' => 'changeUserPassword', 'id' => 'changeUserPassword',  'value' => '1' , 'class'=> 'form-check-input' );  
                                                 					echo form_checkbox($changePass);	
                                             					?>
            													<span class="form-check-sign"> Change Password </span>
            													<small class="form-text text-muted text-danger err_msg" id="err_changeUserPassword"></small>
            												</label>
            											</div>
                                                    <?php 
                                                    } 
                                                    ?>
                                                                
                                                	<div class="form-group form-inline changePass" style="display:<?php echo $showPass?>" id="div_password">
                												<label for="inlineinput" class="col-md-2 col-form-label">Password</label>
                												<div class="col-md-6 p-0">
                												     <?php   
                												        $data = array( 'name' => 'password',  'id' => 'password',  'placeholder' => 'Enter your Password here',
                												        'class' => 'form-control input-full required', 'maxlength' => '20' ,'minlength' => '6');
                                                 						 echo form_password($data);
                                             						 ?>
                                             						   <?php if($action == 'edit'){ ?>
                                                                            <label class="control-label" for="sendPassEmail">
                                                                        <?php  $changePass1 = array( 'name' => 'sendPassEmail', 'id' => 'sendPassEmail',  'value' => '1','checked'=> true  );  
                                                         					echo form_checkbox($changePass1);	
                                                         					?>
                                                                        Send password details to this E-mail ID?</label>
                                                                        <?php }?>
                                                                    
                                                                    <small class="form-text text-muted text-danger err_msg" id="err_password"></small>
                												</div>
                											</div>
                											
        											<div class="form-group form-inline changePass" style="display:<?php echo $showPass?>" id="div_password2">
                												<label for="inlineinput" class="col-md-2 col-form-label">Confirm Password</label>
                												<div class="col-md-6 p-0">
            												        <?php   
            												            $data = array( 'name' => 'password2',  'id' => 'password2',  'placeholder' => 'Enter your Confirm Password here',
            												            'class' => 'form-control input-full required', 'maxlength' => '20','minlength' => '6');
                                             						    echo form_password($data);
                                         						    ?>
                													<small class="form-text text-muted text-danger err_msg" id="err_password2"></small>
                												</div>
                											</div>
        										
    										        <div class="form-group form-inline " id="div_usertype">
                                                        <label class="col-md-2 col-form-label" for="usertype">Account Type<span style="color:red;">&nbsp;*</span></label>
                                                        <div class="col-md-6 p-0">
                                                        <?php  
                                        		                $ARRADMIN_TYPE = array('0'=>'Select UserType','Administrator'=>'Administrator','SubAdmin'=>'Sub Admin');
                                								$js = 'class="form-control input-full required" id="usertype" '; 
                                								echo form_dropdown('usertype',$ARRADMIN_TYPE,isset($adminRow->accountType)?$adminRow->accountType:$this->input->post('usertype'), $js); 
                            								?>
                                                        
                                                        <small class="form-text text-muted text-danger err_msg" id="err_usertype"></small>
                                                        </div>
                                                      </div>
    											
    											
                                                    <div class="form-group form-inline ">
                                                          <label for="inlineinput" class=" col-md-2 ">Privileges<span style="color:red;">&nbsp;*</span></label>
                                                          
                                                          <div class="selectgroup selectgroup-pills col-md-6 p-0">
            												
                                                          <!--<div class="col-md-6 p-0">-->
                                                           <?php 
                                            					if(isset($adminRow->accountAccessPrivilege))
                                            					$accessPrivilege = $adminRow->accountAccessPrivilege;
                                            					else $accessPrivilege = $this->input->post('accessPrivilege');
                                            					if(isset($accessPrivilege) && !empty($accessPrivilege))   
                                            						 $accessPrivilege = explode('|',$accessPrivilege);
                                            						
                                            					 else if(isset($accessPrivilege) &&  is_array($accessPrivilege))
                                            					 $accessPrivilege[] = $accessPrivilege[0]; 
                                            					 else $accessPrivilege[] = '';
                                             					$privillege1 = array( 'name' => 'accessPrivilege[]', 'id' => 'accessPrivilege1', 
                                            					 				'value' => 'VIEW', 'class' =>'');  
                                            					if(isset($accessPrivilege) && is_array($accessPrivilege) && !empty($accessPrivilege) && in_array('VIEW', $accessPrivilege)) $privillege1 = array_merge($privillege1,array('checked' => "checked")); 
                                            					echo '<label class="selectgroup-item">'.form_checkbox($privillege1). ' VIEW  &nbsp;&nbsp;</label> ';
                                            					
                                            					$privillege2 = array( 'name' => 'accessPrivilege[]', 'id' => 'accessPrivilege2', 
                                            					 				'value' => 'INSERT',  'class' =>'accessPrivilege'   );  
                                            					 				
                                            					if(isset($accessPrivilege) && !empty($accessPrivilege)  &&  in_array('INSERT', $accessPrivilege)) $privillege2 = array_merge($privillege2,array('checked' => "checked")); 
                                            					echo '<label class="selectgroup-item">'.form_checkbox($privillege2).'  INSERT &nbsp;&nbsp;</label> ';	
                                            					$privillege3 = array( 'name' => 'accessPrivilege[]', 'id' => 'accessPrivilege3', 
                                            					 				'value' => 'UPDATE',  'class' =>'accessPrivilege'    );  
                                            					if(isset($accessPrivilege) && is_array($accessPrivilege) && !empty($accessPrivilege) && in_array('UPDATE', $accessPrivilege)) $privillege3 = array_merge($privillege3,array('checked' => "checked")); 
                                            					echo '<label class="selectgroup-item">'.form_checkbox($privillege3). '  UPDATE &nbsp;&nbsp;</label> ';	
                                            					$privillege4 = array( 'name' => 'accessPrivilege[]', 'id' => 'accessPrivilege4', 
                                            					 				'value' => 'DELETE', 'class' =>'accessPrivilege'    );  
                                            					if(isset($accessPrivilege) && !empty($accessPrivilege)  &&  in_array('DELETE', $accessPrivilege)) $privillege4 = array_merge($privillege4,array('checked' => "checked")); 
                                            					echo '<label class="selectgroup-item">'.form_checkbox($privillege4).'  DELETE &nbsp;&nbsp;</label> ';	
                                             					 ?>
                                                            
                                                            <small class="form-text text-muted text-danger err_msg" id="err_accessPrivilege"></small>
                                                          </div>
                                                        </div>
                                                    
                                                    <div class="form-group form-inline">
                                                        <label for="inlineinput" class="col-md-2" style="align-self: normal;">Access modules<span style="color:red;">&nbsp;*</span></label>
                                                                      
                                                        <div class="col-md-6 p-0"> 
                                                                <div class="checkbox-scroll selectgroup selectgroup-pills">
                                                                  
                                                                <?php
                                                		  if(isset($adminRow->accountAccessModule))
                                                					$accessModule = $adminRow->accountAccessModule;
                                                					else $accessPrivilege = $this->input->post('accessModule');
                                                					if(isset($accessModule) && !empty($accessModule) && strpos(',',strtolower($accessModule)) === false)   
                                                						 $accessModule = explode('|',$accessModule);
                                                						
                                                					 else if(isset($accessModule) &&  is_array($accessModule))
                                                					 $accessModule[] = $accessModule[0]; 
                                                					 else $accessModule[] = '';
                                                		  $ADMIN_ACCESS_MODULES = $GLOBALS['ARR_ADMIN_MODULES'];//$pageTitleForAdmin
                                                 		  if(!empty($ADMIN_ACCESS_MODULES)){
                                                 			  foreach($ADMIN_ACCESS_MODULES as $accessKeys => $accessModules){
                                                				  $showAccessModule = true;
                                                				   $accessModuleName =  $accessModules;
                                                  				  if($accessKeys == 'admin_accounts' && $this->admin_model->login_type == 'SubAdmin'){
                                                					  $showAccessModule = false;
                                                				  }
                                                 				   if($accessKeys == 'admin_accounts' ){
                                                					  $accessModuleName =  $pageTitleForAdmin;
                                                				   }
                                                				  if(isset($accessModule) && is_array($accessModule) && !empty($accessModule) && in_array($accessKeys, $accessModule)){
                                                				  	$checkedProp = 'checked';
                                                				  }else{
                                                					 $checkedProp = ''; 
                                                				  }
                                                				  if( $showAccessModule){
                                                				      
                                                				  echo '<label class="checkbox-inline col-lg-3 col-md-4 col-sm-6 col-xs-12">';
                                                				  echo form_checkbox(array( 
                                                				 					 'name' => 'accessModule[]',
                                                				  					 'value' => $accessKeys,
                                                									 'class' => 'accessModule selectgroup-item ',
                                                									 'checked' => $checkedProp
                                                				 		 )).'&nbsp;'.$accessModuleName.'</label>';
                                                				  }
                                                			  }
                                                			}
                                                		  ?>
                                                              </div>
                                                              
                                                              
                                                                <p style="margin-bottom: unset;">
                                                                      <a href="#"   onclick="checkUncheckAll(true); return false;">Check All</a> | <a href="#" onclick="checkUncheckAll(false); return false;">Uncheck All</a>
                                                                </p>
                                                                
                                                                <small class="form-text text-muted text-danger err_msg" id="err_accessModule"></small>
                                                            </div>
                                                           
                                                    </div>
                                                    
                                                    <br>
        											
        											<div class="form-group form-inline">
            												<label for="inlineinput" class="col-md-2 col-form-label "></label>
            												<div class="col-md-6 p-0">
            												    <?php
            												        $data = array( 'name' => 'subAdminBtn','value' => 'true', 'id' => 'subAdminBtn',  'type' => 'submit','content' => 'Submit', 'class' => 'btn btn-success',   );
                                                 				    echo form_button($data);
                                                				    $data = array( 'name' => 'resetBtn','value' => 'true', 'id' => 'resetBtn',  'type' => 'reset','content' => 'Cancel', 'class' => 'btn btn-warning' , 'onclick' => 'window.location.href=document.referrer'  );
                                                				    echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                                                 				    echo form_button($data) ;
                                                     			?>
            												</div>
            											</div>
        									    
    								        <?php echo form_close(); ?>
    								</div>
    							</div>
    						</div>
					        </div>
                    	    
                	    <?php
                    	}
                    	else
                    	{
                    	    //view card
                    	    ?>
                    	    <div class="row">
        						<div class="col-md-6">
        							<div class="card">
        								<div class="card-header">
        								    <div class="card-head-row">
        									    <h4 class="card-title">Account Info</h4>
        									</div>
        								</div>
							            <div class="card-body">
    							        <div class="table-responsive">
    										<table id="" class="table table-bordered">
    									    	<tr>
    											    <th>Name</th><td><?php echo ( (!empty($adminRow->accountFirstname)&&(isset($adminRow->accountFirstname))) ? $adminRow->accountFirstname:'-');  ?></td>
    										    </tr>
    										    <tr>
    											    <th>User Name</th><td><?php echo ( (!empty($adminRow->accountUsername)&&(isset($adminRow->accountUsername))) ? $adminRow->accountUsername:'-');  ?></td>
    										    </tr>
    										    <tr>
    											    <th>User Type</th><td><?php echo ( (!empty($adminRow->accountType)&&(isset($adminRow->accountType))) ? $adminRow->accountType:'-');  ?></td>
    										    </tr>
    										    <tr>
    											    <th>Email</th><td><?php echo ( (!empty($adminRow->accountEmail)&&(isset($adminRow->accountEmail))) ? $adminRow->accountEmail:'-');  ?></td>
    										    </tr>
    										    <tr>
    											    <th>Privileges</th><td><?php echo (!empty($adminRow->accountAccessPrivilege))?str_replace('|',', ',$adminRow->accountAccessPrivilege):'--';  ?></td>
    										    </tr>
    										    <tr>
    											    <th>Access Modules</th><td><?php echo (!empty($adminRow->accountAccessModule))?str_replace('|',', ',$adminRow->accountAccessModule):'--';  ?></td>
    										    </tr>
    										</table>
    									</div>
    								</div>
        							</div>
        						</div>
        						<div class="col-md-6">
        							<div class="card">
        								<div class="card-header">
        								    <div class="card-head-row">
        									    <h4 class="card-title">Account Access Info</h4>
        									</div>
        								</div>
							            <div class="card-body">
    							        <div class="table-responsive">
    										<table id="" class="table table-bordered">
    									    	<tr>
    											    <th>Last Login Time</th><td><?php echo !empty($adminRow->accountLastLoginTime)?date('d/M/Y H:i',$adminRow->accountLastLoginTime):'Never';?></td>
    										    </tr>
    										    <tr>
    											    <th>Login count</th><td><?php echo ( (!empty($adminRow->accountLogincount)&&(isset($adminRow->accountLogincount))) ? $adminRow->accountLogincount:'-');  ?></td>
    										    </tr>
    										    <tr>
    											    <th>LastLogin UserAgent</th><td><?php echo ( (!empty($adminRow->accountLastLoginUA)&&(isset($adminRow->accountLastLoginUA))) ? $adminRow->accountLastLoginUA:'-');  ?></td>
    										    </tr>
    										    <tr>
    											    <th>LastLogin IP Address</th><td><?php echo ( (!empty($adminRow->accountLastLoginIP)&&(isset($adminRow->accountLastLoginIP))) ? $adminRow->accountLastLoginIP:'-');  ?></td>
    										    </tr>
										    
    										</table>
    									</div>
    								</div>
        							</div>
        						</div>
        					</div>
                    	    <?php
                    	}
                	?>
				</div>
			</div>
		    <!--<footer class="footer">-->
			    <?php //footer();?>
			<!--</footer>-->
		</div>
	</div>
</body>
</html>
