<?php
/**
 * My Surveys
 * Surveys completed by the current surveyor
 */

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

// Only surveyors can access this
if(!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'surveyor') {
    header('Location: ../login');
    exit;
}

$user_id = $_SESSION['user_id'];

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

$surveys = 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.surveyor_verified_at DESC
");

// Get stats
$total = mysqli_num_rows($surveys);
$result = mysqli_query($conn, "SELECT COUNT(*) as cnt FROM lands WHERE surveyor_id = $user_id AND status = 'fully_verified'");
$completed = mysqli_fetch_assoc($result)['cnt'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Surveys - 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: #8b5cf6; }

        .stats-row { display: flex; gap: 16px; margin-bottom: 24px; }
        .stat-box { background: white; border-radius: 12px; padding: 20px; flex: 1; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
        .stat-box .value { font-size: 28px; font-weight: 700; color: #1a1d21; }
        .stat-box .label { font-size: 13px; color: #64748b; }

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

        .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, #8b5cf6 0%, #6366f1 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-completed { background: #d4edda; color: #155724; }
        .status-pending { 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: #8b5cf6; color: white; }

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

    <main class="admin-main">
        <div class="page-header">
            <h1><i class="fas fa-ruler-combined"></i> My Surveys</h1>
            <p style="color: #64748b; margin: 8px 0 0;">Upimaji Wangu / Lands you have surveyed</p>
        </div>

        <div class="stats-row">
            <div class="stat-box">
                <div class="value"><?php echo $total; ?></div>
                <div class="label">Total Surveys</div>
            </div>
            <div class="stat-box">
                <div class="value"><?php echo $completed; ?></div>
                <div class="label">Completed</div>
            </div>
        </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>

        <div class="table-container">
            <?php if(mysqli_num_rows($surveys) > 0): ?>
            <table class="table">
                <thead>
                    <tr>
                        <th>Land / Ardhi</th>
                        <th>Owner / Mmiliki</th>
                        <th>Area</th>
                        <th>Survey Date</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while($survey = mysqli_fetch_assoc($surveys)): ?>
                    <tr>
                        <td>
                            <div class="land-info">
                                <div class="land-icon"><i class="fas fa-ruler"></i></div>
                                <div>
                                    <div class="land-name"><?php echo htmlspecialchars($survey['land_name']); ?></div>
                                    <div class="land-location"><?php echo htmlspecialchars($survey['village_name'] ?? 'N/A'); ?></div>
                                </div>
                            </div>
                        </td>
                        <td>
                            <div class="land-name"><?php echo htmlspecialchars($survey['owner_name']); ?></div>
                            <div class="land-location"><?php echo htmlspecialchars($survey['owner_phone']); ?></div>
                        </td>
                        <td><?php echo $survey['total_area'] ?: 'N/A'; ?> <?php echo $survey['area_unit'] ?: 'acres'; ?></td>
                        <td><?php echo $survey['surveyor_verified_at'] ? date('M j, Y', strtotime($survey['surveyor_verified_at'])) : 'N/A'; ?></td>
                        <td>
                            <span class="status-badge <?php echo $survey['status'] == 'fully_verified' ? 'status-completed' : 'status-pending'; ?>">
                                <?php echo ucfirst(str_replace('_', ' ', $survey['status'])); ?>
                            </span>
                        </td>
                        <td><a href="../view-land/<?php echo $survey['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-ruler-combined" style="font-size: 48px; margin-bottom: 16px;"></i>
                <p>No surveys completed yet</p>
            </div>
            <?php endif; ?>
        </div>
    </main>
</body>
</html>
