<?php
if ($permissions->hasPermission('AssignVehicleDriver')) {
    include('Views/Garage/assignVehicleDriver.php');
    include ('Views/Garage/addVehicleZone.php');
}
?>
<script src="<?php echo THEME_PATH; ?>plugins/jquery-validation/js/jquery.validate.min.js"></script>
<style>

    #tableWithSearch thead tr >th {
        background-color: #ed9421;
        color: #fff;
    }

    .bg-success-darker{
        color:#fff;
        font-family: 'Montserrat';
        text-transform: uppercase;
        font-size: 13px !important;
        font-weight: 600;
    }
</style>
<div class="container-fluid container-fixed-lg bg-white" style="height:500px">    
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"> <?php print CommonLanguage::getMessage('vehicle_driver_list'); ?></div>
            <?php echo Forms::FloatingAddButton('ed9421', '#assignVehicleDriver', CommonLanguage::getMessage('Add')); ?> 
            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <table class="table table-detailed" id="tableWithSearch" >
                <thead>
                    <tr>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('maker'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('reg_nb'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('oil_exchange'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('driver'); ?></th>
                        <th class="text-center"><?php print CommonLanguage::getMessage('salesman'); ?></th>
                        <th class="text-center"><?php print CommonLanguage::getMessage('zone'); ?></th>
                        <th class="text-center"><?php print CommonLanguage::getMessage('Action'); ?></th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>

        </div>
    </div>
</div>

<div class="modal fade bs-example-modal-md" id="DeleteVehicleFromList" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog" style="width:50%">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title"><?php print CommonLanguage::getMessage('Confirm_Delete'); ?></h4>
                <div id="DeleteVehicle_modalAlert" class="alert alert-success"></div>
            </div>
            <div class="modal-body">
                <form>
                    <div class="row">
                        <div class="col-md-12 ">
                            <div class="col-md-12" style="margin-top: 20px;">
                                <p><?php print CommonLanguage::getMessage('delete_vehicle_driver'); ?> <strong class="Name"></strong>, </p>
                                <p><?php print CommonLanguage::getMessage('Proceed_Delete'); ?></p>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button type="button" class="btn btn-danger" id="Assign_Delete" style="width:auto"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
        </div>
    </div>
</div>
<script>
    var Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            title = <?php echo json_encode(CommonLanguage::getMessage('vehicle_driver_list')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>;
    $(function () {
        RebuildDataTable(title, [0, 1, 2, 3, 4], 'GarageAjax/VehicleDriverListRequest', 1)
    })
    var CurrentVehicle_ToDelete;
    $("#tableWithSearch").on("click", ".Delete", function () {
        var TypeName = $(this).parents("tr").find("td:eq(0)").text(), //parent tr keep searching till it reach tr 
                Name = $(this).parents("tr").find("td:eq(1)").text()//parent tr keep searching till it reach tr 

        $("#DeleteVehicleFromList").find(".Name").text(TypeName + ' ' + Name).end().modal("show")

        CurrentVehicle_ToDelete = $(this).parents("tr").find('.Action').attr('data-id') // current id to delete

    })
    $("#DeleteVehicle_modalAlert").hide(); // hide the alert when submit if true or false
    //on click the delete button in modal get the id of the selected  to delete it 
    $("#Assign_Delete").on("click", function () {
        var del_id = $(this).attr('data-id');
        var oTable = $('#tableWithSearch').dataTable();
        var me = this;
        $.ajax({
            type: 'POST',
            url: 'GarageAjax/deleteVehicleAjax',
            data: {GetID: CurrentVehicle_ToDelete},
            complete: function (data, vv) {
                if (vv == "success" && data.responseText == "OK") {
                    RebuildDataTable(title, [0, 1, 2, 3, 4], 'GarageAjax/VehicleDriverListRequest', 1)

                    $("#DeleteVehicle_modalAlert").attr("class", "alert alert-success").html(Submitted_Successfully).show();
                    setTimeout(function () {
                        $("#DeleteVehicleFromList").modal("hide")
                        $("#DeleteVehicle_modalAlert").remove()
                    }, 1000)
                } else {
                    $("#DeleteVehicle_modalAlert").attr("class", "alert alert-danger").html(Failed_Submit).show();

                }
            }});
    })




</script>