<x-app-layout title="Kelola Izin & Sakit">
@push('styles')
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
<style>
:root{
    --primary:#4f46e5;--primary-d:#3730a3;--primary-l:#eef2ff;--primary-m:rgba(79,70,229,.12);
    --success:#059669;--success-l:#ecfdf5;--danger:#dc2626;--danger-l:#fef2f2;
    --warning:#d97706;--warning-l:#fffbeb;
    --text:#0f172a;--muted:#64748b;--muted2:#94a3b8;
    --border:#e2e8f0;--bg:#f0f4ff;--white:#ffffff;
    --sh:0 1px 3px rgba(0,0,0,.06),0 4px 16px rgba(0,0,0,.05);
}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0;}
body{font-family:'Plus Jakarta Sans',sans-serif;background:var(--bg);color:var(--text);}
.wrap{max-width:1100px;margin:0 auto;padding:28px 28px 60px;}

/* header */
.pg-hd{display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:14px;margin-bottom:22px;}
.pg-title{font-size:1.35rem;font-weight:900;color:var(--text);letter-spacing:-.03em;}
.pg-sub{font-size:12.5px;color:var(--muted);font-weight:500;margin-top:3px;}
.btn{display:inline-flex;align-items:center;gap:7px;padding:8px 16px;border-radius:10px;font-size:13px;font-weight:700;font-family:inherit;cursor:pointer;text-decoration:none;border:none;transition:all .15s;white-space:nowrap;}
.btn svg{width:13px;height:13px;stroke:currentColor;}
.btn-back{background:var(--white);color:var(--muted);border:1.5px solid var(--border);}
.btn-back:hover{background:var(--primary-l);color:var(--primary);border-color:rgba(79,70,229,.3);}

/* kpi */
.kpi-row{display:grid;grid-template-columns:repeat(3,1fr);gap:14px;margin-bottom:20px;}
.kpi-card{background:var(--white);border:1px solid var(--border);border-radius:18px;padding:16px 20px;box-shadow:var(--sh);display:flex;align-items:center;gap:14px;position:relative;overflow:hidden;}
.kpi-card::before{content:'';position:absolute;top:0;left:0;right:0;height:3px;border-radius:18px 18px 0 0;}
.kpi-card.kp::before{background:linear-gradient(90deg,var(--warning),#fbbf24);}
.kpi-card.ks::before{background:linear-gradient(90deg,var(--success),#34d399);}
.kpi-card.kr::before{background:linear-gradient(90deg,var(--danger),#f87171);}
.kpi-ico{width:44px;height:44px;border-radius:12px;display:flex;align-items:center;justify-content:center;flex-shrink:0;}
.kpi-ico svg{width:20px;height:20px;}
.kpi-ico.ip{background:#fef3c7;} .kpi-ico.ip svg{stroke:var(--warning);}
.kpi-ico.is{background:#d1fae5;} .kpi-ico.is svg{stroke:var(--success);}
.kpi-ico.ir{background:#fee2e2;} .kpi-ico.ir svg{stroke:var(--danger);}
.kpi-num{font-size:1.8rem;font-weight:900;letter-spacing:-.04em;line-height:1;}
.kpi-lbl{font-size:11.5px;font-weight:600;color:var(--muted);margin-top:2px;}
.kpi-num.cp{color:var(--warning);}.kpi-num.cs{color:var(--success);}.kpi-num.cr{color:var(--danger);}

/* filter */
.filter-panel{background:var(--white);border:1px solid var(--border);border-radius:16px;padding:14px 20px;margin-bottom:18px;box-shadow:var(--sh);}
.filter-row{display:flex;gap:10px;flex-wrap:wrap;align-items:flex-end;}
.filter-group{display:flex;flex-direction:column;gap:5px;flex: 1; min-width: 150px;}
.filter-lbl{font-size:10.5px;font-weight:800;color:var(--muted2);text-transform:uppercase;letter-spacing:.07em;}
.filter-input{height:38px;padding:0 12px;border:1.5px solid var(--border);border-radius:9px;font-size:13px;font-weight:600;color:var(--text);font-family:inherit;background:var(--white);outline:none;transition:border-color .15s;width: 100%;}
.filter-input:focus{border-color:var(--primary);}
.filter-btn{height:38px;padding:0 16px;border-radius:9px;font-size:13px;font-weight:700;font-family:inherit;cursor:pointer;border:none;transition:all .15s;display:inline-flex;align-items:center;justify-content:center;gap:6px;}
.filter-btn.primary{background:var(--primary);color:#fff;}
.filter-btn.reset{background:#f1f5f9;color:var(--muted);border:1.5px solid var(--border);text-decoration:none;}

/* panel */
.panel{background:var(--white);border:1px solid var(--border);border-radius:20px;overflow:hidden;box-shadow:var(--sh);}
.panel-hd{padding:15px 20px;border-bottom:1px solid #f1f5f9;display:flex;align-items:center;justify-content:space-between;gap:10px;}
.panel-hd-title{font-size:13.5px;font-weight:800;color:var(--text);}
.panel-hd-badge{font-size:11px;font-weight:700;background:#f1f5f9;color:var(--muted);border-radius:100px;padding:3px 10px;}

/* table */
.tbl{width:100%;border-collapse:collapse;font-size:13px;}
.tbl thead tr{background:#fafbff;}
.tbl thead th{padding:10px 16px;text-align:left;font-size:10px;font-weight:800;letter-spacing:.08em;text-transform:uppercase;color:var(--muted2);border-bottom:1px solid #f1f5f9;white-space:nowrap;}
.tbl tbody tr{border-bottom:1px solid #f8f9fc;transition:background .12s;}
.tbl td{padding:12px 16px;vertical-align:middle;}

/* components */
.kar-cell{display:flex;align-items:center;gap:9px;}
.kar-av{width:36px;height:36px;border-radius:10px;display:flex;align-items:center;justify-content:center;font-size:12px;font-weight:800;color:#fff;flex-shrink:0;}
.kar-name{font-size:13px;font-weight:700;color:var(--text);}
.kar-jab{font-size:11px;color:var(--muted2);font-weight:500;margin-top:1px;}

.jenis-badge{display:inline-flex;align-items:center;gap:5px;padding:3px 10px;border-radius:100px;font-size:11.5px;font-weight:700;white-space:nowrap;}
.jenis-izin{background:#fffbeb;color:#d97706;border:1px solid #fde68a;}
.jenis-sakit{background:#eff6ff;color:#2563eb;border:1px solid #bfdbfe;}

.status-badge{display:inline-flex;align-items:center;gap:5px;padding:4px 11px;border-radius:100px;font-size:11.5px;font-weight:700;white-space:nowrap;}
.status-badge::before{content:'';width:6px;height:6px;border-radius:50%;flex-shrink:0;}
.sb-pending{background:var(--warning-l);color:var(--warning);border:1px solid #fde68a;} .sb-pending::before{background:var(--warning);}
.sb-disetujui{background:var(--success-l);color:var(--success);border:1px solid #a7f3d0;} .sb-disetujui::before{background:var(--success);}
.sb-ditolak{background:var(--danger-l);color:var(--danger);border:1px solid #fecaca;} .sb-ditolak::before{background:var(--danger);}

.act-cell{display:flex;gap:5px;align-items:center;}
.act-btn{height:30px;padding:0 12px;border-radius:8px;font-size:11.5px;font-weight:700;font-family:inherit;cursor:pointer;border:none;transition:all .15s;display:inline-flex;align-items:center;gap:5px;white-space:nowrap;}
.act-btn svg{width:11px;height:11px;stroke:currentColor;}
.act-approve{background:#dcfce7;color:#065f46;} .act-approve:hover{background:#bbf7d0;}
.act-reject{background:#fee2e2;color:#991b1b;} .act-reject:hover{background:#fecaca;}
.act-done{font-size:11.5px;font-weight:600;color:var(--muted2);}

/* Responsive */
@media (max-width: 768px) {
    .wrap { padding: 16px 16px 80px; }
    .pg-hd { flex-direction: column; align-items: flex-start; }
    .btn-back { width: 100%; justify-content: center; }
    
    .kpi-row { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .kpi-card { padding: 12px; gap: 10px; }
    .kpi-num { font-size: 1.4rem; }

    .filter-row { flex-direction: column; align-items: stretch; }
    .filter-group { min-width: 100%; }

    /* Stacking Table */
    .tbl thead { display: none; }
    .tbl, .tbl tbody, .tbl tr, .tbl td { display: block; width: 100%; }
    .tbl tr { margin-bottom: 12px; border: 1px solid var(--border); border-radius: 12px; padding: 8px 0; background: #fff; }
    .tbl td { text-align: right; padding: 10px 16px; border-bottom: 1px solid #f1f5f9; display: flex; justify-content: space-between; align-items: center; }
    .tbl td:last-child { border-bottom: none; }
    .tbl td::before { content: attr(data-label); font-weight: 800; text-transform: uppercase; font-size: 10px; color: var(--muted2); }
    
    .kar-cell { text-align: left; }
    .act-cell { justify-content: flex-end; width: 100%; }
}

@media (max-width: 480px) {
    .kpi-row { grid-template-columns: 1fr; }
}

/* Pagination & Toast (Tetap) */
.pagi-wrap{padding:14px 20px;border-top:1px solid #f1f5f9;display:flex;align-items:center;justify-content:space-between;gap:12px;flex-wrap:wrap;}
.pagi-info{font-size:12px;font-weight:600;color:var(--muted);}
.pagi-links{display:flex;gap:4px;}
.pagi-links a,.pagi-links span{display:inline-flex;align-items:center;justify-content:center;min-width:32px;height:32px;padding:0 8px;border-radius:8px;font-size:12.5px;font-weight:700;text-decoration:none;border:1.5px solid transparent;transition:all .15s;}
.pagi-links a{color:var(--muted);border-color:var(--border);}
.pagi-links a:hover{background:var(--primary-l);border-color:rgba(79,70,229,.3);color:var(--primary);}
.pagi-links span.active{background:var(--primary);color:#fff;border-color:var(--primary);}
.pagi-links span.disabled{color:#cbd5e1;border-color:#f1f5f9;}

.toast-wrap{position:fixed;bottom:24px;right:24px;z-index:9999;display:flex;flex-direction:column;gap:8px;}
.toast{padding:12px 18px;border-radius:12px;font-size:13px;font-weight:600;color:white;display:flex;align-items:center;gap:8px;box-shadow:0 8px 24px rgba(0,0,0,.18);animation:toastIn .3s ease;max-width:300px;}
.toast svg{width:14px;height:14px;stroke:currentColor;flex-shrink:0;}
.toast.tok{background:#064e3b;border-left:3px solid #34d399;}
.toast.terr{background:#4c0f0f;border-left:3px solid #f87171;}
@keyframes toastIn{from{opacity:0;transform:translateY(12px)}to{opacity:1;transform:translateY(0)}}
@keyframes fadeUp{from{opacity:0;transform:translateY(12px)}to{opacity:1;transform:translateY(0)}}
.fu1{animation:fadeUp .35s ease both;}
.fu2{animation:fadeUp .35s .06s ease both;}
.fu3{animation:fadeUp .35s .12s ease both;}
</style>
@endpush

<div class="wrap">
    {{-- HEADER --}}
    <div class="pg-hd fu1">
        <div>
            <div class="pg-title">Kelola Izin & Sakit</div>
            <div class="pg-sub">Setujui atau tolak pengajuan izin karyawan</div>
        </div>
        <a href="{{ route('dashboard') }}" class="btn btn-back">
            <svg viewBox="0 0 24 24" fill="none" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"/></svg>
            Dashboard
        </a>
    </div>

    {{-- KPI --}}
    <div class="kpi-row fu2">
        <div class="kpi-card kp">
            <div class="kpi-ico ip"><svg viewBox="0 0 24 24" fill="none" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg></div>
            <div><div class="kpi-num cp">{{ $statPending }}</div><div class="kpi-lbl">Menunggu</div></div>
        </div>
        <div class="kpi-card ks">
            <div class="kpi-ico is"><svg viewBox="0 0 24 24" fill="none" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg></div>
            <div><div class="kpi-num cs">{{ $statDisetujui }}</div><div class="kpi-lbl">Disetujui</div></div>
        </div>
        <div class="kpi-card kr">
            <div class="kpi-ico ir"><svg viewBox="0 0 24 24" fill="none" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg></div>
            <div><div class="kpi-num cr">{{ $statDitolak }}</div><div class="kpi-lbl">Ditolak</div></div>
        </div>
    </div>

    {{-- FILTER --}}
    <div class="filter-panel fu2">
        <form method="GET" action="{{ route('instansi.izin.index') }}">
            <div class="filter-row">
                <div class="filter-group">
                    <span class="filter-lbl">Status</span>
                    <select name="status" class="filter-input">
                        <option value="">Semua Status</option>
                        <option value="pending" @selected($status==='pending')>Pending</option>
                        <option value="disetujui" @selected($status==='disetujui')>Disetujui</option>
                        <option value="ditolak" @selected($status==='ditolak')>Ditolak</option>
                    </select>
                </div>
                <div class="filter-group">
                    <span class="filter-lbl">Karyawan</span>
                    <select name="karyawan_id" class="filter-input">
                        <option value="">Semua Karyawan</option>
                        @foreach($karyawanList as $kar)
                        <option value="{{ $kar->id }}" @selected($karyawanId == $kar->id)>{{ $kar->user->name ?? $kar->nama }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="filter-group" style="flex-direction:row;gap:8px;">
                    <button type="submit" class="filter-btn primary" style="flex:1;">Filter</button>
                    <a href="{{ route('instansi.izin.index') }}" class="filter-btn reset" style="flex:1;">Reset</a>
                </div>
            </div>
        </form>
    </div>

    {{-- TABLE --}}
    <div class="panel fu3">
        <div class="panel-hd">
            <span class="panel-hd-title">Daftar Pengajuan</span>
            <span class="panel-hd-badge">{{ $izinList->total() }} data</span>
        </div>
        <table class="tbl">
            <thead>
                <tr>
                    <th>Karyawan</th>
                    <th>Jenis</th>
                    <th>Tanggal</th>
                    <th>Bukti</th>
                    <th>Status</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                @forelse($izinList as $i => $iz)
                @php
                    $nama = $iz->karyawan->user->name ?? $iz->karyawan->nama ?? '-';
                    $inisial = strtoupper(substr($nama, 0, 1));
                    $colors = ['#6d28d9','#0284c7','#059669','#d97706','#dc2626','#475569'];
                    $color = $colors[ord($inisial) % count($colors)];
                @endphp
                <tr id="row-{{ $iz->id }}">
                    <td data-label="Karyawan">
                        <div class="kar-cell">
                            <div class="kar-av" style="background:{{ $color }};">{{ $inisial }}</div>
                            <div style="text-align: left">
                                <div class="kar-name">{{ $nama }}</div>
                                <div class="kar-jab">{{ $iz->karyawan->jabatan->nama_jabatan ?? '-' }}</div>
                            </div>
                        </div>
                    </td>
                    <td data-label="Jenis">
                        <span class="jenis-badge {{ $iz->jenis === 'izin' ? 'jenis-izin' : 'jenis-sakit' }}">
                            {{ $iz->jenis === 'izin' ? '📋 Izin' : '🏥 Sakit' }}
                        </span>
                    </td>
                    <td data-label="Tanggal">
                        <div style="text-align: right; font-size:12px; font-weight:600;">
                            {{ \Carbon\Carbon::parse($iz->tanggal_mulai)->format('d M Y') }}
                            @if($iz->tanggal_mulai != $iz->tanggal_selesai)
                                <br><span style="color:var(--muted2); font-weight:400;">s/d {{ \Carbon\Carbon::parse($iz->tanggal_selesai)->format('d M Y') }}</span>
                            @endif
                        </div>
                    </td>
                    <td data-label="Bukti">
                        @if($iz->bukti_file)
                            <a href="{{ asset('storage/'.$iz->bukti_file) }}" target="_blank" class="act-btn act-approve" style="padding:4px 10px;">Lihat Bukti</a>
                        @else <span class="kar-jab">Tidak ada</span> @endif
                    </td>
                    <td data-label="Status">
                        <span class="status-badge sb-{{ $iz->status }}">{{ ucfirst($iz->status) }}</span>
                    </td>
                    <td data-label="Aksi">
                        <div class="act-cell">
                            @if($iz->status === 'pending')
                                <button class="act-btn act-approve" onclick="doAction({{ $iz->id }}, 'approve')">Setuju</button>
                                <button class="act-btn act-reject" onclick="doAction({{ $iz->id }}, 'reject')">Tolak</button>
                            @else
                                <span class="act-done">{{ $iz->status === 'disetujui' ? '✓ Disetujui' : '✗ Ditolak' }}</span>
                            @endif
                        </div>
                    </td>
                </tr>
                @empty
                <tr><td colspan="6" style="text-align:center; padding:40px; color:var(--muted2);">Belum ada data pengajuan.</td></tr>
                @endforelse
            </tbody>
        </table>

        @if($izinList->hasPages())
        <div class="pagi-wrap">
            <span class="pagi-info">Hal {{ $izinList->currentPage() }} dari {{ $izinList->lastPage() }}</span>
            <div class="pagi-links">
                @if(!$izinList->onFirstPage()) <a href="{{ $izinList->previousPageUrl() }}">‹</a> @endif
                @if($izinList->hasMorePages()) <a href="{{ $izinList->nextPageUrl() }}">›</a> @endif
            </div>
        </div>
        @endif
    </div>
</div>

<div class="toast-wrap" id="toastWrap"></div>

@push('scripts')
<script>
async function doAction(id, action) {
    const url = `/instansi/izin/${id}/${action}`;
    try {
        const res = await fetch(url, {
            method: 'POST',
            headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content, 'Accept': 'application/json' }
        });
        const data = await res.json();
        if (data.success) {
            const row = document.getElementById(`row-${id}`);
            if (row) {
                row.querySelector('.status-badge').className = `status-badge sb-${action==='approve'?'disetujui':'ditolak'}`;
                row.querySelector('.status-badge').textContent = action==='approve'?'Disetujui':'Ditolak';
                row.querySelector('.act-cell').innerHTML = `<span class="act-done">${action==='approve'?'✓ Disetujui':'✗ Ditolak'}</span>`;
            }
            // Update KPI
            const pen = document.querySelector('.kpi-num.cp');
            if (pen) pen.textContent = Math.max(0, parseInt(pen.textContent) - 1);
            const target = document.querySelector('.kpi-num.' + (action==='approve'?'cs':'cr'));
            if (target) target.textContent = parseInt(target.textContent) + 1;

            showToast('tok', data.message);
        } else { showToast('terr', data.message); }
    } catch { showToast('terr', 'Gagal terhubung ke server'); }
}

function showToast(type, msg) {
    const wrap = document.getElementById('toastWrap');
    const t = document.createElement('div');
    t.className = `toast ${type}`;
    t.innerHTML = `<span>${msg}</span>`;
    wrap.appendChild(t);
    setTimeout(() => { t.style.opacity='0'; setTimeout(()=>t.remove(),300); }, 3000);
}
</script>
@endpush
</x-app-layout>