<?php
/**
 * IBVTE Admin - Profile Settings
 */
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

if (!checkDBConnection()) exit;
Auth()->requireRole(['super_admin', 'admin']);

$db = DB();
$user = Auth()->getUser();
$flash = getFlashMessage();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // CSRF Protection
    if (!Auth()->verifyCSRFToken($_POST['csrf_token'] ?? '')) {
        redirectWithMessage('profile.php', 'Security session expired. Please try again.', 'danger');
    }

    $fullName = cleanInput($_POST['full_name'] ?? '');
    $email = cleanInput($_POST['email'] ?? '');
    $mobile = cleanInput($_POST['mobile'] ?? '');
    
    // Simple update for now
    $db->update('users', [
        'full_name' => $fullName,
        'email' => $email,
        'mobile' => $mobile,
        'updated_at' => date('Y-m-d H:i:s')
    ], ['id' => $user['id']]);

    // Handle password update
    if (!empty($_POST['new_password'])) {
        $password = password_hash($_POST['new_password'], PASSWORD_DEFAULT);
        $db->update('users', ['password' => $password], ['id' => $user['id']]);
    }

    redirectWithMessage('profile.php', 'Profile updated successfully.', 'success');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Profile - <?= APP_SHORT_NAME ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/admin-style.css" rel="stylesheet">
</head>
<body>
    <?php include __DIR__ . '/../includes/admin_header.php'; ?>
    <div class="container-fluid">
        <div class="row">
            <?php include __DIR__ . '/../includes/admin_sidebar.php'; ?>
            
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 py-4">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2 class="h3 fw-bold"><i class="fas fa-user-circle me-2 text-primary"></i>My Profile</h2>
                </div>

                <?php if ($flash): ?>
                    <div class="alert alert-<?= $flash['type'] ?> alert-dismissible fade show rounded-3 shadow-sm">
                        <?= htmlspecialchars($flash['message']) ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                <?php endif; ?>

                <div class="row">
                    <div class="col-md-4 mb-4">
                        <div class="card border-0 shadow-sm text-center p-4 rounded-4">
                            <div class="mb-3">
                                <div class="bg-primary text-white rounded-circle d-inline-flex align-items-center justify-content-center" style="width: 100px; height: 100px; font-size: 40px;">
                                    <?= strtoupper(substr($user['full_name'], 0, 1)) ?>
                                </div>
                            </div>
                            <h4 class="fw-bold mb-1"><?= htmlspecialchars($user['full_name']) ?></h4>
                            <p class="text-muted small mb-3"><?= strtoupper($user['role_slug'] ?? 'ADMIN') ?></p>
                            <div class="d-grid">
                                <button class="btn btn-outline-primary btn-sm">Change Photo</button>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-8">
                        <div class="card border-0 shadow-sm rounded-4">
                            <div class="card-body p-4">
                                <h5 class="fw-bold mb-4">Personal Information</h5>
                                <form method="POST">
                                    <input type="hidden" name="csrf_token" value="<?= Auth()->generateCSRFToken() ?>">
                                    <div class="row g-3">
                                        <div class="col-md-12">
                                            <label class="form-label fw-bold small">Full Name</label>
                                            <input type="text" name="full_name" class="form-control" value="<?= htmlspecialchars($user['full_name']) ?>" required>
                                        </div>
                                        <div class="col-md-6">
                                            <label class="form-label fw-bold small">Email Address</label>
                                            <input type="email" name="email" class="form-control" value="<?= htmlspecialchars($user['email']) ?>" required>
                                        </div>
                                        <div class="col-md-6">
                                            <label class="form-label fw-bold small">Mobile Number</label>
                                            <input type="text" name="mobile" class="form-control" value="<?= htmlspecialchars($user['mobile']) ?>">
                                        </div>
                                        
                                        <div class="col-12 mt-4">
                                            <h5 class="fw-bold mb-3">Security</h5>
                                        </div>
                                        <div class="col-md-6">
                                            <label class="form-label fw-bold small">New Password</label>
                                            <input type="password" name="new_password" class="form-control" placeholder="Leave blank to keep current">
                                        </div>
                                        <div class="col-md-6">
                                            <label class="form-label fw-bold small">Confirm Password</label>
                                            <input type="password" name="confirm_password" class="form-control" placeholder="Leave blank to keep current">
                                        </div>
                                        
                                        <div class="col-12 mt-4">
                                            <button type="submit" class="btn btn-primary px-4 fw-bold">Save Changes</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
