<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="datatable/data.css">
<script src="datatable/data.js" type="text/javascript"></script>
<script src="datatable/table.js" type="text/javascript"></script>
<script src="datatable/datatable.js"></script>
<link rel="stylesheet" href="datatable/data-boot.css">
<link href="javascript/select2.css" rel="stylesheet" >
<script src="javascript/selectt2.js"></script>
<link rel="stylesheet" href="tablehead.css">
<script src="ajax/vehiclemaster.js"></script>
<script src="restriction/vehicle_restriction.js"></script>
<style>
    
    #navbar
    {
        flex:15%;
        height:100%; 
        flex-direction:row;
        overflow:auto;
        
    }
    #content
    {
        flex:90%;
        overflow:auto;
        padding-left:10px;
        padding-right:10px;
    }
    #fullbody
    {
        display:flex;
        
         height:100%;         
    }
    label
    {
      font-size:20px;
    }
    .edit
    {
        font-size:18px;
        color:green;
        cursor:pointer;
    }
    .dele
    {
        font-size:18px;
        color:red;
        cursor:pointer;
    }
    .edit:hover
    {
        font-size:20px;
        
    }
    .dele:hover
    {
        font-size:20px;
        color:red;
        cursor:pointer;
        
    }
    td
    {
      text-align:center;
    }
    th
    {
      text-align:center;
    }
    
    @media (max-width: 800px) {
        #fullbody
    {
        display:flex;
        flex-wrap: wrap;
                 
    }
        #navbar,#content {
    flex: 100%;
  }
}
</style>
<script>
$(document).ready( function () {
    $('#protable').DataTable();
} );
</script>

</head>
<body>
    
    <div id="fullbody"> 
<div id="navbar" class="bg-info"><?php include "navbar.php"?></div>
<div id="content">
<br>
<h3>Vehicle-Master</h3>
<br>
<?php 
        $userall=$_SESSION['log'];
        $usper = mysqli_query($connect,"select * from userpermission where username = '$userall'  && filename='vehiclemaster'");
while($per=mysqli_fetch_array($usper))
{
          ?>
          <?php if($per['dataadd'] =='1'){ ?>
<input type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal" style="float:right;margin-right:10px;" value="+ Add"><?php }?>
<br><br>
<!-- table -->
<div>
<table class="table table-bordered table-hover " style="border:1px solid lightgrey" id="protable">
    <thead >
<tr>
  <th>Sno</th>
    <th>Code</th>
    <th>Vehicle Number</th>
    <!-- <th>Customer</th> -->
    <th>Empty weight</th>
    
    <?php if($per['dataedit'] =='1'){ ?>  <th>Edit</th><?php }?>
    <?php if($per['datadelete'] =='1'){ ?><th>Delete</th><?php }?>
</tr>

    </thead>
    <tbody>
    <?php 
$vehshow=mysqli_query($connect,"select * from vehiclemaster where status !='deleted' order by id desc");
while($vehshshow = mysqli_fetch_array($vehshow))
{
  $customer = $vehshshow['customer'];
// $cus=mysqli_query($connect,"select * from customerdetails where status !='deleted' && code = '$customer'");
// while($cusshow = mysqli_fetch_array($cus))
// {
?>
        <tr>
        <td><?php echo $vehshshow['id']  ?></td>
        <td><?php echo $vehshshow['code']  ?></td>
    <td><?php echo $vehshshow['vehicleno']  ?></td>
    <!-- <td><?php echo $cusshow['name']  ?></td> -->
    <td><?php echo $vehshshow['emptyweight']  ?></td>
    <?php if($per['dataedit'] =='1'){ ?>  <td><a data-toggle="modal" data-target="#edit<?php echo $vehshshow['id']  ?>"><i class="fas fa-edit edit"></i></a></td><?php }?>
    <?php if($per['datadelete'] =='1'){ ?>     <td><a data-toggle="modal" data-target="#delete<?php echo $vehshshow['id']  ?>"><i  class="fas fa-trash-alt dele"></a></i></td><?php }?>
        </tr>
        <?php }
        }?>
    </tbody>



</table>
</div>
<!-- modal -->
<div class="modal fade" id="myModal">
    <div class="modal-dialog modal-dialog-centered modal-lg">
      <div class="modal-content">
      <form action="insertvehicle.php" method="post">
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Add Vehicle</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <!-- Modal body -->
        <div class="modal-body">
            <!--  -->
          <div class="row">
            
          <?php 
$vehco=mysqli_query($connect,"select max(id) as count from vehiclemaster ");
while($veshow = mysqli_fetch_array($vehco))
{
  $code = $veshow['count'];
  $codeco = $code + 1;
}

?>

              <div class="col-sm-3"><label>Vehicle Number</label></div>:
              <div class="col-sm-6"><input type="text" class="form-control vehiclenochk" autocomplete="off" name="vehiclename" placeholder="Number" id="vehicleAdd" >
              <input type="hidden" class="form-control" autocomplete="off" name="code" value="veh-<?php echo $codeco ?>" placeholder="Number">
            </div>
              
            </div><br>
            <?php 
$proname_val=mysqli_query($connect,"select * from vehiclemaster order by id desc");
while($proname_val_show = mysqli_fetch_array($proname_val))
{
?>
<input type="hidden" class="vehicleNoValue" value="<?php echo $proname_val_show['vehicleno'] ?>">
<?php
}
?>
<script>
  var vehNo = document.querySelectorAll(".vehicleNoValue");
  var vehAdd = document.getElementById("vehicleAdd");
  vehAdd.addEventListener("keyup",function(e){
    //console.log("a");
   var vehicle = e.target.value.toLowerCase();
   vehNo.forEach((list)=>{
    if(vehicle.replace(/ /g,"") == list.value.toLowerCase().replace(/ /g,"")){
      document.getElementById("alertUser").click();
      vehAdd.value="";
   }
   });
     
  });
  console.log(userName);
  </script>
            <!--  -->
            <div class="row">
              <div class="col-sm-3"><label>Empty-Weight</label></div>:
              <div class="col-sm-3"><input type="text" class="form-control" autocomplete="off" name="emptyweight" id="getweightadd" placeholder="Empty-weight"></div>
              <div class = "col-sm-3">
              <input type="button" class="btn btn-warning" id="getweightbutton"autocomplete="off" value="Getweight" placeholder="Empty-weight">
            </div>
            </div><br>
            
            <!--  -->
            
            <!-- <div class="row">
              <div class="col-sm-3"><label>Customer Name</label></div>:
              <div class="col-sm-6">
                
                <select class=" selectpro" name="customername" id="customeradd">
                <option value="">--Choose Customer--</option>
                <?php 
$cus=mysqli_query($connect,"select * from customerdetails where status !='deleted'");
while($cusshow = mysqli_fetch_array($cus))
{
?>
<option value="<?php echo $cusshow['code'] ?>"><?php  echo $cusshow['name'] ?></option>
        <?php }?>          
                </select>
              </div>
            </div><br> -->
            <!--  -->
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <input type="button"  class="btn btn-success" data-toggle="modal" data-target="#addpro" value="Add" id="submitbut">
        </div>
        <!-- modal for add -->
<div class="modal fade" id="addpro">
    <div class="modal-dialog modal-sm">
      <div class="modal-content" style="background-color:rgba(77, 219, 106,0.9)">
      
        <!-- Modal Header -->
        
        
        <!-- Modal body -->
        <div class="modal-body">
          <span style="color:white"><i class="fas fa-check" style="font-size:24px"></i> Vehicle Have Been Created Successfully</span>
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="submit" type="button" class="btn btn-warning" >Ok</button>
        </div>
        
      </div>
    </div>
  </div>
</form>
<!-- modal for add -->
      </div>
    </div>
  </div>
<!-- modal -->
<!-- modal for edit -->
<?php 
$vehedit=mysqli_query($connect,"select *,(select customerdetails.name from customerdetails where customerdetails.code=vehiclemaster.customer && status!='deleted')as cusname from vehiclemaster ");
while($veshowedi = mysqli_fetch_array($vehedit))
{
?>

<div class="modal fade" id="edit<?php echo $veshowedi['id'] ?>">
    <div class="modal-dialog modal-dialog-centered modal-lg">
      <div class="modal-content">
      <form action="updatevehicle.php" method="post" >
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Edit Vehicle</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <!-- Modal body -->
        <div class="modal-body">
            <!--  -->
          <div class="row">
              <div class="col-sm-3"><label>Vehicle Number</label></div>:
              <div class="col-sm-6"><input type="text" class="vehiclenochk form-control" value="<?php echo $veshowedi['vehicleno'] ?>" autocomplete="off" name="vehicleedit" placeholder="Number">
              <input type="hidden" class="form-control" value="<?php echo $veshowedi['code'] ?>" autocomplete="off" name="codeedit" placeholder="Number"></div>
              
            </div><br>
            <!--  -->
            <div class="row">
              <div class="col-sm-3"><label>Empty-Weight</label></div>:
              <div class="col-sm-3"><input type="text" value="<?php echo $veshowedi['emptyweight'] ?>" class="form-control getweightedit<?php echo $veshowedi['code'] ?>" autocomplete="off" name="emptyedit"  placeholder="Empty-weight"><input type="hidden" class="form-control" value="<?php echo $veshowedi['code'] ?>" autocomplete="off" name="codeedit" placeholder="Number" id="editget"></div>
              <div class="col-sm-3"><input type="button" value="Getweight" class="btn btn-warning getweightbuttonedit" autocomplete="off" id="" placeholder="Empty-weight"></div>
            </div><br>
            
            <!--  -->
            
            <!-- <div class="row">
              <div class="col-sm-3"><label>Customer Name</label></div>:
              <div class="col-sm-6">
                <select class="form-control selectpro" name="customeredit">
                  <option value="<?php echo $veshowedi['customer'] ?>"><?php echo $veshowedi['cusname'] ?></option>
                  <?php
                  $cuschk = $veshowedi['customer'];
                  $customer= mysqli_query($connect,"select * from customerdetails where code !='$cuschk' && status !='deleted'");
                  while($cusdis = mysqli_fetch_array($customer))
                  {
                  ?>
                  <option value="<?php echo $cusdis['code'] ?>"><?php echo $cusdis['name'] ?></option>
                  <?php }?>
                </select>
              </div>
            </div><br> -->
            <!--  -->

        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <input type="button" class="btn btn-success"data-toggle="modal" data-target="#update<?php echo $veshowedi['id'] ?>" value="Update">
        </div>
        <!-- modal for add popup -->
<div class="modal fade" id="update<?php echo $veshowedi['id'] ?>">
    <div class="modal-dialog modal-sm">
      <div class="modal-content" style="background-color:rgba(77, 219, 106,0.9)">
      
        <!-- Modal Header -->
        
        
        <!-- Modal body -->
        <div class="modal-body">
          <span style="color:white"><i class="fas fa-check" style="font-size:24px"></i> Vehicle Have Been Updated Successfully</span>
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <input type="submit"  class="btn btn-warning" value="Ok" >
        </div>
</form>
      </div>
    </div>
  </div>
  
<!-- modal for add -->
      </div>
    </div>
  </div>
  <?php }?>
<!-- modal for edit -->
<!-- modal for delete -->
<!-- The Modal -->
<?php 
$vehdele=mysqli_query($connect,"select * from vehiclemaster ");
while($veshowdele = mysqli_fetch_array($vehdele))
{
?>

<div class="modal fade" id="delete<?php echo $veshowdele['id'] ?>">
    <div class="modal-dialog modal-sm">
      <div class="modal-content " >
      
        <!-- Modal Header -->
        
        <form action="deletevehicle.php" method="post">
          <input type="hidden" value="<?php echo $veshowdele['code'] ?>" name="delete">
        <!-- Modal body -->
        <div class="modal-body"  >
         Are You Sure Want To Delete !.
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
        <button type="submit" class="btn btn-success" >Yes</button>
          <button type="button" class="btn btn-warning" data-dismiss="modal">No</button>
        </div>
</form>
      </div>
    </div>
  </div>
  <?php
}
 ?>
<!-- ,odal for delete -->
<!-- table -->


<!-- main-content-div -->
</div>
<!-- main-content-div -->
</div>
<!-- alert5 -->
<button type="button" class="btn btn-primary alertalpha" id="blockButton" data-toggle="modal" data-target="#textmodal" style="display:none">
      </button> 

  <!-- The Modal -->
  <div class="modal fade" id="textmodal">
    <div class="modal-dialog modal-lg">
     <div class="alert alert-danger alert-dismissible fade show">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <strong>Special Characters Are Not Allowed</strong>
  </div>
  </div>
        </div>
    

 <!--  alert -->
<!-- modal to blockdate -->
<button type="button" class="btn btn-primary" id="alertUser" data-toggle="modal" data-target="#advanceLimit" style="display:none">
      </button>

  <!-- The Modal -->
  <div class="modal fade" id="advanceLimit">
    <div class="modal-dialog modal-lg">
     <div class="alert alert-danger alert-dismissible fade show">
    <button type="button" class="close"  data-dismiss="alert">&times;</button>
    <strong>Vehicle No Already Exist</strong>
  </div>
  </div>
  </div>
        
<!-- alert -->

<script>
    $('.selectpro').select2({ width: '100%' });
    //$('.selectpro').focus
    
</script>
<script>
  $(document).on('focus', '.select2-selection.select2-selection--single', function (e) {
  $(this).closest(".select2-container").siblings('select:enabled').select2('open');
});
</script>
</body>