<?php
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'driver_add') ) {
    echo '<script>window.location.href="index.php?page=no_permission.php"</script>';die();
  }
    
  $voucher_no          = driveCode($conn);

  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);
    $act_code          = $update_row['driver_code'];
    $name              = $update_row['name'];
    $address           = $update_row['address'];
    $city              = $update_row['city'];
    $state             = $update_row['state'];
    $contact           = $update_row['contact'];
    $lic_no            = $update_row['lic_no'];
    $file_img          = $update_row['file_img'];
    $status            = $update_row['status'];
    $id                = $_GET['eid'];
  }
  if($_GET['eid'] == ''){
    $lic_issue_date    = date('m-d-Y');
    $lic_valid_date    = date('m-d-Y');
  }else{
    $lic_issue_date    = date('Y-m-d', strtotime($update_row['lic_issue_date']));
    $lic_valid_date    = date('Y-m-d', strtotime($update_row['lic_valid_date']));
  }
?>
 <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">
  <div class="row">
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 bottom20">
        <h3 class="capital font15 padding5 bold"><?php echo ($id == 0) ? 'ADD NEW DRIVER' : 'UPDATE DRIVER'; ?></h3>
      </div>
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 text-right bottom20">
        <h3>
          <?php if( hasPermission($_SESSION['user_detail']['role_id'], 'driver_list') ) { ?>
          <a href="index.php?page=driver/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW DRIVER DETAILS</a>
          <?php } ?>
        </h3>
      </div>
  </div>
</section>
<hr class="bottom35"> 
<section class="hitz-top-header">
   <div class="row bottom40">
      <div class="col-xs-12">
         <div class="box box-success">
            <div class="box-body">
              <form action="driver/insert.php" method="post" enctype="multipart/form-data" class="common-frm-validation" id="signupForm">
                <div class="row">
                  <div class="col-md-2">
                    <div class="form-group">
                       <label class="capital">CODE</label><span style="color: red">*</span>
                        <?php 
                        if($_GET['eid'] == ''){
                          $code = $voucher_no;
                        }else{
                          $code = $act_code;
                        }
                        ?>
                       <input type="text" size="35" id="driver_code" name="driver_code" class="form-control required" autocomplete="off" readonly="" value="<?php echo $voucher_no;?>" />
                    </div>
                  </div>
                  <div class="col-md-3">
                    <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 required" placeholder="DRIVER NAME" autocomplete="off" autofocus="" value="<?php echo $name?>" onchange="CheckAname(this.value);" autofocus=""/>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                       <label class="capital">address</label><span style="color: red">*</span>
                       <textarea class="form-control required" name="address" id="address" style="height: 40px !important;"><?php echo $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 required" placeholder="CITY" value="<?php echo $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 required" placeholder="STATE" value="<?php echo $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 required" placeholder="PHONE" autocomplete="off" value="<?php echo $phone;?>" />
                      </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 required" placeholder="LIC.NO" autocomplete="off" value="<?php echo $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="DATE" autocomplete="off" value="<?php echo $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="DATE" autocomplete="off" value="<?php echo $lic_valid_date;?>" />
                        </div>
                      </div>
                    </div>
                    <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">IMAGE</label>
                      <input type="file" size="35" id="image" name="image" autocomplete="off" class="image" /><br/>
                      <?php
                      if($file_img !=''){ ?>
                        <a href="assets/upload/driver/<?php echo $file_img?>" class="btn btn-info" data-toggle="tooltip" data-placement="bottom" title="VIEW IMAGE...!!" target="_blank"><em class="fa fa-file-image-o"></em></a>
                      <?php } ?>
                    </div>
                  </div>
                </div>
                  <div class="row">
                   <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" checked="" <?php if($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($status=='1'){echo "checked";}?>> <span class="label-text">IN-ACTIVE</span>
                         </label>
                      </div>
                   </div>
                </div>
                <input type="hidden" value="<?php echo $id;?>" name="id" id="id">
                <hr>
                <div class="row" align="center">
                  <button id="btn" class="btn btn-primary ajax-submit capital" type="submit"> <?php echo ($id == 0) ? 'Save & List' : 'Update & save'; ?></button>
                  <a href="index.php?page=driver/list.php" class="btn btn-danger capital"> Cancel</a>
                </div>
              </form>
              
            </div>
         </div>
      </div>
   </div>
</section>
<script type="text/javascript">
  $(document).ready(function(){
    $('input').iCheck({
      checkboxClass: 'icheckbox_flat',
      radioClass: 'iradio_flat'
    });
    
  });
  $( 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;
        }
    });
  });
  function CheckAname() {
    var name = $("#name").val();
    $.ajax({
      'type': 'POST',
      'url': 'driver/check_name.php',
      'data': {
        'name': name
      },
      dataType: 'JSON',
        success: function (result) {
          if(result.status == 1) {
             jQuery('.submit').attr('disabled', false);
          }else{
            jQuery('.submit').attr('disabled', true);
            swal("Opps !!", "SAME NAME DETECT...!!!", "info")
            setTimeout("location.reload(true);",2000);  
          }
        }
    });
  }
</script>