@extends('layouts.admin')

@section('content')
    <h4 class="mb-3">{{ $user ? 'Edit User' : 'Tambah User' }}</h4>

    <form method="POST" action="{{ $user ? route('admin.users.update', $user) : route('admin.users.store') }}">
        @csrf
        @if($user)
            @method('PUT')
        @endif

        <div class="row mb-3">
            <div class="col-md-6">
                <label class="form-label">Nama</label>
                <input type="text" name="name" class="form-control" required value="{{ old('name', $user->name ?? '') }}">
            </div>
            <div class="col-md-6">
                <label class="form-label">Email</label>
                <input type="email" name="email" class="form-control" required value="{{ old('email', $user->email ?? '') }}">
            </div>
        </div>

        <div class="row mb-3">
            <div class="col-md-6">
                <label class="form-label">Password {{ $user ? '(kosongkan jika tidak diubah)' : '' }}</label>
                <input type="password" name="password" class="form-control" {{ $user ? '' : 'required' }}>
            </div>
        </div>

        @if($user)
            <div class="row mb-3">
                <div class="col-md-6">
                    <label class="form-label">MFA (TOTP)</label>
                    @if($user->mfa_enabled_at)
                        <p class="mb-2 text-success">Aktif sejak {{ $user->mfa_enabled_at->format('Y-m-d H:i') }}</p>
                        <a href="{{ route('admin.users.edit', $user) }}?regen_mfa=1" class="btn btn-sm btn-outline-primary" data-confirm-modal="Regenerasi secret akan menghapus kode sebelumnya. Lanjutkan?">Regenerate Secret & Recovery</a>
                        <a href="{{ route('admin.users.edit', $user) }}?disable_mfa=1" class="btn btn-sm btn-outline-danger ms-2" data-confirm-modal="Nonaktifkan MFA?">Nonaktifkan MFA</a>
                    @else
                        <p class="mb-2 text-muted">Belum aktif.</p>
                        <a href="{{ route('admin.users.edit', $user) }}?enable_mfa=1" class="btn btn-sm btn-outline-primary">Aktifkan MFA</a>
                    @endif
                </div>
            </div>

            @if(session('mfa_setup'))
                <div class="card card-glass border-info border-opacity-25 mt-3 shadow-lg overflow-hidden">
                    <div class="card-header bg-info bg-opacity-10 py-3 border-bottom border-white border-opacity-10">
                        <h6 class="mb-0 text-info d-flex align-items-center">
                            <i class="bi bi-shield-check me-2 fs-5"></i>
                            Two-Factor Authentication Setup
                        </h6>
                    </div>
                    <div class="card-body p-4">
                        <div class="row g-4 align-items-center">
                            <div class="col-md-4 text-center">
                                <div class="bg-white p-3 d-inline-block rounded-4 shadow-sm mb-3">
                                    <img src="https://api.qrserver.com/v1/create-qr-code/?size=180x180&data={{ urlencode(session('mfa_setup')['otpauth']) }}" alt="QR" class="img-fluid" style="width: 150px;">
                                </div>
                                <div class="p-2 bg-black bg-opacity-25 rounded font-monospace small text-info border border-info border-opacity-10">
                                    {{ session('mfa_setup')['secret'] }}
                                </div>
                            </div>
                            <div class="col-md-8 border-start border-white border-opacity-10 ps-md-4">
                                <div class="mb-4">
                                    <label class="small text-uppercase fw-bold text-muted mb-2 d-block letter-spacing-1">Instructions</label>
                                    <p class="small text-muted mb-0">1. Scan the QR code with your authenticator app.</p>
                                    <p class="small text-muted">2. If you cannot scan, enter the secret key manually.</p>
                                </div>
                                
                                <div>
                                    <label class="small text-uppercase fw-bold text-muted mb-2 d-block letter-spacing-1 text-warning">
                                        <i class="bi bi-exclamation-triangle-fill me-1"></i>
                                        Recovery Codes
                                    </label>
                                    <p class="extra-small text-muted mb-3">Save these codes in a safe place. They are shown only once!</p>
                                    <div class="row g-2">
                                        @foreach(session('mfa_setup')['recovery'] as $code)
                                            <div class="col-6 col-md-4">
                                                <div class="p-2 bg-black bg-opacity-25 rounded font-monospace small text-center border border-white border-opacity-10">
                                                    {{ $code }}
                                                </div>
                                            </div>
                                        @endforeach
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            @endif
        @endif

        <div class="mb-3">
            <label class="form-label">Roles</label>
            <div class="row">
                @foreach($roles as $app => $roleGroup)
                    <div class="col-md-4 mb-2">
                        <div class="fw-semibold text-uppercase small">{{ $app }}</div>
                        @foreach($roleGroup as $role)
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="roles[]"
                                       value="{{ $role->id }}" id="role_{{ $role->id }}"
                                       {{ in_array($role->id, old('roles', $selectedRoles)) ? 'checked' : '' }}>
                                <label class="form-check-label" for="role_{{ $role->id }}">
                                    {{ $role->key }} ({{ $role->name }})
                                </label>
                            </div>
                        @endforeach
                    </div>
                @endforeach
            </div>
        </div>

        <div class="d-flex gap-2">
            <button class="btn btn-primary">{{ $user ? 'Simpan' : 'Buat' }}</button>
            <a class="btn btn-outline-secondary" href="{{ route('admin.users.index') }}">Batal</a>
        </div>
    </form>
@endsection
