@extends('layouts.admin')

@section('title', 'GST Reports')

@section('content')
<div class="admin-page-header d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1>GST Reports</h1>
        <p class="admin-page-subtitle">View GST reports for successfully placed and paid orders</p>
    </div>
    @if($year && $month && $orders && $orders->count() > 0)
    <a href="{{ route('admin.reports.gst.export-csv', ['year' => $year, 'month' => $month, 'order_type' => $orderType ?? 'all']) }}" class="admin-btn-primary" id="exportCsvBtn" style="background: #10b981;">
        <i class="bi bi-download"></i> Download CSV
    </a>
    @endif
</div>

<!-- Filters -->
<div class="admin-card mb-4">
    <div class="card-body">
        <form method="GET" id="filterForm" class="row g-3">
            <div class="col-md-3">
                <label class="admin-form-label">Year</label>
                <select name="year" id="year" class="admin-form-control form-select" required>
                    <option value="">Select Year</option>
                    @foreach($years as $y)
                        <option value="{{ $y }}" {{ $year == $y ? 'selected' : '' }}>{{ $y }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-3">
                <label class="admin-form-label">Month</label>
                <select name="month" id="month" class="admin-form-control form-select" required>
                    <option value="">Select Month</option>
                    @foreach($months as $key => $monthName)
                        <option value="{{ $key }}" {{ $month == $key ? 'selected' : '' }}>{{ $monthName }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-3">
                <label class="admin-form-label">Order Type</label>
                <select name="order_type" id="order_type" class="admin-form-control form-select">
                    <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="col-md-3 d-flex align-items-end gap-2">
                <button type="submit" class="admin-btn-primary flex-grow-1">
                    <i class="bi bi-search"></i> Show
                </button>
                <a href="{{ route('admin.reports.gst') }}" class="btn btn-secondary">
                    <i class="bi bi-arrow-clockwise"></i> Reset
                </a>
            </div>
        </form>
    </div>
</div>

<!-- Results Table -->
@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="admin-card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">GST Report for {{ $months[$month] }} {{ $year }}</h5>
                <span class="badge-modern badge-modern-primary">{{ $orders->total() }} Orders</span>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="admin-table table table-hover">
                        <thead>
                            <tr>
                                <th>Order ID</th>
                                <th>Order Type</th>
                                <th>Customer Name</th>
                                <th>Phone Number</th>
                                <th>GST Number</th>
                                <th>Order Value</th>
                                <th>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="text-primary">
                                            #{{ $order->id }}
                                        </a>
                                    </td>
                                    <td>{{ $orderTypeLabel }}</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>₹{{ number_format($order->total_payable, 2) }}</td>
                                    <td>₹{{ number_format($order->tax_total, 2) }}</td>
                                    <td>{{ $pincode }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                        <tfoot>
                            <tr class="table-active">
                                <th colspan="5">Page Total ({{ $orders->count() }} of {{ $orders->total() }} orders)</th>
                                <th>₹{{ number_format($pageTotalPayable, 2) }}</th>
                                <th>₹{{ number_format($pageTaxTotal, 2) }}</th>
                                <th></th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
                
                <!-- Pagination -->
                <div class="pagination-wrapper mt-4">
                    <div class="d-flex justify-content-between align-items-center gap-3 flex-wrap">
                        <div class="pagination-info-text">
                            Showing <strong>{{ $orders->firstItem() ?? 0 }}</strong>–<strong>{{ $orders->lastItem() ?? 0 }}</strong> 
                            of <strong>{{ $orders->total() }}</strong> orders
                        </div>
                        @if($orders->hasPages())
                        <div class="pagination-controls">
                            {{ $orders->links('vendor.pagination.admin-categories') }}
                        </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    @else
        <div class="admin-empty-state">
            <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>
    @endif
@elseif($year && $month && !$orders)
    <div class="admin-empty-state">
        <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>
@else
    <div class="admin-empty-state">
        <i class="bi bi-calendar-check"></i>
        <h3>Select Period</h3>
        <p>Please select Year and Month to view GST reports</p>
    </div>
@endif
@endsection

