<?php
// admin/disputes.php
require_once __DIR__ . '/../config/db.php';

if (session_status() === PHP_SESSION_NONE) { session_start(); }

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

try {
    // 1. Buscamos órdenes en 'IN_DISPUTE'
    // Añadimos 'dispute_reason' para ver el motivo rápido sin entrar al chat
    $stmt = $pdo->prepare("
        SELECT o.*, p.title as product_name, 
               b.username as buyer_name, 
               s.username as seller_name
        FROM orders o
        JOIN products p ON o.product_id = p.id
        JOIN users b ON o.buyer_id = b.id
        JOIN users s ON o.seller_id = s.id
        WHERE o.status = 'IN_DISPUTE'
        ORDER BY o.created_at DESC
    ");
    $stmt->execute();
    $disputes = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("Database Error: " . $e->getMessage());
}

$page_title = "Dispute Center";
include '../includes/header.php'; 
?>

<style>
    .case-row:hover { background: rgba(255, 77, 77, 0.03) !important; }
    .btn-action { padding: 8px 12px; border-radius: 6px; cursor: pointer; font-size: 10px; font-weight: 800; border: 1px solid; transition: 0.2s; text-decoration: none; display: inline-block; }
    .btn-view { background: rgba(255, 255, 255, 0.05); color: #8b949e; border-color: #30363d; }
    .btn-view:hover { background: #30363d; color: white; }
    .badge-id { background: rgba(255, 77, 77, 0.1); color: #ff4d4d; padding: 2px 6px; border-radius: 4px; font-size: 10px; }
</style>

<div style="max-width: 1200px; margin: 40px auto; padding: 0 20px; color: white; font-family: 'Inter', sans-serif;">
    
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px;">
        <div>
            <h2 style="color: #ff4d4d; font-family: 'Syncopate', sans-serif; margin: 0; letter-spacing: -1px;">
                COURT<span>.SYSTEM</span>
            </h2>
            <p style="color: #555; font-size: 11px; margin-top: 5px;">RESOLUCIÓN DE CONFLICTOS Y ESCROW</p>
        </div>
        <?php if(count($disputes) > 0): ?>
            <div style="text-align: right;">
                <span style="background: #ff4d4d; color: white; padding: 5px 12px; border-radius: 4px; font-size: 10px; font-weight: 900;">
                    <?php echo count($disputes); ?> ACTIVE CASES
                </span>
            </div>
        <?php endif; ?>
    </div>

    <?php if($disputes): ?>
    <div style="background: #161b22; border-radius: 12px; border: 1px solid #30363d; overflow: hidden;">
        <table style="width: 100%; border-collapse: collapse; text-align: left;">
            <thead>
                <tr style="background: rgba(0,0,0,0.2);">
                    <th style="padding: 20px; font-size: 10px; color: #8b949e; text-transform: uppercase;">Case info</th>
                    <th style="padding: 20px; font-size: 10px; color: #8b949e; text-transform: uppercase;">Participants</th>
                    <th style="padding: 20px; font-size: 10px; color: #8b949e; text-transform: uppercase;">Total Value</th>
                    <th style="padding: 20px; font-size: 10px; color: #8b949e; text-transform: uppercase; text-align: right;">Verdict</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($disputes as $d): ?>
                <tr class="case-row" style="border-top: 1px solid #30363d;">
                    <td style="padding: 20px;">
                        <span class="badge-id">#<?php echo $d['id']; ?></span>
                        <div style="font-size: 14px; font-weight: 700; margin-top: 8px;"><?php echo htmlspecialchars($d['product_name']); ?></div>
                        <div style="font-size: 11px; color: #555;"><?php echo date('M d, Y H:i', strtotime($d['created_at'])); ?></div>
                    </td>
                    <td style="padding: 20px;">
                        <div style="font-size: 12px; color: #00ff88;">Buyer: <b>@<?php echo $d['buyer_name']; ?></b></div>
                        <div style="font-size: 12px; color: #d4af37; margin-top: 4px;">Seller: <b>@<?php echo $d['seller_name']; ?></b></div>
                    </td>
                    <td style="padding: 20px;">
                        <div style="font-family: 'Syncopate'; font-size: 16px; font-weight: 800; color: white;">$<?php echo number_format($d['total_price'], 2); ?></div>
                    </td>
                    <td style="padding: 20px; text-align: right;">
                        <div style="display: flex; gap: 8px; justify-content: flex-end;">
                            <a href="dispute_view.php?id=<?php echo $d['id']; ?>" class="btn-action btn-view" title="Ver Chat y Pruebas">
                                <i class="fa-solid fa-eye"></i> EVIDENCE
                            </a>

                            <form action="resolve_dispute.php" method="POST" onsubmit="return confirm('¿CONFIRMAR REEMBOLSO TOTAL AL COMPRADOR?')">
                                <input type="hidden" name="order_id" value="<?php echo $d['id']; ?>">
                                <input type="hidden" name="action" value="refund">
                                <button type="submit" class="btn-action" style="background: rgba(255, 77, 77, 0.1); color: #ff4d4d; border-color: #ff4d4d;">REFUND</button>
                            </form>

                            <form action="resolve_dispute.php" method="POST" onsubmit="return confirm('¿LIBERAR PAGO AL VENDEDOR?')">
                                <input type="hidden" name="order_id" value="<?php echo $d['id']; ?>">
                                <input type="hidden" name="action" value="settle">
                                <button type="submit" class="btn-action" style="background: rgba(0, 255, 136, 0.1); color: #00ff88; border-color: #00ff88;">RELEASE</button>
                            </form>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
        <div style="text-align: center; padding: 100px; background: #161b22; border: 1px dashed #30363d; border-radius: 20px;">
            <i class="fa-solid fa-scale-balanced" style="font-size: 50px; color: #30363d; margin-bottom: 20px;"></i>
            <h3 style="color: #8b949e; font-family: 'Syncopate'; font-size: 14px;">Peace in the Marketplace</h3>
            <p style="color: #555; font-size: 12px;">No active disputes require your intervention.</p>
        </div>
    <?php endif; ?>
</div>

<?php include '../includes/footer.php'; ?>