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

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

RoleMiddleware::auth();
$auth = new AuthService();

$controller = new ScheduleController();

if (isPost() && ($auth->isCoordinador() || $auth->isAdmin())) {
    $controller->update();
}

// Obtener horarios en formato amigable
// ScheduleController por defecto retorna array flat.
// Para la vista es mejor tenerlo indexado por día.
$model = new \App\Models\Schedule();
$schedules = $model->getScheduleArray();

$days = ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'];

// Función helper local para vista inputs
function getTimeVal($schedules, $day, $key) {
    return isset($schedules[$day][$key]) ? substr($schedules[$day][$key], 0, 5) : '';
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Horarios - Contenedor Ibiza</title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <?php include 'partials/navbar.php'; ?>

    <div class="container">
        <h1 class="card-title mb-4">⏰ Horarios de Apertura</h1>

        <?php if ($msg = flash('success')): ?>
            <div class="alert alert-success"><?= e($msg) ?></div>
        <?php endif; ?>

        <?php 
        $canEdit = $auth->isCoordinador() || $auth->isAdmin();
        ?>

        <div class="card">
            <?php if ($canEdit): ?>
                <form method="POST">
                    <input type="hidden" name="csrf_token" value="<?= csrfToken() ?>">
            <?php endif; ?>

            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Día</th>
                            <th>Horario Mañana/Tarde</th>
                            <th>Horario Tarde/Noche</th>
                            <th>Estado</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($days as $idx => $dayName): ?>
                            <tr>
                                <td style="font-weight: 600;"><?= $dayName ?></td>
                                
                                <?php if ($canEdit): ?>
                                    <td>
                                        <input type="time" class="form-control" style="width: 140px;"
                                            name="schedule[<?= $idx ?>][time1]" 
                                            value="<?= getTimeVal($schedules, $idx, 'opening_time_1') ?>">
                                    </td>
                                    <td>
                                        <input type="time" class="form-control" style="width: 140px;"
                                            name="schedule[<?= $idx ?>][time2]" 
                                            value="<?= getTimeVal($schedules, $idx, 'opening_time_2') ?>">
                                    </td>
                                <?php else: ?>
                                    <td>
                                        <?= getTimeVal($schedules, $idx, 'opening_time_1') ?: '-' ?>
                                    </td>
                                    <td>
                                        <?= getTimeVal($schedules, $idx, 'opening_time_2') ?: '-' ?>
                                    </td>
                                <?php endif; ?>
                                
                                <td>
                                    <?php 
                                    $t1 = getTimeVal($schedules, $idx, 'opening_time_1');
                                    $t2 = getTimeVal($schedules, $idx, 'opening_time_2');
                                    if (!$t1 && !$t2) {
                                        echo '<span class="badge badge-danger">Cerrado</span>';
                                    } else {
                                        echo '<span class="badge badge-success">Abierto</span>';
                                    }
                                    ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <?php if ($canEdit): ?>
                <div style="margin-top: 1.5rem; text-align: right;">
                    <button type="submit" class="btn btn-primary">Guardar Cambios</button>
                </div>
                </form>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
