@extends('layouts.app')

@push('head')
<style>
    .damage-shell {
        position: relative;
        display: flex;
        flex-direction: column;
        gap: 28px;
        padding: 36px 36px 32px;
        border-radius: 30px;
        background: linear-gradient(150deg, rgba(12, 24, 52, 0.95), rgba(6, 14, 34, 0.88));
        border: 1px solid rgba(148, 163, 184, 0.22);
        box-shadow: 0 32px 58px rgba(3, 7, 18, 0.6);
        overflow: hidden;
    }
    .damage-shell::before,
    .damage-shell::after {
        content: "";
        position: absolute;
        inset: 0;
        pointer-events: none;
    }
    .damage-shell::before {
        background:
            radial-gradient(circle at 14% 18%, rgba(59, 130, 246, 0.28), transparent 52%),
            radial-gradient(circle at 88% -8%, rgba(14, 165, 233, 0.32), transparent 60%);
        opacity: 0.85;
    }
    .damage-shell::after {
        background: linear-gradient(135deg, transparent 40%, rgba(56, 189, 248, 0.16));
        opacity: 0.7;
    }
    .damage-shell > * { position: relative; z-index: 1; }

    .damage-hero {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 24px;
        flex-wrap: wrap;
    }
    .damage-hero__info {
        display: flex;
        flex-direction: column;
        gap: 12px;
        max-width: 560px;
    }
    .damage-hero__tagline {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 6px 14px;
        border-radius: 999px;
        background: rgba(129, 140, 248, 0.2);
        border: 1px solid rgba(129, 140, 248, 0.4);
        color: #e0e7ff;
        font-size: 12px;
        letter-spacing: 0.1em;
        text-transform: uppercase;
        font-weight: 600;
    }
    .damage-hero h2 {
        margin: 0;
        font-size: 30px;
        font-weight: 700;
        letter-spacing: -0.02em;
        color: #f8fafc;
    }
    .damage-hero p {
        margin: 0;
        color: rgba(226, 232, 240, 0.75);
        font-size: 15px;
        line-height: 1.6;
    }
    .damage-hero__meta {
        display: flex;
        flex-direction: column;
        gap: 10px;
        align-items: flex-end;
        min-width: 220px;
    }
    .damage-hero__chip {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 8px 14px;
        border-radius: 12px;
        background: rgba(37, 99, 235, 0.18);
        border: 1px solid rgba(37, 99, 235, 0.38);
        color: #bfdbfe;
        font-size: 12px;
        letter-spacing: 0.06em;
        text-transform: uppercase;
        font-weight: 600;
    }
    .damage-hero__meta-text {
        color: rgba(148, 163, 184, 0.78);
        font-size: 12px;
    }

    .damage-alert {
        display: flex;
        flex-direction: column;
        gap: 6px;
        padding: 14px 16px;
        border-radius: 16px;
        font-size: 13px;
        border: 1px solid transparent;
        background: rgba(15, 23, 42, 0.72);
    }
    .damage-alert--success {
        background: rgba(34, 197, 94, 0.14);
        border-color: rgba(34, 197, 94, 0.38);
        color: #bbf7d0;
    }
    .damage-alert--error {
        background: rgba(248, 113, 113, 0.16);
        border-color: rgba(248, 113, 113, 0.4);
        color: #fecaca;
    }
    .damage-alert__title {
        margin: 0;
        font-weight: 700;
        letter-spacing: 0.06em;
        text-transform: uppercase;
    }
    .damage-alert__list {
        margin: 0 0 0 18px;
        padding: 0;
    }

    .damage-form {
        display: flex;
        flex-direction: column;
        gap: 24px;
    }
    .damage-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
        gap: 18px 22px;
        align-items: stretch;
    }
    .damage-field {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }
    .damage-label {
        font-size: 13px;
        font-weight: 600;
        color: #dbeafe;
        letter-spacing: 0.02em;
    }
    .damage-input,
    .damage-textarea {
        background: rgba(2, 6, 23, 0.9);
        border: 1px solid rgba(148, 163, 184, 0.28);
        border-radius: 14px;
        padding: 11px 14px;
        color: #e2e8f0;
        font-size: 14px;
        transition: border-color 0.18s ease, box-shadow 0.18s ease, transform 0.18s ease;
        box-shadow: inset 0 1px 0 rgba(148, 163, 184, 0.12);
    }
    .damage-textarea {
        resize: vertical;
        min-height: 90px;
        line-height: 1.55;
    }
    .damage-input::placeholder,
    .damage-textarea::placeholder {
        color: rgba(148, 163, 184, 0.7);
    }
    .damage-input:focus,
    .damage-textarea:focus {
        outline: none;
        border-color: rgba(56, 189, 248, 0.6);
        box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.22);
        background: rgba(2, 6, 23, 0.95);
        transform: translateY(-1px);
    }

    .damage-checkbox-group {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        gap: 12px 18px;
    }
    .damage-checkbox {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 14px 18px;
        border-radius: 14px;
        background: rgba(15, 23, 42, 0.78);
        border: 1px solid rgba(148, 163, 184, 0.22);
        transition: border-color 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
        box-shadow: inset 0 1px 0 rgba(148, 163, 184, 0.1);
    }
    .damage-checkbox:hover {
        border-color: rgba(129, 140, 248, 0.45);
        transform: translateY(-2px);
        box-shadow: 0 18px 32px rgba(3, 7, 18, 0.35);
    }
    .damage-checkbox input[type="checkbox"] {
        width: 16px;
        height: 16px;
        accent-color: #38bdf8;
    }
    .damage-checkbox span {
        color: #e2e8f0;
        font-weight: 500;
        letter-spacing: 0.01em;
    }

    .damage-actions {
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 12px;
    }
    .damage-tip {
        color: rgba(148, 163, 184, 0.82);
        font-size: 12px;
        letter-spacing: 0.04em;
    }
    .damage-submit {
        display: inline-flex;
        align-items: center;
        gap: 10px;
        background: linear-gradient(135deg, #22c55e, #16a34a);
        color: #f8fafc;
        border: none;
        padding: 12px 26px;
        border-radius: 14px;
        font-weight: 600;
        letter-spacing: 0.05em;
        box-shadow: 0 20px 36px rgba(34, 197, 94, 0.32);
        cursor: pointer;
        text-transform: uppercase;
        transition: transform 0.18s ease, box-shadow 0.18s ease;
    }
    .damage-submit:hover {
        transform: translateY(-2px);
        box-shadow: 0 24px 40px rgba(34, 197, 94, 0.38);
    }

    .damage-summary {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        gap: 16px;
    }
    .damage-summary__card {
        position: relative;
        display: flex;
        flex-direction: column;
        gap: 8px;
        padding: 18px 20px;
        border-radius: 18px;
        background: rgba(8, 16, 36, 0.78);
        border: 1px solid rgba(129, 140, 248, 0.24);
        box-shadow: 0 18px 32px rgba(5, 12, 28, 0.4);
        overflow: hidden;
    }
    .damage-summary__card::after {
        content: "";
        position: absolute;
        inset: 0;
        background: radial-gradient(circle at 80% 18%, rgba(59, 130, 246, 0.18), transparent 52%);
        opacity: 0.7;
        pointer-events: none;
    }
    .damage-summary__card > * { position: relative; z-index: 1; }
    .damage-summary__label {
        font-size: 12px;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: rgba(203, 213, 225, 0.72);
        font-weight: 600;
    }
    .damage-summary__value {
        font-size: 28px;
        font-weight: 700;
        color: #f8fafc;
        letter-spacing: -0.02em;
    }
    .damage-summary__meta {
        font-size: 12px;
        color: rgba(148, 163, 184, 0.78);
    }

    .damage-table-section {
        display: flex;
        flex-direction: column;
        gap: 16px;
    }
    .damage-table-head {
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 12px;
    }
    .damage-table-head h3 {
        margin: 0;
        font-size: 20px;
        font-weight: 700;
        letter-spacing: -0.01em;
        color: #f8fafc;
    }
    .damage-table-head p {
        margin: 0;
        color: rgba(148, 163, 184, 0.76);
        font-size: 13px;
    }

    .damage-table-shell {
        border-radius: 24px;
        border: 1px solid rgba(148, 163, 184, 0.22);
        background: rgba(7, 15, 32, 0.84);
        box-shadow: 0 24px 44px rgba(4, 10, 26, 0.5);
        overflow: hidden;
    }
    .damage-table-scroll {
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .damage-table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0 10px;
        min-width: 840px;
    }
    .damage-table thead th {
        background: rgba(10, 20, 44, 0.9);
        color: #e2e8f0;
        font-weight: 600;
        text-align: left;
        padding: 14px 18px;
        letter-spacing: 0.04em;
        text-transform: uppercase;
        font-size: 13px;
        border-bottom: 1px solid rgba(148, 163, 184, 0.2);
    }
    .damage-table tbody tr {
        background: rgba(9, 18, 38, 0.86);
        border: 1px solid rgba(148, 163, 184, 0.16);
        border-radius: 16px;
        box-shadow: 0 18px 34px rgba(7, 16, 32, 0.3);
        transition: transform 0.18s ease, box-shadow 0.18s ease;
    }
    .damage-table tbody tr:hover {
        transform: translateY(-2px);
        box-shadow: 0 24px 40px rgba(14, 65, 120, 0.32);
        border-color: rgba(96, 165, 250, 0.32);
    }
    .damage-table tbody td {
        padding: 14px 18px;
        color: #f1f5f9;
        vertical-align: top;
    }
    .damage-status {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 6px 12px;
        border-radius: 999px;
        font-size: 12px;
        font-weight: 600;
        letter-spacing: 0.05em;
        text-transform: uppercase;
        border: 1px solid transparent;
        width: fit-content;
    }
    .damage-status--dilaporkan { background: rgba(59, 130, 246, 0.18); color: #bfdbfe; border-color: rgba(59, 130, 246, 0.34); }
    .damage-status--diproses { background: rgba(251, 191, 36, 0.18); color: #facc15; border-color: rgba(251, 191, 36, 0.36); }
    .damage-status--selesai { background: rgba(34, 197, 94, 0.18); color: #86efac; border-color: rgba(34, 197, 94, 0.32); }
    .damage-status--rejected { background: rgba(248, 113, 113, 0.18); color: #fecaca; border-color: rgba(248, 113, 113, 0.38); }
    .damage-status--pending { background: rgba(14, 165, 233, 0.18); color: #bae6fd; border-color: rgba(14, 165, 233, 0.34); }

    .damage-empty {
        padding: 18px;
        text-align: center;
        color: rgba(148, 163, 184, 0.78);
        font-weight: 600;
    }

    @media (max-width: 960px) {
        .damage-shell {
            padding: 32px 26px;
        }
        .damage-table {
            min-width: 720px;
        }
    }
    @media (max-width: 768px) {
        .damage-shell {
            padding: 28px 22px;
            border-radius: 26px;
        }
        .damage-hero__meta {
            align-items: flex-start;
        }
        .damage-table {
            border-spacing: 0;
            min-width: 0;
        }
        .damage-table thead {
            display: none;
        }
        .damage-table tbody {
            display: flex;
            flex-direction: column;
            gap: 16px;
        }
        .damage-table tbody tr {
            display: flex;
            flex-direction: column;
            padding: 16px;
        }
        .damage-table tbody td {
            display: flex;
            flex-direction: column;
            gap: 4px;
            padding: 8px 0;
        }
        .damage-table tbody td::before {
            content: attr(data-label);
            font-size: 11px;
            font-weight: 600;
            letter-spacing: 0.06em;
            text-transform: uppercase;
            color: rgba(148, 163, 184, 0.72);
        }
        .damage-table tbody td:first-child {
            padding-top: 0;
        }
        .damage-table tbody td:last-child {
            padding-bottom: 0;
        }
    }
    @media (max-width: 600px) {
        .damage-shell {
            padding: 24px 18px;
        }
        .damage-actions {
            flex-direction: column;
            align-items: flex-start;
        }
        .damage-submit {
            width: 100%;
            justify-content: center;
        }
    }
</style>
@endpush

@section('content')
@php
    $list = isset($items) ? $items : collect();
    $totalLaporan = $list->count();
    $totalDilaporkan = $list->where('status', 'dilaporkan')->count();
    $totalDiproses = $list->where('status', 'diproses')->count();
    $totalSelesai = $list->where('status', 'selesai')->count();
    $updatedAt = now()->timezone(config('app.timezone'));
@endphp

<div class="damage-shell">
    <div class="damage-hero">
        <div class="damage-hero__info">
            <span class="damage-hero__tagline">Pelaporan Aset</span>
            <h2>Barang Rusak</h2>
            <p>Laporkan kondisi peralatan yang membutuhkan perhatian agar tim administrasi dapat merespons lebih cepat dan terarah.</p>
        </div>
        <div class="damage-hero__meta">
            <span class="damage-hero__chip">Menunggu tindakan: {{ number_format($totalDilaporkan) }}</span>
            <span class="damage-hero__meta-text">Total laporan: {{ number_format($totalLaporan) }}</span>
            <span class="damage-hero__meta-text">Terakhir diperbarui {{ $updatedAt->format('d M Y H:i') }}</span>
        </div>
    </div>

    @if (session('success'))
        <div class="damage-alert damage-alert--success">
            <p class="damage-alert__title">Berhasil</p>
            <span>{{ session('success') }}</span>
        </div>
    @endif

    @if ($errors->any())
        <div class="damage-alert damage-alert--error">
            <p class="damage-alert__title">Periksa kembali input</p>
            <ul class="damage-alert__list">
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form method="POST" action="{{ route('barang-rusak.store') }}" class="damage-form">
        @csrf
        <div class="damage-grid">
            <div class="damage-field">
                <label for="tanggal" class="damage-label">Tanggal</label>
                <input type="date" id="tanggal" name="tanggal" value="{{ old('tanggal', date('Y-m-d')) }}" class="damage-input" required />
            </div>
            <div class="damage-field">
                <label for="lokasi" class="damage-label">Lokasi/Bagian</label>
                <input type="text" id="lokasi" name="lokasi" value="{{ old('lokasi') }}" placeholder="Contoh: Gudang A / Produksi" class="damage-input" required />
            </div>

            <div class="damage-field" style="grid-column: 1 / -1;">
                <label class="damage-label">Jenis Kerusakan (bisa pilih lebih dari satu)</label>
                <div class="damage-checkbox-group">
                    @php
                        $opsi = ['Kacamata', 'Helm', 'Tag LOTO', 'Sepatu Safety', 'Lockout/Tagout', 'Ear Plug'];
                        $terpilih = old('kerusakan', []);
                    @endphp
                    @foreach ($opsi as $item)
                        <label class="damage-checkbox">
                            <input type="checkbox" name="kerusakan[]" value="{{ $item }}" {{ in_array($item, (array) $terpilih) ? 'checked' : '' }} />
                            <span>{{ $item }}</span>
                        </label>
                    @endforeach
                </div>
            </div>

            <div class="damage-field" style="grid-column: 1 / -1;">
                <label for="keterangan" class="damage-label">Keterangan</label>
                <textarea id="keterangan" name="keterangan" rows="3" class="damage-textarea" placeholder="Tambahkan detail kerusakan...">{{ old('keterangan') }}</textarea>
            </div>
        </div>

        <div class="damage-actions">
            <span class="damage-tip">Pastikan data sudah benar untuk mempercepat tindak lanjut.</span>
            <button type="submit" class="damage-submit">
                <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" width="18" height="18">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M4.5 12.75l6 6 9-13.5" />
                </svg>
                Simpan Laporan
            </button>
        </div>
    </form>

    <div class="damage-summary">
        <div class="damage-summary__card">
            <span class="damage-summary__label">Total Laporan</span>
            <span class="damage-summary__value">{{ number_format($totalLaporan) }}</span>
            <span class="damage-summary__meta">Keseluruhan laporan yang masuk</span>
        </div>
        <div class="damage-summary__card">
            <span class="damage-summary__label">Belum Diproses</span>
            <span class="damage-summary__value">{{ number_format($totalDilaporkan) }}</span>
            <span class="damage-summary__meta">Laporan menunggu peninjauan</span>
        </div>
        <div class="damage-summary__card">
            <span class="damage-summary__label">Sedang Diproses</span>
            <span class="damage-summary__value">{{ number_format($totalDiproses) }}</span>
            <span class="damage-summary__meta">Penanganan tengah berlangsung</span>
        </div>
        <div class="damage-summary__card">
            <span class="damage-summary__label">Selesai Ditangani</span>
            <span class="damage-summary__value">{{ number_format($totalSelesai) }}</span>
            <span class="damage-summary__meta">Laporan telah ditutup</span>
        </div>
    </div>

    <div class="damage-table-section">
        <div class="damage-table-head">
            <h3>Daftar Laporan</h3>
            <p>Ringkasan laporan kerusakan terbaru dari tim lapangan.</p>
        </div>

        <div class="damage-table-shell">
            <div class="damage-table-scroll">
                <table class="damage-table">
                    <thead>
                        <tr>
                            <th style="white-space:nowrap;">Tanggal</th>
                            <th>Lokasi</th>
                            <th>Kerusakan</th>
                            <th>Keterangan</th>
                            <th>Pelapor</th>
                            <th>Status</th>
                            <th>Catatan Admin</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($list as $row)
                            @php
                                $tgl = $row->tanggal ?? $row->tanggal_rusak ?? null;
                                $kerusakanArr = [];
                                if (isset($row->kerusakan)) {
                                    if (is_array($row->kerusakan)) {
                                        $kerusakanArr = $row->kerusakan;
                                    } elseif (is_string($row->kerusakan)) {
                                        $decoded = json_decode($row->kerusakan, true);
                                        $kerusakanArr = $decoded ?: ($row->kerusakan !== '' ? [$row->kerusakan] : []);
                                    }
                                } elseif (isset($row->nama_barang)) {
                                    $kerusakanArr = [$row->nama_barang];
                                }
                                $status = isset($row->status) ? strtolower($row->status) : null;
                                $statusModifier = match($status) {
                                    'dilaporkan' => 'dilaporkan',
                                    'diproses' => 'diproses',
                                    'selesai' => 'selesai',
                                    'dibatalkan' => 'rejected',
                                    'ditolak', 'rejected' => 'rejected',
                                    'menunggu', 'pending' => 'pending',
                                    default => 'dilaporkan',
                                };
                                $statusClass = 'damage-status damage-status--' . $statusModifier;
                                $statusLabel = isset($row->status) ? ucfirst($row->status) : 'Belum diproses';
                                $adminNote = $row->admin_note ?? ($row->catatan_admin ?? null);
                            @endphp
                            <tr>
                                <td data-label="Tanggal">
                                    @if($tgl)
                                        {{ \Carbon\Carbon::parse($tgl)->format('d/m/Y') }}
                                    @else
                                        -
                                    @endif
                                </td>
                                <td data-label="Lokasi">{{ $row->lokasi ?? '-' }}</td>
                                <td data-label="Kerusakan">{{ implode(', ', $kerusakanArr) ?: '-' }}</td>
                                <td data-label="Keterangan">{{ $row->keterangan ?? '-' }}</td>
                                <td data-label="Pelapor">{{ $row->user_name ?? '-' }}</td>
                                <td data-label="Status">
                                    <span class="{{ $statusClass }}">{{ $statusLabel }}</span>
                                </td>
                                <td data-label="Catatan Admin">
                                    <span title="{{ $adminNote ?? 'Belum ada catatan admin' }}">{{ $adminNote ? $adminNote : 'Belum ada catatan admin' }}</span>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="damage-empty">Belum ada data.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
