<script>
    $("#credit_account").select2();
    $("#debit_account").select2();
    $(document).on('keypress', '.change_focus', function (e) {
        var next_id = $(this).data('id');
        if (e.which === 13) {
            e.preventDefault();
            document.getElementById(next_id).focus();
        }
    });

    function clear_fields() {

        $("#add_journal_voucher_form")[0].reset();
        $(".error").text('');
        $("#credit_account").select2();
        $("#debit_account").select2();

    }

    $("#credit_account").on('change', function (e) {
        var table = $(this).find(':selected').data('table');
        $("#credit_table").val(table);
    });
    $(document).on('change', "#u_credit_account", function (e) {
        var table = $(this).find(':selected').data('table');
        $(document).find("#u_credit_table").val(table);
    });
    $("#debit_account").on('change', function (e) {
        var table = $(this).find(':selected').data('table');
        // console.log(table);
        $("#debit_table").val(table);
    });
    $(document).on('change', "#debit_account", function (e) {
        var table = $(this).find(':selected').data('table');
        // console.log(table);
        $(document).find("#u_debit_table").val(table);
    });
    var journal_voucher_table = $("#journal_voucher_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('journal_voucher_index') }}",
        columns: [{
            name: "DT_RowIndex",
            data: "DT_RowIndex"
        },
            {
                name: "date",
                data: "date"
            },
            {
                name: "debit_account",
                data: "debit_account"
            }, {
                name: "credit_account",
                data: "credit_account"
            },
            {
                name: "amount",
                data: "amount"
            },
            {
                name: "action",
                data: "action",
                orderable: false,
            },
        ]
    });
    $("#add_journal_voucher_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('journal_voucher_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) {
                    journal_voucher_table.ajax.reload();
                    clear_fields();
                    alertify.success("Journal Voucher Created Successfully!");
                    $('#date').focus();
                } 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]);
                    });
                }
                $('#date').focus();
            }
        });
    });
    $(document).on('click', '.edit_head', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('journal_voucher_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $("#editJournalVoucherData").html(resonpse);
                $("#editJournalVoucherModal").modal('toggle');
                setTimeout(() => {
                    $(document).find("#u_credit_account").select2();
                    $(document).find("#u_debit_account").select2();
                    $(document).find("#u_date").focus();
                }, 600)
            }
        });
    });
    $(document).on('submit', "#update_journal_voucher_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('journal_voucher_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) {
                    $(document).find("#date").focus();
                    journal_voucher_table.ajax.reload();
                    $("#editJournalVoucherModal").modal('hide');
                    alertify.success("Voucher 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_head', function () {
        var del_id = $(this).data('id');
        var url = "{{ route('journal_voucher_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('Are You Sure To Delete The Journal Voucher?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (response) {
                    if (response.success) {
                        journal_voucher_table.ajax.reload();
                        alertify.success("Journal Voucher Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
</script>
