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

if(!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    header('Location: ../login');
    exit;
}

$admin_id = $_SESSION['user_id'];

// Get all lands with user info
$lands = mysqli_query($conn, "
    SELECT l.*, u.username, u.full_name, u.email, u.role as user_role,
           (SELECT COUNT(*) FROM beacon_points WHERE land_id = l.id) as point_count,
           (SELECT COUNT(*) FROM overlap_alerts WHERE (land_id_1 = l.id OR land_id_2 = l.id) AND is_resolved = 0) as alert_count
    FROM lands l
    JOIN users u ON l.user_id = u.id
    ORDER BY l.created_at DESC
");

// Get statistics
$total_lands = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM lands"))['c'];
$total_area = mysqli_fetch_assoc(mysqli_query($conn, "SELECT SUM(total_area) as a FROM lands WHERE area_unit = 'sqm'"))['a'];
$with_alerts = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(DISTINCT land_id_1) + COUNT(DISTINCT land_id_2) as c FROM overlap_alerts WHERE is_resolved = 0"))['c'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>All Lands - Admin</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">
    <link href="../assets/DataTables/datatables.min.css" rel="stylesheet">
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>

    <main class="admin-main">
        <!-- Page Header -->
        <div class="page-header">
            <div class="page-header-content">
                <div class="page-title">
                    <h1><i class="fas fa-map"></i> All Registered Lands</h1>
                    <p>View and manage all land parcels in the system</p>
                </div>
            </div>
        </div>

        <!-- Stats Cards -->
        <div class="stats-row">
            <div class="stat-card">
                <div class="stat-icon blue">
                    <i class="fas fa-map-marked-alt"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo number_format($total_lands); ?></h3>
                    <p>Total Lands</p>
                </div>
            </div>
            <div class="stat-card">
                <div class="stat-icon green">
                    <i class="fas fa-ruler-combined"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo number_format($total_area / 10000, 1); ?> Ha</h3>
                    <p>Total Area</p>
                </div>
            </div>
            <div class="stat-card">
                <div class="stat-icon orange">
                    <i class="fas fa-exclamation-triangle"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo $with_alerts; ?></h3>
                    <p>With Alerts</p>
                </div>
            </div>
        </div>

        <div class="content-card">
            <div class="card-header-custom">
                <h3><i class="fas fa-list"></i> Lands Registry</h3>
                <span class="result-count"><?php echo mysqli_num_rows($lands); ?> lands</span>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover modern-table" id="landsTable">
                        <thead>
                            <tr>
                                <th>Land Name</th>
                                <th>Owner</th>
                                <th>Location</th>
                                <th>Area</th>
                                <th>Points</th>
                                <th>Alerts</th>
                                <th>Created</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php while($land = mysqli_fetch_assoc($lands)): ?>
                            <tr>
                                <td>
                                    <div class="land-name-cell">
                                        <strong><?php echo htmlspecialchars($land['land_name']); ?></strong>
                                        <?php if($land['share_token']): ?>
                                        <span class="shared-badge"><i class="fas fa-share-alt"></i> Shared</span>
                                        <?php endif; ?>
                                    </div>
                                </td>
                                <td>
                                    <div class="owner-cell">
                                        <span class="owner-name"><?php echo htmlspecialchars($land['full_name'] ?: $land['username']); ?></span>
                                        <span class="role-badge role-<?php echo $land['user_role']; ?>">
                                            <?php echo ucfirst($land['user_role']); ?>
                                        </span>
                                    </div>
                                </td>
                                <td>
                                    <span class="location-text"><?php echo htmlspecialchars($land['location_address'] ?: 'Not specified'); ?></span>
                                </td>
                                <td>
                                    <span class="area-badge"><?php echo number_format($land['total_area'], 2) . ' ' . $land['area_unit']; ?></span>
                                </td>
                                <td>
                                    <span class="point-count"><?php echo $land['point_count']; ?></span>
                                </td>
                                <td>
                                    <?php if($land['alert_count'] > 0): ?>
                                    <span class="alert-badge warning"><?php echo $land['alert_count']; ?> active</span>
                                    <?php else: ?>
                                    <span class="alert-badge success">Clear</span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo date('M d, Y', strtotime($land['created_at'])); ?></td>
                                <td>
                                    <a href="../view_land.php?id=<?php echo $land['id']; ?>" target="_blank"
                                       class="btn-view" title="View Land">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </main>

    <style>
    /* Page-specific styles */
    .page-header {
        background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
        border-radius: 20px;
        padding: 30px;
        margin-bottom: 30px;
        color: white;
    }

    .page-title h1 {
        font-size: 28px;
        font-weight: 700;
        margin: 0 0 8px 0;
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .page-title p {
        margin: 0;
        opacity: 0.9;
        font-size: 15px;
    }

    .stats-row {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 20px;
        margin-bottom: 30px;
    }

    .stat-card {
        background: white;
        border-radius: 16px;
        padding: 24px;
        display: flex;
        align-items: center;
        gap: 16px;
        box-shadow: 0 4px 20px rgba(0,0,0,0.05);
    }

    .stat-icon {
        width: 56px;
        height: 56px;
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
        color: white;
    }

    .stat-icon.blue {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    }

    .stat-icon.green {
        background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
    }

    .stat-icon.orange {
        background: linear-gradient(135deg, #f5af19 0%, #f12711 100%);
    }

    .stat-info h3 {
        font-size: 24px;
        font-weight: 700;
        margin: 0;
        color: #1e293b;
    }

    .stat-info p {
        margin: 4px 0 0 0;
        color: #64748b;
        font-size: 14px;
    }

    .content-card {
        background: white;
        border-radius: 20px;
        box-shadow: 0 4px 20px rgba(0,0,0,0.05);
        overflow: hidden;
    }

    .card-header-custom {
        padding: 24px 30px;
        border-bottom: 1px solid #f1f5f9;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .card-header-custom h3 {
        font-size: 18px;
        font-weight: 600;
        margin: 0;
        color: #1e293b;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .result-count {
        background: #f1f5f9;
        padding: 6px 14px;
        border-radius: 20px;
        font-size: 13px;
        color: #64748b;
        font-weight: 500;
    }

    .card-body {
        padding: 0;
    }

    .modern-table {
        margin: 0;
    }

    .modern-table thead th {
        background: #f8fafc;
        border: none;
        padding: 16px 20px;
        font-size: 12px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        color: #64748b;
    }

    .modern-table tbody td {
        padding: 16px 20px;
        border-bottom: 1px solid #f1f5f9;
        vertical-align: middle;
    }

    .modern-table tbody tr:hover {
        background: #f8fafc;
    }

    .land-name-cell {
        display: flex;
        flex-direction: column;
        gap: 4px;
    }

    .land-name-cell strong {
        color: #1e293b;
        font-size: 14px;
    }

    .shared-badge {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        font-size: 11px;
        color: #22c55e;
        background: rgba(34, 197, 94, 0.1);
        padding: 3px 8px;
        border-radius: 6px;
        width: fit-content;
    }

    .owner-cell {
        display: flex;
        flex-direction: column;
        gap: 6px;
    }

    .owner-name {
        font-size: 14px;
        color: #1e293b;
    }

    .role-badge {
        display: inline-block;
        padding: 4px 10px;
        border-radius: 12px;
        font-size: 11px;
        font-weight: 600;
        width: fit-content;
    }

    .role-broker {
        background: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
        color: white;
    }

    .role-user {
        background: #e2e8f0;
        color: #475569;
    }

    .location-text {
        font-size: 13px;
        color: #64748b;
    }

    .area-badge {
        display: inline-block;
        padding: 6px 12px;
        background: #f1f5f9;
        border-radius: 8px;
        font-size: 13px;
        font-weight: 500;
        color: #475569;
    }

    .point-count {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 32px;
        height: 32px;
        background: #f1f5f9;
        border-radius: 8px;
        font-weight: 600;
        color: #475569;
    }

    .alert-badge {
        display: inline-block;
        padding: 6px 12px;
        border-radius: 20px;
        font-size: 12px;
        font-weight: 600;
    }

    .alert-badge.warning {
        background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
        color: white;
    }

    .alert-badge.success {
        background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
        color: white;
    }

    .btn-view {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        border-radius: 10px;
        background: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
        color: white;
        text-decoration: none;
        transition: all 0.3s ease;
    }

    .btn-view:hover {
        color: white;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(6, 182, 212, 0.3);
    }

    @media (max-width: 768px) {
        .page-header {
            padding: 20px;
        }

        .page-title h1 {
            font-size: 22px;
        }

        .stats-row {
            grid-template-columns: 1fr 1fr;
        }

        .stat-card {
            padding: 16px;
        }

        .stat-icon {
            width: 44px;
            height: 44px;
            font-size: 18px;
        }

        .stat-info h3 {
            font-size: 18px;
        }

        .card-header-custom {
            padding: 16px 20px;
            flex-direction: column;
            gap: 12px;
            align-items: flex-start;
        }

        .modern-table thead th,
        .modern-table tbody td {
            padding: 12px 14px;
        }
    }
    </style>

    <script src="../assets/vendor/jquery/jquery.min.js"></script>
    <script src="../assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="../assets/DataTables/datatables.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#landsTable').DataTable({
                order: [[6, 'desc']],
                pageLength: 25,
                language: {
                    search: "",
                    searchPlaceholder: "Search lands..."
                }
            });
        });
    </script>
</body>
</html>
