<script type="text/javascript">
/*$(document).ready(function(){
	$("#frm-user").submit(function(e){
            if(!validate_user()){
                e.preventDefault();
            }
	});
        
	
});*/
</script>
<div class="breadcrumb">
    <h1>State</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New State</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 State</div>
                <form action="" id='frm-country' method="post" enctype="multipart/form-data">
                    <?php if(isset($stateDetail->state_id) && $stateDetail->state_id!=0) { ?>
                       <input type="hidden" id="state_id" name="state_id" value="<?php echo $stateDetail->state_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($stateDetail) && $stateDetail->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_name">State name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="state_name" name="state_name" value="<?php echo $stateDetail->state_name; ?>" placeholder="Enter state name">
                            <div class="invalid-tooltip" id="msg_state_name"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="state_shortcode">State Short Code <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="state_shortcode" name="state_shortcode" value="<?php echo $stateDetail->state_shortcode; ?>" placeholder="Enter state shortcode">
                            <div class="invalid-tooltip" id="msg_state_shortcode"></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="state_status" name="state_status">
                                <option value="active" <?php echo (isset($stateDetail) && $stateDetail->state_status == 'active') ? "selected='selected'" : ""; ?>>Active</option>
                                <option value="inactive" <?php echo (isset($stateDetail) && $stateDetail->state_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">
   
    $( "form" ).on( "submit", function(e) {
      e.preventDefault();

      var country_id = $("#country_id").val();
      var state_name = $("#state_name").val();
      var state_shortcode = $("#state_shortcode").val();
      var state_status = $("#state_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_name").val()=='')
       {
          if(country_id !='')
          {
            $("#country_id").removeClass('error');
            $("#msg_country_id").hide();
          }

          $("#state_name").addClass('error');
          $("#state_name").focus();
          $("#msg_state_name").html('Please input state').show();
          return false;
        }
        else if($("#state_shortcode").val()=='')
       {
          if(state_name !='')
          {
              $("#state_name").removeClass('error');
              $("#msg_state_name").hide();
          }

          $("#state_shortcode").addClass('error');
          $("#state_shortcode").focus();
          $("#msg_state_shortcode").html('Please input state short code').show();
          return false;
        }
        else
        {
          
            if(state_shortcode !='')
            {
                $("#state_shortcode").removeClass('error');
                $("#msg_state_shortcode").hide();
            }

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

                $.ajax({      
                    url: "stateadd.php",
                    data: { act:"load",
                          country_id:country_id,state_name:state_name,state_shortcode:state_shortcode,state_status:state_status,state_id:<?php echo $_REQUEST['state_id']; ?> },
                    method: 'POST',
                    success: function(msg){
                        if(msg!=''){
                            window.location='statelist.php';
                        }
                    }
                });

            <?php } ?>
      }

  });
      

</script>