<?= $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, .profile-workspace {
        background-color: var(--bg-body) !important;
        color: var(--text-main) !important;
        transition: var(--transition);
    }

    .profile-card-container {
        max-width: 860px;
        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);
    }

    /* Hero Profile Section */
    .profile-hero-header {
        background: var(--bg-accent);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-md);
        padding: 1.75rem;
    }

    .avatar-hero-lg {
        width: 72px;
        height: 72px;
        border-radius: 50%;
        background: var(--primary-light);
        color: var(--primary);
        font-weight: 800;
        font-size: 1.85rem;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
        box-shadow: inset 0 0 0 2px var(--primary);
    }

    /* Code & Pill Elements */
    .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.85rem;
        font-weight: 600;
        padding: 0.25rem 0.65rem;
        border-radius: var(--radius-sm);
    }

    /* Property Cards Grid */
    .property-card {
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-md);
        padding: 1.15rem 1.25rem;
        transition: var(--transition);
        height: 100%;
    }

    .property-card:hover {
        border-color: var(--border-hover);
        background: var(--bg-accent);
    }

    .property-label {
        font-size: 0.72rem;
        color: var(--text-sub);
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.06em;
        margin-bottom: 0.4rem;
        display: flex;
        align-items: center;
        gap: 0.4rem;
    }

    .property-value {
        font-size: 0.95rem;
        font-weight: 600;
        color: var(--text-main);
        word-break: break-word;
    }

    /* Pulse Dot Active Indicator */
    .pulse-dot {
        width: 8px;
        height: 8px;
        background-color: var(--success);
        border-radius: 50%;
        display: inline-block;
        box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7);
        animation: pulse 1.6s infinite;
    }

    @keyframes pulse {
        0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7); }
        70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(16, 185, 129, 0); }
        100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); }
    }
</style>

<div class="container-fluid py-4 profile-workspace">
    
    <div class="profile-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">Profil Saya</h3>
                <p class="text-sub small mb-0 mt-0.5">Kelola data informasi akun, peranan akses, dan keamanan kredensial Anda.</p>
            </div>
            <div>
                <button onclick="window.location.reload()" 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-clockwise"></i> Refresh
                </button>
            </div>
        </div>

        <!-- FLASH SUCCESS ALERT -->
        <?php if (session()->getFlashdata('success')) : ?>
            <div class="alert alert-success alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4" role="alert">
                <div class="d-flex align-items-center gap-2">
                    <i class="bi bi-check-circle-fill fs-5 text-success"></i>
                    <div><?= session()->getFlashdata('success') ?></div>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php endif; ?>

        <!-- FLASH ERROR ALERT -->
        <?php if (session()->getFlashdata('error')) : ?>
            <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-center gap-2">
                    <i class="bi bi-exclamation-triangle-fill fs-5 text-danger"></i>
                    <div><?= session()->getFlashdata('error') ?></div>
                </div>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php endif; ?>

        <?php if (isset($user) && !empty($user)) : ?>
            <?php 
                $namaLengkap = $user['nama_lengkap'] ?? $user['username'] ?? 'User';
                $initials = strtoupper(substr(trim($namaLengkap), 0, 1));
                $isActive = isset($user['is_active']) && $user['is_active'] == 1;
                $roleName = session()->get('id_role') == 1 ? 'Superadmin' : ($user['nama_role'] ?? 'User');
            ?>

            <!-- MAIN PROFILE CARD -->
            <div class="card-modern">
                
                <!-- HERO PROFILE HEADER -->
                <div class="profile-hero-header mb-4">
                    <div class="d-flex flex-column flex-sm-row align-items-sm-center justify-content-between gap-3">
                        <div class="d-flex align-items-center gap-3">
                            <div class="avatar-hero-lg"><?= $initials ?></div>
                            <div>
                                <h4 class="fw-bold text-main mb-1"><?= esc($namaLengkap) ?></h4>
                                <div class="d-flex flex-wrap align-items-center gap-2">
                                    <span class="code-pill text-primary">
                                        <i class="bi bi-at"></i><?= esc($user['username'] ?? '-') ?>
                                    </span>
                                    <span class="badge bg-primary-subtle text-primary border border-primary-subtle rounded-pill px-2.5 py-1 fw-semibold">
                                        <i class="bi bi-shield-check me-1"></i><?= esc($roleName) ?>
                                    </span>
                                </div>
                            </div>
                        </div>

                        <!-- STATUS BADGE -->
                        <div class="align-self-start align-self-sm-center">
                            <?php if ($isActive) : ?>
                                <span class="badge bg-success-subtle text-success border border-success-subtle rounded-pill px-3 py-1.5 fw-semibold d-inline-flex align-items-center gap-2">
                                    <span class="pulse-dot"></span> Akun Aktif
                                </span>
                            <?php else : ?>
                                <span class="badge bg-danger-subtle text-danger border border-danger-subtle rounded-pill px-3 py-1.5 fw-semibold d-inline-flex align-items-center gap-1.5">
                                    <i class="bi bi-x-circle"></i> Nonaktif
                                </span>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <!-- PROPERTY GRID -->
                <div class="row g-3 mb-4">
                    
                    <!-- Username -->
                    <div class="col-12 col-sm-6">
                        <div class="property-card">
                            <div class="property-label">
                                <i class="bi bi-person-badge text-primary"></i> Username
                            </div>
                            <div class="property-value font-monospace text-primary">
                                @<?= esc($user['username'] ?? '-') ?>
                            </div>
                        </div>
                    </div>

                    <!-- Email -->
                    <div class="col-12 col-sm-6">
                        <div class="property-card">
                            <div class="property-label">
                                <i class="bi bi-envelope text-primary"></i> Alamat Email
                            </div>
                            <div class="property-value">
                                <?= esc($user['email'] ?? '-') ?>
                            </div>
                        </div>
                    </div>

                    <!-- Nama Lengkap -->
                    <div class="col-12 col-sm-6">
                        <div class="property-card">
                            <div class="property-label">
                                <i class="bi bi-card-heading text-primary"></i> Nama Lengkap
                            </div>
                            <div class="property-value">
                                <?= esc($user['nama_lengkap'] ?? '-') ?>
                            </div>
                        </div>
                    </div>

                    <!-- Peran / Role -->
                    <div class="col-12 col-sm-6">
                        <div class="property-card">
                            <div class="property-label">
                                <i class="bi bi-shield-lock text-primary"></i> Peran System (Role)
                            </div>
                            <div class="property-value">
                                <?= esc($roleName) ?>
                            </div>
                        </div>
                    </div>

                </div>

                <!-- ACTION BUTTONS TOOLBAR -->
                <div class="d-flex flex-wrap align-items-center gap-2 pt-3 border-top border-color">
                    <a href="<?= base_url('superadmin/profile/edit') ?>" class="btn btn-warning text-dark rounded-3 px-4 py-2 fw-semibold d-inline-flex align-items-center gap-2 shadow-sm fs-7">
                        <i class="bi bi-pencil-square fs-6"></i> Edit Profil
                    </a>
                    <a href="<?= base_url('superadmin/profile/password') ?>" class="btn btn-outline-danger rounded-3 px-4 py-2 fw-semibold d-inline-flex align-items-center gap-2 fs-7">
                        <i class="bi bi-key-fill fs-6"></i> Ganti Password
                    </a>
                </div>

            </div>
        <?php else : ?>
            <!-- EMPTY STATE CARD -->
            <div class="card-modern py-5 text-center">
                <div class="py-4">
                    <i class="bi bi-person-x fs-1 text-sub opacity-50 d-block mb-2"></i>
                    <h6 class="fw-semibold text-main mb-1">Data Profil Tidak Ditemukan</h6>
                    <p class="text-sub small mb-0">Sistem gagal memuat data profil akun yang sedang login saat ini.</p>
                </div>
            </div>
        <?php endif; ?>

    </div>

</div>

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