@php $title = __('Revenue Report'); @endphp

@extends('layouts.master', [
    'title' => $title
])

@section('content')

    <x-page-header
        :title="$title"
        :breadcrumbs="[
            ['name' => __('Home'), 'url' => route('dashboard.index')]
        ]"
    />

    <form id="form-customers" method="POST" action="{{ route('reports.revenues.filter') }}" enctype="multipart/form-data">
        @csrf
        <x-card class="card-2" cardLabel="fs-5">
            <x-slot:label>{{ __('Report Filters') }}</x-slot:label>

            <x-row>
                <x-col size="3">
                    <x-form-label :label="__('User')" for="user_id"></x-form-label>
                    <x-form-select name="user_id" id="user_id" class="users-select" />
                </x-col>
                <x-col size="3">
                    <x-form-label :label="__('Payment Method')" for="payment_method_id"></x-form-label>
                    <x-form-select name="payment_method_id" id="payment_method_id" class="payment-methods-select" />
                </x-col>
                <x-col size="3">
                    <x-form-label :label="__('From Date')" for="from_date" />
                    <x-form-input type="text" name="from_date" class="flatpickr_date" id="from_date" :value="$request['from_date'] ?? ''" />
                </x-col>
                <x-col size="3">
                    <x-form-label :label="__('To Date')" for="to_date" />
                    <x-form-input type="text" name="to_date" class="flatpickr_date" id="to_date" :value="$request['to_date'] ?? ''" />
                </x-col>
            </x-row>

            <x-row class="text-end">
                <x-col size="12">
                    <x-button type="submit" size="sm" class="btn-primary">{{ __('Save Data') }}</x-button>
                </x-col>
            </x-row>
        </x-card>
    </form>

    @if(isset($summary))
        @php
            $methods = collect($summary['by_method'] ?? []);
            $topMethod = $methods->sortByDesc('amount')->first();
            $topMethodName = $methods->sortByDesc('amount')->keys()->first();

            $average = $summary['count'] > 0
                ? $summary['total_amount'] / $summary['count']
                : 0;
        @endphp

        <x-row id="stats-cards">

            <x-col size="6" class="col-xl-3">
                <div class="card mini-stats-wid shadow-sm border-0">
                    <div class="card-body d-flex align-items-center">
                        <div @class('flex-grow-1')>
                            <p class="text-muted fw-medium mb-1">{{ __('Total Amounts') }}</p>
                            <h4 class="mb-0 fw-bold">
                                {{ getPriceFormat($summary['total_amount']) }}
                            </h4>
                        </div>

                        <div @class('flex-shrink-0')>
                            <div class="avatar-sm rounded-circle bg-info d-flex align-items-center justify-content-center">
                                <i class="bx bx-money font-size-24 text-white"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </x-col>

            {{-- Number of Vouchers --}}
            <x-col size="6" class="col-xl-3">
                <div class="card mini-stats-wid shadow-sm border-0">
                    <div class="card-body d-flex align-items-center">
                        <div @class('flex-grow-1')>
                            <p class="text-muted fw-medium mb-1">{{ __('Number of Vouchers') }}</p>
                            <h4 class="mb-0 fw-bold">
                                {{ $summary['count'] }}
                            </h4>
                        </div>

                        <div @class('flex-shrink-0')>
                            <div class="avatar-sm rounded-circle bg-primary d-flex align-items-center justify-content-center">
                                <i class="bx bx-receipt font-size-24 text-white"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </x-col>

            {{-- Top Payment Method --}}
            @if($topMethod)
                <x-col size="6" class="col-xl-3">
                    <div class="card mini-stats-wid shadow-sm border-0">
                        <div class="card-body d-flex align-items-center">
                            <div @class('flex-grow-1')>
                                <p class="text-muted fw-medium mb-1">{{ __('Top Payment Method') }}</p>
                                <h4 class="mb-0 fw-bold">
                                    {{ $topMethodName }}
                                    <span class="ms-2 font-size-14">
                            {{ getPriceFormat($topMethod['amount']) }}
                        </span>
                                </h4>
                            </div>

                            <div @class('flex-shrink-0')>
                                <div class="avatar-sm rounded-circle bg-success d-flex align-items-center justify-content-center">
                                    <i class="bx bx-credit-card font-size-24 text-white"></i>
                                </div>
                            </div>
                        </div>
                    </div>
                </x-col>
            @endif

            {{-- Average --}}
            <x-col size="6" class="col-xl-3">
                <div class="card mini-stats-wid shadow-sm border-0">
                    <div class="card-body d-flex align-items-center">
                        <div @class('flex-grow-1')>
                            <p class="text-muted fw-medium mb-1">{{ __('Average Amount') }}</p>
                            <h4 class="mb-0 fw-bold">
                                {{ getPriceFormat($average) }}
                            </h4>
                        </div>

                        <div @class('flex-shrink-0')>
                            <div class="avatar-sm rounded-circle bg-warning d-flex align-items-center justify-content-center">
                                <i class="bx bx-calculator font-size-24 text-white"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </x-col>

        </x-row>
    @endif

    <x-card :header="false" >
        <x-data-table
            id="normal-table"
            :checkbox="false"
            :columns="[
                ['label' => 'ID', 'width' => '40px'],
                ['label' => __('Voucher Number')],
                ['label' => __('Customer')],
                ['label' => __('Amount')],
                ['label' => __('Payment Date')],
                ['label' => __('Payment Method')],
                ['label' => __('User')],
                ['label' => __('Subscription')],
                ['label' => __('Status')],
            ]"
        >
            @if(isset($vouchers) && $vouchers->count() > 0)
                @foreach($vouchers as $voucher)
                    <tr>
                        <td>{{ $voucher->id }}</td>
                        <td>{{ $voucher->voucher_number }}</td>
                        <td>{!! CustomerInfo($voucher->customer) !!}</td>
                        <td>{{ getPriceFormat($voucher->amount) }}</td>
                        <td>{{ getFormatDate($voucher->payment_date) }}</td>
                        <td>{{ getLocalizedName($voucher->paymentMethod) }}</td>
                        <td>{{ $voucher->register->name ?? '' }}</td>
                        <td>
                            @if($voucher->subscription_id)
                                <span class="badge badge-soft-primary">{{ __('Related') }}</span>
                            @else
                                <span class="text-muted">{{ __('Not Related') }}</span>
                            @endif
                        </td>

                        <td>
                           <span class="badge badge-soft-{{ config('data-select.receipt-vouchers-status')[$voucher->status]['color'] ?? 'primary' }}">
                               {{ getLocalizedName(config('data-select.receipt-vouchers-status')[$voucher->status]) }}
                           </span>
                        </td>
                    </tr>
                @endforeach
            @endif
        </x-data-table>
    </x-card>

@endsection

@section('scripts')
<script>
    $(document).ready(() => {
        let inputs = {
            users: $(".users-select"),
            paymentMethods: $(".payment-methods-select"),
        };

        lazySelect2Ajax(inputs.users, `{{ route('ajax.get-users') }}`, false, `{{ $request['user_id'] ?? '' }}` || null)
        lazySelect2Ajax(inputs.paymentMethods, `{{ route('ajax.get-payment-methods') }}`, false, `{{ $request['payment_method_id'] ?? '' }}` || null)
    });
</script>
@endsection
