<?php
/**
 * Registered Landowners
 * List of all landowners in the village
 */

session_start();
require_once '../db_connect.php';

$allowed_roles = ['village_chairman', 'land_officer', 'surveyor', 'district_officer', 'land_council'];
if(!isset($_SESSION['user_id']) || !in_array($_SESSION['role'], $allowed_roles)) {
    header('Location: ../login');
    exit;
}

$user_role = $_SESSION['role'];
$village_id = $_SESSION['village_id'] ?? null;

$where = "1=1";
if($user_role !== 'district_officer' && $village_id) {
    $where .= " AND l.village_id = $village_id";
}

$search = isset($_GET['search']) ? mysqli_real_escape_string($conn, $_GET['search']) : '';
if($search) {
    $where .= " AND (u.full_name LIKE '%$search%' OR u.phone LIKE '%$search%')";
}

// Get landowners with their land counts
$owners = mysqli_query($conn, "
    SELECT u.id, u.full_name, u.phone, u.email,
           COUNT(l.id) as land_count,
           SUM(l.total_area) as total_area,
           MAX(l.created_at) as last_registration
    FROM users u
    JOIN lands l ON u.id = l.user_id
    WHERE $where
    GROUP BY u.id
    ORDER BY land_count DESC, u.full_name
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Landowners - Land Secure CBC</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../assets/font-awesome/css/all.min.css">
    <link href="../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .admin-main { margin-left: 280px; padding: 24px; min-height: 100vh; background: #f0f2f5; }
        @media (max-width: 1024px) { .admin-main { margin-left: 0; padding-top: 90px; } }

        .page-header { margin-bottom: 24px; }
        .page-header h1 { font-size: 24px; font-weight: 700; color: #1a1d21; display: flex; align-items: center; gap: 12px; }
        .page-header h1 i { color: #3b82f6; }

        .search-box { display: flex; gap: 12px; margin-bottom: 24px; }
        .search-input { flex: 1; max-width: 400px; padding: 12px 16px; border: 2px solid #e2e8f0; border-radius: 10px; }
        .search-btn { padding: 12px 24px; background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); color: white; border: none; border-radius: 10px; font-weight: 600; cursor: pointer; }

        .owners-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }

        .owner-card { background: white; border-radius: 16px; padding: 24px; box-shadow: 0 4px 20px rgba(0,0,0,0.05); }
        .owner-header { display: flex; align-items: center; gap: 16px; margin-bottom: 16px; }
        .owner-avatar { width: 56px; height: 56px; border-radius: 14px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; align-items: center; justify-content: center; color: white; font-size: 24px; font-weight: 700; }
        .owner-info h3 { font-size: 16px; font-weight: 600; color: #1a1d21; margin: 0 0 4px; }
        .owner-info p { font-size: 13px; color: #64748b; margin: 0; }

        .owner-stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; padding-top: 16px; border-top: 1px solid #f0f2f5; }
        .stat { text-align: center; }
        .stat .value { font-size: 20px; font-weight: 700; color: #1a1d21; }
        .stat .label { font-size: 11px; color: #64748b; text-transform: uppercase; }

        .owner-actions { margin-top: 16px; }
        .btn-view { display: block; text-align: center; padding: 10px; background: #f1f5f9; color: #64748b; border-radius: 8px; text-decoration: none; font-weight: 600; font-size: 13px; }
        .btn-view:hover { background: #11998e; color: white; }

        .empty-state { text-align: center; padding: 60px; background: white; border-radius: 16px; color: #64748b; }
    </style>
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>

    <main class="admin-main">
        <div class="page-header">
            <h1><i class="fas fa-users"></i> Registered Landowners</h1>
            <p style="color: #64748b; margin: 8px 0 0;">Wamiliki wa Ardhi / People with registered land</p>
        </div>

        <form method="GET" class="search-box">
            <input type="text" name="search" class="search-input" placeholder="Search by name or phone..." value="<?php echo htmlspecialchars($search); ?>">
            <button type="submit" class="search-btn"><i class="fas fa-search"></i> Search</button>
        </form>

        <?php if(mysqli_num_rows($owners) > 0): ?>
        <div class="owners-grid">
            <?php while($owner = mysqli_fetch_assoc($owners)): ?>
            <div class="owner-card">
                <div class="owner-header">
                    <div class="owner-avatar"><?php echo strtoupper(substr($owner['full_name'], 0, 1)); ?></div>
                    <div class="owner-info">
                        <h3><?php echo htmlspecialchars($owner['full_name']); ?></h3>
                        <p><i class="fas fa-phone"></i> <?php echo htmlspecialchars($owner['phone']); ?></p>
                    </div>
                </div>
                <div class="owner-stats">
                    <div class="stat">
                        <div class="value"><?php echo $owner['land_count']; ?></div>
                        <div class="label">Lands</div>
                    </div>
                    <div class="stat">
                        <div class="value"><?php echo number_format($owner['total_area'] ?? 0, 2); ?></div>
                        <div class="label">Total Acres</div>
                    </div>
                </div>
                <div class="owner-actions">
                    <a href="all_lands?search=<?php echo urlencode($owner['full_name']); ?>" class="btn-view">
                        <i class="fas fa-map"></i> View Lands
                    </a>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
        <?php else: ?>
        <div class="empty-state">
            <i class="fas fa-users" style="font-size: 48px; margin-bottom: 16px;"></i>
            <p>No landowners found</p>
        </div>
        <?php endif; ?>
    </main>
</body>
</html>
