<?php
/**
 * Pending Lands for Village Verification
 * Lists all lands awaiting village approval
 */

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

// Check access - only chairman, land officer, and land council can verify
$allowed_roles = ['village_chairman', 'land_officer', 'district_officer', 'land_council'];
if(!isset($_SESSION['user_id']) || !in_array($_SESSION['role'], $allowed_roles)) {
    header('Location: ../login');
    exit;
}

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

// Get village info
$village_info = null;
if($village_id) {
    $result = mysqli_query($conn, "
        SELECT v.*, d.name as district_name, r.name as region_name
        FROM villages v
        JOIN districts d ON v.district_id = d.id
        JOIN regions r ON d.region_id = r.id
        WHERE v.id = $village_id
    ");
    $village_info = mysqli_fetch_assoc($result);
}

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

// Search filter
$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%' OR u.phone LIKE '%$search%')";
}

// Get pending lands
$lands = mysqli_query($conn, "
    SELECT l.*, u.full_name as owner_name, u.phone as owner_phone, u.email as owner_email,
           v.name as village_name, d.name as district_name
    FROM lands l
    JOIN users u ON l.user_id = u.id
    LEFT JOIN villages v ON l.village_id = v.id
    LEFT JOIN districts d ON v.district_id = d.id
    WHERE $where
    ORDER BY l.created_at ASC
");

$total_pending = mysqli_num_rows($lands);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pending Lands - Village Verification</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&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 {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 24px;
            flex-wrap: wrap;
            gap: 16px;
        }

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

        .page-header h1 i { color: #f59e0b; }

        .pending-badge {
            background: linear-gradient(135deg, #f5af19 0%, #f12711 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-input:focus {
            outline: none;
            border-color: #11998e;
        }

        .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;
        }

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

        .land-card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.05);
            overflow: hidden;
            transition: transform 0.3s, box-shadow 0.3s;
        }

        .land-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 30px rgba(0,0,0,0.1);
        }

        .land-card-header {
            padding: 20px;
            background: linear-gradient(135deg, rgba(17, 153, 142, 0.1) 0%, rgba(56, 239, 125, 0.1) 100%);
            border-bottom: 1px solid #e2e8f0;
        }

        .land-card-header h3 {
            font-size: 18px;
            font-weight: 600;
            color: #1a1d21;
            margin: 0 0 8px;
        }

        .land-card-header .land-meta {
            font-size: 13px;
            color: #64748b;
            display: flex;
            flex-wrap: wrap;
            gap: 12px;
        }

        .land-card-body {
            padding: 20px;
        }

        .owner-info {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 16px;
            padding-bottom: 16px;
            border-bottom: 1px solid #f0f2f5;
        }

        .owner-avatar {
            width: 48px;
            height: 48px;
            border-radius: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: 700;
            font-size: 18px;
        }

        .owner-details h4 {
            font-size: 15px;
            font-weight: 600;
            color: #1a1d21;
            margin: 0 0 4px;
        }

        .owner-details p {
            font-size: 13px;
            color: #64748b;
            margin: 0;
        }

        .verification-status {
            background: #fffbeb;
            border: 1px solid #fcd34d;
            border-radius: 10px;
            padding: 12px 16px;
            margin-bottom: 16px;
        }

        .verification-status h5 {
            font-size: 13px;
            font-weight: 600;
            color: #92400e;
            margin: 0 0 8px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .neighbor-progress {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .progress-bar {
            flex: 1;
            height: 8px;
            background: #fef3c7;
            border-radius: 4px;
            overflow: hidden;
        }

        .progress-fill {
            height: 100%;
            background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
            border-radius: 4px;
        }

        .progress-text {
            font-size: 13px;
            font-weight: 600;
            color: #92400e;
        }

        .land-stats {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 12px;
            margin-bottom: 16px;
        }

        .stat-item {
            text-align: center;
            padding: 10px;
            background: #f8fafc;
            border-radius: 8px;
        }

        .stat-item .value {
            font-size: 18px;
            font-weight: 700;
            color: #1a1d21;
        }

        .stat-item .label {
            font-size: 11px;
            color: #64748b;
            text-transform: uppercase;
        }

        .land-card-actions {
            display: flex;
            gap: 10px;
        }

        .btn {
            flex: 1;
            padding: 12px 16px;
            border-radius: 10px;
            font-size: 14px;
            font-weight: 600;
            text-decoration: none;
            text-align: center;
            transition: all 0.2s;
            border: none;
            cursor: pointer;
        }

        .btn-primary {
            background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(17, 153, 142, 0.4);
            color: white;
        }

        .btn-outline {
            background: white;
            border: 2px solid #e2e8f0;
            color: #64748b;
        }

        .btn-outline:hover {
            border-color: #11998e;
            color: #11998e;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            background: white;
            border-radius: 16px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.05);
        }

        .empty-state i {
            font-size: 64px;
            color: #22c55e;
            margin-bottom: 16px;
        }

        .empty-state h3 {
            font-size: 20px;
            font-weight: 600;
            color: #1a1d21;
            margin: 0 0 8px;
        }

        .empty-state p {
            color: #64748b;
            margin: 0;
        }

        @media (max-width: 768px) {
            .lands-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>

    <main class="admin-main">
        <div class="page-header">
            <div>
                <h1><i class="fas fa-clock"></i> Pending Land Approvals</h1>
                <p style="color: #64748b; margin: 8px 0 0;">Ardhi Zinazosubiri Uhakiki / Lands awaiting village verification</p>
            </div>
            <span class="pending-badge">
                <i class="fas fa-hourglass-half"></i> <?php echo $total_pending; ?> Pending
            </span>
        </div>

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

        <?php if($total_pending > 0): ?>
        <div class="lands-grid">
            <?php while($land = mysqli_fetch_assoc($lands)): ?>
            <?php
                $neighbor_percent = ($land['neighbor_required_count'] > 0)
                    ? ($land['neighbor_verification_count'] / $land['neighbor_required_count']) * 100
                    : 0;
            ?>
            <div class="land-card">
                <div class="land-card-header">
                    <h3><?php echo htmlspecialchars($land['land_name']); ?></h3>
                    <div class="land-meta">
                        <span><i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($land['village_name'] ?? 'N/A'); ?></span>
                        <span><i class="fas fa-calendar"></i> <?php echo date('M j, Y', strtotime($land['created_at'])); ?></span>
                    </div>
                </div>
                <div class="land-card-body">
                    <div class="owner-info">
                        <div class="owner-avatar">
                            <?php echo strtoupper(substr($land['owner_name'], 0, 1)); ?>
                        </div>
                        <div class="owner-details">
                            <h4><?php echo htmlspecialchars($land['owner_name']); ?></h4>
                            <p><i class="fas fa-phone"></i> <?php echo htmlspecialchars($land['owner_phone']); ?></p>
                        </div>
                    </div>

                    <div class="verification-status">
                        <h5><i class="fas fa-users"></i> Neighbor Verification</h5>
                        <div class="neighbor-progress">
                            <div class="progress-bar">
                                <div class="progress-fill" style="width: <?php echo $neighbor_percent; ?>%"></div>
                            </div>
                            <span class="progress-text">
                                <?php echo $land['neighbor_verification_count']; ?>/<?php echo $land['neighbor_required_count']; ?>
                            </span>
                        </div>
                    </div>

                    <div class="land-stats">
                        <div class="stat-item">
                            <div class="value"><?php echo $land['total_area'] ?: 'N/A'; ?></div>
                            <div class="label"><?php echo $land['area_unit'] ?: 'Acres'; ?></div>
                        </div>
                        <div class="stat-item">
                            <div class="value"><?php echo ucfirst(str_replace('_', ' ', $land['status'])); ?></div>
                            <div class="label">Status</div>
                        </div>
                    </div>

                    <div class="land-card-actions">
                        <a href="verify_land?id=<?php echo $land['id']; ?>" class="btn btn-primary">
                            <i class="fas fa-check-circle"></i> Review & Verify
                        </a>
                        <a href="../view-land/<?php echo $land['id']; ?>" class="btn btn-outline" target="_blank">
                            <i class="fas fa-eye"></i> View
                        </a>
                    </div>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
        <?php else: ?>
        <div class="empty-state">
            <i class="fas fa-check-circle"></i>
            <h3>No Pending Lands!</h3>
            <p>Hakuna ardhi zinazosubiri uhakiki / All lands have been verified</p>
        </div>
        <?php endif; ?>
    </main>

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