 <?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">Edit 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">Edit 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">Edit Branch</h3>
                <div class="card-tools">
                  <a href="<?php echo base_url()?>admin/branch/view_all_atp" 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/update_branch/'.$atp->user_id); ?>
              <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" value="<?php echo $atp->user_business_name; ?>">
                       </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 Name" required="required" value="<?php echo $atp->user_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 Middle Name </label>
                       <div class="col-sm-12">
                          <input class="form-control"  name="user_m_name" placeholder="Enter Branch Owner Middle Name"  value="<?php //echo $atp->user_m_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" value="<?php //echo $atp->user_l_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" value="<?php echo $atp->user_dob; ?>">
                       </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 <?php if($atp->user_gender == 'Male'){echo 'selected';}?> value="Male">Male</option>
                            <option <?php if($atp->user_gender == 'Female'){echo 'selected';}?> 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" value="<?php echo $atp->user_email; ?>">
                       </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" value="<?php echo $atp->user_mobile; ?>">
                       </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" name="user_assign_programm[]" id="assign_prg" class="form-control select2">
                              <option value="">Select</option>
                              <?php foreach($all_programm as $programm): ?>
                                 <option 
                                 data-value='<?php echo $programm->cp_type; ?>'
                                 <?php 
                                foreach(json_decode($atp->user_assign_programm) as $prg)
                                {
                                  if($prg == $programm->cp_id)
                                  {
                                    echo 'selected';
                                  }
                                }
                              ?>
                               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" <?php if($atp->user_is_taluka_head == 0){?> style="display: none;" <?php }; ?> 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 <?php if($atp->user_is_taluka_head == 0){echo 'selected';}?> value="0">No</option>
                              <option <?php if($atp->user_is_taluka_head == 1){echo 'selected';}?> 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" value="<?php echo $atp->user_qualification; ?>">
                         </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" value="<?php //echo $atp->user_atp_lat; ?>">
                         </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" value="<?php //echo $atp->user_atp_long; ?>">
                         </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" value="<?php echo $atp->user_pin_code; ?>">
                         </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" required value="<?php echo $atp->user_village; ?>">
                         </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="<?php echo $atp->user_taluka; ?>"><?php echo $atp->user_taluka; ?></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="<?php echo $atp->user_block; ?>"><?php echo $atp->user_block; ?></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 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="" value="<?php echo $atp->user_dist; ?>">
                         </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="" value="<?php echo $atp->user_state; ?>">
                         </div>
                      </div>
                   </div>
                   
                   <div class="col-sm-3">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Branch Photo </label>
                         <div class="col-sm-12">
                            <input type="file" name="user_photo" class="form-control" placeholder="Enter Branch Registration Fee" >
                         </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" value="<?php echo $atp->user_reg_fee; ?>">
                         </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" value="<?php echo $atp->user_rew_fee; ?>">
                         </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 $atp->user_rew_date ?>">
                         </div>
                      </div>
                   </div>
                   <div class="col-sm-3">
                      <div class="form-group">
                         <label for="inputName" class="col-sm-12 col-form-label">Preview Branch Photo </label>
                         <div class="col-sm-12">
                            <img src="<?php echo base_url() ?><?php echo $atp->user_photo ?>" width="100px">
                         </div>
                      </div>
                   </div>
                   <?php if($atp->user_app_aff_link): ?>
                     <input type="hidden" value="<?php echo $atp->user_app_aff_link;?>" name="user_app_aff_link">
                  <?php else: ?>
                     <input type="hidden" value="<?php echo random_string('alnum', 20);?>" name="user_app_aff_link">
                  <?php endif; ?>
                   

                  <div class="col-md-12">
                    <div class="form-group">
                       <button name="form1" style="float: right" type="submit" class="btn btn-success">Update</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) 
      {
          $('#area').html(data.area);
          $('#block').html(data.block);
          $('#city').val(data.city);
          $('#state').val(data.state);
          $(".loader").hide();
      },
      error: function () {
        $(".loader").hide();
      }
    });
});
</script>