<?php
session_start();
include 'layouts/dbconn.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$user_id = $_SESSION['user_id'];
$sale_id = $_GET['sale_id'] ?? '';

$sql = "SELECT s.*, u.unit_number, p.name as property_name
        FROM unit_sales s
        JOIN units u ON s.unit_id = u.id
        JOIN properties p ON u.property_id = p.id
        WHERE s.buyer_id = ?";

$params = [$user_id];
$types = 'i';

if (!empty($sale_id)) {
    $sql .= " AND s.id = ?";
    $params[] = $sale_id;
    $types .= 'i';
}

$sql .= " ORDER BY s.created_at DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'My Documents')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <?php includeFileWithVariables('layouts/page-title.php', array('pagetitle' => 'Documents', 'title' => 'My Documents')); ?>

                    <div class="row">
                        <?php if ($result && $result->num_rows > 0): ?>
                            <?php while($sale = $result->fetch_assoc()): ?>
                                <div class="col-12">
                                    <div class="card">
                                        <div class="card-header">
                                            <h4 class="card-title mb-0">
                                                <?php echo htmlspecialchars($sale['property_name'] . ' - Unit ' . $sale['unit_number']); ?>
                                            </h4>
                                        </div>
                                        <div class="card-body">
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <h6>Sale Documents</h6>
                                                    <ul class="list-unstyled">
                                                        <?php if ($sale['offer_letter_date']): ?>
                                                            <li class="mb-2">
                                                                <i class="ri-file-pdf-line text-danger me-2"></i>
                                                                <a href="#" class="text-decoration-none">Offer Letter</a>
                                                                <small class="text-muted">(<?php echo date('M d, Y', strtotime($sale['offer_letter_date'])); ?>)</small>
                                                            </li>
                                                        <?php endif; ?>
                                                        <?php if ($sale['sale_agreement_date']): ?>
                                                            <li class="mb-2">
                                                                <i class="ri-file-pdf-line text-danger me-2"></i>
                                                                <a href="#" class="text-decoration-none">Sale Agreement</a>
                                                                <small class="text-muted">(<?php echo date('M d, Y', strtotime($sale['sale_agreement_date'])); ?>)</small>
                                                            </li>
                                                        <?php endif; ?>
                                                        <?php if ($sale['completion_date']): ?>
                                                            <li class="mb-2">
                                                                <i class="ri-file-pdf-line text-danger me-2"></i>
                                                                <a href="#" class="text-decoration-none">Completion Certificate</a>
                                                                <small class="text-muted">(<?php echo date('M d, Y', strtotime($sale['completion_date'])); ?>)</small>
                                                            </li>
                                                        <?php endif; ?>
                                                    </ul>
                                                </div>
                                                <div class="col-md-6">
                                                    <h6>Payment Receipts</h6>
                                                    <a href="my_payment_history.php?sale_id=<?php echo $sale['id']; ?>" 
                                                       class="btn btn-info btn-sm">
                                                        <i class="ri-receipt-line me-1"></i>View Payment Receipts
                                                    </a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <div class="col-12">
                                <div class="card">
                                    <div class="card-body text-center py-5">
                                        <i class="ri-file-line fs-48 text-muted mb-3"></i>
                                        <h5>No Documents</h5>
                                        <p class="text-muted">No documents available at the moment.</p>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
