<?php
/**
 * All Lands in Village
 * Complete list of all registered lands
 */

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;

// Filters
$status_filter = isset($_GET['status']) ? mysqli_real_escape_string($conn, $_GET['status']) : '';
$search = isset($_GET['search']) ? mysqli_real_escape_string($conn, $_GET['search']) : '';

// Build WHERE clause
$where = "1=1";
if($user_role !== 'district_officer' && $village_id) {
    $where .= " AND l.village_id = $village_id";
}
if($status_filter) {
    $where .= " AND l.status = '$status_filter'";
}
if($search) {
    $where .= " AND (l.land_name LIKE '%$search%' OR u.full_name LIKE '%$search%')";
}

// Get all lands
$lands = mysqli_query($conn, "
    SELECT l.*, u.full_name as owner_name, u.phone as owner_phone,
           v.name as village_name
    FROM lands l
    JOIN users u ON l.user_id = u.id
    LEFT JOIN villages v ON l.village_id = v.id
    WHERE $where
    ORDER BY l.created_at DESC
");

// Get status counts
$status_counts = [];
$count_where = ($user_role !== 'district_officer' && $village_id) ? "village_id = $village_id" : "1=1";
$counts_result = mysqli_query($conn, "SELECT status, COUNT(*) as cnt FROM lands WHERE $count_where GROUP BY status");
while($row = mysqli_fetch_assoc($counts_result)) {
    $status_counts[$row['status']] = $row['cnt'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>All Lands - 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: #667eea; }

        .filters-bar { display: flex; gap: 12px; margin-bottom: 24px; flex-wrap: wrap; }
        .search-input { flex: 1; min-width: 200px; max-width: 400px; padding: 12px 16px; border: 2px solid #e2e8f0; border-radius: 10px; font-size: 14px; }
        .filter-select { padding: 12px 16px; border: 2px solid #e2e8f0; border-radius: 10px; font-size: 14px; min-width: 180px; }
        .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; }

        .status-tabs { display: flex; gap: 8px; margin-bottom: 24px; flex-wrap: wrap; }
        .status-tab { padding: 8px 16px; background: white; border: 2px solid #e2e8f0; border-radius: 8px; font-size: 13px; font-weight: 600; color: #64748b; text-decoration: none; transition: all 0.2s; }
        .status-tab:hover, .status-tab.active { border-color: #11998e; color: #11998e; background: rgba(17, 153, 142, 0.05); }
        .status-tab .count { background: #e2e8f0; padding: 2px 8px; border-radius: 10px; margin-left: 6px; font-size: 11px; }
        .status-tab.active .count { background: #11998e; color: white; }

        .table-container { background: white; border-radius: 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.05); overflow: hidden; }
        .table { margin: 0; }
        .table th { background: #f8fafc; color: #64748b; font-size: 12px; text-transform: uppercase; font-weight: 600; padding: 16px 20px; border: none; }
        .table td { padding: 16px 20px; vertical-align: middle; border-color: #f0f2f5; }

        .land-info { display: flex; align-items: center; gap: 12px; }
        .land-icon { width: 40px; height: 40px; border-radius: 10px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; align-items: center; justify-content: center; color: white; }
        .land-name { font-weight: 600; color: #1a1d21; }
        .land-location { font-size: 12px; color: #64748b; }

        .status-badge { padding: 4px 12px; border-radius: 20px; font-size: 11px; font-weight: 600; }
        .status-pending_neighbor, .status-pending_village, .status-pending_survey, .status-neighbor_verified { background: #fff3cd; color: #856404; }
        .status-village_verified, .status-surveyor_verified, .status-fully_verified { background: #d4edda; color: #155724; }
        .status-disputed { background: #f8d7da; color: #721c24; }
        .status-rejected { background: #f1f5f9; color: #64748b; }

        .btn-view { padding: 6px 14px; background: #f1f5f9; color: #64748b; border-radius: 8px; font-size: 13px; text-decoration: none; }
        .btn-view:hover { background: #11998e; color: white; }

        .empty-state { text-align: center; padding: 60px; color: #64748b; }
        .empty-state i { font-size: 48px; margin-bottom: 16px; }
    </style>
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>

    <main class="admin-main">
        <div class="page-header">
            <h1><i class="fas fa-map"></i> All Lands / Ardhi Zote</h1>
            <p style="color: #64748b; margin: 8px 0 0;">Complete list of registered lands in the village</p>
        </div>

        <form method="GET" class="filters-bar">
            <input type="text" name="search" class="search-input" placeholder="Search land or owner..." value="<?php echo htmlspecialchars($search); ?>">
            <select name="status" class="filter-select">
                <option value="">All Statuses</option>
                <option value="pending_neighbor" <?php echo $status_filter == 'pending_neighbor' ? 'selected' : ''; ?>>Pending Neighbor</option>
                <option value="neighbor_verified" <?php echo $status_filter == 'neighbor_verified' ? 'selected' : ''; ?>>Neighbor Verified</option>
                <option value="pending_village" <?php echo $status_filter == 'pending_village' ? 'selected' : ''; ?>>Pending Village</option>
                <option value="village_verified" <?php echo $status_filter == 'village_verified' ? 'selected' : ''; ?>>Village Verified</option>
                <option value="pending_survey" <?php echo $status_filter == 'pending_survey' ? 'selected' : ''; ?>>Pending Survey</option>
                <option value="fully_verified" <?php echo $status_filter == 'fully_verified' ? 'selected' : ''; ?>>Fully Verified</option>
                <option value="disputed" <?php echo $status_filter == 'disputed' ? 'selected' : ''; ?>>Disputed</option>
            </select>
            <button type="submit" class="search-btn"><i class="fas fa-filter"></i> Filter</button>
        </form>

        <div class="table-container">
            <?php if(mysqli_num_rows($lands) > 0): ?>
            <table class="table">
                <thead>
                    <tr>
                        <th>Land / Ardhi</th>
                        <th>Owner / Mmiliki</th>
                        <th>Area</th>
                        <th>Status</th>
                        <th>Registered</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while($land = mysqli_fetch_assoc($lands)): ?>
                    <tr>
                        <td>
                            <div class="land-info">
                                <div class="land-icon"><i class="fas fa-map-marked-alt"></i></div>
                                <div>
                                    <div class="land-name"><?php echo htmlspecialchars($land['land_name']); ?></div>
                                    <div class="land-location"><i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($land['village_name'] ?? 'N/A'); ?></div>
                                </div>
                            </div>
                        </td>
                        <td>
                            <div class="land-name"><?php echo htmlspecialchars($land['owner_name']); ?></div>
                            <div class="land-location"><?php echo htmlspecialchars($land['owner_phone']); ?></div>
                        </td>
                        <td><?php echo $land['total_area'] ?: 'N/A'; ?> <?php echo $land['area_unit'] ?: 'acres'; ?></td>
                        <td><span class="status-badge status-<?php echo $land['status']; ?>"><?php echo ucfirst(str_replace('_', ' ', $land['status'])); ?></span></td>
                        <td><?php echo date('M j, Y', strtotime($land['created_at'])); ?></td>
                        <td>
                            <a href="../view-land/<?php echo $land['id']; ?>" class="btn-view" target="_blank"><i class="fas fa-eye"></i></a>
                            <?php if(in_array($land['status'], ['pending_village', 'neighbor_verified'])): ?>
                            <a href="verify_land?id=<?php echo $land['id']; ?>" class="btn-view" style="background: #d4edda; color: #155724;"><i class="fas fa-check"></i></a>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
            <?php else: ?>
            <div class="empty-state">
                <i class="fas fa-map"></i>
                <p>No lands found</p>
            </div>
            <?php endif; ?>
        </div>
    </main>

    <script src="../assets/vendor/jquery/jquery.min.js"></script>
</body>
</html>
