<?php include("cn.php");?>
<?php include("info.php");?>
<?php $title = "Drivers";?>
<!DOCTYPE html>
<html lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<?php include("head-tag.php");

$sql = mysqli_query($connect, "select count(*) as total from driver");
$data = mysqli_fetch_assoc($sql);

?>
<style>
    .switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
</style>
<body>

<div class="inner-wrapper">
<?php include("pre-loader.php");?>

<?php include("header.php");?>
<div class="page-wrapper">
    <div class="container-fluid">
        <div class="row">
        
      <div class="col-xl-12 col-lg-12 col-md-12">
                <div class="card shadow-sm ctm-border-radius">
                    <div class="card-body">
                        <h4 class="card-title mb-0 mt-2"><?php echo $data["total"]?> Drivers
                        	<?php
                        // 	if($_SESSION['username']=="mainadmin")
                        	{
                        	    
                        	    if ($data["total"]>=$dlimit)
                        	    echo '
                        	    <span id="message1">Your Driver limit has been reached. Please contact your Administrator to Increase limit.</span>
                        	    <script>
                        	    $("#yourbuttonid").attr("disabled", "disabled");
                        	    $("#yourbuttonid").hide()
                        	    </script>
                        	    ';
                        	    
                        	    else
                        	    
                        	    echo '
                        	    <a id="yourbuttonid" href="javascript:void(0)" class="btn btn-theme ctm-border-radius text-white btn-sm float-right" data-toggle="modal" data-target="#add-driver">
                        	    <i class="fa fa-plus" aria-hidden="true"></i> Add Driver
                        	    </a>
                        	    ';
                        	}
                        	?>
                        </h4>
                    </div>
                </div>
                
                <div class="ctm-border-radius shadow-sm card">
                    <div class="card-body">
                        <div class="table-responsive table-togglable table-hover"style="white-space: normal;">
                       <table id="driver" class="table table-striped table-bordered ">
						<thead>
						<tr>
                          <th class="text-center">Sr# </th>
						  <th class="text-center">ID</th>
						  <th class="text-center">Image</th>
						  <th class="text-center">Name</th>
						  <th class="text-center">Avail</th>
                          <th class="text-center">Driving License Number</th>
						  <th class="text-center">Contact</th>
						  <th class="text-center">Email</th>
						  <th class="text-center">Password</th>
                          <th class="text-center">Address</th>
                          <th class="text-center">Date Time</th>
                          <th class="text-center">Action</th>
                          <th class="text-center">Account Enable</th>
                          <th class="text-center">Booking Enable</th>
                          <th class="text-center">Amount Enable</th>
                          
                          <?php if($driverdelete==1){
                                            ?>
                                            <th class="text-center">Delete</th>
                                             <?php } else{ }
                                             ?>
                          
						</tr>
						</thead>
						<tbody>
										<?php
                                        $sql;
                                        $query="SELECT driver.*, app_users.password as password, app_users.email as email from driver 
										left join app_users on driver.driver_id = app_users.driverid ORDER BY driver.driver_id
										";
										$sql = mysqli_query($connect, $query);
                                        $i = 1;   
										while($data = mysqli_fetch_assoc($sql)){
                                            ?>
											<tr>
										<td class="text-center">
											    <?php echo  $i?>
                                            </td>
                                        <td class="text-center">
                                            <?php echo $data["driver_id"]?>

                                            <div class="modal fade" id="edit-driver<?php echo $data['driver_id']?>">
                                            <div class="modal-dialog modal-dialog-centered">
                                                    <div class="modal-content">
                                                        <div class="modal-body float-left text-left">
                                                            <form id="editdriver_<?php echo $data['driver_id']?>" method = "post" action="ajax/edit-driver.php" enctype='multipart/form-data'>
                                            				<input type="hidden" name="id" value="<?php echo $data['driver_id']?>">
                                                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                                                            
                                                            <h4 class="modal-title mb-3">Edit Driver (Driver Id= <?php echo $data['driver_id']?>)</h4>
                                                                <div class="row">
                                                                <div class="col-md-6 col-sm-12 col-xs-12">
                                                                    
                                                                
                        										<div class="form-group">
                        												<input type="file" name="photo" id="photo" class="dropify" data-height="285"
                        												data-default-file='<?php echo $data['image']?>'
                        												data-allowed-file-extensions='["jpg", "png", "jpeg"]'/>
                        											</div>
                                                                </div>
                                                                <div class="col-md-6 col-sm-12 col-xs-12">
                                                                
                                                                   <div class="form-group">
                                                                    <label style="color:blue;">Driver Name</label>
                                                                     <input type="text" class="form-control " placeholder="drivername"  name="dname" value="<?php echo $data['driver_name']?>" required/>
                                                                    </div>
                                                                    <div class="form-group">
                                                                    <label style="color:blue;">Contact No.</label>    
                                                                        <input type="text" class="form-control" placeholder="Contact No." name="contact" value="<?php echo $data['driver_phone']?>" required/>
                                                                    </div>
                                                                    <div class="form-group">
                                                                        <label style="color:blue;">Email Adress</label>
                                                                        <input class="form-control email" name="email" type="email" placeholder="Email" value="<?php echo $data['email']?>" 
                                                                        data-target="#message_email_<?php echo $data['driver_id']?>" data-id="#editdriver_<?php echo $data['driver_id']?>"
                                                                        required
                                                                        <?php if($_SESSION['username']=="mainadmin") {
                                                                            echo 'disabled';
                                                                        }
                                                                        ?>
                                                                        />
                                                                        <span id='message_email_<?php echo $data['driver_id'] ?>'></span>
                                                                    </div>
                                                                    </div>
                                                                    <div class="col-md-6 col-sm-12 col-xs-12">
                                                                   <div class="form-group">
                                                                    <label style="color:blue;">Driving Licence No.</label>   
                                                                        <input type="text" class="form-control dlNumber" placeholder="License Number" name="dlNumber" value="<?php echo $data['dl_number']?>" data-target="#message_dl_<?php echo $data['driver_id']?>" data-id="#editdriver_<?php echo $data['driver_id']?>" required/>
                                                                        <span id='message_dl_<?php echo $data['driver_id']?>'></span>
                                                                    </div>
                                                                    </div>
                                                                    <div class="col-md-6 col-sm-12 col-xs-12">
                                                                    <div class="form-group">
                                                                    <label style="color:blue;">Password:</label>    
                                                                        <input class="form-control" name="Password" type="text" placeholder="Password" value="<?php echo $data['password']?>" required/>
                                                                    </div>
                                                                    </div>
                                                                    <div class="col-md-12 col-sm-12 col-xs-12">
                                                                    <div class="form-group">
                                                                    <label style="color:blue;">Address:</label>    
                                                                        <input class="form-control" name="address" type="text" placeholder="Address" value="<?php echo $data['driver_address']?>" required/>
                                                                    </div>
                                                                    </div>
                                                                    

                                                            </div>
                                                                <div class="col-md-12 col-sm-12 col-xs-12">
                        											<button type="button" class="btn btn-danger float-right ml-3 ctm-border-radius" data-dismiss="modal">Cancel</button>
                                                                    <button type="submit" class="btn btn-theme  button-1 text-white ctm-border-radius float-right">Save</button>
                                                                </div>
                                                            
                                                            </form>
                                                            </div>
                                                        </div>
                                            </div>
                                            </div>
                                            
                                        </td>
                                        <!-- Dsipaly Driver Data Table/ List -->
                                            <td class="text-center">
                                                <div style="width:50px;height:50px;">
                                                    <img class="img-responsive img-thumbnail" src="<?php echo  $data["image"]?>">
                                                </div> 
                                            </td>
                                            <td class="text-center"> <?php echo $data["driver_name"]?> </td>
                                            <td class="text-center"> <?php echo $data["driver_availability"]?></td>
                                            <td class="text-center"> <?php echo $data["dl_number"]?> </td>
                                            <td class="text-center"><?php echo $data["driver_phone"]?></td>
                                            <td class="text-center"> <?php echo $data["email"]?></td>
                                            <td class="text-center"> <?php echo $data["password"]?></td>
                                            <td class="text-center"> <?php echo $data["driver_address"]?></td>
                                            <td class="text-center"> <?php echo date("F j, Y, g:ia", strtotime($data["driver_date"]))?></td>
                                            <td class="text-center">
                                                <a href="javascript:void(0)" 
                                                class="btn btn-theme button-1 ctm-border-radius text-white btn-sm float-center" 
                                                data-toggle="modal" data-target="#edit-driver<?php echo $data['driver_id']?>">
                                                    <i class="fa fa-pencil" aria-hidden="true">
                                                        
                                                    </i></a>
                                            </td>
                                            <td class="text-center">
                                                <label class="switch">
                                                <input class="driverEnable" data-target="<?php echo $data["driver_id"] ?>" type="checkbox"
                                                <?php if($data["driver_enable"] == "enable"){
                                                    echo "checked";
                                                }?>
                                                >
                                                <span class="slider round"></span>
                                                </label>
                                            </td>
                                            <td class="text-center">
                                                <label class="switch">
                                                <input class="driverbookingEnable" data-target="<?php echo $data["driver_id"] ?>" type="checkbox"
                                                <?php if($data["driverbooking_enable"] == "enable"){
                                                    echo "checked";
                                                }?>
                                                >
                                                <span class="slider round"></span>
                                                </label>
                                            </td>
                                            <td class="text-center">
                                                <label class="switch">
                                                <input class="driveramountEnable" data-target="<?php echo $data["driver_id"] ?>" type="checkbox"
                                                <?php if($data["driveramount_enable"] == "enable"){
                                                    echo "checked";
                                                }?>
                                                >
                                                <span class="slider round"></span>
                                                </label>
                                            </td>
                                            <?php if($driverdelete==1){
                                            ?>
                                            <td class="text-center">
                                             <a href="ajax/delete-driver.php?driver_id=<?php echo $data["driver_id"]?>" class="btn btn-danger button-1 ctm-border-radius text-white btn-sm float-center"><i class="fa fa-trash" aria-hidden="true"></i></a>
                                             </td>
                                             <?php } else{ }
                                             ?>
                                        </tr>
                                        <?php $i++; }?>
									</tbody>
						</table>
                        
                    </div>
                </div>
            </div>
            
       </div>
    </div>
</div>
</div>

<div class="modal fade" id="add-driver">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-body">
                <form method = "post" action="ajax/save-driver.php" id="adddriver" enctype='multipart/form-data'>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title mb-3 ">Add Driver</h4>
                <div class="row">
                    <div class="col-md-6 col-sm-12 col-xs-12">
						<div class="form-group">
							<input type="file" name="photo" id="photo" class="dropify" data-height="285"
							data-allowed-file-extensions='["jpg", "png", "jpeg"]'/>
						</div>
                    </div>
                    <div class="col-md-6 col-sm-12 col-xs-12">
                        <div class="form-group">
                            <label style="color:blue;">Driver Name</label>
                         <input type="text" class="form-control " placeholder="Driver Name"  name="dname"  required/>
                        </div>
                        <div class="form-group">
                            <label style="color:blue;">Contact No.</label>
                            <input type="text" class="form-control" placeholder="Contact No." name="contact"  required/>
                        </div>
                         <div class="form-group">
                            <label style="color:blue;">Email Address</label>
                            <input class="form-control" name="email" id="email" type="email" placeholder="Email" required/>
                            <span id='message1'></span>
                        </div>
                    </div>
                    <div class="col-md-6 col-sm-12 col-xs-12">    
                        <div class="form-group">
                            <label style="color:blue;">License Number</label>
                            <input type="text" class="form-control" placeholder="Driving License Number" id="dlNumber"name="dl_number" required/>
                            <span id='message'></span>
                        </div>
                        </div> 
                    <div class="col-md-6 col-sm-12 col-xs-12">    
                        <div class="form-group">
                            <label style="color:blue;">Password</label>
                            <input class="form-control" name="password" type="password" placeholder="Password" required/>
                        </div>
                       
                    </div>    
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <div class="form-group">
                            <label style="color:blue;">Address</label>
                            <input class="form-control" name="address" type="text" placeholder="Address" value="<?php echo $data['driver_address']?>" required/>
                        </div>
                    </div>
                </div>
               <button type="button" class="btn btn-danger float-right ml-3 ctm-border-radius" data-dismiss="modal">Cancel</button>
                 <button type="submit" class="btn btn-theme  button-1 text-white ctm-border-radius float-right">Save</button>  
               
                </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="sidebar-overlay" id="sidebar_overlay"></div>

<?php include("scripts.php");?>
<script>


     var email_state = false;
 var dlstate = false;
 submit();
 		
//  $('#customer_email').on('keyup keypress blur change input', function(){
    $('#email').on('input', function(){
 	var email = $('#email').val();
 	if (email == '') {
 		email_state = false;
 		submit();
 		return;
 	}
 	$.ajax({
      url: 'ajax/save-driver.php',
      type: 'post',
      data: {
      	'email_check' : 1,
      	'email' : email,
      },
      success: function(response){
      	if (response == 'taken' ) {
          email_state = false;
          submit();
        //   $('#customer_email').parent().removeClass();
        //   $('#customer_email').parent().addClass("form_error");
        $('#message1').html('Sorry... Email already taken').css('color', 'red');
        $("#adddriver").preventDefault();
      	}else if (response == 'not_taken') {
      	  email_state = true;
      	  submit();
      	 // $('#customer_email').parent().removeClass();
      	 // $('#customer_email').parent().addClass("form_success");
      	 $('#message1').html('Email available').css('color', 'green');
      	}
      }
 	});
 });
 
     $('#dlNumber').on('keyup', function(){

 	var dlnumber = $("#dlNumber").val();
 	if (dlnumber == '') {
 		dlstate = false;
 		
 		submit();
 		return;
 	}
 	$.ajax({
      url: 'ajax/save-driver.php',
      type: 'post',
      data: {
      	'dlcheck' : 1,
      	'dlnumber' : dlnumber,
      },
      success: function(response){
      	if (response == 'taken' ) {
          dlstate = false;
          submit();

        $('#message').html('This driving license number is already registered').css('color', 'red');
        $("#adddriver").preventDefault();
      	}else if (response == 'not_taken') {
      	  dlstate = true;
      	  submit();
      	 $('#message').html('Driving license number is not registered.').css('color', 'green');
      	}
      }
 	});
 });


function submit(){
        if(email_state == false && dlstate == false) {
           $(':input[type="submit"]').prop('disabled', true);
        }
        else
        {
        $(':input[type="submit"]').prop('disabled', false);
        }
     }

    $('.email').keyup(function(){
 	 	var email = $(this).val();
 	 	var message = $(this).attr('data-target');
 	 	var form = $(this).attr('data-id');

 	if (email == '') {
 		email_state = false;
 		submit();
 		return;
 	}
 	$.ajax({
      url: 'ajax/save-driver.php',
      type: 'post',
      data: {
      	'email_check' : 1,
      	'email' : email,
      },
      success: function(response){
      	if (response == 'taken' ) {
          email_state = false;
          submit();
        //   $('#customer_email').parent().removeClass();
        //   $('#customer_email').parent().addClass("form_error");
        $(message).html('Sorry... Email already taken').css('color', 'red');
        $(form).preventDefault();
      	}else if (response == 'not_taken') {
      	  email_state = true;
      	  submit();
      	 // $('#customer_email').parent().removeClass();
      	 // $('#customer_email').parent().addClass("form_success");
      	 $(message).html('Email available').css('color', 'green');
      	}
      }
 	});
 });
 
     $('.dlNumber').keyup(function(){

 	var dlnumber = $(this).val();
 	var message = $(this).attr('data-target');
 	var form = $(this).attr('data-id');
 	if (dlnumber == '') {
 		dlstate = false;
 		
 		submit();
 		return;
 	}
 	$.ajax({
      url: 'ajax/save-driver.php',
      type: 'post',
      data: {
      	'dlcheck' : 1,
      	'dlnumber' : dlnumber,
      },
      success: function(response){
      	if (response == 'taken' ) {
          dlstate = false;
          submit();

        $(message).html('This driving license number is already registered').css('color', 'red');
        $(form).preventDefault();
      	}else if (response == 'not_taken') {
      	  dlstate = true;
      	  submit();
      	 $(message).html('Driving license number is not registered.').css('color', 'green');
      	}
      }
 	});
 });




$('#driver').DataTable( {
            dom: 'B<clear>frtip',
            "bFilter": true,
            "bSort": true,
            "bPaginate": true,
            "order": [ 0, 'desc' ],
            "lengthMenu": [ [10, 50, 100, -1], [10, 50, 100, "All"] ],
            columnDefs: [
                {
                    targets: 1,
                    className: 'noVis'
                }
            ],
            buttons: [
                {
                    extend: 'copy',
                    title: 'driver Report',
                    exportOptions: {
                        columns: ':visible'
                    }
                    
                },
                {
                    extend: 'excel',
                    title: 'driver Report',
                    exportOptions: {
                        columns: ':visible'
                    }
                },
                {
                extend: 'print',
                orientation: 'landscape',
                title: '<h3 style="text-align:center">Driver Report </h3>',
                pageSize: 'LEGAL',
                exportOptions: {
                    columns: ':visible'
                }
                
            },
                {
                    extend: 'pdf',
                    title: 'driver Report',
                    orientation: 'portrait',
                    pageSize: 'A4',
                    exportOptions: {
                        columns: ':visible'
                    }
                    
                },
                
                {
                extend: 'colvis',
                collectionLayout: 'fixed three-column',
                columns: ':not(.noVis)',
                postfixButtons: [ 'colvisRestore' ]
            }
            ],
            language: {
            buttons: {
                colvis: 'Select Fields'
                }
            },
           
        } );
        


$(document).ready(function() {        
    $(".driverEnable").on('input', function(){
        var id = $(this).attr("data-target");
        var value = $(this).prop('checked');
        var url1 = './ajax/driver-enable.php';
        $.ajax({
      	url: url1,
      	type: 'post',
      	data: {
      		'driver_id' : id,
      		'check_value' : value,

      	},
    });

    })
    
    $(".driverbookingEnable").on('input', function(){
        var id = $(this).attr("data-target");
        var value = $(this).prop('checked');
        var url2 = './ajax/driverbooking-enable.php';
        $.ajax({
      	url: url2,
      	type: 'post',
      	data: {
      		'driver_id' : id,
      		'check_value' : value,

      	},
    });

    })
    
    $(".driveramountEnable").on('input', function(){
        var id = $(this).attr("data-target");
        var value = $(this).prop('checked');
        var url3 = './ajax/driveramount-enable.php';
        $.ajax({
      	url: url3,
      	type: 'post',
      	data: {
      		'driver_id' : id,
      		'check_value' : value,

      	},
    });

    })
    
});
        </script>
</body>
</html>