<?php
session_start();
require_once('config.php');

// Redirect if not logged in
if (!is_logged_in()) {
    redirect(BASE_URL . 'login.php');
}

// Get user data
$user_id = $_SESSION['user_id'] ?? null;
$username = $_SESSION['username'] ?? 'User';
$role = $_SESSION['role'] ?? 'admin';

// Try to get full user details from database
$user = null;
if ($user_id) {
    try {
        global $pdo;
        $stmt = $pdo->prepare("SELECT id, username, email, garage_name, role FROM users WHERE id = ?");
        $stmt->execute([$user_id]);
        $user = $stmt->fetch();
    } catch (Exception $e) {
        // If database fails, use session data
        $user = [
            'id' => $user_id,
            'username' => $username,
            'email' => 'User Email',
            'garage_name' => 'My Garage',
            'role' => $role
        ];
    }
}

// Fallback if no user
if (!$user || !is_array($user)) {
    $user = [
        'id' => $user_id,
        'username' => $username,
        'email' => 'User Email',
        'garage_name' => 'My Garage',
        'role' => $role
    ];
}

// Handle logout
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'logout') {
    session_destroy();
    redirect(BASE_URL . 'login.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - Document Signing System</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        .dashboard-container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
            overflow: hidden;
        }
        .dashboard-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .dashboard-header h1 {
            font-size: 28px;
            margin: 0;
        }
        .user-info {
            text-align: right;
        }
        .user-info p {
            margin: 8px 0;
            font-size: 14px;
        }
        .logout-btn {
            background: #ff6b6b;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-weight: 600;
            margin-top: 15px;
            font-size: 14px;
        }
        .logout-btn:hover {
            background: #ff5252;
        }
        .nav-menu {
            background: #f8f9fa;
            padding: 15px 30px;
            border-bottom: 1px solid #ddd;
            display: flex;
            gap: 20px;
        }
        .nav-menu a {
            color: #667eea;
            text-decoration: none;
            font-weight: 600;
            padding: 10px 15px;
            border-bottom: 2px solid transparent;
            transition: all 0.3s;
        }
        .nav-menu a:hover, .nav-menu a.active {
            border-bottom-color: #667eea;
        }
        .dashboard-content {
            padding: 40px;
        }
        .welcome-box {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
            padding: 20px;
            border-radius: 5px;
            margin-bottom: 30px;
        }
        .welcome-box h2 {
            margin-bottom: 10px;
            font-size: 24px;
        }
        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-top: 30px;
        }
        .info-card {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            border-left: 4px solid #667eea;
        }
        .info-card label {
            display: block;
            color: #999;
            font-size: 12px;
            text-transform: uppercase;
            margin-bottom: 5px;
        }
        .info-card p {
            color: #333;
            font-size: 18px;
            font-weight: 600;
        }
    </style>
</head>
<body>
    <div class="dashboard-container">
        <div class="dashboard-header">
            <h1>📋 Dashboard</h1>
            <div class="user-info">
                <p><strong><?php echo htmlspecialchars($user['garage_name'] ?? 'My Garage'); ?></strong></p>
                <p><?php echo htmlspecialchars($user['username'] ?? 'User'); ?></p>
                <form method="POST">
                    <input type="hidden" name="action" value="logout">
                    <button type="submit" class="logout-btn">🚪 Logout</button>
                </form>
            </div>
        </div>

        <div class="nav-menu">
            <a href="<?php echo BASE_URL; ?>admin-dashboard.php" class="active">Dashboard</a>
            <a href="<?php echo BASE_URL; ?>documents.php">Documents</a>
            <a href="<?php echo BASE_URL; ?>upload.php">Upload Document</a>
        </div>

        <div class="dashboard-content">
            <div class="welcome-box">
                <h2>✅ Welcome, <?php echo htmlspecialchars($user['garage_name'] ?? 'User'); ?>!</h2>
                <p>You are successfully logged in and ready to use the document signing system.</p>
            </div>

            <h3 style="color: #333; margin-bottom: 20px;">Your Account Information</h3>

            <div class="info-grid">
                <div class="info-card">
                    <label>Garage Name</label>
                    <p><?php echo htmlspecialchars($user['garage_name'] ?? 'N/A'); ?></p>
                </div>
                <div class="info-card">
                    <label>Username</label>
                    <p><?php echo htmlspecialchars($user['username'] ?? 'N/A'); ?></p>
                </div>
                <div class="info-card">
                    <label>Email</label>
                    <p><?php echo htmlspecialchars($user['email'] ?? 'N/A'); ?></p>
                </div>
                <div class="info-card">
                    <label>Role</label>
                    <p><?php echo htmlspecialchars($user['role'] ?? 'N/A'); ?></p>
                </div>
            </div>
        </div>
    </div>
</body>
</html>