<?php
/**
 * Land Check / Verification
 * Check if a land is registered and verify its status
 */

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;

$search_result = null;
$search_query = '';

if(isset($_GET['q']) && !empty($_GET['q'])) {
    $search_query = mysqli_real_escape_string($conn, $_GET['q']);

    // Search in lands table
    $where = "(l.land_name LIKE '%$search_query%' OR l.location_description LIKE '%$search_query%' OR u.full_name LIKE '%$search_query%' OR u.phone LIKE '%$search_query%')";

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

    $search_result = mysqli_query($conn, "
        SELECT l.*, u.full_name as owner_name, u.phone as owner_phone,
               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 DESC
        LIMIT 20
    ");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Land Check - 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: #6366f1; }

        .search-card { background: white; border-radius: 16px; padding: 32px; box-shadow: 0 4px 20px rgba(0,0,0,0.05); margin-bottom: 24px; }
        .search-card h2 { font-size: 18px; font-weight: 600; color: #1a1d21; margin: 0 0 8px; }
        .search-card p { color: #64748b; margin: 0 0 24px; }

        .search-form { display: flex; gap: 12px; }
        .search-input { flex: 1; padding: 16px 20px; border: 2px solid #e2e8f0; border-radius: 12px; font-size: 16px; }
        .search-input:focus { outline: none; border-color: #6366f1; }
        .search-btn { padding: 16px 32px; background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%); color: white; border: none; border-radius: 12px; font-weight: 600; font-size: 16px; cursor: pointer; }
        .search-btn:hover { transform: translateY(-2px); box-shadow: 0 4px 15px rgba(99, 102, 241, 0.4); }

        .results-card { background: white; border-radius: 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.05); overflow: hidden; }
        .results-header { padding: 20px 24px; border-bottom: 1px solid #e2e8f0; }
        .results-header h3 { font-size: 16px; font-weight: 600; color: #1a1d21; margin: 0; }

        .result-item { display: flex; align-items: center; padding: 20px 24px; border-bottom: 1px solid #f0f2f5; }
        .result-item:hover { background: #f8fafc; }

        .result-icon { width: 48px; height: 48px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 20px; margin-right: 16px; }
        .result-icon.verified { background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%); color: white; }
        .result-icon.pending { background: linear-gradient(135deg, #f5af19 0%, #f12711 100%); color: white; }
        .result-icon.disputed { background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); color: white; }

        .result-info { flex: 1; }
        .result-name { font-size: 16px; font-weight: 600; color: #1a1d21; margin-bottom: 4px; }
        .result-meta { font-size: 13px; color: #64748b; }
        .result-meta span { margin-right: 16px; }

        .result-status { text-align: right; }
        .status-badge { display: inline-block; padding: 6px 14px; border-radius: 20px; font-size: 12px; font-weight: 600; }
        .status-verified { background: #d4edda; color: #155724; }
        .status-pending { background: #fff3cd; color: #856404; }
        .status-disputed { background: #f8d7da; color: #721c24; }

        .result-actions { margin-left: 16px; }
        .btn-view { padding: 8px 16px; background: #f1f5f9; color: #64748b; border-radius: 8px; text-decoration: none; font-size: 13px; font-weight: 600; }
        .btn-view:hover { background: #6366f1; color: white; }

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

        .search-tips { margin-top: 24px; padding: 20px; background: #eff6ff; border-radius: 12px; border: 1px solid #bfdbfe; }
        .search-tips h4 { font-size: 14px; font-weight: 600; color: #1e40af; margin: 0 0 12px; display: flex; align-items: center; gap: 8px; }
        .search-tips ul { margin: 0; padding-left: 20px; font-size: 13px; color: #1e3a8a; }
        .search-tips li { margin-bottom: 6px; }
    </style>
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>

    <main class="admin-main">
        <div class="page-header">
            <h1><i class="fas fa-search-location"></i> Land Check</h1>
            <p style="color: #64748b; margin: 8px 0 0;">Ukaguzi wa Ardhi / Verify land registration status</p>
        </div>

        <div class="search-card">
            <h2><i class="fas fa-search"></i> Search for Land</h2>
            <p>Enter land name, location, owner name, or phone number to check registration status</p>

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

            <div class="search-tips">
                <h4><i class="fas fa-lightbulb"></i> Search Tips</h4>
                <ul>
                    <li>Search by land name (e.g., "Shamba la Nyumba")</li>
                    <li>Search by owner name (e.g., "John Makamba")</li>
                    <li>Search by phone number (e.g., "0712345678")</li>
                    <li>Search by location description</li>
                </ul>
            </div>
        </div>

        <?php if($search_result !== null): ?>
        <div class="results-card">
            <div class="results-header">
                <h3><i class="fas fa-list"></i> Search Results (<?php echo mysqli_num_rows($search_result); ?> found)</h3>
            </div>

            <?php if(mysqli_num_rows($search_result) > 0): ?>
                <?php while($land = mysqli_fetch_assoc($search_result)):
                    $is_verified = in_array($land['status'], ['village_verified', 'surveyor_verified', 'fully_verified']);
                    $is_disputed = $land['status'] === 'disputed';
                    $icon_class = $is_verified ? 'verified' : ($is_disputed ? 'disputed' : 'pending');
                    $status_class = $icon_class;
                ?>
                <div class="result-item">
                    <div class="result-icon <?php echo $icon_class; ?>">
                        <i class="fas fa-<?php echo $is_verified ? 'check-circle' : ($is_disputed ? 'exclamation-triangle' : 'clock'); ?>"></i>
                    </div>
                    <div class="result-info">
                        <div class="result-name"><?php echo htmlspecialchars($land['land_name']); ?></div>
                        <div class="result-meta">
                            <span><i class="fas fa-user"></i> <?php echo htmlspecialchars($land['owner_name']); ?></span>
                            <span><i class="fas fa-phone"></i> <?php echo htmlspecialchars($land['owner_phone']); ?></span>
                            <span><i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($land['village_name'] ?? 'N/A'); ?></span>
                        </div>
                    </div>
                    <div class="result-status">
                        <span class="status-badge status-<?php echo $status_class; ?>">
                            <?php echo ucfirst(str_replace('_', ' ', $land['status'])); ?>
                        </span>
                        <div style="font-size: 12px; color: #64748b; margin-top: 6px;">
                            <?php echo $land['total_area']; ?> <?php echo $land['area_unit'] ?: 'acres'; ?>
                        </div>
                    </div>
                    <div class="result-actions">
                        <a href="../view-land/<?php echo $land['id']; ?>" class="btn-view" target="_blank">
                            <i class="fas fa-eye"></i> View
                        </a>
                    </div>
                </div>
                <?php endwhile; ?>
            <?php else: ?>
                <div class="empty-state">
                    <i class="fas fa-search"></i>
                    <h3>No Results Found</h3>
                    <p>Hakuna ardhi iliyopatikana / No matching lands in this village</p>
                </div>
            <?php endif; ?>
        </div>
        <?php endif; ?>
    </main>
</body>
</html>
