<?php
// user/open_dispute.php
require_once __DIR__ . '/../config/db.php';
if (session_status() === PHP_SESSION_NONE) { session_start(); }

if (!isset($_SESSION['user_id'])) { header("Location: ../login.php"); exit; }

$user_id = $_SESSION['user_id'];

// Obtener las órdenes del usuario que están 'COMPLETED' o 'PAID' (para poder abrir disputa)
$orders = $pdo->prepare("SELECT id, product_id FROM orders WHERE (buyer_id = ? OR seller_id = ?) AND status IN ('paid', 'completed')");
$orders->execute([$user_id, $user_id]);
$my_orders = $orders->fetchAll();

include '../includes/header.php';
?>

<div style="max-width: 600px; margin: 50px auto; padding: 20px; color: white;">
    <h2>ABRIR DISPUTA</h2>
    <form action="process_dispute.php" method="POST" style="background:#161b22; padding:20px; border-radius:12px;">
        
        <label>Selecciona la orden:</label>
        <select name="order_id" style="width:100%; padding:10px; margin:10px 0; background:#0d1117; color:white; border:1px solid #30363d;">
            <?php foreach($my_orders as $o): ?>
                <option value="<?php echo $o['id']; ?>">Orden #<?php echo $o['id']; ?></option>
            <?php endforeach; ?>
        </select>

        <label>Motivo:</label>
        <textarea name="reason" required placeholder="Explica brevemente el problema..." style="width:100%; padding:10px; margin:10px 0; background:#0d1117; color:white; border:1px solid #30363d;"></textarea>

        <label>Link de Prueba (Discord/Imgur):</label>
        <input type="url" name="evidence_url" placeholder="https://cdn.discordapp.com/..." style="width:100%; padding:10px; margin:10px 0; background:#0d1117; color:white; border:1px solid #30363d;">

        <button type="submit" style="width:100%; padding:12px; background:#ff4d4d; border:none; color:white; font-weight:bold; cursor:pointer;">ABRIR CASO</button>
    </form>
</div>