<?php
/**
 * Verified Lands List
 * Shows all lands that have been verified by 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;

// Build WHERE clause
$where = "l.status IN ('village_verified', 'surveyor_verified', 'fully_verified')";
if($user_role !== 'district_officer' && $village_id) {
    $where .= " AND l.village_id = $village_id";
}

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

// Get verified 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.village_verified_at DESC
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Verified 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: #22c55e; }

        .verified-badge { background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); color: white; padding: 6px 16px; border-radius: 20px; font-size: 14px; font-weight: 600; }

        .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; font-size: 14px; }
        .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; }

        .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; }
        .table tbody tr:hover { background: #f8fafc; }

        .land-info { display: flex; align-items: center; gap: 12px; }
        .land-icon { width: 40px; height: 40px; border-radius: 10px; background: linear-gradient(135deg, #11998e 0%, #38ef7d 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-verified { background: #d4edda; color: #155724; }
        .status-survey { background: #e2d5f1; color: #5a3d8a; }

        .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" style="display: flex; justify-content: space-between; align-items: center;">
            <div>
                <h1><i class="fas fa-check-circle"></i> Verified Lands</h1>
                <p style="color: #64748b; margin: 8px 0 0;">Ardhi Zilizohakikiwa / Successfully verified lands</p>
            </div>
            <span class="verified-badge">
                <i class="fas fa-check"></i> <?php echo mysqli_num_rows($lands); ?> Verified
            </span>
        </div>

        <form method="GET" class="search-box">
            <input type="text" name="search" class="search-input" placeholder="Search by land name or owner..." value="<?php echo htmlspecialchars($search); ?>">
            <button type="submit" class="search-btn"><i class="fas fa-search"></i> Search</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 / Ukubwa</th>
                        <th>Verified Date</th>
                        <th>Status</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><?php echo $land['village_verified_at'] ? date('M j, Y', strtotime($land['village_verified_at'])) : 'N/A'; ?></td>
                        <td>
                            <span class="status-badge <?php echo $land['status'] == 'fully_verified' ? 'status-verified' : 'status-survey'; ?>">
                                <?php echo ucfirst(str_replace('_', ' ', $land['status'])); ?>
                            </span>
                        </td>
                        <td><a href="../view-land/<?php echo $land['id']; ?>" class="btn-view" target="_blank"><i class="fas fa-eye"></i> View</a></td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
            <?php else: ?>
            <div class="empty-state">
                <i class="fas fa-map"></i>
                <p>No verified lands yet</p>
            </div>
            <?php endif; ?>
        </div>
    </main>

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