<?php
/**
 * Survey Requests
 * Lands awaiting professional survey
 */

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 = "l.status = 'pending_survey'";
if($user_role !== 'district_officer' && $village_id && $user_role !== 'surveyor') {
    $where .= " AND l.village_id = $village_id";
}

$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%')";
}

$requests = 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.village_verified_at ASC
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Survey Requests - 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 { 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; display: flex; align-items: center; gap: 12px; }
        .page-header h1 i { color: #8b5cf6; }

        .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, #8b5cf6 0%, #6366f1 100%); color: white; border: none; border-radius: 10px; font-weight: 600; cursor: pointer; }

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

        .request-card { background: white; border-radius: 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.05); overflow: hidden; border-left: 4px solid #8b5cf6; }
        .request-header { padding: 20px; background: linear-gradient(135deg, rgba(139, 92, 246, 0.1) 0%, rgba(99, 102, 241, 0.1) 100%); }
        .request-header h3 { font-size: 18px; font-weight: 600; color: #1a1d21; margin: 0 0 8px; }
        .request-meta { font-size: 13px; color: #64748b; }

        .request-body { padding: 20px; }
        .owner-row { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; padding-bottom: 16px; border-bottom: 1px solid #f0f2f5; }
        .owner-avatar { width: 44px; height: 44px; border-radius: 12px; background: linear-gradient(135deg, #8b5cf6 0%, #6366f1 100%); display: flex; align-items: center; justify-content: center; color: white; font-weight: 700; font-size: 18px; }
        .owner-info h4 { font-size: 15px; font-weight: 600; color: #1a1d21; margin: 0 0 4px; }
        .owner-info p { font-size: 13px; color: #64748b; margin: 0; }

        .land-details { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
        .detail { padding: 12px; background: #f8fafc; border-radius: 8px; text-align: center; }
        .detail .value { font-size: 16px; font-weight: 600; color: #1a1d21; }
        .detail .label { font-size: 11px; color: #64748b; text-transform: uppercase; }

        .request-actions { padding: 16px 20px; background: #f8fafc; display: flex; gap: 10px; }
        .btn { flex: 1; padding: 12px; border-radius: 8px; font-size: 13px; font-weight: 600; text-align: center; text-decoration: none; border: none; cursor: pointer; }
        .btn-primary { background: linear-gradient(135deg, #8b5cf6 0%, #6366f1 100%); color: white; }
        .btn-outline { background: white; border: 2px solid #e2e8f0; color: #64748b; }

        .empty-state { text-align: center; padding: 60px; background: white; border-radius: 16px; color: #64748b; }
        .empty-state i { font-size: 48px; color: #22c55e; margin-bottom: 16px; }

        @media (max-width: 768px) { .requests-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-ruler-combined"></i> Survey Requests</h1>
                <p style="color: #64748b; margin: 8px 0 0;">Maombi ya Upimaji / Lands awaiting professional survey</p>
            </div>
            <span style="background: linear-gradient(135deg, #8b5cf6 0%, #6366f1 100%); color: white; padding: 6px 16px; border-radius: 20px; font-size: 14px; font-weight: 600;">
                <i class="fas fa-ruler"></i> <?php echo mysqli_num_rows($requests); ?> Pending
            </span>
        </div>

        <form method="GET" class="search-box">
            <input type="text" name="search" class="search-input" placeholder="Search land or owner..." 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($requests) > 0): ?>
        <div class="requests-grid">
            <?php while($land = mysqli_fetch_assoc($requests)): ?>
            <div class="request-card">
                <div class="request-header">
                    <h3><?php echo htmlspecialchars($land['land_name']); ?></h3>
                    <div class="request-meta">
                        <i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($land['village_name'] ?? 'N/A'); ?>, <?php echo htmlspecialchars($land['district_name'] ?? ''); ?>
                    </div>
                </div>
                <div class="request-body">
                    <div class="owner-row">
                        <div class="owner-avatar"><?php echo strtoupper(substr($land['owner_name'], 0, 1)); ?></div>
                        <div class="owner-info">
                            <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="land-details">
                        <div class="detail">
                            <div class="value"><?php echo $land['total_area'] ?: 'N/A'; ?></div>
                            <div class="label"><?php echo $land['area_unit'] ?: 'Acres'; ?></div>
                        </div>
                        <div class="detail">
                            <div class="value"><?php echo $land['village_verified_at'] ? date('M j', strtotime($land['village_verified_at'])) : 'N/A'; ?></div>
                            <div class="label">Village Verified</div>
                        </div>
                    </div>
                </div>
                <div class="request-actions">
                    <?php if($user_role === 'surveyor'): ?>
                    <a href="conduct_survey?id=<?php echo $land['id']; ?>" class="btn btn-primary"><i class="fas fa-ruler-combined"></i> Conduct Survey</a>
                    <?php endif; ?>
                    <a href="../view-land/<?php echo $land['id']; ?>" class="btn btn-outline" target="_blank"><i class="fas fa-eye"></i> View Details</a>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
        <?php else: ?>
        <div class="empty-state">
            <i class="fas fa-check-circle"></i>
            <h3>No Pending Survey Requests</h3>
            <p>Hakuna maombi ya upimaji / All surveys have been completed</p>
        </div>
        <?php endif; ?>
    </main>
</body>
</html>
