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

@section('content')

    <x-page-header
        :title="__('Home')"
        :breadcrumbs="[
            ['name' => __('Dashboards')],
        ]"
    />

    <!-- Main Stats Cards -->
    <x-row>
        <x-col size="4" class="col-lg-3 mb-2">
            <x-stat-card :title="__('Total Subscriptions')" :value="\App\Models\Subscription::count() ?? 0" icon="bx bx-layer" bg="bg-primary" />
        </x-col>
        <x-col size="4" class="col-lg-3 mb-2">
            <x-stat-card :title="__('Active Subscriptions')" :value="$stats->active ?? 0" icon="bx bx-check-circle" bg="bg-success" />
        </x-col>
        <x-col size="4" class="col-lg-3 mb-2">
            <x-stat-card :title="__('Expiring in Month')" :value="$stats->month_expiring ?? 0" icon="bx bx-calendar" bg="bg-warning" />
        </x-col>
        <x-col size="4" class="col-lg-3 mb-2">
            <x-stat-card :title="__('Expiring Soon (7 days)')" :value="$expiringSoonCount ?? 0" icon="bx bx-time-five" bg="bg-warning" />
        </x-col>
        <x-col size="4" class="col-lg-3 mb-2">
            <x-stat-card :title="__('Expired Subscriptions')" :value="$expiredNow ?? 0" icon="bx bx-x-circle" bg="bg-secondary" />
        </x-col>
        <x-col size="4" class="col-lg-3 mb-2">
            <x-stat-card :title="__('canceled Subscriptions')" :value="$canceled ?? 0" icon="bx bx-block" bg="bg-danger" />
        </x-col>
        <x-col size="4" class="col-lg-3 mb-2">
            <x-stat-card :title="__('Total Transferred Subscriptions')" :value="$transfers ?? 0" icon="bx bx-transfer" bg="bg-info" />
        </x-col>
        <x-col size="4" class="col-lg-3 mb-2">
            <x-stat-card :title="__('Total Renewals')" :value="$renewals ?? 0" icon="bx bx-repeat" bg="bg-primary" />
        </x-col>
    </x-row>

    @php
        $growth = null;

        if ($lastMonth > 0) {
            $growth = (($currentMonth - $lastMonth) / $lastMonth) * 100;
        }
     @endphp

    <!-- Additional Stats Cards -->
    <x-row>
        <x-col size="12" class="mb-2">
            <x-stat-card title="{{ __('Current Month') }}" :value="$currentMonth ?? 0" icon="bx bx-calendar" bg="bg-info-subtle text-info">
                <small class="text-muted">{{ __('New subscriptions') }}</small>

                @if(!is_null($growth))
                    <div class="d-flex align-items-center mt-2">
                        @php
                            $badgeClass = $growth >= 0 ? 'badge-soft-success' : 'badge-soft-danger';
                        @endphp

                        <span class="badge {{ $badgeClass }} font-size-12">
                        {{ number_format($growth, 1) }}%
                    </span>

                        <span class="ms-2 text-truncate">
                        vs last month
                    </span>

                        <i class="fas {{ $growth >= 0 ? 'fa-arrow-up text-success' : 'fa-arrow-down text-danger' }} ms-2"></i>
                    </div>
                @endif
            </x-stat-card>
        </x-col>
    </x-row>

    <!-- Top Lists -->
    <div class="row">
        <x-col size="12" class="mb-2">
            <x-card class="card-2" cardLabel="fs-5">
                <x-slot:label>{{ __('Top Customers') }}</x-slot:label>

                <x-data-table
                    id="top-customers-table"
                    :checkbox="false"
                    :columns="[
                            ['label' => 'ID', 'width' => '40px'],
                            ['label' => __('Customer')],
                            ['label' => __('Gender')],
                            ['label' => __('Nationality')],
                            ['label' => __('National ID')],
                            ['label' => __('Mobile')],
                            ['label' => __('Total Subscriptions')],
                        ]"
                >
                    @forelse($topCustomers as $customer)
                        <tr>
                            <td style="width: 50px;">{{ $customer->customer_id }}</td>
                            <td>{{ $customer->customer?->name .' ('. $customer->customer?->code . ')' }}</td>
                            <td>{{ getLocalizedName(config('data-select.gender')[$customer->customer->gender]) }}</td>
                            <td>{{ getLocalizedName($customer->customer->nationality) }}</td>
                            <td>{{ $customer->customer->national_id ?? '' }}</td>
                            <td>{!! getMobileFormat($customer->customer) !!}</td>
                            <td><span class="fw-bold text-primary ms-1">{{ (int) $customer->total }}</span> {{ __('subscription') }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center text-muted">{{ __("No data available") }}</td>
                        </tr>
                    @endforelse
                </x-data-table>
            </x-card>
        </x-col>

        <x-col size="12" class="mb-2">
            <x-card class="card-2" cardLabel="fs-5">
                <x-slot:label>{{ __('Top Selling Plans') }}</x-slot:label>

                <x-data-table
                    id="top-customers-table"
                    :checkbox="false"
                    :columns="[
                            ['label' => 'ID', 'width' => '40px'],
                            ['label' => __('Plan')],
                            ['label' => __('Type')],
                            ['label' => __('Price')],
                            ['label' => __('Duration Days')],
                            ['label' => __('Sessions Count')],
                            ['label' => __('Total Subscriptions')],
                        ]"
                >
                    @forelse($topPlans as $plan)
                        <tr>
                            <td style="width: 50px;">{{ $plan->plan_id }}</td>
                            <td>{{ getLocalizedName($plan->plan) }}</td>
                            <td>{{ getLocalizedName(config('data-select.plans-types')[$plan->plan->type]) }}</td>
                            <td>{{ getPriceFormat($plan->plan->price) }}</td>
                            <td>{{ getPriceFormat($plan->plan->duration_days) }}</td>
                            <td>{{ getPriceFormat($plan->plan->sessions_count) }}</td>
                            <td><span class="fw-bold text-primary ms-1">{{ (int) $plan->total }}</span> {{ __('subscription') }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="3" class="text-center text-muted">{{ __("No data available") }}</td>
                        </tr>
                    @endforelse
                </x-data-table>
            </x-card>
        </x-col>

        <x-col size="12" class="mb-2">
            <x-card class="card-2" cardLabel="fs-5">
                <x-slot:label>{{ __('Subscriptions Expiring in (7 days)') }}</x-slot:label>

                <x-data-table
                    id="top-customers-table"
                    :checkbox="false"
                    :columns="[
                            ['label' => 'ID', 'width' => '40px'],
                            ['label' => __('Code')],
                            ['label' => __('Customer')],
                            ['label' => __('plan')],
                            ['label' => __('register')],
                            ['label' => __('end_date')],
                            ['label' => __('status')],
                        ]"
                >
                    @forelse($expiringSoon as $sup)
                        <tr>
                            <td style="width: 50px;">{{ $sup->id }}</td>
                            <td style="width: 50px;">{{ $sup->code }}</td>
                            <td style="width: 50px;">{{ $sup->customer?->name }}</td>
                            <td style="width: 50px;">{{ getLocalizedName($sup->plan) }}</td>
                            <td style="width: 50px;">{{ $sup->register?->name }}</td>
                            <td style="width: 50px;">{{ getFormatDate($sup->end_date) }}</td>
                            <td style="width: 50px;">{!! subscriptionStatus($sup) !!}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="3" class="text-center text-muted">{{ __("No data available") }}</td>
                        </tr>
                    @endforelse
                </x-data-table>
            </x-card>
        </x-col>

    </div>

@endsection

