@extends('layouts.admin')

@section('title', 'Kelola Admin')
@section('page_title', 'Kelola Admin')

@section('style')
    <style>
        .master-panel {
            margin-bottom: 0;
            border: 1px solid rgba(0, 0, 0, 0.05);
            border-radius: 8px;
            box-shadow: none;
            overflow: hidden;
        }

        .master-panel .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 1rem;
            padding: 1rem 1.15rem;
            color: #4a3728;
            background: #ffffff;
            border-bottom: 1px solid #e8edf2;
            border-radius: 0;
        }

        .master-panel .card-header h5 {
            margin: 0;
            font-size: 1rem;
            font-weight: 750;
        }

        .panel-subtitle {
            margin-top: 0.2rem;
            color: #64748b;
            font-size: 0.8rem;
        }

        .table-clean {
            margin: 0;
        }

        .table-clean thead th {
            padding: 0.85rem;
            color: #637181;
            background: #ffffff;
            border-bottom: 2px solid #e7ecf1;
            font-size: 0.8rem;
            text-transform: uppercase;
            white-space: nowrap;
            vertical-align: middle;
        }

        .table-clean tbody td {
            padding: 0.9rem 0.85rem;
            color: #4a3728;
            border-color: rgba(0, 0, 0, 0.05);
            vertical-align: middle;
        }

        .name-strong {
            color: #334155;
            font-weight: 700;
        }

        .meta-text {
            color: #64748b;
            font-size: 0.8rem;
        }

        .type-pill {
            display: inline-flex;
            padding: 0.3rem 0.55rem;
            border-radius: 6px;
            color: #7a5c1e;
            background: #e6ddc5;
            font-size: 0.75rem;
            font-weight: 700;
            white-space: nowrap;
        }

        .type-pill.danger {
            color: #8a4843;
            background: #f2d5d3;
        }

        .empty-state {
            padding: 2rem 1rem;
            color: #64748b;
            text-align: center;
        }

        .action-group {
            display: flex;
            gap: 0.4rem;
            justify-content: flex-end;
        }

        .form-surface {
            padding: 1rem;
            border-bottom: 1px solid #e7ecf1;
            background: #ffffff;
        }

        @media (max-width: 575.98px) {}
    </style>
@endsection

@section('content')
    <section class="page-hero">
        <div>
            <h3><i class="fas fa-user-shield"></i> Kelola Admin</h3>
            <p>Manajemen akun admin yang memiliki akses ke panel administrasi.</p>
        </div>
        <a href="{{ route('admin.admin-users.create') }}" class="btn btn-sage">
            <i class="fas fa-plus"></i> Tambah Admin
        </a>
    </section>

    <div class="card master-panel">
        <div class="card-header">
            <div>
                <h5>Daftar Akun Admin</h5>
                <div class="panel-subtitle">Admin SDM BKPSDM mengelola data pegawai; Super Admin mengelola pengaturan
                    sistem.</div>
            </div>
        </div>
        <div class="form-surface">
            <form action="{{ route('admin.admin-users.index') }}" method="GET">
                <div class="row g-3">
                    <div class="col-lg-4 col-md-12">
                        <label class="form-label">Pencarian</label>
                        <div class="input-group">
                            <span class="input-group-text bg-transparent text-muted border-end-0"><i
                                    class="fas fa-search"></i></span>
                            <input type="text" name="search" class="form-control border-start-0 ps-0 js-auto-search"
                                placeholder="Cari nama atau email..." value="{{ request('search') }}">
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <div id="ajax-content">
            <div class="table-responsive">
                <table class="table table-clean">
                    <thead>
                        <tr>
                            <th>Nama</th>
                            <th>Email</th>
                            <th>Role</th>
                            <th>Kewenangan</th>
                            <th class="text-center">Dibuat</th>
                            <th class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($admins as $admin)
                            <tr>
                                <td>
                                    <div class="name-strong">{{ $admin->name }}</div>
                                </td>
                                <td>
                                    <div class="meta-text">{{ $admin->email }}</div>
                                </td>
                                <td>
                                    @if ($admin->isSuperAdmin())
                                        <span class="type-pill danger">{{ $admin->role_label }}</span>
                                    @else
                                        <span class="type-pill">Admin SDM BKPSDM</span>
                                    @endif
                                </td>
                                <td>
                                    <div class="meta-text">
                                        {{ $admin->isSuperAdmin() ? 'Pengaturan sistem dan master data' : 'Seluruh data SDM BKPSDM' }}
                                    </div>
                                </td>
                                <td class="text-center">
                                    <div class="meta-text">{{ $admin->created_at->format('d-m-Y') }}</div>
                                </td>
                                <td class="text-center">
                                    <div class="action-group">
                                        <a href="{{ route('admin.admin-users.edit', $admin) }}"
                                            class="btn btn-sm btn-outline-warning" title="Edit">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <form action="{{ route('admin.admin-users.destroy', $admin) }}" method="POST"
                                            style="display: inline-block;">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-outline-danger" title="Hapus"
                                                @disabled($admin->id === auth()->id())
                                                onclick="return confirm('Yakin ingin menghapus akun admin ini?')">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="empty-state"><i class="fas fa-inbox d-block mb-2 fs-4"></i>Belum ada akun
                                    admin.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            @if ($admins->hasPages())
                {{ $admins->links() }}
            @endif
        </div>
    </div>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const form = document.querySelector('form[method="GET"], .filter-card form');
            if (!form) return;

            const contentContainer = document.getElementById('ajax-content');

            const submitFilter = function () {
                const url = new URL(form.action || window.location.href);
                const formData = new FormData(form);
                const params = new URLSearchParams(formData);
                url.search = params.toString();

                if (contentContainer) contentContainer.style.opacity = '0.5';

                fetch(url, { headers: { 'X-Requested-With': 'XMLHttpRequest' } })
                    .then(response => response.text())
                    .then(html => {
                        const parser = new DOMParser();
                        const doc = parser.parseFromString(html, 'text/html');

                        const newContent = doc.getElementById('ajax-content');
                        if (newContent && contentContainer) {
                            contentContainer.innerHTML = newContent.innerHTML;
                            contentContainer.style.opacity = '1';
                        }

                        window.history.replaceState({}, '', url);
                    })
                    .catch(() => { if (contentContainer) contentContainer.style.opacity = '1'; });
            };

            form.addEventListener('submit', function (e) {
                e.preventDefault();
                submitFilter();
            });

            form.querySelectorAll('select, .js-auto-filter').forEach(function (input) {
                input.addEventListener('change', submitFilter);
            });

            let searchTimer;
            form.querySelectorAll('.js-auto-search').forEach(function (input) {
                input.addEventListener('input', function () {
                    clearTimeout(searchTimer);
                    searchTimer = setTimeout(submitFilter, 500);
                });
            });
        });
    </script>
@endsection