<?php
/**
 * Account Settings Page
 */
require_once 'includes/db.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) { 
    header("Location: login"); 
    exit; 
}
$user_id = $_SESSION['user_id'];

$success_msg = '';
$error_msg = '';

// Handle Profile Update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_profile'])) {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $new_password = $_POST['new_password'];

    if (!empty($name) && !empty($email)) {
        if (!empty($new_password)) {
            $hashed_pass = password_hash($new_password, PASSWORD_DEFAULT);
            $stmt = $pdo->prepare("UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?");
            $stmt->execute([$name, $email, $hashed_pass, $user_id]);
        } else {
            $stmt = $pdo->prepare("UPDATE users SET name = ?, email = ? WHERE id = ?");
            $stmt->execute([$name, $email, $user_id]);
        }
        
        // Update session to reflect immediately
        $_SESSION['user_name'] = $name;
        
        $success_msg = "Your profile has been updated successfully!";
    } else {
        $error_msg = "Name and Email cannot be empty.";
    }
}

// Fetch User Data for Profile Header and Form (After update logic to get fresh data)
$u = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$u->execute([$user_id]);
$user = $u->fetch();

$page_title = 'Account Settings';
require_once 'includes/header.php';
?>

<style>
    @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&family=Noto+Serif+Bengali:wght@300;400;500;600;700&display=swap');

    body { 
        background-color: #f8fafc; 
        color: #334155; 
        font-family: 'Roboto', 'Noto Serif Bengali', sans-serif !important;
        margin: 0;
        padding: 0;
    }

    /* Hide the default website header to make it a full page app-like view */
    header, .main-header { 
        display: none !important; 
    }
    
    .account-wrapper { 
        margin: 0 auto 100px;
        max-width: 100%; 
    }

    /* Full Width Top App Bar */
    .app-header {
        display: flex;
        align-items: center;
        gap: 15px;
        background: #00059E;
        color: #ffffff;
        padding: 16px 20px;
        position: sticky;
        top: 0;
        z-index: 999;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }

    .btn-back {
        color: #ffffff;
        display: flex;
        align-items: center;
        justify-content: center;
        text-decoration: none;
    }

    .section-title { 
        font-size: 1.25rem; 
        font-weight: 700; 
        color: #ffffff; 
        margin: 0; 
        display: flex;
        align-items: center;
        gap: 8px;
    }

    /* Main Content Area */
    .main-content {
        padding: 20px;
        max-width: 600px;
        margin: 0 auto;
    }

    .alert {
        padding: 15px; 
        border-radius: 8px; 
        margin-bottom: 25px; 
        font-weight: 600; 
        font-size: 0.95rem;
        display: flex;
        align-items: center;
        gap: 8px;
    }
    .alert-success { background: #ecfdf5; color: #10b981; border: 1px solid #a7f3d0; }
    .alert-danger { background: #fef2f2; color: #ef4444; border: 1px solid #fecaca; }

    /* Form Styles */
    .settings-form-box {
        background: #ffffff; 
        padding: 30px; 
        border-radius: 8px; 
        border: 1px solid #e2e8f0; 
    }
    
    .form-group { margin-bottom: 20px; }
    
    .form-label {
        display: block; 
        font-size: 0.95rem; 
        font-weight: 600; 
        margin-bottom: 8px; 
        color: #0f172a;
    }
    
    .form-control { 
        width: 100%; 
        padding: 12px 15px; 
        border: 1px solid #e2e8f0; 
        border-radius: 6px; 
        font-family: 'Roboto', 'Noto Serif Bengali', sans-serif !important; 
        font-size: 0.95rem; 
        color: #334155;
        outline: none; 
        transition: border-color 0.3s;
        box-sizing: border-box;
    }
    
    .form-control:focus {
        border-color: #00059E;
    }
    
    .form-text {
        display: block; 
        margin-top: 6px; 
        color: #64748b; 
        font-size: 0.85rem;
    }

    .btn-submit {
        background: #00059E;
        color: white;
        padding: 14px 24px;
        border-radius: 6px;
        font-weight: 700;
        border: none;
        cursor: pointer;
        font-family: 'Roboto', 'Noto Serif Bengali', sans-serif !important;
        transition: background 0.3s;
        display: inline-block;
        width: 100%;
        font-size: 1.05rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    
    .btn-submit:hover {
        background: #00037a;
    }

    .divider {
        height: 1px;
        background: #e2e8f0;
        margin: 25px 0;
    }
</style>

<div class="account-wrapper">
    
    <div class="app-header">
        <a href="account" class="btn-back">
            <svg width="24" height="24" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7" /></svg>
        </a>
        <h2 class="section-title">
            Account Settings
        </h2>
    </div>

    <div class="main-content">

        <?php if($success_msg): ?>
            <div class="alert alert-success">
                <svg width="20" height="20" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" /></svg>
                <?php echo $success_msg; ?>
            </div>
        <?php endif; ?>

        <?php if($error_msg): ?>
            <div class="alert alert-danger">
                <svg width="20" height="20" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" /></svg>
                <?php echo $error_msg; ?>
            </div>
        <?php endif; ?>

        <div class="settings-form-box">
            <form action="account_settings" method="POST">
                <input type="hidden" name="update_profile" value="1">
                
                <div class="form-group">
                    <label class="form-label">Full Name</label>
                    <input type="text" name="name" class="form-control" value="<?php echo htmlspecialchars($user['name'] ?? ''); ?>" required>
                </div>
                
                <div class="form-group">
                    <label class="form-label">Email Address</label>
                    <input type="email" name="email" class="form-control" value="<?php echo htmlspecialchars($user['email'] ?? ''); ?>" required>
                </div>
                
                <div class="divider"></div>
                
                <div class="form-group">
                    <label class="form-label">Update Password</label>
                    <input type="password" name="new_password" class="form-control" placeholder="New password">
                    <small class="form-text">Leave blank to keep your current password. Only fill this out if you want to change it.</small>
                </div>
                
                <button type="submit" class="btn-submit">Save Changes</button>
            </form>
        </div>

    </div>
</div>

<?php require_once 'includes/mobile-nav.php'; ?>
