<script>
    function cancel_update_advance_form() {
        var trip_id = $(document).find('#advance_trip_id').val();
        show_trip_advance_modal(trip_id);
    }

    $(document).on('click', '.add_trip_advance', function () {
        var trip_id = $(this).data('id');
        show_trip_advance_modal(trip_id)

    });

    function show_trip_advance_modal(trip_id) {
        var url = "{{ route('trip_advance_index', ':id') }}";
        url = url.replace(':id', trip_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $("#addTripAdvanceData").html(resonpse); 
                $("#addTripAdvanceModal").modal('show');
                setTimeout(() => {
                    $(document).find("#adv_date").focus();
                }, 500);
            }
        });
    }

    function advance_amount_change(update) {
        if (update == 'no') {
            var table = $(document).find("#adv_broker").find(':selected').data('table');
            $(document).find("#adv_party_table").val(table);
        } else {
            var table = $(document).find("#u_adv_broker").find(':selected').data('table');
            $(document).find("#u_adv_party_table").val(table);


        }

    }

    function adv_payment_change(value, update = 'no') {
        if (value == 'cash') {
            $(document).find("#adv_bank_dev").html('');
            $(document).find("#u_adv_bank_dev").html('');
        } else {
            console.log({
                'type': value,
                'update_value': update
            });
            $.ajax({
                url: "{{ route('advance_payment_type_change') }}",
                type: "POST",
                headers: {
                    "X-CSRF-TOKEN": "{{ csrf_token() }}"
                },
                data: {
                    'type': value,
                    'update_value': update
                },
                success: function (resonpse) {
                    // console.log(resonpse);
                    if (update == 'no') {
                        $(document).find("#adv_bank_dev").html(resonpse);
                    } else {
                        $(document).find("#u_adv_bank_dev").html(resonpse);

                    }
                    setTimeout(() => {
                        $(document).find("#adv_bank").select2();
                        $(document).find("#adv_broker").select2();
                        $(document).find("#u_adv_broker").select2();
                        $(document).find("#u_adv_bank").select2();
                    }, 500);
                }
            });
        }
    }

    $(document).on('submit', "#addAdvanceInTripForm", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('trip_advance_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(".error").text('');
                $(document).find("#addAdvanceInTripSubmit").text('Processing....');
                $(document).find("#addAdvanceInTripSubmit").prop('disabled', true);
            },
            success: function (data) {
                $(document).find("#addAdvanceInTripSubmit").text('مخفوظ');
                $(document).find("#addAdvanceInTripSubmit").prop('disabled', false);
                if (data.success) {
                    alertify.success("Advance Added To Trip Successfully!");
                    $(document).find("#trip_advance_table_body").html(data.view);
                    setTimeout(() => {
                        $(document).find("#adv_date").focus();
                    }, 500);

                } else {
                    $("#adv_date_error").text(data.message);
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $(document).find("#addAdvanceInTripSubmit").text('مخفوظ');
                $(document).find("#addAdvanceInTripSubmit").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".advance_error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $(document).find("#" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });
    $(document).on('submit', "#updateAdvanceInTripForm", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('trip_advance_update') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(".error").text('');
                $(document).find("#addAdvanceInTripSubmit").text('Processing....');
                $(document).find("#addAdvanceInTripSubmit").prop('disabled', true);
            },
            success: function (data) {
                $(document).find("#addAdvanceInTripSubmit").text('اپڈیٹ');
                $(document).find("#addAdvanceInTripSubmit").prop('disabled', false);
                if (data.success) {
                    // $(document).find("#addTripAdvanceModal").modal('toggle');
                    alertify.success("Advance Update To Trip Successfully!");
                    // window.location.reload();
                    show_trip_advance_modal(data.trip_id);
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $(document).find("#addAdvanceInTripSubmit").text('اپڈیٹ');
                $(document).find("#addAdvanceInTripSubmit").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".advance_error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $(document).find("#" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });
    $(document).on('click', '.edit_advance', function () {
        var edit_id = $(this).data('id');
        console.log(edit_id);
        var url = "{{ route('trip_advance_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $(document).find("#trip_advance_modal_body").html(resonpse);
                setTimeout(() => {
                    $(document).find('#adv_amount').focus();
                    $(document).find("#adv_bank").select2();
                    $(document).find("#adv_broker").select2();
                    $(document).find("#u_adv_broker").select2();
                    $(document).find("#u_adv_bank").select2();
                }, 500);
            }
        });
    });
    // delete_adjust
    $(document).on('click', '.delete_advance', function () {
        var del_id = $(this).data('id');
        console.log(del_id);
        var url = "{{ route('trip_advance_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('Are You Sure To Delete The Trip Advance?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (data) {
                    console.log(data);
                    if (data.success) {
                        // show_trip_advance_modal(data.trip_id);
                        $(document).find("#trip_advance_table_body").html(data.view);
                        setTimeout(() => {
                            $(document).find("#adv_amount").focus();
                        }, 500);
                        alertify.success("Trip Advance Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
</script>
