<?php
if ($permissions->hasPermission('add_vehicle_checkup')) {
    include 'Views/Garage/add_vehicle_checkup.php';
}
?>
<style>
    #tableWithSearch thead tr >th {
        background-color: #652d90;
        color: #fff;
    }
</style>
<!-- End of logging alert -->
<div class="container-fluid container-fixed-lg bg-white" style="height:100%">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"> <?php print CommonLanguage::getMessage('checkup_list'); ?></div>
            <?php
            if ($permissions->hasPermission('add_vehicle_checkup')) {
                echo Forms::FloatingAddButton('652d90', '#add_vehicle_checkup', CommonLanguage::getMessage('add'));
            }
            ?> 
            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <table class="table table-bordered table-hover demo-table-search" id="tableWithSearch" >
                <thead>
                    <tr>
                        <th class="text-center"><?php print CommonLanguage::getMessage('vehicle'); ?></th>
                        <th class="text-center"><?php print CommonLanguage::getMessage('vehicle_name'); ?></th>
                        <th class="text-center"><?php print CommonLanguage::getMessage('employee_name'); ?></th>
                        <th class="text-center"  ><?php print CommonLanguage::getMessage('Date'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('milage'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('checkup'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('note'); ?></th>
                        <th class="text-center" ><?php print CommonLanguage::getMessage('Action'); ?></th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="modal fade" id="GlobalDeleteObject" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label bold"><?php echo CommonLanguage::getMessage('confirmdelete');?></h5>
                <?php echo Forms::AlertDiv('modal_alert_deletes'); ?>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12 m-t-10"><div class="alert hidden" id="GlobalDeleteAlert"></div></div>
                    <div class="Conf col-md-12 m-t-10">
                        <p><?php echo (CommonLanguage::getMessage('sure_to_delete')); ?></p>
                        <p><?php echo (CommonLanguage::getMessage('proceedMsg')); ?></p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button"  class="btn btn-default" data-dismiss="modal"><?php echo (CommonLanguage::getMessage('Close')) ?></button>
                <button type="button" id="deleteCheckup" class="RemoveThisItem btn btn-danger danger widthauto"><?php echo (CommonLanguage::getMessage('Save')) ?></button>
            </div>
        </div>
        <div ID="GlobalDeleteLoader" style="position:absolute;left:0;right:0;top:0px;height:100%;background:rgba(255,255,255,0.9);z-index:999" class="hidden"><img class="image-responsive-height demo-mw-50" src="img/demo/progress.svg" alt="Progress" style="position: absolute;left: 50%;top: 50%;margin: -25px 0 0 -25px;" /></div>
    </div>
</div>
<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('checkup_list')); ?>;
    var Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
        Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>;
        var vehicle_checkup_id;
    $(function () {
        RebuildDataTable(title, [0, 1, 2, 3, 4], 'GarageAjax/vehiclecheckupListRequest');
        $("#tableWithSearch").on("click", ".Edit", function () {
            vehicle_checkup_id = $(this).parents('div.Action').attr("data-id");
            $('#add_vehicle_checkup').modal("show");
            $('#IS_Edit').val('1');
            $.ajax({
                url: 'GarageAjax/ViewVehicleCheckupByID?vehicle_checkup_id=' + vehicle_checkup_id,
                dataType: 'JSON',
                complete: function (v, v1) {
                    if (v1 == 'success') {
                        var Data = v.responseJSON;
                        $('#checkup_date').val(Data['checkup_date']);
                        $('#checkup_note').val(Data['checkup_note']);
                        $('#vehicle_milage').val(Data['vehicle_milage']);
                        $('#checkup_note').val(Data['checkup_note']);
                        $('#vehicle_id').val(Data['vehicle_id']);
                        $('#vehicle_id').attr('data-select-name',Data['vehicle_name']);
                        $("#vehicle_action > [name='vehicle_action'][value='" + (Data['vehicle_action']) + "']").prop("checked", true) // radio
                        //                            build_select2($('#vehicle_id'), 'xhr/CommonSearch?col_id=Vehicle_ID&col_name=concat(Vehicle_Code,"-",Vehicle_Registration_Number)&table=tbl_garage_vehicle');
                        build_select2($('#vehicle_id'), 'xhr/truckSearch');
                    }
                }
            });
        });
        //        
        $("#tableWithSearch").on("click", ".Delete", function () {
            vehicle_checkup_id = $(this).parents('div.Action').attr("data-id");
            $('#GlobalDeleteObject').modal('show');
        });
        $('#deleteCheckup').on('click', function () {
            $.ajax({
                url: 'GarageAjax/vehiclecheckupListRequest?vehicle_checkup_id=' + vehicle_checkup_id,
                dataType: 'JSON',
                complete: function (v, v2) {
                    if (v2 == 'success') {
                        if (v.responseJSON[0] == 'ok') {
                            $('#modal_alert_deletes').removeClass('hidden').addClass('alert-success').empty().html(Submitted_Successfully);
                            RebuildDataTable(title, [0, 1, 2, 3, 4], 'GarageAjax/vehiclecheckupListRequest');
                            setTimeout(function () {
                                $('#modal_alert_deletes').removeClass('alert-success').addClass('hidden');
                                $('#GlobalDeleteObject').modal('hide');
                            }, 800);
                        } else {
                            $('#modal_alert_deletes').removeClass('hidden').addClass('alert-danger').empty().html(v.responseJSON[1]);
                        }
                    } else {
                        $('#modal_alert_deletes').removeClass('hidden').addClass('alert-danger').empty().html(Failed_Submit);
                    }
                }
            });
        });
    });
</script>
