@extends('layouts.admin')

@section('title', 'GST Reports')

@section('content')
<x-admin.report-page title="GST Reports" subtitle="View GST reports for successfully placed and paid orders">
    @if($year && $month && $orders && $orders->count() > 0)
        <x-slot:actions>
            <a href="{{ route('admin.reports.gst.export-csv', ['year' => $year, 'month' => $month, 'order_type' => $orderType ?? 'all', 'location_id' => request('location_id', ($locationId ?? null) === null ? 'all' : $locationId)]) }}" class="ci-btn ci-btn--primary" id="exportCsvBtn">
                <i class="bi bi-download"></i> Download CSV
            </a>
        </x-slot:actions>
    @endif

    <form method="GET" id="filterForm" class="ci-filters ci-filters--report">
        @include('admin.reports.partials.location-filter', ['ci' => true])
        <div class="ci-filters__field">
            <label for="year">Year</label>
            <select name="year" id="year" required>
                <option value="">Select Year</option>
                @foreach($years as $y)
                    <option value="{{ $y }}" {{ $year == $y ? 'selected' : '' }}>{{ $y }}</option>
                @endforeach
            </select>
        </div>
        <div class="ci-filters__field">
            <label for="month">Month</label>
            <select name="month" id="month" required>
                <option value="">Select Month</option>
                @foreach($months as $key => $monthName)
                    <option value="{{ $key }}" {{ $month == $key ? 'selected' : '' }}>{{ $monthName }}</option>
                @endforeach
            </select>
        </div>
        <div class="ci-filters__field">
            <label for="order_type">Order Type</label>
            <select name="order_type" id="order_type">
                <option value="all" {{ ($orderType ?? 'all') === 'all' ? 'selected' : '' }}>All</option>
                <option value="pos" {{ ($orderType ?? 'all') === 'pos' ? 'selected' : '' }}>POS</option>
                <option value="online" {{ ($orderType ?? 'all') === 'online' ? 'selected' : '' }}>Online</option>
                <option value="pos_to_online" {{ ($orderType ?? 'all') === 'pos_to_online' ? 'selected' : '' }}>POS to Online</option>
            </select>
        </div>
        <div class="ci-filters__actions">
            <button type="submit" class="ci-btn ci-btn--primary">
                <i class="bi bi-search"></i> Show
            </button>
            <a href="{{ route('admin.reports.gst') }}" class="ci-btn ci-btn--ghost ci-btn--icon" title="Reset">
                <i class="bi bi-arrow-clockwise"></i>
            </a>
        </div>
    </form>

    @if($year && $month && $orders)
        @if($orders->count() > 0)
            @php
                // Calculate totals for current page
                $pageTotalPayable = $orders->sum('total_payable');
                $pageTaxTotal = $orders->sum('tax_total');
            @endphp
            <div class="ci-table-card">
                <div class="ci-section__head" style="border-bottom:1px solid var(--ci-border);">
                    <h5 class="mb-0">GST Report for {{ $months[$month] }} {{ $year }}</h5>
                    <span class="ci-pill ci-pill--info">{{ $orders->total() }} Orders</span>
                </div>
                <div class="ci-table-wrap">
                    <table class="ci-table">
                        <thead>
                            <tr>
                                <th>Order ID</th>
                                <th>Order Type</th>
                                <th>Customer Name</th>
                                <th>Phone Number</th>
                                <th>GST Number</th>
                                <th class="ci-end">Order Value</th>
                                <th class="ci-end">Tax Value</th>
                                <th>Pincode</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($orders as $order)
                                @php
                                    $addressSnapshot = $order->address_snapshot ?? [];
                                    $pincode = $addressSnapshot['pin'] ?? 'N/A';
                                    $orderTypeLabel = match($order->source) {
                                        'pos' => 'POS',
                                        'online' => 'Online',
                                        'pos_to_online' => 'POS to Online',
                                        default => ucfirst(str_replace('_', ' ', (string) $order->source)),
                                    };
                                @endphp
                                <tr>
                                    <td>
                                        <a href="{{ route('admin.orders.show', $order->id) }}" class="ci-link">
                                            #{{ $order->id }}
                                        </a>
                                    </td>
                                    <td>
                                        <span class="ci-pill ci-pill--info">{{ $orderTypeLabel }}</span>
                                    </td>
                                    <td>{{ $order->user ? $order->user->name : 'N/A' }}</td>
                                    <td>{{ $order->user ? $order->user->mobile : 'N/A' }}</td>
                                    <td>{{ $order->user ? ($order->user->gst_number ?? 'N/A') : 'N/A' }}</td>
                                    <td class="ci-end"><span class="ci-money">₹{{ number_format($order->total_payable, 2) }}</span></td>
                                    <td class="ci-end"><span class="ci-money">₹{{ number_format($order->tax_total, 2) }}</span></td>
                                    <td class="ci-num">{{ $pincode }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                        <tfoot>
                            <tr>
                                <th colspan="5">Page Total ({{ $orders->count() }} of {{ $orders->total() }} orders)</th>
                                <th class="ci-end"><span class="ci-money">₹{{ number_format($pageTotalPayable, 2) }}</span></th>
                                <th class="ci-end"><span class="ci-money">₹{{ number_format($pageTaxTotal, 2) }}</span></th>
                                <th></th>
                            </tr>
                        </tfoot>
                    </table>
                </div>

                <div class="ci-pagination">
                    <div class="ci-pagination__info">
                        Showing <strong>{{ $orders->firstItem() ?? 0 }}</strong>–<strong>{{ $orders->lastItem() ?? 0 }}</strong>
                        of <strong>{{ $orders->total() }}</strong> orders
                    </div>
                    @if($orders->hasPages())
                        <div>
                            {{ $orders->links('vendor.pagination.admin-categories') }}
                        </div>
                    @endif
                </div>
            </div>
        @else
            <div class="ci-table-card">
                <div class="ci-empty">
                    <i class="bi bi-inbox"></i>
                    <h3>No Orders Found</h3>
                    <p>No orders found for {{ $months[$month] }} {{ $year }}. Please select a different year and month.</p>
                </div>
            </div>
        @endif
    @elseif($year && $month && !$orders)
        <div class="ci-table-card">
            <div class="ci-empty">
                <i class="bi bi-inbox"></i>
                <h3>No Orders Found</h3>
                <p>No orders found for {{ $months[$month] }} {{ $year }}. Please select a different year and month.</p>
            </div>
        </div>
    @else
        <div class="ci-table-card">
            <div class="ci-empty">
                <i class="bi bi-calendar-check"></i>
                <h3>Select Period</h3>
                <p>Please select Year and Month to view GST reports</p>
            </div>
        </div>
    @endif
</x-admin.report-page>
@endsection
