@extends('backend.layouts.app')
@section('title', @$data['title'])

@section('content')
    {!! breadcrumb([
        'title' => @$data['title'],
        route('admin.dashboard') => _trans('common.Dashboard'),
        '#' => @$data['title'],
    ]) !!}

    @include('travel::travel-plans.nav', ['plan_id' => $data['plan_id'], 'is_approved' => true])

    <div class="table-content table-basic">
        <div class="card">
            <div class="card-body">
                <!-- toolbar table start -->
                <div
                    class="table-toolbar d-flex flex-wrap gap-2 flex-xl-row justify-content-center justify-content-xxl-between align-content-center pb-3">
                    <div class="align-self-center">
                        <div class="d-flex flex-wrap gap-2  flex-lg-row justify-content-center align-content-center">
                            <!-- show per page -->
                            <div class="align-self-center">
                                <label>
                                    <span class="mr-8">{{ _trans('common.Show') }}</span>
                                    <select class="form-select d-inline-block" id="entries"
                                        onchange="travelPlanExpenseDatatable()">
                                        @include('backend.partials.tableLimit')
                                    </select>
                                    <span class="ml-8">{{ _trans('common.Entries') }}</span>
                                </label>
                            </div>



                            <div class="align-self-center d-flex flex-wrap gap-2">
                                <!-- add btn -->
                                @if (hasPermission('travel_expense_create'))
                                    <div class="align-self-center">
                                        <a href="{{ route('travel.plans.expenses.create', $data['plan_id']) }}" role="button" class="btn-add"
                                            data-bs-toggle="tooltip" data-bs-placement="right">
                                            <span><i class="fa-solid fa-plus"></i> </span>
                                            <span class="d-none d-xl-inline">{{ _trans('common.Create') }}</span>
                                        </a>
                                    </div>
                                @endif

                                <!-- search -->
                                <div class="align-self-center">
                                    <div class="search-box d-flex">
                                        <input class="form-control" placeholder="{{ _trans('common.Search') }}" name="search" onkeyup="travelPlanExpenseDatatable()" autocomplete="off">
                                        <span class="icon"><i class="fa-solid fa-magnifying-glass"></i></span>
                                    </div>
                                </div>
                                
                                <div class="align-self-center">
                                    <button type="button" class="btn-daterange" id="daterange" data-bs-toggle="tooltip"
                                        data-bs-placement="right" data-bs-title="{{ _trans('common.Date Range') }}">
                                        <span class="icon"><i class="fa-solid fa-calendar-days"></i>
                                        </span>
                                        <span class="d-none d-xl-inline">{{ _trans('common.Date Range') }}</span>
                                    </button>
                                    <input type="hidden" id="daterange-input" onchange="travelPlanExpenseDatatable()">
                                </div>
                            </div>
                            {{-- exportOnMobileDevice --}}
                            <div class="d-flex d-lg-none">
                                @include('backend.partials.buttons')
                            </div>
                            {{-- exportOnMobileDevice::end --}}
                        </div>
                    </div>
                    <!-- export -->
                    <div class="d-none d-lg-flex">
                        @include('backend.partials.buttons')
                    </div>
                </div>
                <!-- toolbar table end -->
                <!--  table start -->
                <div class="table-responsive  min-height-500">
                    @include('backend.partials.table')
                </div>
                <!--  table end -->
            </div>
        </div>
    </div>
@endsection
@section('script')
    @include('backend.partials.table_js')

    <script>
        const approveExpense = (id, totalAmount) => {

            Swal.fire({
                title: `{{ _trans('common.Are you sure to approve this Expense?') }}`,
                html: `<p class='text-16'>{{ _trans('common.Total Amount: ') }} <b>${totalAmount}</b><p>`,
                input: "text",
                inputPlaceholder: `{{ _trans('common.Enter the approved amount') }}`,
                inputValidator: (value) => {
                    return new Promise((resolve) => {
                        if (!value.trim()) {
                            resolve('{{ _trans("common.Input cannot be empty!") }}');
                        } else if (isNaN(value) || parseFloat(value) <= 0) {
                            resolve('{{ _trans("common.Input must be a valid number greater than 0!") }}');
                        } else if (parseFloat(value) > totalAmount) {
                            resolve('{{ _trans("common.Input cannot exceed total amount!") }}');
                        } else {
                            resolve();
                        }
                    });
                },
                icon: "warning",
                showCancelButton: true,
                confirmButtonColor: "#3085d6",
                cancelButtonColor: "#d33",
                confirmButtonText: `{{ _trans('common.Submit') }}`,
                showLoaderOnConfirm: true,
            }).then((result) => {
                if (result.isConfirmed) {
                    let url = `{{ route('travel.plans.expenses.approve', ':id') }}`;
                    url = url.replace(':id', id);

                    const approvalAmount = result.value;

                    $.ajax({
                        url,
                        type: 'POST',
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        data: {
                            approval_amount: approvalAmount
                        },
                        success: function ({ status, message }) {
                            if (status) {
                                Toast.fire({
                                    icon: 'success',
                                    title: message
                                });
                            }

                            setTimeout(() => {
                                location.reload();
                            }, 1000);
                        },
                        error: function (error) {
                            let err = JSON.parse(error.responseText);

                            Toast.fire({
                                icon: 'error',
                                title: err
                            });
                        }
                    });
                }
            });
        }
    </script>
@endsection
