<script>
    $("#broker").select2();
    $(document).on('change', '#broker', function (e) {
        var table = $(this).find(':selected').data('table');
        $("#user_type").val(table);
    });
    $(document).on('change', '#u_broker', function (e) {
        var table = $(this).find(':selected').data('table');
        $(document).find("#u_user_type").val(table);
    });

    $(document).on('keypress', '.change_focus', function (e) {
        var next_id = $(this).data('id');
        if (e.which === 13) {
            e.preventDefault();
            setTimeout(() => {
                document.getElementById(next_id).focus();
            }, 200);
        }
    });

    function payment_type_change(type, update = 'no') {

       if(type=='in-account'){
            $("#bank_dev").html('');

        $.ajax({
                url: "{{ route('get_banks') }}",
                type: "POST",
                headers: {
                    "X-CSRF-TOKEN": "{{ csrf_token() }}"
                },
                data: {
                    params:'get_workshop' // Ensure `get_workshop` is a valid variable or replace with actual value
                },
                success: function (data) {
                    $("#workshop_div").html(data);

                }
            });
        return false;
       }
        if (type == 'cash') {
            $("#bank_dev").html('');
            $("#workshop_div").html('');
            $(document).find("#u_bank_dev").html('');
        } else {
            $("#workshop_div").html('');
            $.ajax({
                url: "{{ route('get_banks') }}",
                type: "POST",
                headers: {
                    "X-CSRF-TOKEN": "{{ csrf_token() }}"
                },
                success: function (data) {
                    if (update == 'yes') {

                        $(document).find("#u_bank_dev").html(data);
                        setTimeout(() => {
                            $(document).find("#all_bank").select2();
                        }, 200);
                    } else {
                        $("#bank_dev").html(data);
                        setTimeout(() => {
                            $(document).find("#all_bank").select2({
                                tags: true
                            });
                        }, 200);
                    }

                }
            });
        }
    }

    function clear_fields() {
        $("#add_receipt_form")[0].reset();
        $(".error").text('');
        $("#bank_dev").html('');

    }

    var driver_table = $("#driver_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('receipt_index') }}",
        columns: [{
            name: "DT_RowIndex",
            data: "DT_RowIndex"
        },
            {
                name: "payment_type",
                data: "payment_type"
            },
            {
                name: "date",
                data: "date"
            },
            {
                name: "amount",
                data: "amount"
            },
            {
                name: "broker",
                data: "broker"
            },
            {
                name: "action",
                data: "action",
                orderable: false,
            },
        ]
    });
    $("#add_receipt_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('receipt_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('Save');
                $("#submit_btn").prop('disabled', false);
                if (data.success) {
                    driver_table.ajax.reload();
                    clear_fields();
                    alertify.success("Receipt Voucher Created Successfully!");
                    setTimeout(() => {
                        document.getElementById('date').focus();
                    }, 300);
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $("#submit_btn").text('Save');
                $("#submit_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#" + index + "_error").text(value[0]);
                    });
                }
                $('#date').focus();

            }
        });
    });
    $(document).on('click', '.edit_voucher', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('receipt_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $("#editVoucherData").html(resonpse);
                $("#editVoucherModal").modal('toggle');
                setTimeout(() => {
                    $(document).find('#u_date').focus();
                    $(document).find('#u_broker').select2();
                    $(document).find('#all_bank').select2();

                }, 500);
            }
        });
    });
    $(document).on('submit', "#update_voucher_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('receipt_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');
                $("#update_btn").prop('disabled', false);
                if (data.success) {
                    driver_table.ajax.reload();
                    $("#editVoucherModal").modal('hide');
                    alertify.success("Voucher Updated Successfully!");
                    setTimeout(() => {
                        document.getElementById('date').focus();
                    }, 300);
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $("#update_btn").text('Update');
                $("#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]);
                    });
                }
                setTimeout(() => {
                    $(document).find('#u_date').focus();
                    $(document).find('#u_broker').select2();
                    $(document).find('#all_bank').select2();

                }, 500);
            }
        });
    });
    $(document).on('click', '.delete_voucher', function () {
        var del_id = $(this).data('id');
        var url = "{{ route('receipt_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('Are You Sure To Delete The Receipt Voucher?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (response) {
                    if (response.success) {
                        driver_table.ajax.reload();
                        alertify.success("Voucher Deleted Successfully!");
                        setTimeout(() => {
                            document.getElementById('date').focus();
                        }, 300);
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
</script>
