<script>
    function clear_fields() {
        $("#add_driver_form")[0].reset();
        $(".error").text('');
    }

    $('#name').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('cnic').focus();
        }
    });
    $('#cnic').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('mobile_number').focus();
        }
    });
    $('#mobile_number').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('joining_date').focus();
        }
    });
     $('#joining_date').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('cnic_front').focus();
        }
    });
     $('#cnic_front').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('cnic_back').focus();
        }
    });


    var driver_table = $("#driver_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('driver_index') }}",
        columns: [{
                name: "DT_RowIndex",
                data: "DT_RowIndex"
            },
            {
                name: "name",
                data: "name"
            },
            {
                name: "mobile",
                data: "mobile"
            },
            {
                name: "cnic",
                data: "cnic"
            },{
                name: "images",
                data: "images"
            },
            {
                name: "salary",
                data: "salary"
            },  {
                name: "status",
                data: "status"
            },
            {
                name: "action",
                data: "action",
                orderable: false,
            },
        ]
    });
    $("#add_driver_form").on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('driver_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function() {
                $(".error").text('');
                $("#submit_btn").text('پراسسنگ۔۔۔۔');
                $("#submit_btn").prop('disabled', true);
            },
            success: function(data) {
                $("#submit_btn").text('محفوظ');
                $("#submit_btn").prop('disabled', false);
                if (data.success) {
                    driver_table.ajax.reload();
                    clear_fields();
                    alertify.success("Driver Stored Successfully!");
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function(error) {
                $("#submit_btn").text('محفوظ');
                $("#submit_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });
    $(document).on('click', '.resign_driver', function() {
        var edit_id = $(this).data('id');
        var url = "{{ route('driver_resign', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function(resonpse) {
                // console.log(resonpse);
                $("#editDriverData").html(resonpse);
                $("#editDriverModal").modal('toggle');
            }
        });
    });
    $(document).on('click', '.edit_driver', function() {
        var edit_id = $(this).data('id');
        var url = "{{ route('driver_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function(resonpse) {
                // console.log(resonpse);
                $("#editDriverData").html(resonpse);
                $("#editDriverModal").modal('toggle');
            }
        });
    });
    $(document).on('submit', "#update_driver_form", function(e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('driver_update') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function() {
                $("#update_btn").text('پراسسنگ۔۔۔۔');
                $("#update_btn").prop('disabled', true);
            },
            success: function(data) {
                $("#update_btn").text('اپ ڈیٹ');
                $("#update_btn").prop('disabled', false);
                if (data.success) {
                    driver_table.ajax.reload();
                    $("#editDriverModal").modal('hide');
                    alertify.success("Driver Updated Successfully!");

                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function(error) {
                $("#update_btn").text('اپ ڈیٹ');
                $("#update_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".u_error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#u_" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });
    $(document).on('submit', "#add_resign_form", function(e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('driver_resign_stroe') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function() {
                $("#update_btn").text('پراسسنگ۔۔۔۔');
                $("#update_btn").prop('disabled', true);
            },
            success: function(data) {
                $("#update_btn").text('اپ ڈیٹ');
                $("#update_btn").prop('disabled', false);
                if (data.success) {
                    driver_table.ajax.reload();
                    $("#editDriverModal").modal('hide');
                    alertify.success("Driver Updated Successfully!");

                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function(error) {
                $("#update_btn").text('اپ ڈیٹ');
                $("#update_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".u_error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#u_" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });
    $(document).on('click', '.delete_driver', function() {
        var del_id = $(this).data('id');
        var url = "{{ route('driver_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('Are You Sure To Delete The Driver?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function(response) {
                    if (response.success) {
                        driver_table.ajax.reload();
                        alertify.success("Driver Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
    $(document).on('click', '.active', function() {
        var edit_id = $(this).data('id');
        var url = "{{ route('activate_driver', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function(resonpse) {
                // console.log(resonpse);
                if (resonpse.success) {
                    driver_table.ajax.reload();
                    alertify.success("Driver Activated Successfully!");

                }

            }
        });
    });
    $(document).on('click', '.deactivate', function() {
        var edit_id = $(this).data('id');
        var url = "{{ route('deactivate_driver', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function(resonpse) {
                // console.log(resonpse);
                if (resonpse.success) {
                    driver_table.ajax.reload();
                    alertify.success("Driver Deactivated Successfully!");

                }

            }
        });
    });
</script>
