<?php
	include('common/header.php');
  $company_branch_name='';
  $company_branch_pan_no='';
  $company_branch_gst_no='';
  $company_branch_address1='';
  $company_branch_address2='';
  $postal_code='';
  $contact_person_name='';
  $branch_mobile_no='';
  $branch_email_id='';  
  $state_id='';
  $city_id='';
  $state_name='';
  $city_name='';
  $company_branch_id="";
  $std_code='';
  $country_id='';
  $branch_salable_geography_id='';
  ?>
 <?php 
 if ($this->uri->segment(4)) {
   foreach ($company_branch as  $value){
    $company_branch_id=$value['company_branch_id'];
    $country_id=$value['country_id'];
    $state_id=$value['state_id'];
    $city_id=$value['city_id'];
    $state_name=$value['state_name'];
    $city_name=$value['city_name'];
    $branch_salable_geography_id=$value['branch_salable_geography_id'];
    $std_code=$value['std_code'];

   $company_branch_name=$value['company_branch_name'];
   $company_branch_pan_no=$value['company_branch_pan_no'];
   $company_branch_gst_no= $value['company_branch_gst_no'];
   $company_branch_address1= $value['company_branch_address1'];
   $company_branch_address2= $value['company_branch_address2'];
   $postal_code= $value['postal_code'];
   $contact_person_name= $value['contact_person_name'];
   $branch_mobile_no=$value['branch_mobile_no'];
   $branch_email_id=  $value['branch_email_id'];


   }
 }  
 ?>
  <div id="messages" style="color: red;"><h5><?php echo $this->session->flashdata('message'); ?></h5></div>
	<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 COMPANY_BRANCH;?>add_company_branch" name="manage_client" onsubmit="return validateForm()" id="manage_client">
                  <input type="hidden" value="<?php echo $company_branch_id ?>" name="hidden" id="hidden">
                  <section class="panel panel-default">
                  <header class="panel-heading bg-info lt font-bold">Branch Company 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" id="client_code">
                              <label for="client_code" class="col-sm-5 control-label required"><?php echo $comp_Branch_Company_Name; ?>:<font style="color:red">*</font></label>
                              <div class="col-sm-7">
                                <input type="text" name="company_branch_name" value="<?php echo $company_branch_name;?>" id="company_branch_name" placeholder="Branch Company Name" maxlength="75" class="form-control"  />
                                <span id="company_branch_name1" class="text-danger font-weight-bold"></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 $comp_Branch_PAN_Number; ?>:</label>
                              <div class="col-sm-7">
                                
                                <input type="text" name="branch_pan" value="<?php echo $company_branch_pan_no;?>" id="branch_pan" maxlength="10" onkeypress="return RestrictSpace()" placeholder="Branch PAN  Number" class="form-control"  />
                                <span id="branch_pan1" class="text-danger font-weight-bold"></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 $comp_Branch_GST_Number; ?>:</label>
                              <div class="col-sm-7">
                               
                                <input type="text" name="branch_gst" value="<?php echo $company_branch_gst_no;?>" id="branch_gst" maxlength="15"  onkeypress="return RestrictSpace()" placeholder="Branch GST  Number" class="form-control"  />                                 
                                <span id="branch_gst1" class="text-danger font-weight-bold"></span>             
                              </div>
                            </div>

                          </div>

                          </div>
                         <!-- main col-sm-12  close-->
                       </div> <!-- blank close -->
        </div><!-- blank close -->
      </div>   <!-- panel body close -->
    </section> <!-- section close -->

    <!-- client location start -->
    <section class="panel panel-default">
      <header class="panel-heading bg-info lt font-bold">Location 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">
                  <label class="col-sm-5 control-label required" for="form-field-2 "><?php echo $comp_Branch_Address_line_1; ?>:<font style="color:red">*</font></label>
                  <div class="col-sm-7">
                    
                    <input type="text" name="branch_addressl1" value="<?php echo $company_branch_address1;?>" id="branch_addressl1" placeholder="Branch  Address line 1" class="form-control" maxlength="85"  />
                    <span id="branch_addressl11" class="text-danger font-weight-bold"></span>                  
                  </div>
                </div>
              </div>
              <div class="col-sm-6">               
                <div class="form-group" id="qc_name">
                  <label for="qc_name" class="col-sm-5 control-label required"><?php echo $comp_Branch_Address_line_2; ?>:</label>
                  <div class="col-sm-7">
                                              
                      <input type="text" name="branch_addressl2" value="<?php echo $company_branch_address2;?>" id="branch _addressl2" maxlength="85" placeholder="Branch  Address line 2" class="form-control"  />                    
                  </div>
                </div>
              </div>

                <div class="col-sm-6">   
                <div class="form-group" id="qc_name">
                  <label for="qc_name" class="col-sm-5 control-label required"><?php echo $comp_Branch_Country; ?>:<font style="color:red">*</font></label>
                  <div class="col-sm-7">                             
                    <select name="country_id" id="country_id" onchange="get_state(this.value);" class="form-control">
                     <option value="default">Select Country </option>
                                     <?php  
                                          foreach ($country as  $co) {
                                           if ($country_id == $co['country_id']) {?>
                                            <option value="<?php echo $co['country_id'] ?>"selected><?php echo $co['country_name'] ; ?></option>
                                            <?php
                                           }else{?>
                                           <option value="<?php echo $co['country_id'] ?>"><?php echo $co['country_name'] ; ?></option><?php 
                                          
                                          }}
                                         
                                      ?>
                    </select>
                    <span id="country_id1" class="text-danger font-weight-bold"></span>                     
                  </div>
                </div>
              </div>



              <div class="col-sm-6">
                
                <div class="form-group" id="qc_name">
                  <label for="qc_name" class="col-sm-5 control-label required"><?php echo $comp_Branch_State; ?>:<font style="color:red">*</font></label>
                  <div class="col-sm-7">                             
                    <select name="state_id" id="state_id" onchange="get_city(this.value);" class="form-control"  >
                  <?php if ($this->uri->segment(4)){?> 
                    <option value="<?php echo $state_id;?>"><?php echo $state_name;?></option>
                    <?php }else{?>
                      <option value="default" selected>Select State</option> 
                    <?php } ?>

                    
                                    
                  </select>
                  <span id="state_id1" class="text-danger font-weight-bold"></span>                      
                  </div>                
                </div>
              </div>

            
             
          </div>  




              <!-- col-ms-6 close -->
            <div class="col-sm-12">
              <div class="col-sm-6">
                <div class="form-group" id="qc_name">
                  <label for="qc_name" class="col-sm-5 control-label required"><?php echo $comp_Branch_City; ?>:<font style="color:red">*</font></label>
                  <div class="col-sm-7">                             
                   <select name="city_id" id="city_id" onChange="" class="form-control">                    
                   <?php if ($this->uri->segment(4)){?> 
                    <option value="<?php echo $city_id;?>"><?php echo $city_name;?></option>
                  <?php }else{?>
                    <option value="default">Select City </option>
                  <?php } ?>
                 
                </select> 
                <span id="city_id1" class="text-danger font-weight-bold"></span>   
                  
                  </div>
                </div>
              </div> 

              <div class="col-sm-6">               
                <div class="form-group" id="qc_name">
                  <label for="qc_name" class="col-sm-5 control-label required"><?php echo $comp_Branch_Saleble_Geography; ?>:</label>
                  <div class="col-sm-7">                             
                    <select name="geography_id" id="geography_id" onChange="" class="form-control">
                     <option value="default">Select Branch Saleble Geography</option>
                     <?php  
                                          foreach ($geography as  $ge) {
                                           if ($branch_salable_geography_id == $ge['branch_salable_geography_id']) {?>
                                            <option value="<?php echo $ge['branch_salable_geography_id'] ?>"selected><?php echo $ge['saleble_geography_name'] ; ?></option>
                                            <?php
                                           }else{?>
                                           <option value="<?php echo $ge['branch_salable_geography_id'] ?>"><?php echo $ge['saleble_geography_name'] ; ?></option><?php 
                                          
                                          }}
                                         
                                      ?>
                    </select>  
                    <span id="geography_id1" class="text-danger font-weight-bold"></span>                   
                  </div>
                </div>
              </div>  

               
              <div class="col-sm-6">  
                 <div class="form-group" >
                  <label for="pincode" class="col-sm-5 control-label required"><?php echo $comp_Branch_Postal_Code; ?>:<font style="color:red">*</font></label>
                  <div class="col-sm-7">
                                            
                  <input type="text" name="postalcode" value="<?php echo $postal_code;?>" id="postalcode" placeholder="Postal Code" onkeyup = "Validate_number(this)"  maxlength="6" onkeypress="return RestrictSpace()" class="form-control"  />

                  <span id="postalcode1" class="text-danger font-weight-bold"></span>                      
                  </div>
                </div>
              </div>
          </div> 
                <!-- tax -->
                
                <!-- tax -->
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>


    <section class="panel panel-default">
      <header class="panel-heading bg-info lt font-bold">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">
                    <label for="client_code" class="col-sm-5 control-label required"><?php echo $comp_Branch_Contact_Person_Name; ?>:<font style="color:red">*</font></label>
                      <div class="col-sm-7">
                        
                          <input type="text" name="contact_person_name" value="<?php echo $contact_person_name; ?>" id="contact_person_name" maxlength="75" placeholder="Contact Person Name" class="form-control"/>
                                <span id="contact_person_name1" 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 $comp_Branch_Mobile_Number; ?>:<font style="color:red">*</font></label>
                        <div class="col-sm-1" >
                                 <input type="text" value="<?php echo $std_code; ?>"  name="std_code" id="std_code" class="form-control"   style=" width: 120%;" readonly> 
                              </div>
                          <div class="col-sm-6">
                             
                               <input type="text" name="branch_mobile" value="<?php echo $branch_mobile_no ?>" id="branch_mobile" onkeyup = "Validate_number(this)"  maxlength="15" placeholder="Branch Mobile Number" class="form-control"  />
                                <span id="branch_mobile1" class="text-danger font-weight-bold"></span>             
                              </div>

                            </div>

                           <input type="hidden" id="fetched_state" value="<?php echo $state_id; ?>">
                           <input type="hidden" id="fetched_city" value="<?php echo $city_id; ?>">
                          <input type="hidden" id="checking" value="<?php echo $check; ?>">


                          </div>

                          <div class="col-sm-6">
                           <div class="form-group">
                             <label for="client_code" class="col-sm-5 control-label required"><?php echo $comp_Branch_Email_Id; ?>:<font style="color:red">*</font></label>

                              <div class="col-sm-7">
                               
                                <input type="text" name="branch_email" value="<?php echo $branch_email_id;?>" id="branch_email" onkeypress="return RestrictSpace()" placeholder="Branch Email Id" maxlength="75" class="form-control" />

                                <span id="branch_email1" class="text-danger font-weight-bold"></span>             
                              </div>
                            </div>
                        </div>

                

               

        

               

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

            
                <!-- tax -->
                
                <!-- tax -->
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
							</div>
						</div>
					</div>	
				</div>
			</div>
		</div>
		<!-- /.col -->
	</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>
		<!-- /.page-row -->
        <!-- /.page-content --> 
    </div>
    <!-- /.main-content --> 
	</div>
    <!-- /#ace-settings-container --> 
	</div>
	<!-- /.main-container-inner --> 	
<?php
	include('common/footer.php');
?>
<!-- <script type="text/javascript">
$(document).ready(function() {
    $('form:first *:input[type!=hidden]:first').focus();
});


  $( function() {
    $( "#datepicker" ).datepicker();
  } );
</script> -->
<script>
function Validate_number(txt) 
{
     txt.value = txt.value.replace(/[^0-9\n\r]+/g, '');
}
  var checking = $('#checking').val();
     //var country_id = $('#country_id').val();
    if (checking=='edit') {
      get_state();
      get_city();

    }
  function get_state()
{
  var country_id = $('#country_id').val();
  var fetched_state = $('#fetched_state').val();
  if (fetched_state!='') {
    var state_id=fetched_state;
  }
  $.ajax({
    url:"<?php echo base_url();?>index.php/gc/company_branch/get_state",
    method:"POST",
    data:{
      country_id:country_id,
      state_id:state_id
    },
    success:function(data)
    {
     $('#state_id').html(data);
    }
   });

  $.ajax({
    url:"<?php echo base_url();?>index.php/gc/company_branch/get_std_code",
    method:"POST",
    data:{
      country_id:country_id
    },
    success:function(data)
    {
     $('#std_code').val(data);
    }
   });
}

function get_city()
{
  var state_id = $('#state_id').val();
   var fetched_city = $('#fetched_city').val();
  if (fetched_city!='') {
    var city_id=fetched_city;
  }
  $.ajax({
    url:"<?php echo base_url();?>index.php/gc/company_branch/get_city",
    method:"POST",
    data:{
      state_id:state_id,
      city_id:city_id
    },
    success:function(data)
    {
     $('#city_id').html(data);
    }
   });
}
</script>
<script type="text/javascript">
          
function validateForm() {

       // var reg_gst ='/^([0-9]{2}[a-zA-Z]{4}([a-zA-Z]{1}|[0-9]{1})[0-9]{4}[a-zA-Z]{1}([a-zA-Z]|[0-9]){3}){0,15}$/';
  var reg_gst ='^([0][1-9]|[1-2][0-9]|[3][0-5])([a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}[1-9a-zA-Z]{1}[zZ]{1}[0-9a-zA-Z]{1})+$';

  var reg_pan = '^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}?$';                                                                            
      
  var error=false;  
  var company_branch_name=$("#company_branch_name").val();  
  if(company_branch_name=='')
  {
    $("#company_branch_name1").html("Please Enter Company Branch Name");
    error=true; 
  }
  else if(!isNaN(company_branch_name))
  {
    $("#company_branch_name1").html("Please Enter Company Branch Name");
    error=true;      
  }else{
    $('#company_branch_name1').html("");
  }

  
  var branch_pan=$("#branch_pan").val().trim();  
  if (branch_pan!='') {
  
  if(!branch_pan.match(reg_pan)){
    $("#branch_pan1").html("PAN number is not valid");
    error=true;
  }else
  {
    $('#branch_pan1').html("");
      
  }
}
/*   var branch_gst=$("#branch_gst").val();
  var gt=branch_gst.slice(2, 12);
  if (branch_gst!='') {  
  if(!branch_gst.match(reg_gst)){
    $("#branch_gst1").html("GST number is not valid");
    error=true;
  }else if(gt!=branch_pan){
	$("#branch_gst1").html("Please check the gst number you have entered");
    error=true;
  }else
  {
    $('#branch_gst1').html("");
      
  }
} */

	const gstRegex = /^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/;
  var branch_gst=$("#branch_gst").val().trim(); 
  var gt=branch_gst.slice(2, 12);
  if(branch_gst!="")
  {
   if (!gstRegex.test(branch_gst)) {
         $("#branch_gst1").html("Please enter valid gst number");
    error=true;
    } else {
       $('#branch_gst1').html("");
    }
  }

  var branch_addressl1=$("#branch_addressl1").val().trim();  
  if(branch_addressl1=='')
  {
    $("#branch_addressl11").html("Please fill the Address line 1 field");
    error=true; 
  }
  else
  {
    $('#branch_addressl11').html("");
      
  }
  
  var country_id =$("#country_id").val();  
  if(country_id =='default')
  {
    $("#country_id1").html("Please Select Country Name field");
    error=true; 
  }
  else
  {
    $('#country_id1').html("");
      
  }
  // var geography_id =$("#geography_id").val();  
  // if(geography_id =='default')
  // {
  //   $("#geography_id1").html("Please Select Branch Salable Geography Name field");
  //   error=true; 
  // }
  // else
  // {
  //   $('#geography_id1').html("");
      
  // }
  var state_id =$("#state_id").val();  
  if(state_id =='default')
  {
    $("#state_id1").html("Please Select State  Name field");
    error=true; 
  }
  else
  {
    $('#state_id1').html("");
      
  }
  var city_id =$("#city_id").val();  
  if(city_id =='default')
  {
    $("#city_id1").html("Please Select City  Name field");
    error=true; 
  }
  else
  {
    $('#city_id1').html("");
      
  }
  var postalcode  =$("#postalcode").val();  
  if(postalcode  =='')
  {
    $("#postalcode1").html("Please fill the Postal Code field");
    error=true; 
  }else if(isNaN(postalcode)){
    $("#postalcode1").html("user must write digits only not characters");
    error=true;
  }
  else
  {
    $('#postalcode1').html("");
      
  }
 
  var contact_person_name  =$("#contact_person_name").val();  
  if(contact_person_name  =='')
  {
    $("#contact_person_name1").html("Please fill the Contact Person Name field");
    error=true; 
  }else if(!isNaN(contact_person_name)){
    $("#contact_person_name1").html("**only characters are allowed");
    error=true;
  }
  else
  {
    $('#contact_person_name1').html("");
      
  }
  var branch_mobile  =$("#branch_mobile").val();  
//alert(branch_mobile.length)
  if(branch_mobile  =='')
  {
    $("#branch_mobile1").html("Please fill the Branch Mobile Number field");
    error=true; 
  }
  else 
	if(branch_mobile.length<10)
	{
		$("#branch_mobile1").html("Please fill the 10 digit Branch Mobile Number");
    error=true; 
	}
  else
  {
    $('#branch_mobile1').html("");    
  }


  var branch_email  =$("#branch_email").val();  
  if(branch_email  =='')
  {
    $("#branch_email1").html("Please fill the email id field");
    error=true; 
  }
  else if(branch_email!='')
  {
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if(!emailReg.test(branch_email)) {
		$("#branch_email1").html('Please Enter a valid Email Id');
		 error=true; 
	}else{
		$("#branch_email1").html('');
	}
   }
  /* else if(branch_email.indexOf('@') <= 0 ){
    $("#branch_email1").html("Invalid  email format");
    error=true;
  }else if((branch_email.charAt(branch_email.length-4)!='.') && (branch_email.charAt(branch_email.length-3)!='.')){
    $("#branch_email1").html("Invalid  email format");
    error=true;
  }
  else
  {
    $('#branch_email1').html("");    
  } */
  var hidden=$("#hidden").val();
  if(error==true)
  {
    return false;
  }
  else
  {
   if(hidden==''){
      var r = confirm("Are you sure you want to create Company Branch!");
      if (r==true) {return true;}else{return false;} 
    }else{
      var r = confirm("Are you sure you want to Edit Company Branch!");
      if (r==true) {return true;}else{return false;} 
    }
  }
}   

function RestrictSpace() {
    if (event.keyCode == 32) {
        return false;
    }
}

</script>