 <?php $this->load->helper('string');?>
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <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 Branch</h1>
          </div><!-- /.col -->
          <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="#">Branch</a></li>
              <li class="breadcrumb-item active">Add Branch</li>

            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">

        <div class="row">
          <div class="col-sm-12">
            <div class="card card-default">
              <div class="card-header">
                <h3 class="card-title">Add Branch</h3>
                <div class="card-tools">
                  <a href="<?php echo base_url()?>admin/branch/view_all_branch" class="btn btn-primary btn-sm"><i class="fa fa-eye" aria-hidden="true"></i> View Branch</a>
                </div>
              </div>
              <!-- /.card-header -->
              <?php echo form_open_multipart('admin/branch/add_branch'); ?>
              <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">Branch Name <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="user_business_name" placeholder="Enter Branch 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">Branch Owner Name <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="user_name" placeholder="Enter Branch Owner First Name" required="required" >
                       </div>
                    </div>
                  </div>

                  <div class="col-md-4 d-none">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Branch Owner Middle Name </label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="user_m_name" placeholder="Enter Branch Owner Middle Name" >
                       </div>
                    </div>
                  </div>

                  <div class="col-md-4 d-none">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Branch Owner Last Name <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="user_l_name" placeholder="Enter Branch Owner Last Name" >
                       </div>
                    </div>
                  </div>

                  <div class="col-md-6">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Owner Date of Birth <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control" type="date"  name="user_dob" placeholder="Enter Branch Owner 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">Owner Gender <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <select class="form-control"  name="user_gender" placeholder="Enter Branch Owner Name" required="required">
                            <option value="Male">Male</option>
                            <option value="Female">Female</option>
                          </select>
                       </div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Branch Email ID <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control" type="email"  name="user_email" placeholder="Enter Branch Email ID" required="required" >
                       </div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                       <label for="inputEmail" class="col-sm-12 col-form-label">Branch Mobile No. <span class="required">*</span></label>
                       <div class="col-sm-12">
                          <input class="form-control" type="number"  name="user_mobile" placeholder="Enter Branch Mobile No." required="required" >
                       </div>
                    </div>
                  </div>
                  <div class="col-sm-5 d-none">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Assign Programm <span class="required">*</span></label>
                         <div class="col-sm-12">
                            <select type="text"  id="assign_prg" name="user_assign_programm[]" class="form-control select2" >
                              <option value="">Select</option>
                              <?php foreach($all_programm as $programm): ?>
                                 <option data-value='<?php echo $programm->cp_type; ?>' value="<?php echo $programm->cp_id; ?>"><?php echo $programm->cp_name; ?></option>
                              <?php endforeach; ?>
                            </select>
                         </div>
                      </div>
                  </div>
                  <script type="text/javascript">
                     $(document).ready(function() {
                       // Get the selected option value and data-value attribute
                       $('#assign_prg').on('change', function() {
                         var selectedOption = $(this).find(':selected');
                         var dataValue = selectedOption.data('value');
                         if(dataValue == 2)
                         {
                           $('#taluka_head').show();
                         }
                         else
                         {
                           $('#taluka_head').hide();
                         }
                       });
                     });
                  </script>
                  <div class="col-sm-3" style="display: none;" id="taluka_head">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch is Taluka Head ?</label>
                         <div class="col-sm-12">
                            <select type="text" name="user_is_taluka_head" class="form-control" placeholder="Enter Branch Owner Qulification">
                              <option value="0">No</option>
                              <option value="1">Yes</option>
                           </select>
                         </div>
                      </div>
                   </div>
                  <div class="col-sm-2">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Qulification</label>
                         <div class="col-sm-12">
                            <input type="text" name="user_qualification" class="form-control" placeholder="Enter Branch Owner Qulification">
                         </div>
                      </div>
                   </div>

                   <div class="col-sm-4 d-none">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Latitude <span >*</span></label>
                         <div class="col-sm-12">
                            <input type="text" name="user_atp_lat" class="form-control" placeholder="Enter Branch Latitude" >
                         </div>
                      </div>
                   </div>
                   <div class="col-sm-4 d-none">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Longitude <span >*</span></label>
                         <div class="col-sm-12">
                            <input type="text" name="user_atp_long" class="form-control" placeholder="Enter Branch Longitude" >
                         </div>
                      </div>
                   </div>
                  <div class="col-sm-4">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Pin Code <span >*</span></label>
                         <div class="col-sm-12">
                            <input type="number" name="user_pin_code" id="pin_code" class="form-control" placeholder="Enter Branch Pin Code" required="required" >
                         </div>
                      </div>
                   </div>
                   <div class="col-sm-6">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Address *</label>
                         <div class="col-sm-12">
                            <input type="text" name="user_village" class="form-control" placeholder="Enter Branch Address" >
                         </div>
                      </div>
                   </div>
                   <div class="col-sm-3">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Area <span >*</span></label>
                         <div class="col-sm-12">
                            <select name="user_taluka" id="area" class="form-control" required="required">
                               <option value=""></option>
                            </select>
                         </div>
                      </div>
                   </div>
                   <div class="col-sm-3">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Block <span >*</span></label>
                         <div class="col-sm-12">
                            <select name="user_block" id="block" class="form-control" required="required">
                               <option value=""></option>
                            </select>
                         </div>
                      </div>
                   </div>
                   <div class="col-sm-3">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch State <span >*</span></label>
                         <div class="col-sm-12">
                            <input type="text" name="user_state" id="state" class="form-control" placeholder="Enter Branch State" required="required" readonly="" >
                         </div>
                      </div>
                   </div>
                   <div class="col-sm-3">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Dist <span >*</span></label>
                         <div class="col-sm-12">
                            <input type="text" name="user_dist" id="city" class="form-control" placeholder="Enter Branch Dist" required="required" readonly="" >
                         </div>
                      </div>
                   </div>
                   
                   
                   <div class="col-sm-3">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Photo <span >*</span></label>
                         <div class="col-sm-12">
                            <input type="file" name="user_photo" class="form-control" placeholder="Enter Branch Registration Fee" required>
                         </div>
                      </div>
                   </div>
                   <div class="col-sm-3">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Registration Fee <span >*</span></label>
                         <div class="col-sm-12">
                            <input type="text" name="user_reg_fee" class="form-control" placeholder="Enter Branch Registration Fee" required="required" >
                         </div>
                      </div>
                   </div>
                    <div class="col-sm-3">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Renewal Fee <span >*</span></label>
                         <div class="col-sm-12">
                            <input type="text" name="user_rew_fee" class="form-control" placeholder="Enter Branch Renewal Fee" required="required" >
                         </div>
                      </div>
                   </div>
                   <div class="col-sm-3">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Renewal Date</label>
                         <div class="col-sm-12">
                            <input type="date" readonly name="user_rew_date" class="form-control" placeholder="Enter Branch Renewal Date" required="required" value="<?php echo date('Y-m-d', strtotime('+1 year', strtotime(date('Y-m-d'))));?>">                  
                          </div>
                      </div>
                   </div>
                   <input type="hidden" value="<?php echo random_string('alnum', 20);?>" name="user_app_aff_link">
                  <div class="col-md-12">
                    <div class="form-group">
                       <button name="form1" style="float: right" type="submit" class="btn btn-success">Add</button>
                    </div>
                  </div>
                  <?php echo form_close() ?>
                </div>
              </div>
            </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) 
      {
         console.log(data);
          $('#area').html(data.area);
          $('#block').html(data.block);
          $('#city').val(data.city);
          $('#state').val(data.state);
          $(".loader").hide();
      },
      error: function (e) {
         console.log(e.responseText)
        $(".loader").hide();
      }
    });
});
</script>