<?php
require_once '../config/db.php';
require_once '../includes/auth.php';
require_once '../includes/functions.php';
requireLogin();

$page_title = 'Profile';
$active_nav = 'profile';
$user = getCurrentUser($conn);
$errors = [];
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $full_name = htmlspecialchars($_POST['full_name'] ?? '', ENT_QUOTES);
    $email     = htmlspecialchars($_POST['email']     ?? '', ENT_QUOTES);
    $phone     = htmlspecialchars($_POST['phone']     ?? '', ENT_QUOTES);
    $new_pass  = $_POST['new_password']    ?? '';
    $confirm   = $_POST['confirm_password']?? '';

    if (empty($full_name)) $errors[] = 'Full name is required';
    if ($new_pass && $new_pass !== $confirm) $errors[] = 'Passwords do not match';
    if ($new_pass && strlen($new_pass) < 6)  $errors[] = 'Password must be at least 8 characters';

    if (empty($errors)) {
        $password_set = $new_pass ? hashPassword($new_pass) : $user['password'] ?? '';
        executeQuery($conn,
            "UPDATE users SET full_name=?, email=?, phone=?" . ($new_pass ? ", password=?" : "") . " WHERE id=?",
            $new_pass
                ? [$full_name, $email, $phone, hashPassword($new_pass), $user['id']]
                : [$full_name, $email, $phone, $user['id']],
            $new_pass ? "ssssi" : "sssi"
        );
        $_SESSION['full_name'] = $full_name;
        $success = 'Profile updated successfully';
        $user = getCurrentUser($conn);
    }
}

include 'header.php';
?>

<?php if ($success): ?>
    <div class="alert alert-success"><i class="fas fa-check-circle"></i> <?php echo htmlspecialchars($success); ?></div>
<?php endif; ?>
<?php if (!empty($errors)): ?>
    <div class="alert alert-danger"><i class="fas fa-exclamation-circle"></i> <?php echo implode('<br>', array_map('htmlspecialchars', $errors)); ?></div>
<?php endif; ?>

<!-- Avatar -->
<div class="text-center mb-3">
    <div style="width:80px;height:80px;border-radius:50%;background:var(--primary);
                display:flex;align-items:center;justify-content:center;
                font-size:30px;font-weight:800;color:#fff;margin:0 auto;">
        <?php echo strtoupper(substr($user['full_name'] ?? 'U', 0, 1)); ?>
    </div>
    <div class="fw-bold mt-2" style="font-size:16px;"><?php echo htmlspecialchars($user['full_name'] ?? ''); ?></div>
    <div><span class="badge badge-primary"><?php echo ucfirst($user['role'] ?? 'user'); ?></span></div>
</div>

<div class="card mb-3">
    <div class="card-header"><i class="fas fa-user-edit"></i> Edit Profile</div>
    <div class="card-body">
        <form method="POST">
            <div class="form-group">
                <label class="form-label">Full Name <span class="required">*</span></label>
                <input type="text" class="form-control" name="full_name" required
                       value="<?php echo htmlspecialchars($user['full_name'] ?? ''); ?>">
            </div>
            <div class="form-group">
                <label class="form-label">Email</label>
                <input type="email" class="form-control" name="email"
                       value="<?php echo htmlspecialchars($user['email'] ?? ''); ?>">
            </div>
            <div class="form-group">
                <label class="form-label">Phone</label>
                <input type="tel" class="form-control" name="phone"
                       value="<?php echo htmlspecialchars($user['phone'] ?? ''); ?>">
            </div>
            <div class="form-group">
                <label class="form-label">New Password <small class="text-muted">(leave blank to keep current)</small></label>
                <input type="password" class="form-control" name="new_password" placeholder="New password">
            </div>
            <div class="form-group">
                <label class="form-label">Confirm Password</label>
                <input type="password" class="form-control" name="confirm_password" placeholder="Confirm password">
            </div>
            <button type="submit" class="btn btn-primary btn-block mt-2"><i class="fas fa-save"></i> Save Changes</button>
        </form>
    </div>
</div>

<!-- Switch to desktop -->
<div class="card mb-3">
    <div class="card-body" style="display:flex;align-items:center;gap:12px;">
        <i class="fas fa-desktop" style="font-size:22px;color:var(--primary);"></i>
        <div style="flex:1;">
            <div class="fw-bold" style="font-size:14px;">Switch to Desktop View</div>
            <div class="text-muted" style="font-size:12px;">Access full admin interface</div>
        </div>
        <a href="/cash_lending/dashboard.php" class="btn btn-outline btn-sm">Switch</a>
    </div>
</div>

<a href="/cash_lending/mobile/logout.php" class="btn btn-block" style="background:#fee2e2;color:#991b1b;border:none;">
    <i class="fas fa-sign-out-alt"></i> Logout
</a>

<?php include 'footer.php'; ?>
