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

if (!is_logged_in()) {
    redirect(BASE_URL . 'login.php');
}

$user = get_current_user();
if (!$user || !is_array($user)) {
    $user = ['id' => $_SESSION['user_id'] ?? null, 'username' => $_SESSION['username'] ?? 'User', 'garage_name' => 'My Garage'];
}

$documents = [];
try {
    global $pdo;
    $stmt = $pdo->prepare("SELECT id, filename, uploaded_by, created_at, status FROM documents WHERE user_id = ? ORDER BY created_at DESC");
    $stmt->execute([$user['id']]);
    $documents = $stmt->fetchAll();
} catch (Exception $e) {
    $error = "Could not load documents";
}

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>Documents - 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;
        }
        .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;
        }
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .header h1 {
            font-size: 28px;
            margin: 0;
        }
        .user-info {
            text-align: right;
        }
        .user-info p {
            margin: 5px 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: 10px;
            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, .nav-menu button {
            color: #667eea;
            text-decoration: none;
            font-weight: 600;
            padding: 10px 15px;
            border: none;
            background: none;
            cursor: pointer;
            border-bottom: 2px solid transparent;
        }
        .nav-menu a:hover, .nav-menu a.active {
            border-bottom-color: #667eea;
        }
        .content {
            padding: 40px;
        }
        .action-buttons {
            margin-bottom: 30px;
        }
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 5px;
            cursor: pointer;
            font-weight: 600;
            text-decoration: none;
            display: inline-block;
            margin-right: 10px;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
        }
        .documents-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        .documents-table th {
            background: #f8f9fa;
            padding: 15px;
            text-align: left;
            font-weight: 600;
            color: #333;
            border-bottom: 2px solid #ddd;
        }
        .documents-table td {
            padding: 15px;
            border-bottom: 1px solid #ddd;
        }
        .documents-table tr:hover {
            background: #f8f9fa;
        }
        .status-badge {
            padding: 5px 10px;
            border-radius: 3px;
            font-size: 12px;
            font-weight: 600;
        }
        .status-pending {
            background: #fff3cd;
            color: #856404;
        }
        .status-signed {
            background: #d4edda;
            color: #155724;
        }
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #999;
        }
        .empty-state h3 {
            margin-bottom: 10px;
            color: #333;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📄 Documents</h1>
            <div class="user-info">
                <p><strong><?php echo htmlspecialchars($user['garage_name']); ?></strong></p>
                <p><?php echo htmlspecialchars($user['username']); ?></p>
                <form method="POST" style="display: inline;">
                    <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">Dashboard</a>
            <a href="<?php echo BASE_URL; ?>documents.php" class="active">Documents</a>
            <a href="<?php echo BASE_URL; ?>upload.php">Upload Document</a>
        </div>

        <div class="content">
            <h2>Your Documents</h2>

            <div class="action-buttons">
                <a href="<?php echo BASE_URL; ?>upload.php" class="btn">📤 Upload New Document</a>
            </div>

            <?php if (count($documents) > 0): ?>
                <table class="documents-table">
                    <thead>
                        <tr>
                            <th>Filename</th>
                            <th>Uploaded By</th>
                            <th>Date</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($documents as $doc): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($doc['filename']); ?></td>
                                <td><?php echo htmlspecialchars($doc['uploaded_by']); ?></td>
                                <td><?php echo date('M d, Y', strtotime($doc['created_at'])); ?></td>
                                <td>
                                    <span class="status-badge status-<?php echo htmlspecialchars($doc['status']); ?>">
                                        <?php echo ucfirst(htmlspecialchars($doc['status'])); ?>
                                    </span>
                                </td>
                                <td>
                                    <a href="<?php echo BASE_URL; ?>view-document.php?id=<?php echo $doc['id']; ?>" style="color: #667eea; text-decoration: none;">View</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <div class="empty-state">
                    <h3>No Documents Yet</h3>
                    <p>Start by uploading a document for signing.</p>
                    <a href="<?php echo BASE_URL; ?>upload.php" class="btn" style="margin-top: 20px;">Upload Your First Document</a>
                </div>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>