<script type="text/javascript">
/*$(document).ready(function(){
	$("#frm-user").submit(function(e){
            if(!validate_user()){
                e.preventDefault();
            }
	});
        
	
});*/
</script>
<div class="breadcrumb">
    <h1>City</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New City</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 City</div>
                <form action="" id='frm-city' method="post" enctype="multipart/form-data">
                    <?php if(isset($cityDetail->city_id) && $cityDetail->city_id!=0) { ?>
                       <input type="hidden" id="city_id" name="city_id" value="<?php echo $cityDetail->city_id ?>" />
                    <?php } ?>
                    <div class="row">
                        <div class="col-md-6 form-group mb-3">
                            <label for="country_id">Country name <span class="asterisk">*</span></label>
                            <select class="form-control" id="country_id" name="country_id">
                            <option value="">Select Country</option>
                            <?php
                                  $countryobj = new country;
                                  $fields = array('country_id,country_name');
                                  $countryArg = array("fields"=>$fields);
                                  $countrydata = $countryobj->getAllRecords($countryArg);
                                  foreach ($countrydata as $ckey => $country_value) 
                                    { 
                                      ?>
                                        <option value="<?php echo $country_value->country_id; ?>" <?php echo (isset($cityDetail) && $cityDetail->country_id == $country_value->country_id) ? "selected='selected'" : ""; ?> ><?php echo $country_value->country_name; ?></option>
                              <?php }
                            ?>

                        </select>
                            <div class="invalid-tooltip" id="msg_country_id"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="state_id">State name <span class="asterisk">*</span></label>
                            <select class="form-control" id="state_id" name="state_id">
                            
                            </select>
                            <div class="invalid-tooltip" id="msg_state_id"></div>
                        </div>

                    	<div class="col-md-6 form-group mb-3">
                            <label for="state_name">City name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="city_name" name="city_name" value="<?php echo $cityDetail->city_name; ?>" placeholder="Enter city name">
                            <div class="invalid-tooltip" id="msg_city_name"></div>
                        </div>
                        
                        <div class="col-md-3 form-group mb-3">
                            <label class="control-label">Status</label>
                            <div class="controls">

                            <select class="form-control" id="city_status" name="city_status">
                                <option value="active" <?php echo (isset($cityDetail) && $cityDetail->city_status == 'active') ? "selected='selected'" : ""; ?>>Active</option>
                                <option value="inactive" <?php echo (isset($cityDetail) && $cityDetail->city_status == 'inactive') ? "selected='selected'" : ""; ?>>Inactive</option>
                            </select>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Submit">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
   $(document).ready(function() {

    

        $("#country_id").on('change',function(){

        var country_id = $("#country_id").val();
        $.ajax({
                  url: "cityadd.php?act=getstatedata&country_id="+country_id, 
                  type: "POST",
                  success: function(data)
                  {  
                    $("#state_id").html(data);  
                  }
             });
    });
   <?php if(!isset($_REQUEST['city_id']))
   { ?>
        $("#country_id").on('change',function(){

            var country_id = $("#country_id").val();
            $.ajax({
                      url: "cityadd.php?act=getstatedata&country_id="+country_id, 
                      type: "POST",
                      success: function(data)
                      {  
                        $("#state_id").html(data);  
                      }
                 });
        });
    <?php }else{ ?>
        
            var country_id = <?php echo $cityDetail->country_id; ?>;
            var state_id = <?php echo $cityDetail->state_id; ?>;
            $.ajax({
                      url: "cityadd.php?act=getstatedata&country_id="+country_id+"&state_id="+state_id, 
                      type: "POST",
                      success: function(data)
                      {  
                        $("#state_id").html(data);  
                      }
                 });
        
    <?php } ?>

    $( "form" ).on( "submit", function(e) {
      e.preventDefault();

      var country_id = $("#country_id").val();
      var state_id = $("#state_id").val();
      var city_name = $("#city_name").val();
      var city_status = $("#city_status").val();
      
      if($("#country_id").val()=='')
      {
          $("#country_id").addClass('error');
          $("#country_id").focus();
          $("#msg_country_id").html('Please select country').show();
          return false;
      }
      else if($("#state_id").val()=='')
      {
          if(country_id !='')
          {
            $("#country_id").removeClass('error');
            $("#msg_country_id").hide();
          }

          $("#state_id").addClass('error');
          $("#state_id").focus();
          $("#msg_state_id").html('Please select state').show();
          return false;
      }
      else if($("#city_name").val()=='')
       {
          if(state_id !='')
          {
            $("#state_id").removeClass('error');
            $("#msg_state_id").hide();
          }

          $("#city_name").addClass('error');
          $("#city_name").focus();
          $("#msg_city_name").html('Please input city name').show();
          return false;
        }
        else
        {
          if(city_name !='')
            {
              $("#city_name").removeClass('error');
              $("#msg_city_name").hide();
            }

          <?php if(!isset($_REQUEST['city_id']))
          { ?>  
              $.ajax({
                      url: "cityadd.php",
                      data: { act:"load",
                            country_id:country_id,state_id:state_id,city_name:city_name,city_status:city_status },
                      method: 'POST',
                      success: function(msg){
                          if(msg!=''){
                             window.location='citylist.php';
                              //return false;
                          }
                      }
                  });
            <?php }else{ ?>

                $.ajax({      
                    url: "cityadd.php",
                    data: { act:"load",
                          country_id:country_id,state_id:state_id,city_name:city_name,city_status:city_status,city_id:<?php echo $_REQUEST['city_id']; ?> },
                    method: 'POST',
                    success: function(msg){
                        if(msg!=''){
                            window.location='citylist.php';
                        }
                    }
                });

            <?php } ?>
      }

  });

});

</script>