@extends('layouts.anggota')

@section('title', 'Pengaturan Sistem - Pustaka Nawasena')

@section('content')
<div class="container py-5">
    <h2 class="dashboard-title mb-4 text-center">
        <i class="bi bi-gear-fill me-2"></i> Pengaturan Sistem
    </h2>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            {{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="glass shadow-sm mb-5">
        <h5 class="fw-bold text-primary mb-3">
            <i class="bi bi-person-plus-fill me-2"></i> Jadikan User Sebagai Admin
        </h5>
        <form action="{{ route('admin.system.add') }}" method="POST" class="row g-3">
            @csrf
            <div class="col-md-8">
                <label for="user_id" class="form-label fw-semibold">Pilih User</label>
                <select name="user_id" id="user_id" class="form-select" required>
                    <option value="">-- Pilih User --</option>
                    @foreach($users as $user)
                        @if($user->role !== 'admin')
                            <option value="{{ $user->id }}">
                                {{ $user->name }} ({{ $user->email }})
                            </option>
                        @endif
                    @endforeach
                </select>
            </div>
            <div class="col-md-4 d-flex align-items-end">
                <button type="submit" class="btn btn-primary w-100">
                    <i class="bi bi-person-check-fill me-1"></i> Jadikan Admin
                </button>
            </div>
        </form>
    </div>

    <div class="glass shadow-sm">
        <h5 class="fw-bold text-primary mb-3">
            <i class="bi bi-people-fill me-2"></i> Daftar User & Admin
        </h5>

        <table class="table table-bordered align-middle table-hover">
            <thead class="table-primary">
                <tr>
                    <th>Nama</th>
                    <th>Email</th>
                    <th>Role</th>
                    <th>Status</th>
                    <th class="text-center">Aksi</th>
                </tr>
            </thead>

            <tbody>
                @foreach($users as $user)
                <tr>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user->email }}</td>

                    <td>
                        @if($user->role === 'admin')
                            <span class="badge bg-success">Admin</span>
                        @else
                            <span class="badge bg-secondary">User</span>
                        @endif
                    </td>

                    <td>
                        @if($user->blocked_at)
                            <span class="badge bg-danger">Diblokir</span>
                        @else
                            <span class="badge bg-success">Aktif</span>
                        @endif
                    </td>

                    <td class="text-center">

                        @if($user->role === 'admin')
                            @if($user->id !== auth()->id())
                                <form action="{{ route('admin.system.remove') }}" method="POST" class="d-inline">
                                    @csrf
                                    <input type="hidden" name="user_id" value="{{ $user->id }}">
                                    <button type="submit" class="btn btn-sm btn-outline-danger"
                                        onclick="return confirm('Cabut status admin untuk {{ $user->name }}?')">
                                        <i class="bi bi-person-dash-fill me-1"></i> Cabut Admin
                                    </button>
                                </form>
                            @else
                                <small class="text-muted fst-italic">Anda sendiri</small>
                            @endif

                        @else
                            <form action="{{ route('admin.system.add') }}" method="POST" class="d-inline">
                                @csrf
                                <input type="hidden" name="user_id" value="{{ $user->id }}">
                                <button type="submit" class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-person-check-fill me-1"></i> Jadikan Admin
                                </button>
                            </form>
                        @endif

                        @if(!$user->blocked_at)
                            <form action="{{ route('admin.system.block') }}" method="POST" class="d-inline">
                                @csrf
                                <input type="hidden" name="user_id" value="{{ $user->id }}">
                                <button type="submit" class="btn btn-sm btn-outline-warning"
                                    onclick="return confirm('Blokir user {{ $user->name }}? User tidak dapat login dan meminjam buku!')">
                                    <i class="bi bi-lock-fill me-1"></i> Blokir
                                </button>
                            </form>
                        @else
                            <form action="{{ route('admin.system.unblock') }}" method="POST" class="d-inline">
                                @csrf
                                <input type="hidden" name="user_id" value="{{ $user->id }}">
                                <button type="submit" class="btn btn-sm btn-outline-success"
                                    onclick="return confirm('Buka blokir untuk {{ $user->name }}?')">
                                    <i class="bi bi-unlock-fill me-1"></i> Buka Blokir
                                </button>
                            </form>
                        @endif

                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>
@endsection
