<?php
/**
 * Resolved Disputes
 * History of resolved land disputes
 */

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 = "d.status IN ('resolved', 'dismissed')";
if($user_role !== 'district_officer' && $village_id) {
    $where .= " AND d.village_id = $village_id";
}

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

$disputes = mysqli_query($conn, "
    SELECT d.*, l.land_name,
           u1.full_name as owner_name,
           u2.full_name as resolved_by_name,
           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.resolved_by = u2.id
    LEFT JOIN villages v ON d.village_id = v.id
    WHERE $where
    ORDER BY d.resolved_at DESC
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Resolved 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: #22c55e; }

        .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, #11998e 0%, #38ef7d 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; }

        .dispute-info { display: flex; align-items: center; gap: 12px; }
        .dispute-icon { width: 40px; height: 40px; border-radius: 10px; background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%); display: flex; align-items: center; justify-content: center; color: white; }
        .dispute-number { font-weight: 600; color: #1a1d21; font-size: 13px; }
        .dispute-land { font-size: 14px; color: #1a1d21; }

        .status-badge { padding: 4px 12px; border-radius: 20px; font-size: 11px; font-weight: 600; }
        .status-resolved { background: #d4edda; color: #155724; }
        .status-dismissed { background: #e2e8f0; color: #64748b; }

        .resolution-type { font-size: 12px; padding: 4px 10px; background: #eff6ff; color: #1d4ed8; border-radius: 6px; }

        .btn-view { padding: 6px 14px; background: #f1f5f9; color: #64748b; border-radius: 8px; font-size: 13px; text-decoration: none; }
        .btn-view:hover { background: #22c55e; color: white; }

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

    <main class="admin-main">
        <div class="page-header">
            <div>
                <h1><i class="fas fa-check-double"></i> Resolved Disputes</h1>
                <p style="color: #64748b; margin: 8px 0 0;">Migogoro Iliyotatuliwa / History of resolved conflicts</p>
            </div>
            <span style="background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%); color: white; padding: 6px 16px; border-radius: 20px; font-size: 14px; font-weight: 600;">
                <i class="fas fa-check"></i> <?php echo mysqli_num_rows($disputes); ?> Resolved
            </span>
        </div>

        <form method="GET" class="search-box">
            <input type="text" name="search" class="search-input" placeholder="Search by dispute number or land name..." 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($disputes) > 0): ?>
            <table class="table">
                <thead>
                    <tr>
                        <th>Dispute</th>
                        <th>Land / Owner</th>
                        <th>Type</th>
                        <th>Resolution</th>
                        <th>Resolved By</th>
                        <th>Date</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while($dispute = mysqli_fetch_assoc($disputes)): ?>
                    <tr>
                        <td>
                            <div class="dispute-info">
                                <div class="dispute-icon"><i class="fas fa-check"></i></div>
                                <div class="dispute-number">#<?php echo htmlspecialchars($dispute['dispute_number']); ?></div>
                            </div>
                        </td>
                        <td>
                            <div class="dispute-land"><?php echo htmlspecialchars($dispute['land_name']); ?></div>
                            <div style="font-size: 12px; color: #64748b;"><?php echo htmlspecialchars($dispute['owner_name']); ?></div>
                        </td>
                        <td><?php echo ucfirst(str_replace('_', ' ', $dispute['dispute_type'])); ?></td>
                        <td>
                            <span class="status-badge status-<?php echo $dispute['status']; ?>">
                                <?php echo ucfirst($dispute['status']); ?>
                            </span>
                            <?php if($dispute['resolution_type']): ?>
                            <br><span class="resolution-type"><?php echo ucfirst(str_replace('_', ' ', $dispute['resolution_type'])); ?></span>
                            <?php endif; ?>
                        </td>
                        <td><?php echo htmlspecialchars($dispute['resolved_by_name'] ?? 'N/A'); ?></td>
                        <td><?php echo $dispute['resolved_at'] ? date('M j, Y', strtotime($dispute['resolved_at'])) : 'N/A'; ?></td>
                        <td><a href="view_dispute?id=<?php echo $dispute['id']; ?>" class="btn-view"><i class="fas fa-eye"></i> View</a></td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
            <?php else: ?>
            <div class="empty-state">
                <i class="fas fa-folder-open"></i>
                <p>No resolved disputes yet</p>
            </div>
            <?php endif; ?>
        </div>
    </main>
</body>
</html>
