@extends('admin.layouts.app')

@section('content')

<style>
@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap');
*, *::before, *::after { box-sizing: border-box; }
body, * { font-family: 'Plus Jakarta Sans', sans-serif; }

.user-wrap { padding: 2rem 2.5rem 3rem; max-width: 1200px; margin: 0 auto; }

/* ALERTS */
.alert { padding: 12px 18px; border-radius: 12px; margin-bottom: 1.2rem; font-size: 13px; font-weight: 600; }
.alert-success { background: #dcfce7; color: #166534; }
.alert-error   { background: #fee2e2; color: #b91c1c; }

/* HEADER */
.user-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}
.user-title h2 { font-size: 26px; font-weight: 800; color: #0f172a; margin: 0; }
.user-title p  { font-size: 13px; color: #94a3b8; margin-top: 5px; }

.top-btn { display: flex; gap: 12px; }
.btn-ui {
    border: none;
    padding: 10px 18px;
    border-radius: 12px;
    font-size: 13px;
    font-weight: 700;
    text-decoration: none;
    transition: .2s;
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
}
.btn-dashboard { background: #eef2ff; color: #3730a3; }
.btn-dashboard:hover { background: #dbeafe; transform: translateY(-2px); }
.btn-add { background: #0D215C; color: #fff; box-shadow: 0 4px 14px rgba(13,33,92,.2); }
.btn-add:hover { background: #1e3a8a; transform: translateY(-2px); }

/* STATS */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
    margin-bottom: 2rem;
}
.stat-card {
    background: #fff;
    border-radius: 18px;
    padding: 1.2rem 1.4rem;
    border: 1px solid #edf1f7;
    transition: .25s;
    cursor: default;
    box-shadow: 0 2px 8px rgba(13,33,92,.04);
}
.stat-card:hover { transform: translateY(-4px); box-shadow: 0 12px 30px rgba(13,33,92,.1); }
.stat-label  { font-size: 11px; font-weight: 700; color: #94a3b8; text-transform: uppercase; letter-spacing: .5px; margin-bottom: 8px; }
.stat-number { font-size: 34px; font-weight: 800; color: #0f172a; }

/* TABLE */
.user-card {
    background: #fff;
    border-radius: 20px;
    border: 1px solid #edf1f7;
    overflow: hidden;
    box-shadow: 0 2px 12px rgba(13,33,92,.04);
}
.user-card-header {
    padding: 1.2rem 1.5rem;
    border-bottom: 1px solid #f1f5f9;
    background: #f8fafc;
}
.user-card-header h3 { font-size: 15px; font-weight: 700; color: #0D215C; margin: 0; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
thead { background: #f8fafc; }
thead th {
    padding: 14px 18px;
    font-size: 11px;
    text-transform: uppercase;
    color: #94a3b8;
    text-align: left;
    font-weight: 700;
    letter-spacing: .5px;
}
tbody td {
    padding: 15px 18px;
    border-top: 1px solid #f1f5f9;
    vertical-align: middle;
    font-size: 13px;
    color: #334155;
}
tbody tr:hover { background: #f8fafc; }

.user-info { display: flex; align-items: center; gap: 12px; }
.avatar {
    width: 40px; height: 40px;
    border-radius: 12px;
    background: linear-gradient(135deg, #0D215C, #2563eb);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700;
    font-size: 13px;
    flex-shrink: 0;
}
.user-name { font-weight: 700; color: #0f172a; font-size: 14px; }
.user-id   { font-size: 11px; color: #94a3b8; margin-top: 2px; }

.role-badge {
    padding: 5px 14px;
    border-radius: 50px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .3px;
}
.role-superadmin { background: #fef3c7; color: #92400e; }
.role-admin      { background: #dbeafe; color: #1d4ed8; }
.role-user       { background: #dcfce7; color: #166534; }

.action-group { display: flex; gap: 8px; }
.action-btn {
    border: none;
    padding: 7px 14px;
    border-radius: 10px;
    font-size: 11px;
    font-weight: 700;
    cursor: pointer;
    text-decoration: none;
    transition: .15s;
    display: inline-flex;
    align-items: center;
    gap: 5px;
}
.btn-edit   { background: #dbeafe; color: #2563eb; }
.btn-edit:hover { background: #bfdbfe; }
.btn-delete { background: #fee2e2; color: #dc2626; }
.btn-delete:hover { background: #fecaca; }

/* RESPONSIVE */
@media (max-width: 768px) {
    .user-wrap { padding: 1rem 0.5rem; }
    .user-header { flex-direction: column; align-items: flex-start; gap: 16px; margin-bottom: 1.5rem; }
    .top-btn { flex-wrap: wrap; width: 100%; }
    .top-btn .btn-ui { flex: 1; justify-content: center; text-align: center; }
    .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; margin-bottom: 1.5rem; }
    .user-card-header { padding: 1rem; }
}
@media (max-width: 480px) {
    .stats-grid { grid-template-columns: 1fr; }
    .user-title h2 { font-size: 22px; }
    .user-card { border-radius: 16px; }
}
</style>

<div class="user-wrap">

    {{-- ALERTS --}}
    @if(session('success'))
    <div class="alert alert-success">✓ {{ session('success') }}</div>
    @endif
    @if(session('error'))
    <div class="alert alert-error">✗ {{ session('error') }}</div>
    @endif

    {{-- HEADER --}}
    <div class="user-header">
        <div class="user-title">
            <h2>Manajemen Pengguna</h2>
            <p>Kelola seluruh akun dan hak akses pengguna sistem</p>
        </div>
        <div class="top-btn">
            <a href="{{ route('admin.dashboard') }}" class="btn-ui btn-dashboard">← Dashboard</a>
            <a href="{{ route('admin.users.create') }}" class="btn-ui btn-add">+ Tambah Pengguna</a>
        </div>
    </div>

    {{-- STATS --}}
    @php
        $usersCol      = collect($users);
        $totalUsers    = $usersCol->count();
        $totalAdmin    = $usersCol->where('role', 'admin')->count();
        $totalPetugas  = $usersCol->where('role', 'user')->count();
    @endphp
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-label">Total Pengguna</div>
            <div class="stat-number">{{ $totalUsers }}</div>
        </div>
        <div class="stat-card">
            <div class="stat-label">Administrator</div>
            <div class="stat-number">{{ $totalAdmin }}</div>
        </div>
        <div class="stat-card">
            <div class="stat-label">Petugas Lapangan</div>
            <div class="stat-number">{{ $totalPetugas }}</div>
        </div>
    </div>

    {{-- TABLE --}}
    <div class="user-card">
        <div class="user-card-header">
            <h3>Daftar Pengguna Sistem</h3>
        </div>
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Pengguna</th>
                        <th>Username</th>
                        <th>Jabatan</th>
                        <th>Role</th>
                        <th>Terdaftar</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($users as $user)
                    @php $u = is_array($user) ? (object)$user : $user; @endphp
                    <tr>
                        <td>
                            <div class="user-info">
                                <div class="avatar">{{ strtoupper(substr($u->name ?? '-', 0, 2)) }}</div>
                                <div>
                                    <div class="user-name">{{ $u->name ?? '-' }}</div>
                                    <div class="user-id">ID #{{ $u->id ?? '-' }}</div>
                                </div>
                            </div>
                        </td>
                        <td>{{ $u->username ?? '-' }}</td>
                        <td>{{ $u->jabatan ?? '-' }}</td>
                        <td>
                            @if(($u->role ?? '') == 'superadmin')
                                <span class="role-badge role-superadmin">Super Admin</span>
                            @elseif(($u->role ?? '') == 'admin')
                                <span class="role-badge role-admin">Administrator</span>
                            @else
                                <span class="role-badge role-user">Petugas</span>
                            @endif
                        </td>
                        <td>
                            @if(!empty($u->created_at))
                                {{ \Carbon\Carbon::parse($u->created_at)->format('d M Y') }}
                            @else
                                -
                            @endif
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" style="text-align:center;padding:2.5rem;color:#94a3b8;font-size:13px;">
                            Belum ada data pengguna.
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

</div>

@endsection