<?php
require_once __DIR__ . '/../../vendor/autoload.php';

use App\Controllers\AssignmentController;
use App\Middleware\RoleMiddleware;
use App\Services\AuthService;

RoleMiddleware::coordinador(); // Admin o Coord

$controller = new AssignmentController();

// Si es AJAX save_order
if (isset($_GET['action']) && $_GET['action'] == 'save_order') {
    $controller->saveOrder();
    exit;
}

$assignments = $controller->getHelpersForReorder();
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reorganizar Turnos - Contenedor Ibiza</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <style>
        .warning-box {
            background: #fffbeb;
            border: 1px solid #fcd34d;
            padding: 1rem;
            border-radius: 0.5rem;
            margin-bottom: 1.5rem;
            color: #92400e;
        }
    </style>
</head>
<body>
    <?php include '../partials/navbar.php'; ?>

    <div class="container">
        <div class="card-header">
            <h1 class="card-title">↔️ Reorganizar Orden de Ayudantes</h1>
            <a href="../rotaciones.php" class="btn btn-secondary">Volver</a>
        </div>

        <div class="warning-box">
            <strong>Instrucciones:</strong> Arrastra y suelta para definir el <strong>Orden Maestro de Rotación</strong>.
            <br>
            Al cambiar este orden, el sistema <strong>regenerará automáticamente</strong> todas las rotaciones futuras para seguir esta nueva secuencia cíclica.
        </div>
        
        <div id="save-feedback" style="height: 20px; text-align: right; margin-bottom: 10px; font-weight: bold;"></div>

        <div class="card">
            <ul id="sortable-list" class="drag-list">
                <?php foreach ($assignments as $user): ?>
                    <li class="drag-item" data-id="<?= $user['id'] ?>" draggable="true">
                        <div style="display: flex; align-items: center; width: 100%;">
                            <span class="drag-handle">☰</span>
                            
                            <div style="flex-grow: 1;">
                                <strong style="font-size: 1.1rem; color: var(--text);"><?= e($user['full_name']) ?></strong>
                                <span style="color: #64748b; font-size: 0.9em; margin-left: 10px;">
                                    (@<?= e($user['username']) ?>)
                                </span>
                            </div>
                            
                            <div>
                                <span class="badge badge-<?= $user['role'] === 'coordinador' ? 'success' : 'secondary' ?>">
                                    <?= ucfirst($user['role']) ?>
                                </span>
                            </div>
                        </div>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>

    <script src="../assets/js/dragdrop.js"></script>
    <script src="../assets/js/main.js"></script>
</body>
</html>
