<script>
    function calculate_remaining_deposit_payment(update = 'no') {
        if (update == 'no') {

            var total_amount = $(document).find("#hidden_actual_deposit").val();
            var deposit_amount = $(document).find("#trip_deposit_amount").val();

        }
        if (update == 'yes') {
            var total_amount = $(document).find("#u_hidden_actual_deposit").val();
            var deposit_amount = $(document).find("#u_trip_deposit_amount").val();
        }
        var remaining_amount = parseFloat(total_amount) - parseFloat(deposit_amount);
        $(document).find("#trip_deposit_remaining_amount").val(remaining_amount);
        $(document).find("#u_trip_deposit_remaining_amount").val(remaining_amount);
    }

    $(document).on('click', '.trip_company_deposit', function () {
        var trip_id = $(this).data('id');
        // console.log(trip_id);
        var url = "{{ route('trip_deposit_index') }}";
        // url = url.replace(':id', trip_id);
        $.ajax({
            url: url,
            type: "POST",
            data: {
                trip_id: trip_id
            },
            headers: {
                "X-CSRF-TOKEN": "{{ csrf_token() }}"
            },
            success: function (response) {
                // console.log(response);
                $(document).find("#addTripAdvanceData").html(response);
                $(document).find("#addTripAdvanceModal").modal('toggle');
            }
        });
    });
    $(document).on('submit', "#addTripDepositForm", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('trip_deposit_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(document).find(".r_error").text('');
                $(document).find("#tripDepositSubmit").text('Processing....');
                $(document).find("#tripDepositSubmit").prop('disabled', true);
            },
            success: function (data) {
                $(document).find(".r_error").text('');
                $(document).find("#tripDepositSubmit").text('جمع');
                $(document).find("#tripDepositSubmit").prop('disabled', false);
                if (data.success) {
                    // $(document).find("#addTripAdvanceModal").modal('toggle');
                    $(document).find("#deposit_at_co_modal_table").html(data.view);
                    alertify.success("Trip Deposit Stored Successfully!");
                    setTimeout(() => {
                        $(document).find("#deposit_date").focus();
                    }, 200);
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $(document).find("#tripDepositSubmit").text('جمع');
                $(document).find("#tripDepositSubmit").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(document).find(".error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $(document).find("#" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });

</script>
