<?= $this->extend('layouts/app') ?>

<?= $this->section('content') ?>

<style>
    /* ===== MODERN SAAS DESIGN SYSTEM TOKENS ===== */
    :root {
        --bg-body: #f8fafc;
        --bg-card: #ffffff;
        --bg-accent: #f1f5f9;
        --bg-input: #ffffff;
        --border-color: #e2e8f0;
        --border-hover: #cbd5e1;
        --text-main: #0f172a;
        --text-sub: #64748b;
        --primary: #4f46e5;
        --primary-hover: #4338ca;
        --primary-light: rgba(79, 70, 229, 0.08);
        --success: #10b981;
        --warning: #f59e0b;
        --danger: #ef4444;
        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
        --radius-lg: 16px;
        --radius-md: 12px;
        --radius-sm: 8px;
        --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    }

    @media (prefers-color-scheme: dark) {
        :root {
            --bg-body: #0b0f19;
            --bg-card: #151e2e;
            --bg-accent: #1e293b;
            --bg-input: #0f172a;
            --border-color: #243044;
            --border-hover: #334155;
            --text-main: #f8fafc;
            --text-sub: #94a3b8;
            --primary-light: rgba(79, 70, 229, 0.18);
            --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
            --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
        }
    }

    [data-bs-theme="dark"] {
        --bg-body: #0b0f19;
        --bg-card: #151e2e;
        --bg-accent: #1e293b;
        --bg-input: #0f172a;
        --border-color: #243044;
        --border-hover: #334155;
        --text-main: #f8fafc;
        --text-sub: #94a3b8;
        --primary-light: rgba(79, 70, 229, 0.18);
        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
    }

    body, .content-wrapper, .main-content, .content, .page-content, main, .edit-user-workspace {
        background-color: var(--bg-body) !important;
        color: var(--text-main) !important;
        transition: var(--transition);
    }

    .edit-card-container {
        max-width: 800px;
        margin: 0 auto;
    }

    /* Card Premium Styling */
    .card-modern {
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-lg);
        padding: 2rem;
        box-shadow: var(--shadow-sm);
        transition: var(--transition);
    }

    /* User Profile Context Box */
    .user-context-box {
        background: var(--bg-accent);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-md);
        padding: 1rem 1.25rem;
    }

    .avatar-circle-md {
        width: 46px;
        height: 46px;
        border-radius: 50%;
        background: var(--primary-light);
        color: var(--primary);
        font-weight: 700;
        font-size: 1.1rem;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }

    /* Form Modern Inputs */
    .form-control-modern, .form-select-modern {
        background-color: var(--bg-input) !important;
        border: 1px solid var(--border-color) !important;
        color: var(--text-main) !important;
        border-radius: var(--radius-md) !important;
        padding: 0.6rem 0.9rem !important;
        font-size: 0.9rem !important;
        transition: var(--transition) !important;
    }

    .form-control-modern:focus, .form-select-modern:focus {
        border-color: var(--primary) !important;
        box-shadow: 0 0 0 3px var(--primary-light) !important;
        outline: none !important;
    }

    .custom-input-addon {
        background-color: var(--bg-accent) !important;
        border: 1px solid var(--border-color) !important;
        color: var(--text-sub) !important;
        border-top-left-radius: var(--radius-md) !important;
        border-bottom-left-radius: var(--radius-md) !important;
    }

    .custom-input-addon-btn {
        background-color: var(--bg-accent) !important;
        border: 1px solid var(--border-color) !important;
        color: var(--text-sub) !important;
        border-top-right-radius: var(--radius-md) !important;
        border-bottom-right-radius: var(--radius-md) !important;
        transition: var(--transition);
    }

    .custom-input-addon-btn:hover {
        background-color: var(--border-color) !important;
        color: var(--text-main) !important;
    }

    .code-pill {
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        color: var(--text-main);
        font-family: var(--bs-font-monospace);
        font-size: 0.8rem;
        font-weight: 600;
        padding: 0.15rem 0.5rem;
        border-radius: var(--radius-sm);
    }
</style>

<div class="container-fluid py-4 edit-user-workspace">
    
    <div class="edit-card-container">
        
        <!-- PAGE HEADER -->
        <div class="d-flex flex-column flex-sm-row justify-content-between align-items-sm-center mb-4 gap-3">
            <div>
                <h3 class="fw-bold mb-0 text-main">Edit Data User</h3>
                <p class="text-sub small mb-0 mt-0.5">Perbarui informasi profil dan hak akses pengguna dalam sistem.</p>
            </div>
            <div>
                <a href="<?= base_url('superadmin/user') ?>" class="btn btn-outline-secondary rounded-3 px-3 py-2 fw-semibold d-inline-flex align-items-center gap-2 fs-7">
                    <i class="bi bi-arrow-left"></i> Kembali
                </a>
            </div>
        </div>

        <!-- FLASH ERRORS -->
        <?php if (session()->getFlashdata('errors')) : ?>
            <div class="alert alert-danger alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4" role="alert">
                <div class="d-flex align-items-start gap-2.5">
                    <i class="bi bi-exclamation-triangle-fill fs-5 text-danger flex-shrink-0 mt-0.5"></i>
                    <div>
                        <strong class="d-block mb-1">Gagal memperbarui data:</strong>
                        <ul class="mb-0 ps-3 small">
                            <?php foreach (session()->getFlashdata('errors') as $error) : ?>
                                <li><?= $error ?></li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php endif; ?>

        <!-- MAIN FORM CARD -->
        <div class="card-modern">

            <?php 
                $namaLengkap = $user['nama_lengkap'] ?? 'Tanpa Nama';
                $initial = strtoupper(substr(trim($namaLengkap), 0, 1));
            ?>

            <!-- USER IDENTITY CONTEXT -->
            <div class="user-context-box mb-4">
                <div class="d-flex align-items-center gap-3">
                    <div class="avatar-circle-md"><?= $initial ?></div>
                    <div class="overflow-hidden">
                        <div class="fw-bold text-main" style="font-size: 0.95rem;"><?= esc($namaLengkap) ?></div>
                        <div class="d-flex align-items-center gap-2 mt-0.5">
                            <span class="code-pill text-primary">
                                <i class="bi bi-at"></i><?= esc($user['username']) ?>
                            </span>
                            <span class="text-sub small font-monospace">ID: #<?= esc($user['id_user']) ?></span>
                        </div>
                    </div>
                </div>
            </div>

            <!-- FORM START -->
            <form method="post" action="<?= base_url('superadmin/user/update/' . $user['id_user']) ?>">
                <?= csrf_field() ?>

                <div class="row g-3">
                    
                    <!-- Username -->
                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-main small">Username <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-at"></i></span>
                            <input type="text" 
                                   name="username" 
                                   class="form-control form-control-modern" 
                                   value="<?= old('username', $user['username']) ?>" 
                                   placeholder="Contoh: john_doe"
                                   required>
                        </div>
                    </div>

                    <!-- Email -->
                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-main small">Email <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-envelope"></i></span>
                            <input type="email" 
                                   name="email" 
                                   class="form-control form-control-modern" 
                                   value="<?= old('email', $user['email']) ?>" 
                                   placeholder="contoh@domain.com"
                                   required>
                        </div>
                    </div>

                    <!-- Nama Lengkap -->
                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-main small">Nama Lengkap <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-person"></i></span>
                            <input type="text" 
                                   name="nama_lengkap" 
                                   class="form-control form-control-modern" 
                                   value="<?= old('nama_lengkap', $user['nama_lengkap']) ?>" 
                                   placeholder="Masukkan nama lengkap"
                                   required>
                        </div>
                    </div>

                    <!-- Role -->
                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-main small">Role / Hak Akses <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-shield-check"></i></span>
                            <select name="id_role" class="form-select form-select-modern" required>
                                <option value="">-- Pilih Role --</option>
                                <?php foreach ($roles as $role): ?>
                                    <option value="<?= $role['id_role'] ?>" <?= ($role['id_role'] == old('id_role', $user['id_role'])) ? 'selected' : '' ?>>
                                        <?= esc($role['nama_role']) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>

                    <!-- Password Baru (Optional) -->
                    <div class="col-12 mt-4">
                        <label class="form-label fw-semibold text-main small">
                            Password Baru 
                            <span class="text-sub font-normal fs-8 ms-1">(Kosongkan jika tidak ingin diubah)</span>
                        </label>
                        <div class="input-group">
                            <span class="input-group-text custom-input-addon"><i class="bi bi-key"></i></span>
                            <input type="password" 
                                   name="password" 
                                   id="passwordInput" 
                                   class="form-control form-control-modern" 
                                   placeholder="Masukkan password baru jika ingin mengubah">
                            <button class="btn custom-input-addon-btn" type="button" id="togglePassword">
                                <i class="bi bi-eye" id="toggleIcon"></i>
                            </button>
                        </div>
                    </div>

                </div>

                <!-- ACTION BUTTONS -->
                <div class="d-flex flex-column flex-sm-row justify-content-end gap-2 mt-4 pt-3 border-top border-color">
                    <a href="<?= base_url('superadmin/user') ?>" class="btn btn-outline-secondary rounded-3 py-2 px-4 fw-semibold">
                        <i class="bi bi-x-circle me-1"></i> Batal
                    </a>
                    <button type="submit" class="btn btn-primary rounded-3 py-2 px-4 fw-semibold shadow-sm">
                        <i class="bi bi-floppy-fill me-1"></i> Simpan Perubahan
                    </button>
                </div>

            </form>

        </div>

    </div>

</div>

<!-- TOGGLE PASSWORD VISIBILITY JS -->
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const togglePassword = document.getElementById('togglePassword');
        const passwordInput = document.getElementById('passwordInput');
        const toggleIcon = document.getElementById('toggleIcon');

        if (togglePassword && passwordInput) {
            togglePassword.addEventListener('click', function () {
                const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
                passwordInput.setAttribute('type', type);
                
                if (type === 'text') {
                    toggleIcon.classList.remove('bi-eye');
                    toggleIcon.classList.add('bi-eye-slash');
                } else {
                    toggleIcon.classList.remove('bi-eye-slash');
                    toggleIcon.classList.add('bi-eye');
                }
            });
        }
    });
</script>

<?= $this->endSection() ?>