<?php 
  if (isset($_REQUEST['eid']) && !empty($_REQUEST['eid']) && $_REQUEST['eid']!=0){
    $select_update     = "SELECT * FROM driver_mst WHERE id='".$_REQUEST['eid']."'";
    $select_update_res = mysqli_query($conn,$select_update);
    $update_row        = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
  }

 if(isset($_POST['insert'])){
    $driver_code      = $_POST['driver_code'];
    $name             = $_POST['name'];
    $address          = $_POST['address'];
    $city             = $_POST['city'];
    $state            = $_POST['state'];
    $contact          = $_POST['contact'];
    $lic_no           = $_POST['lic_no'];
    $lic_issue_date   = date('Y-m-d', strtotime($_POST['lic_issue_date']));
    $lic_valid_date   = date('Y-m-d', strtotime($_POST['lic_valid_date']));
    $status           = $_POST['status'];
    $type             = 'update_driver';
    $user_id          = $_SESSION['user_detail']['id'];
    $data             = serialize($_POST);
    $created_datetime = date('Y-m-d H:i:s');
    $created_id       = $_SESSION['user_detail']['id'];
    $updated_id       = $_SESSION['user_detail']['id'];
    $updated_datetime = date('Y-m-d H:i:s');
    
    
    $user_query = "UPDATE driver_mst SET driver_code='$driver_code',name='$name',address='$address',city='$city',state='$state',contact='$contact',status='$status',updated_id='$updated_id',updated_datetime='$updated_datetime' WHERE id='".$_REQUEST['eid']."'";

    $user_result = mysqli_query($conn,$user_query) or die( mysqli_error($conn) );
    
    $activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    $acttivity_res = mysqli_query($conn,$activiti);

    if($user_result){ 
      echo "<script type='text/javascript'> showMessage(1, 1); </script>";
    } else {
      echo "<script type='text/javascript'> showMessage(0, 0); </script>";
    }

    if( !hasPermission($_SESSION['user_detail']['role_id'], 'driver_edit') ) {
      echo '<script>window.location.href="index.php?page=no_permission.php"</script>';
      die();
    }
  }
?>
 <style>
      .datepicker {
         z-index: 1051 !important;
      }

      /* Optional: for Bootstrap Datepicker dropdown */
      .datepicker-dropdown {
         z-index: 9999 !important;
      }

      /* Optional: for jQuery UI Datepicker */
      .ui-datepicker {
         z-index: 9999 !important;
      }
   </style>
<section class="content-header hitz-braed-crumb">
   <h1 class="hitz-border">edit DRIVER</h1>
   <ol class="breadcrumb">
      <?php if( hasPermission($_SESSION['user_detail']['role_id'], 'driver_list') ) { ?>
      <a class="btn btn-sm btn-primary btn-create btn hitz-bread-btn" href="index.php?page=driver/list.php">&nbsp; DRIVER LIST</a>
      <?php } ?>
    </ol>
</section>
<section class="" style="margin-top: 30px;">
   <div class="row">
      <div class="col-xs-12">
         <div class="box box-success">
            <div class="box-body">
                <form class="" id="signupForm" method="post">
                  <div class="row">
                     <div class="col-md-1">
                        <div class="form-group">
                          <label class="capital">CODE</label><span style="color: red">*</span>
                          <input type="text" size="35" id="driver_code" name="driver_code" class="form-control" autocomplete="off" readonly="" value="<?php echo $update_row['driver_code'];?>" />
                        </div>
                     </div>
                     <div class="col-md-5">
                        <div class="form-group">
                          <label class="capital">name</label><span style="color: red">*</span>
                          <input type="text" size="35" id="name" name="name" class="form-control" placeholder="DRIVER NAME" autocomplete="off" value="<?php echo $update_row['name'];?>" />
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                          <label class="capital">address</label><span style="color: red">*</span>
                          <textarea class="form-control" name="address" id="address"><?php echo $update_row['address'];?></textarea> 
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                          <label class="capital">city</label><span style="color: red">*</span>
                          <input type="text" name="city" id="get_city" class="form-control" placeholder="CITY" value="<?php echo $update_row['city'];?>" />
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">state</label><span style="color: red">*</span>
                           <input type="text" name="state" id="get_state" class="form-control" placeholder="STATE" value="<?php echo $update_row['state'];?>" />    
                        </div>
                     </div>
                  </div>
                  <div class="row">
                      <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">phone</label><span style="color: red">*</span>
                           <input type="text" size="35" id="contact" name="contact" class="form-control" placeholder="PHONE" autocomplete="off" value="<?php echo $update_row['contact'];?>" />
                        </div>
                     </div>
                     <div class="col-md-2">
                        <div class="form-group">
                           <label class="capital">Lic.no</label><span style="color: red">*</span>
                           <input type="text" size="35" id="lic_no" name="lic_no" class="form-control" placeholder="LIC.NO" autocomplete="off" value="<?php echo $update_row['lic_no'];?>"/>
                        </div>
                      </div>
                      <div class="col-md-2">
                        <div class="form-group">
                          <label class="capital">Lic Issue Date</label>
                          <div class="input-group date">
                             <div class="input-group-addon">
                                <i class="fa fa-calendar"></i>
                             </div>
                             <input type="text" size="35" id="lic_issue_date" name="lic_issue_date" class="form-control date-picker" placeholder="LIC.ISSUE DATE" autocomplete="off" value="<?php echo date('d-m-Y', strtotime($update_row['lic_issue_date']));?>"/>
                          </div>
                        </div>
                      </div>
                      <div class="col-md-2">
                        <div class="form-group">
                          <label class="capital">Lic Valid Date</label>
                          <div class="input-group date">
                             <div class="input-group-addon">
                                <i class="fa fa-calendar"></i>
                             </div>
                             <input type="text" size="35" id="lic_valid_date" name="lic_valid_date" class="form-control date-picker" placeholder="LIC.VALID DATE" autocomplete="off" value="<?php echo date('d-m-Y', strtotime($update_row['lic_valid_date']));?>"/>
                          </div>
                        </div>
                      </div>
                     <div class="col-md-3">
                        <div class="form-group">
                          <label class="capital">Status</label><span style="color: red">*</span><br>
                          <label class="radio-inline fontradiobuttion">
                          <input type="radio" name="status" value="0" <?php if($update_row['status']=='0'){echo "checked";}?>> <span class="label-text">ACTIVE</span>
                          </label>
                          <label class="radio-inline fontradiobuttion">
                          <input type="radio" name="status" value="1" <?php if($update_row['status']=='1'){echo "checked";}?>> <span class="label-text">IN-ACTIVE</span>
                          </label>
                        </div>
                     </div>
                  </div>
                  <div class="row">
                     <div class="col-md-12">
                        <p>Mark (*) fields are compulsory</p>
                     </div>
                  </div>
                  <hr>
                  <div class="row" align="center">
                     <button name="insert" class="btn btn-primary submit capital" type="submit"> UPDATE & LIST</button>
                     <a href="index.php?page=driver/list.php" class="btn btn-danger capital"> Cancle</a>
                  </div>
                </form>
            </div>
         </div>
      </div>
   </div>
</section>
<script type="text/javascript">
  $(document).ready(function(){
    $('input').iCheck({
       checkboxClass: 'icheckbox_flat',
       radioClass: 'iradio_flat'
    });
  });
</script>
<script type="text/javascript">
  $( function() {
    $( "#get_state" ).autocomplete({
        source: function( request, response ) {
            
            $.ajax({
                url: "driver/get_state.php",
                type: 'post',
                dataType: "json",
                data: {
                    search: request.term
                },
                success: function( data ) {
                    response( data );
                }
            });
        },
        select: function (event, ui) {
            $('#get_state').val(ui.item.label);
            return false;
        }
    });
    
    $( "#get_city" ).autocomplete({
        source: function( request, response ) {
          $.ajax({
              url: "driver/get_city.php",
              type: 'post',
              dataType: "json",
              data: {
                  search: request.term
              },
              success: function( data ) {
                  response( data );
              }
          });
        },
        select: function (event, ui) {
            $('#get_city').val(ui.item.label);
            return false;
        }
    });
  });
</script>