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

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

$user_id = $_SESSION['user_id'];

$sql = "SELECT p.*, 
               COUNT(DISTINCT u.id) as total_units,
               COUNT(DISTINCT CASE WHEN u.status = 'occupied' THEN u.id END) as occupied_units,
               COUNT(DISTINCT CASE WHEN u.status = 'vacant' THEN u.id END) as vacant_units
        FROM properties p
        LEFT JOIN units u ON p.id = u.property_id
        WHERE p.owner_id = ?
        GROUP BY p.id
        ORDER BY p.name";

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'My Properties')); ?>
    <?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' => 'My Investment', 'title' => 'My Properties')); ?>

                    <div class="row">
                        <?php if ($result && $result->num_rows > 0): ?>
                            <?php while($property = $result->fetch_assoc()): 
                                $occupancy_rate = $property['total_units'] > 0 
                                    ? round(($property['occupied_units'] / $property['total_units']) * 100, 1) 
                                    : 0;
                            ?>
                                <div class="col-xl-4 col-md-6">
                                    <div class="card">
                                        <div class="card-body">
                                            <h5 class="card-title"><?php echo htmlspecialchars($property['name']); ?></h5>
                                            <p class="text-muted mb-2">
                                                <i class="ri-map-pin-line me-1"></i>
                                                <?php echo htmlspecialchars($property['location_area']); ?>
                                            </p>
                                            <p class="text-muted mb-3">
                                                <i class="ri-building-line me-1"></i>
                                                <?php echo htmlspecialchars($property['type']); ?>
                                            </p>
                                            
                                            <div class="row mb-3">
                                                <div class="col-6">
                                                    <div class="text-center p-2 bg-soft-primary rounded">
                                                        <h4 class="mb-0"><?php echo $property['total_units']; ?></h4>
                                                        <small class="text-muted">Total Units</small>
                                                    </div>
                                                </div>
                                                <div class="col-6">
                                                    <div class="text-center p-2 bg-soft-success rounded">
                                                        <h4 class="mb-0"><?php echo $occupancy_rate; ?>%</h4>
                                                        <small class="text-muted">Occupancy</small>
                                                    </div>
                                                </div>
                                            </div>
                                            
                                            <div class="d-flex gap-2">
                                                <a href="property_details.php?id=<?php echo $property['id']; ?>" 
                                                   class="btn btn-primary btn-sm flex-fill">
                                                    <i class="ri-eye-line me-1"></i>View Details
                                                </a>
                                                <a href="my_rental_income.php?property_id=<?php echo $property['id']; ?>" 
                                                   class="btn btn-success btn-sm flex-fill">
                                                    <i class="ri-money-dollar-circle-line me-1"></i>Income
                                                </a>
                                            </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-building-line fs-48 text-muted mb-3"></i>
                                        <h5>No Properties</h5>
                                        <p class="text-muted">You don't have any properties registered yet.</p>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>
                    </div>

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

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