<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
$pageTitle = 'My profile';
$activeNav = 'dashboard';

$uid = (string) ($_SESSION['user_id'] ?? '');
$row = null;
$dbError = null;

if ($uid !== '') {
    $sql = "SELECT u.id, u.employee_number, u.first_name, u.last_name, u.email, u.phone, u.user_type,
                   u.designation, u.status, u.email_verified, u.last_login, u.created_at,
                   r.role_name, r.role_code,
                   o.org_name,
                   d.dept_name
            FROM users u
            INNER JOIN roles r ON u.role_id = r.id
            LEFT JOIN organizations o ON u.org_id = o.id
            LEFT JOIN departments d ON u.dept_id = d.id
            WHERE u.id = ?
            LIMIT 1";
    $st = $conn->prepare($sql);
    if ($st) {
        $st->bind_param('s', $uid);
        $st->execute();
        $res = $st->get_result();
        $row = $res ? $res->fetch_assoc() : null;
        $st->close();
    } else {
        $dbError = $conn->error;
    }
}

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>My profile</h1>
        <p>Account details from the users register.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Account</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="change_password.php"><i class="ri-lock-password-line"></i> Change password</a>
            <a class="qa-btn" href="notifications.php"><i class="ri-notification-3-line"></i> Notifications</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-user-line"></i> Details</h2>
        </div>
        <div class="p-3" style="font-size:.9rem">
            <?php if ($dbError): ?>
                <div class="alert alert-danger" style="font-size:.85rem">Unable to load profile: <?= iscms_h($dbError) ?></div>
            <?php elseif (!$row): ?>
                <p class="text-muted mb-0">Profile not found for the current session.</p>
            <?php else: ?>
            <table class="table table-sm table-borderless mb-0" style="max-width:32rem">
                <tbody>
                    <tr><th class="text-muted" style="width:40%">Name</th><td><?= iscms_h(trim((string) $row['first_name'] . ' ' . (string) $row['last_name'])) ?></td></tr>
                    <tr><th class="text-muted">Email</th><td class="iscms-mono"><?= iscms_h((string) $row['email']) ?></td></tr>
                    <tr><th class="text-muted">Phone</th><td><?= iscms_val($row['phone'] ?? null) ?></td></tr>
                    <tr><th class="text-muted">User type</th><td><?= iscms_h(str_replace('_', ' ', (string) $row['user_type'])) ?></td></tr>
                    <tr><th class="text-muted">Role</th><td><?= iscms_h((string) $row['role_name']) ?> <span class="text-muted">(<?= iscms_h((string) $row['role_code']) ?>)</span></td></tr>
                    <tr><th class="text-muted">Organisation</th><td><?= iscms_val($row['org_name'] ?? null) ?></td></tr>
                    <tr><th class="text-muted">Department</th><td><?= iscms_val($row['dept_name'] ?? null) ?></td></tr>
                    <tr><th class="text-muted">Designation</th><td><?= iscms_val($row['designation'] ?? null) ?></td></tr>
                    <tr><th class="text-muted">Employee no.</th><td><?= iscms_val($row['employee_number'] ?? null) ?></td></tr>
                    <tr><th class="text-muted">Status</th><td><?= iscms_status_badge((string) ($row['status'] ?? '')) ?></td></tr>
                    <tr><th class="text-muted">Email verified</th><td><?= !empty($row['email_verified']) ? 'Yes' : 'No' ?></td></tr>
                    <tr><th class="text-muted">Last login</th><td><small class="text-muted"><?= !empty($row['last_login']) ? iscms_h(substr((string) $row['last_login'], 0, 19)) : '-' ?></small></td></tr>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
