<?php
/**
 * Active Disputes
 * List of all active land disputes in the village
 */

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;

// Build WHERE clause
$where = "d.status NOT IN ('resolved', 'dismissed')";
if($user_role !== 'district_officer' && $village_id) {
    $where .= " AND d.village_id = $village_id";
}

// Filter by priority
$priority_filter = isset($_GET['priority']) ? mysqli_real_escape_string($conn, $_GET['priority']) : '';
if($priority_filter) {
    $where .= " AND d.priority = '$priority_filter'";
}

// Get disputes
$disputes = mysqli_query($conn, "
    SELECT d.*, l.land_name,
           u1.full_name as owner_name, u1.phone as owner_phone,
           u2.full_name as complainant_name, u2.phone as complainant_phone,
           COALESCE(d.reported_by_name, u2.full_name) as reporter_name,
           COALESCE(d.reported_by_phone, u2.phone) as reporter_phone,
           v.name as village_name
    FROM land_disputes d
    JOIN lands l ON d.land_id = l.id
    JOIN users u1 ON d.land_owner_id = u1.id
    LEFT JOIN users u2 ON d.reported_by_user_id = u2.id
    LEFT JOIN villages v ON d.village_id = v.id
    WHERE $where
    ORDER BY
        CASE d.priority WHEN 'urgent' THEN 1 WHEN 'high' THEN 2 WHEN 'medium' THEN 3 ELSE 4 END,
        d.created_at DESC
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Active Disputes - 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: #ef4444; }

        .disputes-badge { background: linear-gradient(135deg, #f5af19 0%, #f12711 100%); color: white; padding: 6px 16px; border-radius: 20px; font-size: 14px; font-weight: 600; }

        .filters-bar { display: flex; gap: 12px; margin-bottom: 24px; flex-wrap: wrap; }
        .filter-btn { padding: 10px 20px; background: white; border: 2px solid #e2e8f0; border-radius: 10px; font-size: 13px; font-weight: 600; color: #64748b; cursor: pointer; text-decoration: none; transition: all 0.2s; }
        .filter-btn:hover, .filter-btn.active { border-color: #ef4444; color: #ef4444; }

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

        .dispute-card { background: white; border-radius: 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.05); overflow: hidden; border-left: 4px solid #f59e0b; }
        .dispute-card.urgent { border-left-color: #ef4444; }
        .dispute-card.high { border-left-color: #f59e0b; }
        .dispute-card.medium { border-left-color: #3b82f6; }
        .dispute-card.low { border-left-color: #22c55e; }

        .dispute-header { padding: 20px; background: #f8fafc; border-bottom: 1px solid #e2e8f0; }
        .dispute-number { font-size: 12px; color: #64748b; font-weight: 600; margin-bottom: 4px; }
        .dispute-title { font-size: 16px; font-weight: 600; color: #1a1d21; margin: 0 0 8px; }
        .dispute-type { display: inline-block; padding: 4px 10px; background: #e2e8f0; border-radius: 20px; font-size: 11px; font-weight: 600; color: #64748b; }

        .dispute-body { padding: 20px; }
        .dispute-parties { margin-bottom: 16px; }
        .party { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
        .party-icon { width: 32px; height: 32px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 14px; }
        .party-icon.owner { background: #dbeafe; color: #2563eb; }
        .party-icon.complainant { background: #fee2e2; color: #dc2626; }
        .party-name { font-weight: 500; color: #1a1d21; font-size: 14px; }
        .party-phone { font-size: 12px; color: #64748b; }

        .dispute-meta { display: flex; flex-wrap: wrap; gap: 16px; font-size: 13px; color: #64748b; padding-top: 16px; border-top: 1px solid #f0f2f5; }
        .dispute-meta span { display: flex; align-items: center; gap: 6px; }

        .priority-badge { padding: 4px 12px; border-radius: 20px; font-size: 11px; font-weight: 600; }
        .priority-urgent { background: #fee2e2; color: #dc2626; }
        .priority-high { background: #ffedd5; color: #ea580c; }
        .priority-medium { background: #fef3c7; color: #d97706; }
        .priority-low { background: #d1fae5; color: #059669; }

        .dispute-actions { padding: 16px 20px; background: #f8fafc; display: flex; gap: 10px; }
        .btn { flex: 1; padding: 10px 16px; border-radius: 8px; font-size: 13px; font-weight: 600; text-align: center; text-decoration: none; transition: all 0.2s; border: none; cursor: pointer; }
        .btn-primary { background: linear-gradient(135deg, #11998e 0%, #38ef7d 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) { .disputes-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-exclamation-triangle"></i> Active Disputes</h1>
                <p style="color: #64748b; margin: 8px 0 0;">Migogoro ya Ardhi / Land conflicts requiring attention</p>
            </div>
            <span class="disputes-badge">
                <i class="fas fa-gavel"></i> <?php echo mysqli_num_rows($disputes); ?> Active
            </span>
        </div>

        <div class="filters-bar">
            <a href="disputes" class="filter-btn <?php echo !$priority_filter ? 'active' : ''; ?>">All</a>
            <a href="disputes?priority=urgent" class="filter-btn <?php echo $priority_filter == 'urgent' ? 'active' : ''; ?>">Urgent</a>
            <a href="disputes?priority=high" class="filter-btn <?php echo $priority_filter == 'high' ? 'active' : ''; ?>">High</a>
            <a href="disputes?priority=medium" class="filter-btn <?php echo $priority_filter == 'medium' ? 'active' : ''; ?>">Medium</a>
            <a href="disputes?priority=low" class="filter-btn <?php echo $priority_filter == 'low' ? 'active' : ''; ?>">Low</a>
        </div>

        <?php if(mysqli_num_rows($disputes) > 0): ?>
        <div class="disputes-grid">
            <?php while($dispute = mysqli_fetch_assoc($disputes)): ?>
            <div class="dispute-card <?php echo $dispute['priority']; ?>">
                <div class="dispute-header">
                    <div class="dispute-number">#<?php echo htmlspecialchars($dispute['dispute_number']); ?></div>
                    <h3 class="dispute-title"><?php echo htmlspecialchars($dispute['land_name']); ?></h3>
                    <span class="dispute-type"><?php echo ucfirst(str_replace('_', ' ', $dispute['dispute_type'])); ?></span>
                </div>
                <div class="dispute-body">
                    <div class="dispute-parties">
                        <div class="party">
                            <div class="party-icon owner"><i class="fas fa-user"></i></div>
                            <div>
                                <div class="party-name"><?php echo htmlspecialchars($dispute['owner_name']); ?> (Owner)</div>
                                <div class="party-phone"><?php echo htmlspecialchars($dispute['owner_phone']); ?></div>
                            </div>
                        </div>
                        <?php if($dispute['complainant_name']): ?>
                        <div class="party">
                            <div class="party-icon complainant"><i class="fas fa-user-times"></i></div>
                            <div>
                                <div class="party-name"><?php echo htmlspecialchars($dispute['complainant_name']); ?> (Complainant)</div>
                                <div class="party-phone"><?php echo htmlspecialchars($dispute['complainant_phone']); ?></div>
                            </div>
                        </div>
                        <?php endif; ?>
                    </div>
                    <div class="dispute-meta">
                        <span><i class="fas fa-calendar"></i> <?php echo date('M j, Y', strtotime($dispute['created_at'])); ?></span>
                        <span><i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($dispute['village_name'] ?? 'N/A'); ?></span>
                        <span class="priority-badge priority-<?php echo $dispute['priority']; ?>"><?php echo ucfirst($dispute['priority']); ?></span>
                    </div>
                </div>
                <div class="dispute-actions">
                    <a href="view_dispute?id=<?php echo $dispute['id']; ?>" class="btn btn-primary"><i class="fas fa-eye"></i> View Details</a>
                    <a href="resolve_dispute?id=<?php echo $dispute['id']; ?>" class="btn btn-outline"><i class="fas fa-gavel"></i> Resolve</a>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
        <?php else: ?>
        <div class="empty-state">
            <i class="fas fa-check-circle"></i>
            <h3>No Active Disputes!</h3>
            <p>Hakuna migogoro ya ardhi inayosubiri / All disputes have been resolved</p>
        </div>
        <?php endif; ?>
    </main>

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