<script>
    $("#driver").select2();

    $('#driver').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('vehicle_number').focus();
        }
    });

    $('#vehicle_number').bind('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById('driver_phone').focus();
        }
    });

    function get_driver_detail() {
        var drivers = $("#driver").val();
        console.log(drivers);
        $.ajax({
            url: "{{ route('get_driver_detail') }}",
            type: "POST",
            headers: {
                'X-CSRF-TOKEN': "{{ csrf_token() }}",
            },
            data: {
                drivers: drivers
            },
            beforeSend: function () {

            },
            success: function (data) {
                $("#driver_phone").val(data);
            },

        });

    }

    function clear_fields() {
        $("#add_vehicle_form")[0].reset();
        $(".error").text('');
        $("#driver").select2();

    }

    var vehicle_table = $("#vehicle_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('vehicle_index') }}",
        columns: [{
            name: "DT_RowIndex",
            data: "DT_RowIndex"
        },
            {
                name: "vehicle_no",
                data: "vehicle_no"
            },
            {
                name: "driver",
                data: "driver"
            },
            {
                name: "phone",
                data: "phone"
            },
            {
                name: "action",
                data: "action",
                orderable: false,
            },
        ]
    });

    $("#add_vehicle_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('vehicle_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(".error").text('');
                $("#submit_btn").text('Processing....');
                $("#submit_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#submit_btn").text('محفوظ');
                $("#submit_btn").prop('disabled', false);
                if (data.success) {
                    vehicle_table.ajax.reload();
                    clear_fields();
                    $('#v_count').text(data.v_count)
                    alertify.success("Vechicle 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', '.edit_vehicle', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('vehicle_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (response) {
                // console.log(resonpse);
                $("#editVehicleData").html(response.view);
                $("#editVehicleModal").modal('toggle');
                setTimeout(() => {
                    $(document).find("#u_driver").select2().val(response.ids).trigger();
                }, 200)
            }
        });
    });

    $(document).on('submit', "#update_vehicle_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('vehicle_update') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $("#update_btn").text('Processing....');
                $("#update_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#update_btn").text('اپڈیٹ');
                $("#update_btn").prop('disabled', false);
                if (data.success) {
                    vehicle_table.ajax.reload();
                    $("#editVehicleModal").modal('hide');
                    alertify.success("Vehicle 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_vehicle', function () {
        var del_id = $(this).data('id');
        var url = "{{ route('vehicle_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('Are You Sure To Delete The Vehicle?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (response) {
                    if (response.success) {
                        vehicle_table.ajax.reload();
                        $('#v_count').text(response.v_count)

                        alertify.success("Vehicle Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
</script>
