<div class="content-wrapper">
   <div class="content-header">
      <div class="container-fluid">
         <div class="row mb-2">
            <div class="col-sm-6">
               <h1 class="m-0 text-dark">Add Camp</h1>
            </div>
            <div class="col-sm-6">
               <ol class="breadcrumb float-sm-right">
                  <li class="breadcrumb-item"><a href="<?php echo base_url() ?>admin/admin/dashboard">Dashboard</a></li>
                  <li class="breadcrumb-item"><a href="#">Sharnarthi</a></li>
                  <li class="breadcrumb-item active">Add Camp </li>
               </ol>
            </div>
         </div>
      </div>
   </div>
   <section class="content">
      <div class="container-fluid">
         <div class="card card-default">
            <div class="card-header">
               <h3 class="card-title">Add New Camp</h3>
               <div class="card-tools">
                  <a href="<?php echo base_url()?>/admin/sharnarthi/view_all_camp" class="btn btn-success btn-sm"><i class="fa fa-eye" aria-hidden="true"></i> View All Camp</a>
               </div>
            </div>
            <?php echo form_open_multipart('admin/sharnarthi/add_camp', ['id' => 'camp_form']); ?>
            <div class="card-body">
               <div class="row">
                  <div class="col-md-6">
                     <div class="form-group">
                        <label for="inputEmail" class="col-sm-12 col-form-label">Camp Name (शिविर का नाम)<span class="required">*</span></label>
                        <div class="col-sm-12">
                           <input type="text" class="form-control" name="camp_name" placeholder="Enter Camp Name" required="required" >
                        </div>
                     </div>
                  </div>
                  <div class="col-md-6">
                     <div class="form-group">
                        <label for="inputEmail" class="col-sm-12 col-form-label">Camp Head Name (शिविर प्रमुख का नाम)<span class="required">*</span></label>
                        <div class="col-sm-12">
                           <input type="text" class="form-control" name="camp_head_name" placeholder="Enter Camp Head Name" required="required" >
                        </div>
                     </div>
                  </div>
                  
                  <div class="col-sm-12">
                     <div class="form-group">
                        <label for="inputName" class="col-sm-12 col-form-label">Camp Address (शिविर का पता) <span class="required">*</span></label>
                        <div class="col-sm-12">
                           <textarea type="text" name="camp_address" class="form-control" placeholder="Enter Camp Address" required></textarea>
                        </div>
                     </div>
                  </div>
                  <div class="col-sm-3">
                     <div class="form-group">
                        <label for="inputName" class="col-sm-12 col-form-label">Camp Pin Code (शिविर पिन कोड) <span class="required">*</span></label>
                        <div class="col-sm-12">
                           <input type="number" name="camp_pin_code" id="pin_code" class="form-control" placeholder="Enter Camp Pin Code" required>
                        </div>
                     </div>
                  </div>
                  <div class="col-sm-3">
                     <div class="form-group">
                        <label for="inputName" class="col-sm-12 col-form-label">Camp Post Office (शिविर का डाकघर) <span class="required">*</span></label>
                        <div class="col-sm-12">
                           <select name="camp_post_office" id="area" class="form-control" required>
                              <option value="">Enter Pin Code First</option>
                           </select>
                        </div>
                     </div>
                  </div>
                  <div class="col-sm-3">
                     <div class="form-group">
                        <label for="inputName" class="col-sm-12 col-form-label">Camp Dist (शिविर का जिला) <span class="required">*</span></label>
                        <div class="col-sm-12">
                           <input type="text" name="camp_dist" id="city" class="form-control" placeholder="Camp Dist"  readonly="" required>
                        </div>
                     </div>
                  </div>
                  <div class="col-sm-3">
                     <div class="form-group">
                        <label for="inputName" class="col-sm-12 col-form-label">Camp State (शिविर का राज्य) <span class="required">*</span></label>
                        <div class="col-sm-12">
                           <input type="text" name="camp_state" id="state" class="form-control" placeholder="Camp State" readonly="" required>
                        </div>
                     </div>
                  </div>
                  <div class="col-md-12">
                     <div class="form-group">
                        <button name="submit" id="submit_button" style="float: right" type="submit" class="btn btn-success">Add</button>
                     </div>
                  </div>
                  <?php echo form_close() ?>
               </div>
            </div>
         </div>
      </div>
   </section>
</div>

<script type="text/javascript">
$('body').on('change', '#pin_code', function () {
    let pin_code = $('#pin_code').val();
    if (pin_code == 0) {
            swal ( "Oops" ,  "Please Enter Pin Code!" ,  "warning" );
        return false;
    }
    $.ajax({
      beforeSend: function() {
         $(".loader").show();
      },
      type: "GET",
      dataType:"JSON",
      async: true,
      url: '<?php echo base_url(); ?>/admin/admin/get_aadress_detaile_by_pin_code/'+pin_code,
      success: function (data) 
      {
          $('#area').html(data.area);
          $('#city').val(data.city);
          $('#state').val(data.state);
          $(".loader").hide();
      },
      error: function () {
        $(".loader").hide();
      }
    });
    $.ajax({
       type: "GET",
       dataType: "JSON",
       url: '<?php echo base_url(); ?>/admin/sharnarthi/check_camp_availability/' + pin_code,
       beforeSend: function () {
         $(".loader").show();
       },
       success: function (response) {
         $(".loader").hide();

         if (response) {
           swal('Oops!', "Camp Already Exists in this Pin Code", 'error');
           $('#submit_button').hide();
         } else {
           $('#submit_button').show()
         }
       },
       error: function (xhr, status, error) {
         console.log(xhr.responseText);
         $(".loader").hide();
         swal('Error!', "An error occurred while checking camp availability.", 'error');
       }
     });
});
</script>