<?php
/**
 * customer-portal.php - Customer Document Signing Portal
 * Allows customers to view, sign, and manage their documents
 */

require_once('config.php');
require_once('Document.php');

session_start();

// Get customer from session or URL token
$customer_email = $_SESSION['customer_email'] ?? $_GET['email'] ?? null;
$auth_token = $_GET['token'] ?? null;

if (!$customer_email) {
    header('Location: ' . BASE_URL . 'customer-login.php');
    exit;
}

// Get all documents for this customer
$document = new Document($pdo);
$documents = $document->getByCustomerEmail($customer_email);

// Organize documents by status
$pending = array_filter($documents, fn($d) => $d['status'] === 'pending_signature');
$signed = array_filter($documents, fn($d) => $d['status'] === 'signed');

// Get sale information if available
$sale_id = $_GET['sale_id'] ?? null;
$sale_info = null;

if ($sale_id) {
    try {
        $stmt = $pdo->prepare("
            SELECT s.*, c.first_name, c.last_name, c.email
            FROM sales s
            LEFT JOIN customers c ON s.customer_id = c.id
            WHERE s.id = :id
        ");
        $stmt->execute([':id' => $sale_id]);
        $sale_info = $stmt->fetch(PDO::FETCH_ASSOC);
    } catch (Exception $e) {
        // Sale not found
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document Signing Portal</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: 800px;
            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;
            text-align: center;
        }

        .header h1 {
            font-size: 28px;
            margin-bottom: 10px;
        }

        .header p {
            opacity: 0.9;
            font-size: 14px;
        }

        .content {
            padding: 30px;
        }

        .sale-info {
            background: #f9f9f9;
            border-left: 4px solid #667eea;
            padding: 15px;
            margin-bottom: 30px;
            border-radius: 4px;
        }

        .sale-info h3 {
            color: #667eea;
            margin-bottom: 10px;
            font-size: 16px;
        }

        .sale-info p {
            margin: 5px 0;
            font-size: 14px;
            color: #555;
        }

        .documents-section {
            margin-bottom: 40px;
        }

        .section-title {
            font-size: 18px;
            color: #333;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #667eea;
            display: flex;
            align-items: center;
        }

        .badge {
            display: inline-block;
            background: #667eea;
            color: white;
            border-radius: 20px;
            padding: 4px 12px;
            font-size: 12px;
            margin-left: 10px;
        }

        .badge.pending {
            background: #ff9800;
        }

        .badge.signed {
            background: #4caf50;
        }

        .document-card {
            background: white;
            border: 1px solid #ddd;
            border-radius: 6px;
            padding: 15px;
            margin-bottom: 12px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            transition: all 0.3s ease;
        }

        .document-card:hover {
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        .document-info {
            flex: 1;
        }

        .document-name {
            font-weight: bold;
            color: #333;
            margin-bottom: 5px;
        }

        .document-type {
            font-size: 12px;
            color: #999;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .document-status {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .status-badge {
            padding: 6px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: bold;
            text-transform: uppercase;
        }

        .status-badge.pending {
            background: #fff3cd;
            color: #856404;
        }

        .status-badge.signed {
            background: #d4edda;
            color: #155724;
        }

        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            font-size: 14px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            transition: all 0.3s ease;
        }

        .btn-primary {
            background: #667eea;
            color: white;
        }

        .btn-primary:hover {
            background: #5568d3;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        }

        .btn-secondary {
            background: #f0f0f0;
            color: #333;
        }

        .btn-secondary:hover {
            background: #e0e0e0;
        }

        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: #999;
        }

        .empty-state p {
            font-size: 16px;
            margin-bottom: 10px;
        }

        .progress-container {
            margin-bottom: 20px;
        }

        .progress-label {
            font-size: 14px;
            color: #333;
            margin-bottom: 8px;
            display: flex;
            justify-content: space-between;
        }

        .progress-bar {
            height: 10px;
            background: #e0e0e0;
            border-radius: 10px;
            overflow: hidden;
        }

        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #667eea, #764ba2);
            transition: width 0.3s ease;
        }

        .footer {
            background: #f9f9f9;
            padding: 15px 30px;
            text-align: center;
            color: #999;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📋 Your Documents</h1>
            <p>Sign your documents securely and quickly</p>
        </div>

        <div class="content">
            <?php if ($sale_info): ?>
            <div class="sale-info">
                <h3>Vehicle Sale Information</h3>
                <p><strong>Vehicle:</strong> <?php echo htmlspecialchars($sale_info['vehicle_year'] . ' ' . $sale_info['vehicle_make'] . ' ' . $sale_info['vehicle_model']); ?></p>
                <p><strong>Registration:</strong> <?php echo htmlspecialchars($sale_info['vehicle_registration']); ?></p>
                <p><strong>Sale Type:</strong> <?php echo ucwords(str_replace('_', ' ', $sale_info['sale_type'])); ?></p>
                <?php if ($sale_info['sale_price']): ?>
                <p><strong>Price:</strong> £<?php echo number_format($sale_info['sale_price'], 2); ?></p>
                <?php endif; ?>
            </div>

            <?php if (count($pending) > 0 || count($signed) > 0): ?>
            <div class="progress-container">
                <div class="progress-label">
                    <span>Documents Completion</span>
                    <span><?php echo count($signed); ?>/<?php echo count($pending) + count($signed); ?> Signed</span>
                </div>
                <div class="progress-bar">
                    <div class="progress-fill" style="width: <?php echo count($signed) > 0 ? round((count($signed) / (count($pending) + count($signed))) * 100) : 0; ?>%"></div>
                </div>
            </div>
            <?php endif; ?>
            <?php endif; ?>

            <!-- Pending Documents -->
            <?php if (count($pending) > 0): ?>
            <div class="documents-section">
                <div class="section-title">
                    Awaiting Your Signature
                    <span class="badge pending"><?php echo count($pending); ?></span>
                </div>

                <?php foreach ($pending as $doc): ?>
                <div class="document-card">
                    <div class="document-info">
                        <div class="document-name"><?php echo htmlspecialchars($doc['document_type']); ?></div>
                        <div class="document-type">Created: <?php echo date('d/m/Y', strtotime($doc['created_at'])); ?></div>
                    </div>
                    <div class="document-status">
                        <span class="status-badge pending">⏳ Pending</span>
                        <a href="sign-document.php?id=<?php echo $doc['id']; ?>" class="btn btn-primary">Sign Now</a>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>

            <!-- Signed Documents -->
            <?php if (count($signed) > 0): ?>
            <div class="documents-section">
                <div class="section-title">
                    Signed Documents
                    <span class="badge signed"><?php echo count($signed); ?></span>
                </div>

                <?php foreach ($signed as $doc): ?>
                <div class="document-card">
                    <div class="document-info">
                        <div class="document-name"><?php echo htmlspecialchars($doc['document_type']); ?></div>
                        <div class="document-type">Signed: <?php echo date('d/m/Y H:i', strtotime($doc['signed_at'])); ?></div>
                    </div>
                    <div class="document-status">
                        <span class="status-badge signed">✓ Signed</span>
                        <?php if ($doc['pdf_path']): ?>
                        <a href="download-pdf.php?id=<?php echo $doc['id']; ?>" class="btn btn-secondary">Download PDF</a>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>

            <!-- Empty State -->
            <?php if (count($documents) === 0): ?>
            <div class="empty-state">
                <p>No documents found.</p>
                <p>Once documents are ready for signature, they'll appear here.</p>
            </div>
            <?php endif; ?>
        </div>

        <div class="footer">
            <p>Your documents are secure and stored safely. Questions? Contact your garage.</p>
        </div>
    </div>
</body>
</html>
