<?php
  include('common/header.php');
  $city_name='';
  $state_id='';
  $city_id='';
  $state_name='';
  $country_id='';
  $country_name='';
  $ci_City_Name = $this->_page_data['ci_City_Name'];
  $st_State_Name = $this->_page_data['st_State_Name']; 
?>
<div id="messages" style="color: red;"><h5><?php echo $this->session->flashdata('message'); ?></h5></div>
 <?php 
 if ($this->uri->segment(4)) {
   foreach ($city as  $value1){
    $city_id=$value1['city_id'];
    $state_id=$value1['state_id'];
    $city_name=$value1['city_name'];
    $state_name=$value1['state_name'];
    $country_name=$value1['country_name'];
    $country_id=$value1['country_id'];
  }
}
?> 
  <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"  name="myForm" method="post" action="<?php echo base_url()?>index.php/<?php echo CITY;?>add_city" name="manage_client" onsubmit="return validateForm()" id="manage_client">
                   <input type="hidden" value="<?php echo $city_id; ?>" name="hidden" id="hidden">
                  <section class="panel panel-default">
                  <header class="panel-heading bg-info lt font-bold">City 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="qc_name">
                            <label for="qc_name" class="col-sm-5 control-label required"><?php echo $st_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 Name</option>
               
                                     <!-- <?php  
                                          foreach ($country as  $co) {
                                           if ($country_id == $co['country_id']) {?>
                                            <option value="<?php echo $value1['country_id'];?>" selected><?php echo $value1['country_name'] ; ?></option>
                                            <?php
                                           }else{?>
                                            <option value="<?php echo $co['country_id'];?>"><?php echo $co['country_name'] ; ?><?php 
                                          
                                          }
                                        }
                                        ?>  -->    
                                        <?php  
                                          foreach ($country as  $co) {?>                                  
                                            <option value="<?php echo $co['country_id'];?>" <?php if ($co['country_id']==$country_id) {
                                              echo 'selected="selected"';
                                          }; ?>><?php echo $co['country_name'] ; ?></option> <?php } ?>      
                                </select>  
                              <span id="country1" 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 $st_State_Name; ?>:<font style="color:red">*</font></label>
                               <div class="col-sm-7">  
                                  <select name="state_name" id="state_name" class="form-control">
                                    <option value="default">Select State Name</option>
                                 <!--     <?php if ($this->uri->segment(4)){?> 
                                      <option value="<?php echo $state_id;?>" selected><?php echo $state_name;?></option>
                                      <?php } ?> --> 
                                    
                                  

                                      <select name="state_name" id="state_name" >
                                     
                                  </select>
                                  <span id="state_name1" class="text-danger font-weight-bold"></span>
                                  
                                </div>
                             </div>
                          </div>
                          
                          <input type="hidden" id="fetched_state" value="<?php echo $state_id; ?>">
                          <input type="hidden" id="checking" value="<?php echo $check; ?>">

                      </div>  
                      <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 $ci_City_Name; ?>:<font style="color:red">*</font></label>
                              <div class="col-sm-7">
                                
                                <!-- <input type="text" id="city_name" name="city_name" value="<?php echo $value1['city_name']; ?>" placeholder="City Name" class="form-control"  /> -->
                                <input type="text" id="city_name" name="city_name" value="<?php echo $city_name; ?>" placeholder="City Name" class="form-control"  maxlength="20"/>
                                
                                 
                               <span id="city_name1" class="text-danger font-weight-bold"></span> 
                                <span><?php echo form_error('city_name'); ?></span>       
                              </div>
                            </div>
                       </div>

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

                     

                    
                    

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

              </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>
  //$(document).ready(function(){
     var checking = $('#checking').val();
  
     //var country_id = $('#country_id').val();
    if (checking=='edit') {
      get_state();
    }


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

  // });
}
 
 //});

//}
</script>
<script type="text/javascript">
  
function validateForm(){
  var error=false;  
  

  var country_id=$("#country_id").val();  
  if(country_id=='default')
  {
    $("#country1").html("Please Select Country Name");
    error=true; 
  }
  else
  {
    $('#country1').html("");
      
  }

  var city_name=$("#city_name").val();  
  if(city_name=='')
  {
    $("#city_name1").html("Please Enter City Name");
    error=true; 
  }
  else if(!isNaN(city_name))
  {
    $("#city_name1").html("City Name must be in alphabets");
    error=true;     
  }else{
    $('#city_name1').html("");
  }
  
  var state_name=$("#state_name").val();  
  if(state_name=='default')
  {
    $("#state_name1").html("Please Select State Name");
    error=true; 
  }
  else
  {
    $('#state_name1').html("");
      
  }

  
 var hidden=$("#hidden").val();   
  if(error==true)
  {
    return false;
  }
  else
  {
  if(hidden==''){
      var r = confirm("Are you sure you want to create City!");
      if (r==true) {return true;}else{return false;} 
    }else{
      var r = confirm("Are you sure you want to Edit City!");
      if (r==true) {return true;}else{return false;} 
    }
  }


    }
</script>