<script type="text/javascript">
/*$(document).ready(function(){
	$("#frm-user").submit(function(e){
            if(!validate_user()){
                e.preventDefault();
            }
	});
        
	
});*/
</script>
<div class="breadcrumb">
    <h1>Country</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Country</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 Country</div>
                <form action="" id='frm-country' method="post" enctype="multipart/form-data">
                    <?php if(isset($countryDetail->country_id) && $countryDetail->country_id!=0) { ?>
                       <input type="hidden" id="country_id" name="country_id" value="<?php echo $countryDetail->country_id ?>" />
                    <?php } ?>
                    <div class="row">
                    	<div class="col-md-6 form-group mb-3">
                            <label for="country_name">Country name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="country_name" name="country_name" value="<?php echo $countryDetail->country_name; ?>" placeholder="Enter country name">
                            <div class="invalid-tooltip" id="msg_country_name"></div>
                        </div>
                        
                        <div class="col-md-6 form-group mb-3">
                            <label for="country_short_code">Country Short code <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="country_short_code" name="country_short_code" value="<?php echo $countryDetail->country_shortcode; ?>" placeholder="Enter country short code">
                            <div class="invalid-tooltip" id="msg_country_short_code"></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="country_status" name="country_status">
                                <option value="active" <?php echo (isset($countryDetail) && $countryDetail->country_status == 'active') ? "selected='selected'" : ""; ?>>Active</option>
                                <option value="inactive" <?php echo (isset($countryDetail) && $countryDetail->country_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_name = $("#country_name").val();
      var country_short_code = $("#country_short_code").val();
      var country_status = $("#country_status").val();
      

      if($("#country_name").val()=='')
       {
          $("#country_name").addClass('error');
          $("#country_name").focus();
          $("#msg_country_name").html('Please input country').show();
          return false;
        }
        else if($("#country_short_code").val()=='')
       {

          if(country_name !='')
          {
              $("#country_name").removeClass('error');
              $("#msg_country_name").hide();
          }

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

          <?php if(!isset($_REQUEST['country_id']))
          { ?>  
              $.ajax({
                      url: "countryadd.php",
                      data: { act:"load",
                            country_name:country_name,country_short_code:country_short_code,country_status:country_status },
                      method: 'POST',
                      success: function(msg){
                          if(msg!=''){
                             window.location='countrylist.php';
                              //return false;
                          }
                      }
                  });
            <?php }else{ ?>

                $.ajax({      
                    url: "countryadd.php",
                    data: { act:"load",
                          country_name:country_name,country_short_code:country_short_code,country_status:country_status,country_id:<?php echo $_REQUEST['country_id']; ?> },
                    method: 'POST',
                    success: function(msg){
                        if(msg!=''){
                            window.location='countrylist.php';
                        }
                    }
                });

            <?php } ?>
      }

  });
      

</script>