<?php
  include('common/header.php');
  $user_id='';
  $last_name='';
  $first_name='';
  $designation='';
  $mobile_no='';
  $email_id='';
  $company_branch_id='';
  $accessible_branches='';
?>
<div id="messages" style="color: red;"><h5><?php echo $this->session->flashdata('message'); ?></h5></div>
<?php 
 
  //if ($this->uri->segment(4)) {
    foreach ($users as  $value)
	{
    $user_id=$value['user_id'];
    $company_branch_id=$value['company_branch_id'];
    $user_type_id=$value['user_type_id'];
    $last_name= $value['last_name'];
    $first_name=$value['first_name'];
    $designation=$value['designation'];
    $mobile_no= $value['mobile_no'];
    $email_id=$value['email_id'];
    $accessible_branches=$value['accessible_branches'];
    
                      
   } 
   //echo  $acb ;
  //}
 ?>
 
  <div class="">
    <div class="col-xm-12">
      <div class="widget-box light-border">
        
        <div class="widget-body">
          <div class="widget-main">
            <div class="row">
              <div class="col-sm-12">
          
                     <form class="form-horizontal" method="post" action="<?php echo base_url()?>index.php/<?php echo USER_PATH;?>add_user" name="manage_client" onsubmit="return validateForm()" id="manage_client">
                      <input type="hidden" value="<?php echo $user_id ?>" name="hidden" id="hidden">
                     
                  <section class="panel panel-default">
                  <header class="panel-heading bg-info lt font-bold"><?php echo $u_User_Details; ?></header>
                  <!-- panel body start -->
                  <div class="panel-body bs-example form-horizontal">
                    <div class="">
                     <div class="hr hr12 dotted"></div>
                       <div class="">
                        <!-- main col-sm-12 -->
                      <div class="col-sm-12"> 

                         <div class="col-sm-6">                            
                           
                            <div class="form-group">  
                              <label class="col-sm-5 control-label" for="form-field-2" ><?php echo $u_Branch_Name;?><font style="color:red">*</font></label>
                                 <div class="col-sm-7">  
                                   <select name="branch" id="branch" required>
                                    <option value="default">Select Branch Name</option>
                                       <?php											
                                            foreach($branch as $value1) 
											{
												if ($company_branch_id == $value1['company_branch_id'])
												{
													?>
													<option value="<?php echo $value1['company_branch_id'] ?>" selected ><?php echo $value1['company_branch_name'] ; ?></option>
													<?php
												}
												else
												{
													?>
													<option value="<?php echo $value1['company_branch_id'] ?>"><?php echo $value1['company_branch_name'] ; ?></option>
													<?php
												}
											}  
                                      ?>
                                   </select>
                                   <span id="branch1" class="text-danger font-weight-bold"></span>
                                 </div>
                            </div>
                          </div>
                          

                        <div class="col-sm-6">
                        <div class="form-group">  
                          <label class="col-sm-5 control-label" for="form-field-2" ><?php echo $u_User_Type; ?>:<font style="color:red">*</font></label>
                            <div class="col-sm-7">  
                              <select name="user_type" id="user_type" required>

                                <option value="default">Select User Type</option>
                                <?php  
                                           foreach ($user_type as  $value3) {
                                           if ($user_type_id == $value3['user_type_id']) {?>
                                            <option value="<?php echo $value['user_type_id'] ?>"selected><?php echo $value3['user_type'] ; ?></option>
                                            <?php
                                           }else{?>
                                           <option value="<?php echo $value3['user_type_id'] ?>"><?php echo $value3['user_type'] ; ?></option><?php 
                                          
                                          }} 
                                         
                                      ?>
                              </select>
                              <span id="user_type1" class="text-danger font-weight-bold"></span>
                              
                            </div>
                        </div>
                        </div>

                          <div class="col-sm-6">
                            <div class="form-group" id="client_code">
                              <label for="client_code" class="col-sm-5 control-label required"><?php echo $u_First_Name; ?>:<font style="color:red">*</font></label>
                              <div class="col-sm-7">
                                 
                                <input type="text" name="first_name" value="<?php echo $first_name;?>" id="first_name" onkeypress="return RestrictSpace()" maxlength="50" placeholder="First Name" class="form-control"  />

                                <span id="f_name" class="text-danger font-weight-bold"></span> 
                                <span><?php echo form_error('first_name'); ?></span>              
                              </div>
                            </div>
                          </div>
                                                
                          <div class="col-sm-6">                            
                             
                            <div class="form-group" id="client_code">
                              <label for="client_code" class="col-sm-5 control-label required"><?php echo $u_Last_Name; ?>:<font style="color:red">*</font></label>
                              <div class="col-sm-7">
                                 
                                <input type="text" name="last_name" value="<?php echo $last_name;?>" id="last_name" onkeypress="return RestrictSpace()" placeholder="Last Name" maxlength="50" class="form-control"  /> 

                                 <span id="l_name" class="text-danger font-weight-bold"></span> 
                                 <span><?php echo form_error('last_name'); ?></span>          
                              </div>
                            </div>
                          </div>
   
                          <div class="col-sm-6">                            
                              <div class="form-group">  
                              <label class="col-sm-5 control-label" for="form-field-2" ><?php echo $u_Accessible_Company_Branches;?><font style="color:red">*</font></label>                              
								<?php echo $accessible_branches;?>
                                 <div class="col-sm-7">  
                                  
                                      <select  class="js-example-basic-multiple" name="access_branch[]" multiple="multiple" id="access_branch" style="width: 250px;">
                         <!--  <option value="default">Select Accessible Company Branche</option> -->
                                       <?php                         
                           $acb = explode(',' ,$accessible_branches);
                           // print_r($acb);
                                    
                               foreach($branch as $bnc)
                                {?>
                        <option value="<?php echo $bnc['company_branch_id'] ?>"<?php  if (in_array($acb,$bnc['company_branch_id'])) {
                          echo 'selected="selected"';
                        } ?>><?php echo $bnc['company_branch_name']; ?></option>
                      <?php
                      }?>
                            
                                   </select>
                                 </div>
                            </div> 
                       </div> 

                         <!-- </div> -->
                         <!-- main col-sm-12  close-->
                       </div> <!-- blank close -->
        </div><!-- blank close -->
      </div>   <!-- panel body close -->
    </section> <!-- section close -->
       <!-- User contact start -->
    <section class="panel panel-default">
      <header class="panel-heading bg-info lt font-bold"><?php echo $u_Contact_Details; ?></header>
       <div class="panel-body bs-example form-horizontal">
        <div class="">
          <div class="hr hr12 dotted"></div>
          <div class="">
            <div class="col-sm-12">

              <div class="col-sm-6">  
                <div class="form-group" id="client_code">
                              <label for="client_code" class="col-sm-5 control-label required"><?php echo $u_Designation; ?>:<font style="color:red">*</font></label>
                              <div class="col-sm-7">
                               
                                <input type="text" name="designation" value="<?php echo $designation;?>" id="designation" placeholder="Designation" class="form-control" maxlength="50"  />   

                                   <span id="designation1" class="text-danger font-weight-bold"></span>
                                   <span><?php echo form_error('designation'); ?></span>      
                              </div>
                            </div>
              </div>

  


                        <div class="col-sm-6">
                              <div class="form-group">
                             <label for="client_code" class="col-sm-5 control-label required"> <?php echo $u_Mobile_Number; ?>:<font style="color:red">*</font></label>
                              <div class="col-sm-7">
                                
                                <input type="text" name="mobile_number" value="<?php echo $mobile_no;?>" id="mobile_number" onkeypress="return RestrictSpace()" placeholder="Mobile Number" class="form-control" maxlength="10"  />  

                                <span id="mobile1" class="text-danger font-weight-bold"></span>
                                <span><?php echo form_error('mobile_number'); ?></span>          
                              </div>
                            </div>
                          </div>



              <div class="col-sm-6">    
                <div class="form-group">
                             <label for="client_code" class="col-sm-5 control-label required"><?php echo $u_Email_Id; ?>:<font style="color:red">*</font></label>
                              <div class="col-sm-7">
                                 
                                <input type="text" name="email_id" value="<?php echo $email_id;?>" id="email_id" onkeypress="return RestrictSpace()" placeholder="Email Id" class="form-control" maxlength="50" />

                                <span id="email1" class="text-danger font-weight-bold"></span> 
                                <span><?php echo form_error('email_id'); ?></span>         
                              </div>
                  </div>
              

                         
             </div> <!-- col-ms-6 close -->

          
            </div>
          </div>
        </div>
      </div>
    </section>


    <!-- User location start -->


              </div>
            </div>
          <!-- </div>  --> 
        </div>
      </div>
      </div>

      </div>

  <div class="clearfix form-actions">
    <div class="col-md-offset-5 col-md-5">
      
      <button class="btn btn-info" type="submit">
        Submit
      </button>
    </div>
  </div>

  </form>

  <div class="space-4"></div>
   
    <!-- /.main-content --> 
  </div>
  </div>
  </div></section>

<?php
  include('common/footer.php');
?>

<script type="text/javascript">
    

function validateForm(){

  var error=false;  
  var branch=$("#branch").val();  
  if(branch=='default')
  {
    $("#branch1").html("Please Select Branch Name");
    error=true; 
  }
  else
  {
    $('#branch1').html("");
      
  }
  
  var user_type=$("#user_type").val();  
  if(user_type=='default')
  {
    $("#user_type1").html("Please Select User_Type");
    error=true; 
  }
  else
  {
    $('#user_type1').html("");
      
  }
  
  var first_name=$("#first_name").val();  
  if(first_name=="")
  {
    $("#f_name").html("Please Enter First Name");
    error=true; 
  }
  else if(!isNaN(first_name))
  {
    $("#f_name").html("First Name must be in characters");
    error=true; 
        
  }else{
    $('#f_name').html("");

  }
  
  
  var last_name=$("#last_name").val();  
  if(last_name=="")
  {
    $("#l_name").html("Please Enter Last Name");
    error=true; 
  }
  else if(!isNaN(first_name))
  {
    $("#l_name").html("Last Name must be in characters");
    error=true;    
  }else{
    $('#l_name').html("");
  }
  
  var designation=$("#designation").val();  
  if(designation=="")
  {
    $("#designation1").html("Please Enter Designtation");
    error=true; 
  }
  else
  {
    $('#designation1').html("");
      
  }

  
  var mobile_number=$("#mobile_number").val();  
  if(mobile_number=="")
  {
    $("#mobile1").html("Please Enter Mobile Number");
    error=true; 
  }
  else if(isNaN(mobile_number))
  {
    $("#mobile1").html("Mobile Number must be in numbers");
    error=true;    
  }else{
    $('#mobile1').html("");
  }


  
  var email_id=$("#email_id").val();  
  if(email_id=="")
  {
    $("#email1").html("Please Enter Email Id");
    error=true; 
  }
  else if(email_id.indexOf('@') <= 0)
  {
    $("#email1").html("Invalid Email Id");
    error=true;
    
      
  }else if((email_id.charAt(email_id.length-4)!='.') && (email_id.charAt(email_id.length-3)!='.')){
    $("#email1").html("Invalid Email Id");
    error=true;
  }
  else{
    $('#email1').html("");
  }
  var hidden=$("#hidden").val(); 
  if(error==true)
  {
    return false;
  }
  else
  {
  if(hidden==''){
      var r = confirm("Are you sure you want to create User!");
      if (r==true) {return true;}else{return false;} 
    }else{
      var r = confirm("Are you sure you want to Edit User!");
      if (r==true) {return true;}else{return false;} 
    } 
  } 
}
</script>
<script>
$(document).ready(function() {
    $('.js-example-basic-multiple').select2();
});
</script>
<script>
  function RestrictSpace() {
    if (event.keyCode == 32) {
        return false;
    }
}
</script>