@extends('layouts.app')

@section('page-title', 'Invoices')

@push('styles')
<style>
    /* =============================================
       CSS CUSTOM PROPERTIES
    ============================================= */
    :root {
        --radius-xl: 20px;
        --radius-lg: 16px;
        --radius-md: 12px;
        --radius-sm: 8px;
        --shadow-sm: 0 2px 12px rgba(0,0,0,0.06);
        --shadow-md: 0 8px 32px rgba(0,0,0,0.10);
        --shadow-hover: 0 16px 48px rgba(0,0,0,0.13);
        --transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
        --color-primary:   #667eea;
        --color-paid:      #11998e;
        --color-pending:   #f59e0b;
        --color-partial:   #06b6d4;
        --color-cancelled: #eb3349;
    }

    /* =============================================
       PAGE WRAPPER
    ============================================= */
    .invoices-page { padding: 4px 0 28px; }

    /* =============================================
       PAGE HEADER
    ============================================= */
    .page-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: wrap;
        gap: 12px;
        margin-bottom: 20px;
    }

    .page-header h2 {
        font-size: clamp(1.2rem, 3vw, 1.65rem);
        font-weight: 700;
        color: #1a1a2e;
        margin: 0;
    }

    /* =============================================
       BUTTONS
    ============================================= */
    .btn {
        border-radius: var(--radius-md);
        padding: 9px 18px;
        font-weight: 600;
        font-size: 0.88rem;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 7px;
        border: none;
        text-decoration: none;
        transition: var(--transition);
        cursor: pointer;
        white-space: nowrap;
    }

    .btn:hover { transform: translateY(-2px); }

    .btn-primary {
        background: linear-gradient(135deg, #667eea, #764ba2);
        color: #fff;
        box-shadow: 0 4px 12px rgba(102,126,234,0.35);
    }

    .btn-primary:hover { color: #fff; }

    .btn-secondary {
        background: #f1f5f9;
        color: #475569;
        box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    }

    .btn-secondary:hover { background: #e2e8f0; color: #334155; }

    .btn-info {
        background: linear-gradient(135deg, #06b6d4, #4facfe);
        color: #fff;
        box-shadow: 0 3px 10px rgba(6,182,212,0.3);
    }

    .btn-info:hover { color: #fff; }

    .btn-sm {
        padding: 6px 11px;
        font-size: 0.8rem;
        border-radius: var(--radius-sm);
    }

    .btn-w-full { width: 100%; }

    /* =============================================
       FILTER CARD
    ============================================= */
    .filter-card {
        background: #fff;
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow-sm);
        border: none;
        margin-bottom: 20px;
        overflow: hidden;
    }

    .filter-card .card-body { padding: 18px 20px; }

    .filter-grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr) auto;
        gap: 12px;
        align-items: end;
    }

    .form-group { display: flex; flex-direction: column; gap: 6px; }

    .form-label {
        font-size: 0.78rem;
        font-weight: 700;
        color: #475569;
        text-transform: uppercase;
        letter-spacing: 0.4px;
        margin: 0;
    }

    .form-control,
    .form-select {
        border-radius: var(--radius-md);
        border: 2px solid #e2e8f0;
        padding: 9px 13px;
        font-size: 0.88rem;
        font-weight: 500;
        color: #334155;
        background: #fff;
        transition: var(--transition);
        width: 100%;
        appearance: none;
        -webkit-appearance: none;
    }

    .form-control:focus,
    .form-select:focus {
        border-color: var(--color-primary);
        box-shadow: 0 0 0 4px rgba(102,126,234,0.15);
        outline: none;
    }

    .form-select {
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2364748b' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
        background-repeat: no-repeat;
        background-position: right 12px center;
        padding-right: 32px;
    }

    .filter-actions-row { display: contents; }

    /* =============================================
       TABLE CARD
    ============================================= */
    .table-card {
        background: #fff;
        border-radius: var(--radius-xl);
        box-shadow: var(--shadow-sm);
        border: none;
        overflow: hidden;
    }

    .table-card .card-body { padding: 0; }

    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    table { width: 100%; border-collapse: collapse; }

    thead th {
        background: #f8fafc;
        padding: 13px 16px;
        font-size: 0.73rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        color: #64748b;
        border-bottom: 2px solid #e2e8f0;
        white-space: nowrap;
    }

    tbody td {
        padding: 13px 16px;
        font-size: 0.88rem;
        font-weight: 500;
        color: #334155;
        border-bottom: 1px solid #f1f5f9;
        vertical-align: middle;
    }

    tbody tr { transition: var(--transition); }
    tbody tr:hover { background: rgba(102,126,234,0.04); }
    tbody tr:last-child td { border-bottom: none; }

    /* =============================================
       INVOICE NUMBER
    ============================================= */
    .invoice-num {
        font-family: monospace;
        font-size: 0.85rem;
        font-weight: 700;
        background: #f1f5f9;
        color: var(--color-primary);
        padding: 3px 9px;
        border-radius: var(--radius-sm);
        white-space: nowrap;
    }

    /* =============================================
       CUSTOMER NAME
    ============================================= */
    .customer-name {
        font-weight: 600;
        color: #1a1a2e;
        max-width: 140px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display: block;
    }

    /* =============================================
       AMOUNT CELL
    ============================================= */
    .amount-cell {
        font-weight: 700;
        color: #334155;
        white-space: nowrap;
    }

    /* =============================================
       DUE DATE — overdue highlight
    ============================================= */
    .due-overdue { color: var(--color-cancelled); font-weight: 600; }
    .due-soon    { color: var(--color-pending);   font-weight: 600; }
    .due-normal  { color: #64748b; }

    /* =============================================
       STATUS BADGES
    ============================================= */
    .badge {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        padding: 4px 11px;
        border-radius: 20px;
        font-size: 0.73rem;
        font-weight: 700;
        white-space: nowrap;
    }

    .badge-paid      { background: rgba(17,153,142,0.12); color: var(--color-paid); }
    .badge-pending   { background: rgba(245,158,11,0.15); color: #92400e; }
    .badge-partial   { background: rgba(6,182,212,0.12);  color: #0e7490; }
    .badge-cancelled { background: rgba(235,51,73,0.12);  color: var(--color-cancelled); }

    /* =============================================
       ACTION BUTTONS
    ============================================= */
    .action-group {
        display: flex;
        gap: 6px;
        align-items: center;
        flex-wrap: nowrap;
    }

    /* =============================================
       EMPTY STATE
    ============================================= */
    .empty-state {
        text-align: center;
        padding: 56px 20px;
        color: #94a3b8;
    }

    .empty-state i { display: block; margin-bottom: 14px; opacity: 0.4; }
    .empty-state p { font-size: 0.9rem; margin-bottom: 18px; }

    /* =============================================
       PAGINATION
    ============================================= */
    .pagination-wrapper {
        padding: 16px 20px;
        border-top: 1px solid #f1f5f9;
        display: flex;
        justify-content: center;
        flex-wrap: wrap;
        gap: 6px;
    }

    /* =============================================
       MOBILE CARD LIST  (hidden on ≥ 576px)
    ============================================= */
    .inv-card-list { display: none; padding: 12px; }

    .inv-card {
        background: #fafafa;
        border: 1px solid #f1f5f9;
        border-radius: var(--radius-md);
        padding: 14px 16px;
        margin-bottom: 10px;
        transition: var(--transition);
        position: relative;
        overflow: hidden;
    }

    /* left accent bar coloured by status */
    .inv-card::before {
        content: '';
        position: absolute;
        top: 0; left: 0;
        width: 4px;
        height: 100%;
        border-radius: 4px 0 0 4px;
    }

    .inv-card.status-paid::before      { background: var(--color-paid); }
    .inv-card.status-pending::before   { background: var(--color-pending); }
    .inv-card.status-partial::before   { background: var(--color-partial); }
    .inv-card.status-cancelled::before { background: var(--color-cancelled); }

    .inv-card:hover { background: #f1f5f9; }

    /* top row */
    .inv-card-top {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 10px;
        margin-bottom: 10px;
        padding-left: 8px;
    }

    .inv-card-left { min-width: 0; flex: 1; }

    .inv-card-num {
        font-family: monospace;
        font-size: 0.78rem;
        font-weight: 700;
        color: var(--color-primary);
        background: rgba(102,126,234,0.1);
        padding: 2px 8px;
        border-radius: 6px;
        display: inline-block;
        margin-bottom: 4px;
    }

    .inv-card-customer {
        font-size: 0.92rem;
        font-weight: 700;
        color: #1a1a2e;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .inv-card-dates {
        font-size: 0.75rem;
        color: #94a3b8;
        margin-top: 3px;
    }

    .inv-card-amount {
        font-size: 1.1rem;
        font-weight: 800;
        color: #1a1a2e;
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* bottom row */
    .inv-card-bottom {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        flex-wrap: wrap;
        padding-left: 8px;
    }

    .inv-card-actions {
        display: flex;
        gap: 6px;
    }

    /* =============================================
       ============================================
       RESPONSIVE BREAKPOINTS
       ============================================
    ============================================= */

    /* ---- EXTRA SMALL < 480px ---- */
    @media (max-width: 479px) {
        .invoices-page { padding: 0 0 16px; }

        .page-header { flex-direction: column; align-items: stretch; }
        .page-header h2 { font-size: 1.2rem; }
        .page-header .btn { width: 100%; }

        .filter-card .card-body { padding: 14px; }
        .filter-grid { grid-template-columns: 1fr; gap: 10px; }
        .filter-actions-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
        }

        /* Hide table, show cards */
        .table-responsive { display: none; }
        .inv-card-list { display: block; }

        .pagination-wrapper { padding: 12px; }
    }

    /* ---- SMALL 480–575px ---- */
    @media (min-width: 480px) and (max-width: 575px) {
        .page-header { flex-direction: column; align-items: stretch; }
        .page-header .btn { width: 100%; }

        .filter-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
        .filter-actions-row {
            grid-column: 1 / -1;
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
        }

        /* Hide table, show cards */
        .table-responsive { display: none; }
        .inv-card-list { display: block; }
    }

    /* ---- MEDIUM 576–767px ---- */
    @media (min-width: 576px) and (max-width: 767px) {
        .filter-grid { grid-template-columns: 1fr 1fr; gap: 12px; }
        .filter-actions-row {
            grid-column: 1 / -1;
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 12px;
        }

        thead th, tbody td { padding: 11px 10px; font-size: 0.8rem; }

        /* Hide due date col on narrow table */
        .col-due-date { display: none; }

        .customer-name { max-width: 100px; }
    }

    /* ---- TABLET 768–991px ---- */
    @media (min-width: 768px) and (max-width: 991px) {
        .filter-grid { grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
        .filter-actions-row {
            grid-column: 1 / -1;
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 12px;
        }

        .customer-name { max-width: 120px; }
    }

    /* ---- DESKTOP 992px+ ---- */
    @media (min-width: 992px) {
        .filter-grid { grid-template-columns: repeat(3, 1fr) auto; }
        .filter-actions-row { display: contents; }
        .col-due-date { display: table-cell; }
    }

    /* ---- WIDE 1400px+ ---- */
    @media (min-width: 1400px) {
        .customer-name { max-width: 200px; }
    }

    /* =============================================
       TOUCH OPTIMISATIONS
    ============================================= */
    @media (hover: none) and (pointer: coarse) {
        .btn:hover       { transform: none; }
        tbody tr:hover   { background: transparent; }
        .inv-card:hover  { background: #fafafa; }
        .btn { padding: 11px 18px; }
        .btn-sm { padding: 9px 13px; }
        thead th, tbody td { padding: 14px 12px; }
    }

    /* =============================================
       ACCESSIBILITY
    ============================================= */
    @media (prefers-reduced-motion: reduce) {
        *, *::before, *::after {
            transition-duration: 0.01ms !important;
            animation-duration: 0.01ms !important;
        }
    }

    :focus-visible {
        outline: 3px solid #667eea;
        outline-offset: 2px;
        border-radius: 4px;
    }

    /* =============================================
       LANDSCAPE PHONES
    ============================================= */
    @media (max-height: 500px) and (orientation: landscape) {
        .filter-card .card-body { padding: 12px 14px; }
        .inv-card { padding: 11px 14px; }
    }

    /* =============================================
       PRINT
    ============================================= */
    @media print {
        .page-header .btn,
        .filter-card { display: none !important; }

        .inv-card-list { display: none !important; }
        .table-responsive { display: block !important; overflow: visible !important; }
        .table-card { box-shadow: none !important; border: 1px solid #ccc !important; }
        .col-due-date { display: table-cell !important; }
        thead th, tbody td { padding: 7px 10px !important; font-size: 0.78rem !important; }
    }
</style>
@endpush

@section('content')
<div class="invoices-page">

    {{-- ========================================
         PAGE HEADER
    ======================================== --}}
    <div class="page-header">
        <h2>Invoices</h2>
        <a href="{{ route('invoices.create') }}" class="btn btn-primary">
            <i class="fas fa-plus"></i> Create Invoice
        </a>
    </div>

    {{-- ========================================
         FILTERS
    ======================================== --}}
    <div class="filter-card">
        <div class="card-body">
            <form method="GET" action="{{ route('invoices.index') }}">
                <div class="filter-grid">

                    <div class="form-group">
                        <label for="from_date" class="form-label">From Date</label>
                        <input type="date" class="form-control" id="from_date" name="from_date"
                               value="{{ request('from_date') }}">
                    </div>

                    <div class="form-group">
                        <label for="to_date" class="form-label">To Date</label>
                        <input type="date" class="form-control" id="to_date" name="to_date"
                               value="{{ request('to_date') }}">
                    </div>

                    <div class="form-group">
                        <label for="status" class="form-label">Status</label>
                        <select class="form-select" id="status" name="status">
                            <option value="">All</option>
                            <option value="pending"   {{ request('status') == 'pending'   ? 'selected' : '' }}>Pending</option>
                            <option value="paid"      {{ request('status') == 'paid'      ? 'selected' : '' }}>Paid</option>
                            <option value="partial"   {{ request('status') == 'partial'   ? 'selected' : '' }}>Partial</option>
                            <option value="cancelled" {{ request('status') == 'cancelled' ? 'selected' : '' }}>Cancelled</option>
                        </select>
                    </div>

                    <div class="filter-actions-row">
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary btn-w-full">
                                <i class="fas fa-filter"></i> Filter
                            </button>
                        </div>
                        <div class="form-group">
                            <a href="{{ route('invoices.index') }}" class="btn btn-secondary btn-w-full">
                                <i class="fas fa-redo"></i> Reset
                            </a>
                        </div>
                    </div>

                </div>
            </form>
        </div>
    </div>

    {{-- ========================================
         TABLE CARD
    ======================================== --}}
    <div class="table-card">
        <div class="card-body">

            {{-- Desktop / Tablet Table --}}
            <div class="table-responsive">
                <table>
                    <thead>
                        <tr>
                            <th>Invoice #</th>
                            <th>Customer</th>
                            <th>Date</th>
                            <th class="col-due-date">Due Date</th>
                            <th>Amount</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($invoices as $invoice)
                            @php
                                $isOverdue = $invoice->due_date
                                    && $invoice->due_date->isPast()
                                    && !in_array($invoice->status, ['paid', 'cancelled']);
                                $isSoon = $invoice->due_date
                                    && !$isOverdue
                                    && $invoice->due_date->diffInDays(now()) <= 3
                                    && !in_array($invoice->status, ['paid', 'cancelled']);
                            @endphp
                            <tr>
                                <td>
                                    <span class="invoice-num">#{{ $invoice->id }}</span>
                                </td>
                                <td>
                                    <span class="customer-name" title="{{ $invoice->customer->name ?? '' }}">
                                        {{ $invoice->customer->name ?? '—' }}
                                    </span>
                                </td>
                                <td style="white-space:nowrap;">
                                    {{ $invoice->date->format('M d, Y') }}
                                </td>
                                <td class="col-due-date" style="white-space:nowrap;">
                                    @if($invoice->due_date)
                                        <span class="{{ $isOverdue ? 'due-overdue' : ($isSoon ? 'due-soon' : 'due-normal') }}">
                                            @if($isOverdue)<i class="fas fa-exclamation-circle" style="font-size:0.7rem;"></i> @endif
                                            {{ $invoice->due_date->format('M d, Y') }}
                                        </span>
                                    @else
                                        <span style="color:#94a3b8;">—</span>
                                    @endif
                                </td>
                                <td class="amount-cell">
                                    ৳{{ number_format($invoice->total, 2) }}
                                </td>
                                <td>
                                    @switch($invoice->status)
                                        @case('paid')
                                            <span class="badge badge-paid">
                                                <i class="fas fa-check" style="font-size:0.6rem;"></i> Paid
                                            </span>
                                            @break
                                        @case('pending')
                                            <span class="badge badge-pending">
                                                <i class="fas fa-clock" style="font-size:0.6rem;"></i> Pending
                                            </span>
                                            @break
                                        @case('partial')
                                            <span class="badge badge-partial">
                                                <i class="fas fa-adjust" style="font-size:0.6rem;"></i> Partial
                                            </span>
                                            @break
                                        @case('cancelled')
                                            <span class="badge badge-cancelled">
                                                <i class="fas fa-times" style="font-size:0.6rem;"></i> Cancelled
                                            </span>
                                            @break
                                    @endswitch
                                </td>
                                <td>
                                    <div class="action-group">
                                        <a href="{{ route('invoices.show', $invoice->id) }}"
                                           class="btn btn-sm btn-info"
                                           title="View invoice">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{{ route('invoices.print', $invoice->id) }}"
                                           class="btn btn-sm btn-secondary"
                                           target="_blank"
                                           title="Print invoice">
                                            <i class="fas fa-print"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7">
                                    <div class="empty-state">
                                        <i class="fas fa-file-invoice fa-3x"></i>
                                        <p>No invoices found.</p>
                                        <a href="{{ route('invoices.create') }}" class="btn btn-primary btn-sm">
                                            + Create Invoice
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            {{-- Mobile Card List (< 576px) --}}
            <div class="inv-card-list">
                @forelse($invoices as $invoice)
                    @php
                        $isOverdue = $invoice->due_date
                            && $invoice->due_date->isPast()
                            && !in_array($invoice->status, ['paid', 'cancelled']);
                    @endphp
                    <div class="inv-card status-{{ $invoice->status }}">
                        <div class="inv-card-top">
                            <div class="inv-card-left">
                                <span class="inv-card-num">#{{ $invoice->id }}</span>
                                <div class="inv-card-customer">
                                    {{ $invoice->customer->name ?? '—' }}
                                </div>
                                <div class="inv-card-dates">
                                    Issued: {{ $invoice->date->format('M d, Y') }}
                                    @if($invoice->due_date)
                                        &nbsp;·&nbsp;
                                        <span class="{{ $isOverdue ? 'due-overdue' : '' }}">
                                            @if($isOverdue)<i class="fas fa-exclamation-circle" style="font-size:0.65rem;"></i> @endif
                                            Due: {{ $invoice->due_date->format('M d, Y') }}
                                        </span>
                                    @endif
                                </div>
                            </div>
                            <div class="inv-card-amount">
                                ৳{{ number_format($invoice->total, 2) }}
                            </div>
                        </div>
                        <div class="inv-card-bottom">
                            <div>
                                @switch($invoice->status)
                                    @case('paid')
                                        <span class="badge badge-paid">
                                            <i class="fas fa-check" style="font-size:0.6rem;"></i> Paid
                                        </span>
                                        @break
                                    @case('pending')
                                        <span class="badge badge-pending">
                                            <i class="fas fa-clock" style="font-size:0.6rem;"></i> Pending
                                        </span>
                                        @break
                                    @case('partial')
                                        <span class="badge badge-partial">
                                            <i class="fas fa-adjust" style="font-size:0.6rem;"></i> Partial
                                        </span>
                                        @break
                                    @case('cancelled')
                                        <span class="badge badge-cancelled">
                                            <i class="fas fa-times" style="font-size:0.6rem;"></i> Cancelled
                                        </span>
                                        @break
                                @endswitch
                            </div>
                            <div class="inv-card-actions">
                                <a href="{{ route('invoices.show', $invoice->id) }}"
                                   class="btn btn-sm btn-info">
                                    <i class="fas fa-eye"></i> View
                                </a>
                                <a href="{{ route('invoices.print', $invoice->id) }}"
                                   class="btn btn-sm btn-secondary"
                                   target="_blank">
                                    <i class="fas fa-print"></i>
                                </a>
                            </div>
                        </div>
                    </div>
                @empty
                    <div class="empty-state">
                        <i class="fas fa-file-invoice fa-2x"></i>
                        <p>No invoices found.</p>
                        <a href="{{ route('invoices.create') }}" class="btn btn-primary btn-sm">
                            + Create Invoice
                        </a>
                    </div>
                @endforelse
            </div>

            {{-- Pagination --}}
            @if($invoices->hasPages())
                <div class="pagination-wrapper">
                    {{ $invoices->links() }}
                </div>
            @endif

        </div>
    </div>

</div>
@endsection
