@extends('layouts.app')

@push('head')
<style>
    .leave-card {
        position: relative;
        display: flex;
        flex-direction: column;
        gap: 28px;
        padding: 34px 36px 32px;
        border-radius: 28px;
        background: linear-gradient(150deg, rgba(13, 22, 48, 0.95), rgba(8, 16, 36, 0.9));
        border: 1px solid rgba(148, 163, 184, 0.22);
        box-shadow: 0 28px 58px rgba(3, 7, 18, 0.6);
        overflow: hidden;
    }
    .leave-card::before,
    .leave-card::after {
        content: "";
        position: absolute;
        inset: 0;
        pointer-events: none;
    }
    .leave-card::before {
        background:
            radial-gradient(circle at 15% 20%, rgba(56, 189, 248, 0.28), transparent 55%),
            radial-gradient(circle at 88% -8%, rgba(129, 140, 248, 0.32), transparent 60%);
        opacity: 0.85;
    }
    .leave-card::after {
        background: linear-gradient(135deg, transparent 40%, rgba(79, 70, 229, 0.18));
        opacity: 0.75;
    }
    .leave-card > * { position: relative; z-index: 1; }

    .leave-hero {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        flex-wrap: wrap;
        gap: 24px;
    }
    .leave-hero__info {
        display: flex;
        flex-direction: column;
        gap: 12px;
        max-width: 560px;
    }
    .leave-hero__tagline {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 6px 14px;
        border-radius: 999px;
        background: rgba(129, 140, 248, 0.18);
        border: 1px solid rgba(129, 140, 248, 0.4);
        color: #e0e7ff;
        font-size: 12px;
        letter-spacing: 0.1em;
        text-transform: uppercase;
        font-weight: 600;
    }
    .leave-hero h2 {
        margin: 0;
        font-size: 30px;
        font-weight: 700;
        letter-spacing: -0.02em;
        color: #f8fafc;
    }
    .leave-hero p {
        margin: 0;
        color: rgba(226, 232, 240, 0.75);
        font-size: 15px;
        line-height: 1.6;
    }
    .leave-hero__actions {
        display: flex;
        flex-wrap: wrap;
        gap: 12px;
    }
    .leave-hero__button {
        display: inline-flex;
        align-items: center;
        gap: 10px;
        padding: 10px 20px;
        border-radius: 14px;
        background: linear-gradient(135deg, rgba(14, 165, 233, 0.28), rgba(99, 102, 241, 0.4));
        border: 1px solid rgba(129, 140, 248, 0.5);
        color: #e0f2fe;
        font-weight: 600;
        letter-spacing: 0.05em;
        text-transform: uppercase;
        text-decoration: none;
        box-shadow: 0 22px 32px rgba(6, 15, 38, 0.52);
        transition: transform 0.2s ease, border-color 0.2s ease, background 0.2s ease;
    }
    .leave-hero__button:hover {
        transform: translateY(-2px);
        border-color: rgba(165, 180, 252, 0.72);
        background: linear-gradient(135deg, rgba(56, 189, 248, 0.34), rgba(99, 102, 241, 0.48));
    }

    .leave-search {
        display: flex;
        align-items: center;
        gap: 14px;
        padding: 12px 18px;
        border-radius: 18px;
        border: 1px solid rgba(148, 163, 184, 0.24);
        background: rgba(7, 16, 36, 0.82);
        box-shadow: inset 0 1px 0 rgba(148, 163, 184, 0.12);
    }
    .leave-search svg {
        width: 18px;
        height: 18px;
        color: rgba(148, 163, 184, 0.75);
    }
    .leave-search input {
        flex: 1 1 auto;
        background: transparent;
        border: none;
        color: #f1f5f9;
        font-size: 13px;
        outline: none;
    }
    .leave-search input::placeholder {
        color: rgba(148, 163, 184, 0.7);
    }
    .leave-search button {
        padding: 8px 18px;
        border-radius: 12px;
        border: none;
        background: linear-gradient(135deg, #38bdf8, #2563eb);
        color: #f8fafc;
        font-weight: 600;
        letter-spacing: 0.04em;
        cursor: pointer;
        box-shadow: 0 14px 28px rgba(37, 99, 235, 0.28);
        transition: transform 0.18s ease, box-shadow 0.18s ease;
    }
    .leave-search button:hover {
        transform: translateY(-1px);
        box-shadow: 0 18px 32px rgba(37, 99, 235, 0.32);
    }

    .leave-summary {
        display: flex;
        flex-direction: column;
        gap: 16px;
    }
    .leave-summary__subtitle {
        margin: 0;
        font-size: 13px;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: rgba(148, 163, 184, 0.72);
    }
    .leave-summary__grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        gap: 16px;
    }
    .leave-summary__card {
        position: relative;
        display: flex;
        flex-direction: column;
        gap: 8px;
        padding: 18px 20px;
        border-radius: 20px;
        background: linear-gradient(145deg, rgba(12, 21, 44, 0.92), rgba(22, 36, 70, 0.84));
        border: 1px solid rgba(148, 163, 184, 0.24);
        box-shadow: 0 18px 36px rgba(5, 12, 28, 0.5);
        overflow: hidden;
    }
    .leave-summary__card::after {
        content: "";
        position: absolute;
        inset: 0;
        background: radial-gradient(circle at 80% 18%, rgba(59, 130, 246, 0.24), transparent 52%);
        opacity: 0.7;
        pointer-events: none;
    }
    .leave-summary__label {
        font-size: 12px;
        letter-spacing: 0.12em;
        text-transform: uppercase;
        color: rgba(203, 213, 225, 0.78);
        font-weight: 600;
    }
    .leave-summary__value {
        font-size: 30px;
        font-weight: 700;
        color: #f8fafc;
        letter-spacing: -0.02em;
    }
    .leave-summary__meta {
        font-size: 12px;
        color: rgba(226, 232, 240, 0.68);
    }

    .leave-alert {
        padding: 12px 14px;
        border-radius: 12px;
        font-size: 13px;
        display: flex;
        gap: 12px;
        align-items: flex-start;
    }
    .leave-alert__message { margin: 0; }

    .leave-table-shell {
        border-radius: 24px;
        border: 1px solid rgba(148, 163, 184, 0.2);
        background: rgba(7, 16, 34, 0.84);
        box-shadow: 0 24px 44px rgba(4, 10, 26, 0.55);
        overflow: hidden;
    }
    .leave-table-scroll {
        width: 100%;
        overflow-x: auto;
    }
    .leave-table {
        width: 100%;
        min-width: 920px;
        border-collapse: separate;
        border-spacing: 0;
    }
    .leave-table thead th {
        position: sticky;
        top: 0;
        background: rgba(10, 19, 40, 0.95);
        color: #cbd5f5;
        padding: 14px 20px;
        font-weight: 600;
        font-size: 13px;
        letter-spacing: 0.04em;
        text-transform: uppercase;
        border-bottom: 1px solid rgba(148, 163, 184, 0.22);
    }
    .leave-table tbody td {
        padding: 16px 20px;
        color: #e2e8f0;
        font-size: 13px;
        border-bottom: 1px solid rgba(148, 163, 184, 0.1);
        vertical-align: top;
    }
    .leave-table tbody tr:nth-child(even) {
        background: rgba(10, 23, 55, 0.3);
    }
    .leave-table tbody tr:hover {
        background: rgba(17, 30, 60, 0.52);
    }

    .leave-text {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .leave-status {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 6px 14px;
        border-radius: 999px;
        font-weight: 600;
        font-size: 12px;
        letter-spacing: 0.05em;
        text-transform: uppercase;
        border: 1px solid transparent;
        white-space: nowrap;
    }
    .leave-status.wait {
        background: rgba(148, 163, 184, 0.18);
        color: #cbd5f5;
        border-color: rgba(148, 163, 184, 0.3);
    }
    .leave-status.ok {
        background: rgba(34, 197, 94, 0.2);
        color: #bbf7d0;
        border-color: rgba(34, 197, 94, 0.34);
    }
    .leave-status.no {
        background: rgba(248, 113, 113, 0.2);
        color: #fecaca;
        border-color: rgba(248, 113, 113, 0.34);
    }

    .leave-actions {
        display: inline-flex;
        gap: 10px;
    }
    .leave-actions form {
        margin: 0;
    }
    .leave-actions button {
        padding: 9px 18px;
        border-radius: 12px;
        border: none;
        font-weight: 700;
        letter-spacing: 0.05em;
        text-transform: uppercase;
        cursor: pointer;
        transition: transform 0.18s ease, box-shadow 0.18s ease;
    }
    .leave-actions .approve {
        background: linear-gradient(135deg, #22c55e, #16a34a);
        color: #f7fee7;
        box-shadow: 0 14px 26px rgba(34, 197, 94, 0.32);
    }
    .leave-actions .reject {
        background: linear-gradient(135deg, #ef4444, #b91c1c);
        color: #fee2e2;
        box-shadow: 0 14px 26px rgba(239, 68, 68, 0.3);
    }
    .leave-actions button:hover {
        transform: translateY(-1px);
    }

    .leave-empty {
        padding: 24px;
        text-align: center;
        color: rgba(148, 163, 184, 0.72);
        font-size: 14px;
    }

    .leave-footer {
        display: flex;
        flex-wrap: wrap;
        gap: 16px;
        align-items: center;
        justify-content: space-between;
    }
    .leave-footer__info {
        font-size: 12px;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: rgba(148, 163, 184, 0.72);
    }
    .leave-footer nav > * {
        display: inline-flex;
        align-items: center;
        padding: 6px 12px;
        border-radius: 10px;
        background: rgba(15, 23, 42, 0.8);
        color: #cbd5f5;
        border: 1px solid rgba(148, 163, 184, 0.2);
        box-shadow: 0 12px 24px rgba(7, 16, 36, 0.35);
    }

    @media (max-width: 1280px) {
        .leave-card {
            padding: 30px 28px;
        }
        .leave-table {
            min-width: 860px;
        }
    }
    @media (max-width: 960px) {
        .leave-card {
            padding: 28px 24px;
        }
        .leave-hero {
            flex-direction: column;
        }
        .leave-hero__actions {
            width: 100%;
            justify-content: flex-start;
        }
    }
    @media (max-width: 768px) {
        .leave-card {
            padding: 24px 20px;
        }
        .leave-summary__grid {
            grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
        }
        .leave-table {
            min-width: 720px;
        }
        .leave-actions {
            flex-direction: column;
        }
        .leave-actions button {
            width: 100%;
        }
    }
    @media (max-width: 620px) {
        .leave-card {
            padding: 22px 18px;
            border-radius: 22px;
        }
        .leave-hero__button,
        .leave-search {
            width: 100%;
        }
        .leave-summary__grid {
            grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        }
        .leave-footer {
            flex-direction: column;
            align-items: flex-start;
        }
    }
</style>
@endpush

@section('content')
<div class="leave-card">
    @php
        $totalPengajuan = $items->total();
        $totalMenunggu = $items->filter(fn ($row) => strtolower($row->status) === 'menunggu')->count();
        $totalDisetujui = $items->filter(fn ($row) => strtolower($row->status) === 'disetujui')->count();
        $totalDitolak = $items->filter(fn ($row) => strtolower($row->status) === 'ditolak')->count();
    @endphp

    <div class="leave-hero">
        <div class="leave-hero__info">
            <span class="leave-hero__tagline">Manajemen Cuti</span>
            <h2>Persetujuan Cuti</h2>
            <p>Gunakan panel ini untuk memonitor pengajuan cuti dan memberikan keputusan dengan cepat serta terstruktur.</p>
        </div>
        <div class="leave-hero__actions">
            <a href="{{ route('admin.cuti.index') }}" class="leave-hero__button">Segarkan Data</a>
        </div>
    </div>

    <form method="GET" action="{{ route('admin.cuti.index') }}" class="leave-search">
        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="m21 21-4.35-4.35m0 0A7.5 7.5 0 1 0 5.25 5.25a7.5 7.5 0 0 0 11.4 10.9Z" /></svg>
        <input type="text" name="q" value="{{ $q ?? '' }}" placeholder="Cari nama, tanggal, jenis, atau status cuti">
        <button type="submit">Cari</button>
    </form>

    <div class="leave-summary">
        <p class="leave-summary__subtitle">Ringkasan Pengajuan</p>
        <div class="leave-summary__grid">
            <div class="leave-summary__card">
                <span class="leave-summary__label">Total Pengajuan</span>
                <span class="leave-summary__value">{{ number_format($totalPengajuan) }}</span>
                <span class="leave-summary__meta">Keseluruhan permintaan masuk</span>
            </div>
            <div class="leave-summary__card">
                <span class="leave-summary__label">Menunggu</span>
                <span class="leave-summary__value">{{ number_format($totalMenunggu) }}</span>
                <span class="leave-summary__meta">Menanti keputusan admin</span>
            </div>
            <div class="leave-summary__card">
                <span class="leave-summary__label">Disetujui</span>
                <span class="leave-summary__value">{{ number_format($totalDisetujui) }}</span>
                <span class="leave-summary__meta">Cuti telah dikonfirmasi</span>
            </div>
            <div class="leave-summary__card">
                <span class="leave-summary__label">Ditolak</span>
                <span class="leave-summary__value">{{ number_format($totalDitolak) }}</span>
                <span class="leave-summary__meta">Permintaan tidak disetujui</span>
            </div>
        </div>
    </div>

    @if (session('success'))
        <div class="leave-alert" style="background: rgba(34,197,94,0.14); border:1px solid rgba(34,197,94,0.38); color:#bbf7d0;">
            <p class="leave-alert__message">{{ session('success') }}</p>
        </div>
    @endif

    @if ($errors->any())
        <div class="leave-alert" style="background: rgba(248,113,113,0.14); border:1px solid rgba(248,113,113,0.4); color:#fecaca; flex-direction: column; align-items:flex-start;">
            <p class="leave-alert__message" style="font-weight:600;">Periksa kembali input Anda:</p>
            <ul style="margin:0 0 0 18px; padding:0;">
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <div class="leave-table-shell">
        <div class="leave-table-scroll">
            <table class="leave-table">
                <thead>
                    <tr>
                        <th>Pemohon</th>
                        <th>Tanggal</th>
                        <th>Durasi</th>
                        <th>Jenis</th>
                        <th>Alasan</th>
                        <th>Status</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($items as $row)
                        <tr>
                            @php
                                $mulai = \Carbon\Carbon::parse($row->tanggal_mulai);
                                $selesai = \Carbon\Carbon::parse($row->tanggal_selesai);
                                $durasi = $mulai->diffInDays($selesai) + 1;
                            @endphp
                            <td data-label="Pemohon" class="leave-text" title="{{ $row->user_name }}">
                                {{ $row->user_name }}
                                @if($row->username)
                                    <span style="color:#94a3b8;">(@ {{ $row->username }})</span>
                                @endif
                            </td>
                            <td data-label="Tanggal">{{ $mulai->format('d/m/Y') }} - {{ $selesai->format('d/m/Y') }}</td>
                            <td data-label="Durasi">{{ $durasi }} hari</td>
                            <td data-label="Jenis">{{ $row->jenis }}</td>
                            <td data-label="Alasan" class="leave-text" title="{{ $row->alasan }}">{{ $row->alasan }}</td>
                            <td data-label="Status">
                                @php $status = strtolower($row->status); @endphp
                                @if($status === 'menunggu')
                                    <span class="leave-status wait">Menunggu</span>
                                @elseif($status === 'disetujui')
                                    <span class="leave-status ok">Disetujui</span>
                                @else
                                    <span class="leave-status no">Ditolak</span>
                                @endif
                            </td>
                            <td data-label="Aksi">
                                <div class="leave-actions">
                                    <form method="POST" action="{{ route('admin.cuti.update', $row->id) }}">
                                        @csrf
                                        <input type="hidden" name="action" value="setujui" />
                                        <button type="submit" class="approve">Setujui</button>
                                    </form>
                                    <form method="POST" action="{{ route('admin.cuti.update', $row->id) }}">
                                        @csrf
                                        <input type="hidden" name="action" value="tolak" />
                                        <button type="submit" class="reject">Tolak</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="leave-empty">Belum ada pengajuan.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <div class="leave-footer">
        <span class="leave-footer__info">Terakhir diperbarui {{ now()->timezone(config('app.timezone'))->format('d M Y H:i') }}</span>
        <nav>
            {{ $items->links() }}
        </nav>
    </div>
</div>
@endsection
