<?php
include_once '../../controllers/MateriaController.php';
include_once '../../controllers/ModalidadController.php';
include_once '../../controllers/TurnoController.php';

$materiaController = new MateriaController();
$materias = $materiaController->listarMaterias();

$modalidadController = new ModalidadController();
$modalidades = $modalidadController->listarModalidades();

$turnoController = new TurnoController();
$turnos = $turnoController->listarTurnos();

$carrera_id = isset($_GET["carrera"]) && $_GET["carrera"] !== "" ? $_GET["carrera"] : null; // Tratamos cadena vacía como 'null'
$materias = $materiaController->listarMaterias($carrera_id); // Asignamos las materias retornadas a la variable

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (isset($_POST["agregar"])) {
    // Procesar horarios
    $horarios = [];
    for ($i = 0; $i < count($_POST["dia"]); $i++) {
      $horarios[] = [
                'dia' => $_POST['dia'][$i],
                'hora_ingreso' => $_POST['hora_ingreso'][$i],
                'hora_salida' => $_POST['hora_salida'][$i]
            ];
    }

    $resultado = $materiaController->agregarMateria(
            $_POST['cod_carrera_int'],
            $_POST['codigo_mat_int'],
            $_POST['sigla_mat_varchar'],
            $_POST['nombre_mat_varchar'],
            $_POST['codigo_nivel_int'],
            $_POST['estado_bt'],
            $_POST['sucursal_varchar'],
            $_POST['semestre'],
            $_POST['modalidad_id'],
            $_POST['turno_id'],
            $horarios
        );

    if ($resultado) {
      // Usamos SweetAlert2 en lugar de alert nativo
      echo "<script>
        document.addEventListener('DOMContentLoaded', function() {
          Swal.fire({
            title: '¡Éxito!',
            text: 'Materia agregada correctamente',
            icon: 'success',
            timer: 2000,
            showConfirmButton: false
          }).then(function() {
            window.location.href = 'gestionar_materias.php';
          });
        });
      </script>";
    } else {
      echo "<script>
        document.addEventListener('DOMContentLoaded', function() {
          Swal.fire({
            title: 'Error',
            text: 'No se pudo agregar la materia',
            icon: 'error',
            confirmButtonText: 'Aceptar'
          });
        });
      </script>";
    }
  }
}
?>

<!DOCTYPE html>
<html lang="es">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gestionar Materias</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
    <script src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <style>
        :root {
            --primary: #6366f1;
            --primary-hover: #4f46e5;
            --secondary: #06b6d4;
            --secondary-hover: #0891b2;
            --accent: #f43f5e;
            --accent-hover: #e11d48;
            --success: #10b981;
            --warning: #f59e0b;
            --danger: #ef4444;
            --info: #3b82f6;
            --background: #f8fafc;
            --foreground: #0f172a;
            --card: rgba(255, 255, 255, 0.8);
            --card-foreground: #1e293b;
            --border: #e2e8f0;
            --input: #e2e8f0;
            --ring: #6366f1;
            --muted: #94a3b8;
        }

        body {
            font-family: 'Inter', sans-serif;
            background-color: var(--background);
            color: var(--foreground);
            background-image: 
                radial-gradient(at 0% 0%, rgba(99, 102, 241, 0.1) 0px, transparent 50%),
                radial-gradient(at 100% 0%, rgba(6, 182, 212, 0.1) 0px, transparent 50%),
                radial-gradient(at 100% 100%, rgba(244, 63, 94, 0.1) 0px, transparent 50%),
                radial-gradient(at 0% 100%, rgba(16, 185, 129, 0.1) 0px, transparent 50%);
            background-attachment: fixed;
            background-size: cover;
        }

        .glass {
            background: var(--card);
            backdrop-filter: blur(12px);
            -webkit-backdrop-filter: blur(12px);
            border: 1px solid rgba(255, 255, 255, 0.3);
            box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.07);
        }

        .card {
            border-radius: 1.5rem;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.05), 0 10px 10px -5px rgba(0, 0, 0, 0.02);
        }

        .btn {
            position: relative;
            overflow: hidden;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            font-weight: 500;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            border-radius: 0.75rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }

        .btn::after {
            content: '';
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            pointer-events: none;
            background-image: radial-gradient(circle, #fff 10%, transparent 10.01%);
            background-repeat: no-repeat;
            background-position: 50%;
            transform: scale(10, 10);
            opacity: 0;
            transition: transform 0.5s, opacity 1s;
        }

        .btn:active::after {
            transform: scale(0, 0);
            opacity: 0.3;
            transition: 0s;
        }

        .btn-primary {
            background-color: var(--primary);
            color: white;
        }

        .btn-primary:hover {
            background-color: var(--primary-hover);
            transform: translateY(-2px);
        }

        .btn-secondary {
            background-color: var(--secondary);
            color: white;
        }

        .btn-secondary:hover {
            background-color: var(--secondary-hover);
            transform: translateY(-2px);
        }

        .btn-accent {
            background-color: var(--accent);
            color: white;
        }

        .btn-accent:hover {
            background-color: var(--accent-hover);
            transform: translateY(-2px);
        }

        .btn-success {
            background-color: var(--success);
            color: white;
        }

        .btn-success:hover {
            background-color: #0d9488;
            transform: translateY(-2px);
        }

        .btn-warning {
            background-color: var(--warning);
            color: white;
        }

        .btn-warning:hover {
            background-color: #d97706;
            transform: translateY(-2px);
        }

        .form-input {
            border: 1px solid var(--input);
            border-radius: 0.75rem;
            padding: 0.75rem 1rem 0.75rem 2.5rem;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            font-size: 0.875rem;
            box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
            background-color: white;
        }

        .form-input:focus {
            outline: none;
            border-color: var(--ring);
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
        }

        .form-select {
            border: 1px solid var(--input);
            border-radius: 0.75rem;
            padding: 0.75rem 1rem 0.75rem 2.5rem;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            font-size: 0.875rem;
            box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
            background-color: white;
            background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
            background-position: right 0.75rem center;
            background-repeat: no-repeat;
            background-size: 1.25em 1.25em;
            padding-right: 2.5rem;
            -webkit-appearance: none;
            -moz-appearance: none;
            appearance: none;
        }

        .form-select:focus {
            outline: none;
            border-color: var(--ring);
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
        }

        .table-container {
            overflow-x: auto;
            border-radius: 1rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }

        .table {
            width: 100%;
            border-collapse: separate;
            border-spacing: 0;
        }

        .table th {
            background-color: rgba(243, 244, 246, 0.8);
            padding: 1rem;
            text-align: left;
            font-weight: 600;
            text-transform: uppercase;
            font-size: 0.75rem;
            letter-spacing: 0.05em;
            color: var(--foreground);
            border-bottom: 1px solid var(--border);
        }

        .table td {
            padding: 1rem;
            border-bottom: 1px solid var(--border);
            background-color: rgba(255, 255, 255, 0.6);
            transition: all 0.2s;
        }

        .table tr:last-child td {
            border-bottom: none;
        }

        .table tr:hover td {
            background-color: rgba(243, 244, 246, 0.8);
        }

        .badge {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 0.35rem 0.75rem;
            border-radius: 9999px;
            font-size: 0.75rem;
            font-weight: 500;
            letter-spacing: 0.025em;
            box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
        }

        .badge-primary {
            background-color: rgba(99, 102, 241, 0.1);
            color: var(--primary);
        }

        .badge-secondary {
            background-color: rgba(6, 182, 212, 0.1);
            color: var(--secondary);
        }

        .badge-accent {
            background-color: rgba(244, 63, 94, 0.1);
            color: var(--accent);
        }

        .badge-success {
            background-color: rgba(16, 185, 129, 0.1);
            color: var(--success);
        }

        .badge-warning {
            background-color: rgba(245, 158, 11, 0.1);
            color: var(--warning);
        }

        .badge-info {
            background-color: rgba(59, 130, 246, 0.1);
            color: var(--info);
        }

        .horario-item {
            border: 1px solid var(--border);
            border-radius: 1rem;
            padding: 1.25rem;
            margin-bottom: 1rem;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            background-color: white;
        }

        .horario-item:hover {
            border-color: var(--ring);
            box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1);
        }

        .tab-container {
            display: flex;
            margin-bottom: 1.5rem;
            background-color: rgba(255, 255, 255, 0.5);
            backdrop-filter: blur(8px);
            border-radius: 9999px;
            padding: 0.25rem;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
        }

        .tab {
            padding: 0.75rem 1.5rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            border-radius: 9999px;
            position: relative;
            z-index: 1;
        }

        .tab.active {
            color: white;
            background-color: var(--primary);
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }

        .tab:hover:not(.active) {
            background-color: rgba(243, 244, 246, 0.8);
        }

        .fade-in {
            animation: fadeIn 0.5s cubic-bezier(0.4, 0, 0.2, 1);
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .slide-in {
            animation: slideIn 0.5s cubic-bezier(0.4, 0, 0.2, 1);
        }

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateX(-20px);
            }
            to {
                opacity: 1;
                transform: translateX(0);
            }
        }

        .scale-in {
            animation: scaleIn 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }

        @keyframes scaleIn {
            from {
                opacity: 0;
                transform: scale(0.95);
            }
            to {
                opacity: 1;
                transform: scale(1);
            }
        }

        .tooltip {
            position: relative;
            display: inline-block;
        }

        .tooltip .tooltiptext {
            visibility: hidden;
            width: 200px;
            background-color: rgba(15, 23, 42, 0.9);
            color: white;
            text-align: center;
            border-radius: 0.5rem;
            padding: 0.5rem;
            position: absolute;
            z-index: 10;
            bottom: 125%;
            left: 50%;
            margin-left: -100px;
            opacity: 0;
            transition: opacity 0.3s, transform 0.3s;
            transform: translateY(10px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
            font-size: 0.75rem;
        }

        .tooltip:hover .tooltiptext {
            visibility: visible;
            opacity: 1;
            transform: translateY(0);
        }

        .tooltip .tooltiptext::after {
            content: "";
            position: absolute;
            top: 100%;
            left: 50%;
            margin-left: -5px;
            border-width: 5px;
            border-style: solid;
            border-color: rgba(15, 23, 42, 0.9) transparent transparent transparent;
        }

        /* Scrollbar */
        ::-webkit-scrollbar {
            width: 8px;
            height: 8px;
        }

        ::-webkit-scrollbar-track {
            background: rgba(243, 244, 246, 0.5);
            border-radius: 9999px;
        }

        ::-webkit-scrollbar-thumb {
            background: rgba(99, 102, 241, 0.5);
            border-radius: 9999px;
        }

        ::-webkit-scrollbar-thumb:hover {
            background: rgba(99, 102, 241, 0.7);
        }

        /* Improved floating label */
        .input-container {
            position: relative;
        }

        .input-container svg {
            position: absolute;
            left: 0.75rem;
            top: 50%;
            transform: translateY(-50%);
            color: var(--muted);
            width: 1.25rem;
            height: 1.25rem;
            z-index: 1;
        }

        .input-container label {
            position: absolute;
            left: 2.5rem;
            top: 0;
            padding: 0 0.25rem;
            color: var(--muted);
            font-size: 0.75rem;
            font-weight: 500;
            transform: translateY(-50%);
            background-color: white;
            z-index: 1;
        }

        /* Animated icons */
        .icon-container {
            position: relative;
            width: 40px;
            height: 40px;
        }

        .icon-pulse {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: currentColor;
            opacity: 0.2;
            animation: iconPulse 2s infinite;
        }

        @keyframes iconPulse {
            0% {
                transform: scale(1);
                opacity: 0.2;
            }
            50% {
                transform: scale(1.3);
                opacity: 0;
            }
            100% {
                transform: scale(1);
                opacity: 0.2;
            }
        }

        /* Animated background */
        .animated-bg {
            position: relative;
            overflow: hidden;
        }

        .animated-bg::before {
            content: "";
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: conic-gradient(
                transparent, 
                rgba(99, 102, 241, 0.1), 
                transparent 30%
            );
            animation: rotate 4s linear infinite;
        }

        .animated-bg::after {
            content: "";
            position: absolute;
            inset: 2px;
            background: white;
            border-radius: inherit;
            z-index: 0;
        }

        .animated-bg > * {
            position: relative;
            z-index: 1;
        }

        @keyframes rotate {
            100% {
                transform: rotate(1turn);
            }
        }

        /* Confetti animation */
        @keyframes confetti {
            0% { transform: translateY(0) rotateZ(0); opacity: 1; }
            100% { transform: translateY(25px) rotateZ(360deg); opacity: 0; }
        }

        .confetti {
            position: absolute;
            width: 8px;
            height: 8px;
            opacity: 0;
        }

        /* Notification badge */
        .notification-badge {
            position: absolute;
            top: -5px;
            right: -5px;
            width: 18px;
            height: 18px;
            background-color: var(--accent);
            color: white;
            border-radius: 50%;
            font-size: 0.7rem;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>

<body>
    <div class="max-w-7xl mx-auto p-6">
        <!-- Header con animación -->
        <div class="text-center mb-8 animate__animated animate__fadeInDown">
            <div class="flex justify-center items-center mb-4">
                <div class="relative">
                    <img src="../../public/images/logo.png" alt="Logo de la Institución" class="w-32 z-10 relative">
                    <div class="absolute inset-0 bg-indigo-500 filter blur-xl opacity-20 rounded-full"></div>
                </div>
                <lottie-player src="https://assets2.lottiefiles.com/packages/lf20_inti4oxf.json" background="transparent" speed="1" style="width: 60px; height: 60px;" loop autoplay></lottie-player>
            </div>
            <h2 class="text-4xl font-bold text-gray-800 mt-4 relative inline-block">
                Gestionar Materias
                <span class="absolute bottom-0 left-0 w-full h-1.5 bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500 rounded-full"></span>
            </h2>
        </div>

        <!-- Botón de volver con animación -->
        <a href="../superadmin/dashboard.php" class="inline-flex items-center btn btn-primary px-6 py-2.5 rounded-full mb-6 animate__animated animate__fadeInLeft">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 20 20" fill="currentColor">
                <path fill-rule="evenodd" d="M9.707 16.707a1 1 0 01-1.414 0l-6-6a1 1 0 010-1.414l6-6a1 1 0 011.414 1.414L5.414 9H17a1 1 0 110 2H5.414l4.293 4.293a1 1 0 010 1.414z" clip-rule="evenodd" />
            </svg>
            Volver al Menú Principal
        </a>

        <!-- Tabs para navegación -->
        <div class="tab-container mb-8 animate__animated animate__fadeIn max-w-md mx-auto">
            <div class="tab active" onclick="showTab('agregar')">
                <div class="flex items-center">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 20 20" fill="currentColor">
                        <path fill-rule="evenodd" d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z" clip-rule="evenodd" />
                    </svg>
                    Agregar Materia
                </div>
            </div>
            <div class="tab" onclick="showTab('listar')">
                <div class="flex items-center">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 20 20" fill="currentColor">
                        <path fill-rule="evenodd" d="M3 4a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm0 4a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm0 4a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm0 4a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z" clip-rule="evenodd" />
                    </svg>
                    Listar Materias
                </div>
            </div>
        </div>

        <!-- Formulario para agregar materias -->
        <div id="tab-agregar" class="card glass p-8 mb-8 fade-in">
            <div class="flex items-center mb-6">
                <div class="icon-container mr-4">
                    <div class="icon-pulse bg-indigo-500"></div>
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10 text-indigo-500 relative z-10" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
                    </svg>
                </div>
                <h3 class="text-2xl font-bold text-gray-800">Agregar Materia</h3>
            </div>
            
            <form action="gestionar_materias.php" method="post" id="materiaForm" class="grid grid-cols-1 md:grid-cols-2 gap-8">
                <div class="slide-in input-container" style="animation-delay: 0.1s">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"></path>
                    </svg>
                    <label for="cod_carrera_int">Carrera</label>
                    <select id="cod_carrera_int" name="cod_carrera_int" required class="form-select w-full">
                        <option value="" disabled selected></option>
                        <?php
                        include_once '../../controllers/CarreraController.php';
                        $carreraController = new CarreraController();
                        $carreras = $carreraController->listarCarreras();
                        foreach ($carreras as $carrera) {
                            echo "<option value='" . $carrera['id'] . "'>" . $carrera['carrera'] . "</option>";
                        }
                        ?>
                    </select>
                </div>
                <div class="slide-in input-container" style="animation-delay: 0.2s">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"></path>
                        <path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"></path>
                    </svg>
                    <label for="codigo_mat_int">Código de la Materia</label>
                    <input type="number" id="codigo_mat_int" name="codigo_mat_int" required class="form-input w-full">
                </div>
                <div class="slide-in input-container" style="animation-delay: 0.3s">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M20 14.66V20a2  stroke-linecap="round" stroke-linejoin="round">
                        <path d="M20 14.66V20a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h5.34"></path>
                        <polygon points="18 2 22 6 12 16 8 16 8 12 18 2"></polygon>
                    </svg>
                    <label for="sigla_mat_varchar">Sigla de la Materia</label>
                    <input type="text" id="sigla_mat_varchar" name="sigla_mat_varchar" required class="form-input w-full">
                </div>
                <div class="slide-in input-container" style="animation-delay: 0.4s">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
                        <polyline points="14 2 14 8 20 8"></polyline>
                        <line x1="16" y1="13" x2="8" y2="13"></line>
                        <line x1="16" y1="17" x2="8" y2="17"></line>
                        <polyline points="10 9 9 9 8 9"></polyline>
                    </svg>
                    <label for="nombre_mat_varchar">Nombre de la Materia</label>
                    <input type="text" id="nombre_mat_varchar" name="nombre_mat_varchar" required class="form-input w-full">
                </div>
                <div class="slide-in input-container" style="animation-delay: 0.5s">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M22 12h-4l-3 9L9 3l-3 9H2"></path>
                    </svg>
                    <label for="codigo_nivel_int">Código del Nivel</label>
                    <select id="codigo_nivel_int" name="codigo_nivel_int" required class="form-select w-full">
                        <option value="" disabled selected></option>
                        <option value="1">Medio</option>
                        <option value="2">Superior</option>
                    </select>
                </div>
                <div class="slide-in input-container" style="animation-delay: 0.6s">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <circle cx="12" cy="12" r="10"></circle>
                        <polyline points="12 6 12 12 16 14"></polyline>
                    </svg>
                    <label for="estado_bt">Estado</label>
                    <select id="estado_bt" name="estado_bt" required class="form-select w-full">
                        <option value="" disabled selected></option>
                        <option value="0">Activo</option>
                        <option value="1">Inactivo</option>
                    </select>
                </div>
                <div class="slide-in input-container" style="animation-delay: 0.7s">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"></path>
                        <circle cx="12" cy="10" r="3"></circle>
                    </svg>
                    <label for="sucursal_varchar">Sucursal</label>
                    <input type="text" id="sucursal_varchar" name="sucursal_varchar" required class="form-input w-full">
                </div>
                <div class="slide-in input-container" style="animation-delay: 0.8s">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect>
                        <line x1="16" y1="2" x2="16" y2="6"></line>
                        <line x1="8" y1="2" x2="8" y2="6"></line>
                        <line x1="3" y1="10" x2="21" y2="10"></line>
                    </svg>
                    <label for="semestre">Semestre</label>
                    <select id="semestre" name="semestre" required class="form-select w-full">
                        <option value="" disabled selected></option>
                        <option value="1">1er Semestre</option>
                        <option value="2">2do Semestre</option>
                        <option value="3">3er Semestre</option>
                        <option value="4">4to Semestre</option>
                        <option value="5">5to Semestre</option>
                        <option value="6">6to Semestre</option>
                    </select>
                </div>
                <div class="slide-in input-container" style="animation-delay: 0.9s">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path>
                    </svg>
                    <label for="modalidad_id">Modalidad</label>
                    <select id="modalidad_id" name="modalidad_id" required class="form-select w-full">
                        <option value="" disabled selected></option>
                        <?php foreach ($modalidades as $modalidad): ?>
                            <option value="<?php echo $modalidad['id']; ?>"><?php echo $modalidad['modalidad']; ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="slide-in input-container" style="animation-delay: 1s">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <circle cx="12" cy="12" r="10"></circle>
                        <polyline points="12 6 12 12 16 14"></polyline>
                    </svg>
                    <label for="turno_id">Turno</label>
                    <select id="turno_id" name="turno_id" required class="form-select w-full">
                        <option value="" disabled selected></option>
                        <?php foreach ($turnos as $turno): ?>
                            <option value="<?php echo $turno['id']; ?>"><?php echo $turno['turno']; ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="col-span-full slide-in" style="animation-delay: 1.1s">
                    <div class="flex items-center mb-4">
                        <div class="icon-container mr-3">
                            <div class="icon-pulse bg-green-500"></div>
                            <svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10 text-green-500 relative z-10" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
                            </svg>
                        </div>
                        <h4 class="text-xl font-bold text-gray-800">Horarios</h4>
                    </div>
                    
                    <div id="horarios-container">
                        <div class="horario-item animated-bg scale-in">
                            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                                <div class="input-container">
                                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect>
                                        <line x1="16" y1="2" x2="16" y2="6"></line>
                                        <line x1="8" y1="2" x2="8" y2="6"></line>
                                        <line x1="3" y1="10" x2="21" y2="10"></line>
                                    </svg>
                                    <label for="dia">Día</label>
                                    <select name="dia[]" required class="form-select w-full">
                                        <option value="" disabled selected></option>
                                        <option value="Lunes">Lunes</option>
                                        <option value="Martes">Martes</option>
                                        <option value="Miércoles">Miércoles</option>
                                        <option value="Jueves">Jueves</option>
                                        <option value="Viernes">Viernes</option>
                                        <option value="Sábado">Sábado</option>
                                    </select>
                                </div>
                                <div class="input-container">
                                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <circle cx="12" cy="12" r="10"></circle>
                                        <polyline points="12 6 12 12 16 14"></polyline>
                                    </svg>
                                    <label for="hora_ingreso">Hora de Ingreso</label>
                                    <input type="time" name="hora_ingreso[]" required class="form-input w-full">
                                </div>
                                <div class="input-container">
                                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <circle cx="12" cy="12" r="10"></circle>
                                        <polyline points="12 6 12 12 8 14"></polyline>
                                    </svg>
                                    <label for="hora_salida">Hora de Salida</label>
                                    <input type="time" name="hora_salida[]" required class="form-input w-full">
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <button type="button" onclick="addHorario()" class="mt-4 btn btn-secondary px-5 py-2.5 rounded-full flex items-center">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 20 20" fill="currentColor">
                            <path fill-rule="evenodd" d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z" clip-rule="evenodd" />
                        </svg>
                        Añadir Horario
                    </button>
                </div>

                <div class="col-span-full text-center slide-in" style="animation-delay: 1.2s">
                    <button type="submit" name="agregar" id="submitBtn" class="btn btn-primary px-8 py-3 rounded-full flex items-center justify-center mx-auto">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 20 20" fill="currentColor">
                            <path fill-rule="evenodd" d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z" clip-rule="evenodd" />
                        </svg>
                        Agregar Materia
                    </button>
                </div>
            </form>
        </div>

        <!-- Contenedor principal para listar materias -->
        <div id="tab-listar" class="card glass p-8 fade-in" style="display: none;">
            <div class="flex items-center mb-6">
                <div class="icon-container mr-4">
                    <div class="icon-pulse bg-blue-500"></div>
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10 text-blue-500 relative z-10" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
                    </svg>
                </div>
                <h3 class="text-2xl font-bold text-gray-800">Lista de Materias</h3>
            </div>

            <!-- Formulario para Filtrar por Carrera con estilo mejorado -->
            <form action="gestionar_materias.php" method="get" class="mb-8 glass p-6 rounded-xl">
                <div class="flex flex-col md:flex-row items-center gap-6">
                    <div class="w-full md:w-1/2 input-container">
                        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                            <path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"></path>
                        </svg>
                        <label for="carrera">Filtrar por Carrera</label>
                        <select id="carrera" name="carrera" class="form-select w-full">
                            <option value="" selected></option>
                            <?php
                            include_once '../../controllers/CarreraController.php';
                            $carreraController = new CarreraController();
                            $carreras = $carreraController->listarCarreras();
                            foreach ($carreras as $carrera) {
                                $selected = isset($_GET['carrera']) && $_GET['carrera'] == $carrera['id'] ? 'selected' : '';
                                echo "<option value='" . $carrera['id'] . "' $selected>" . $carrera['carrera'] . "</option>";
                            }
                            ?>
                        </select>
                    </div>
                    <div class="w-full md:w-1/2 md:flex md:justify-end md:items-end">
                        <button type="submit" class="btn btn-primary px-5 py-2.5 rounded-full w-full md:w-auto">
                            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 inline mr-2" viewBox="0 0 20 20" fill="currentColor">
                                <path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd" />
                            </svg>
                            Aplicar Filtro
                        </button>
                    </div>
                </div>
            </form>

            <!-- Tabla para mostrar las materias con estilos mejorados -->
            <div class="table-container glass">
                <table class="table">
                    <thead>
                        <tr>
                            <th class="rounded-tl-lg">Carrera</th>
                            <th>Nombre</th>
                            <th>Semestre</th>
                            <th>Modalidad</th>
                            <th>Turno</th>
                            <th>Horario</th>
                            <th class="rounded-tr-lg">Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (count($materias) > 0): ?>
                            <?php foreach ($materias as $index => $materia): ?>
                                <tr class="hover:bg-gray-50 transition-colors duration-150 ease-in-out" style="animation-delay: <?php echo $index * 0.05; ?>s">
                                    <td>
                                        <span class="badge badge-primary">
                                            <?php echo $materia['carrera_nombre']; ?>
                                        </span>
                                    </td>
                                    <td class="font-medium"><?php echo $materia['materia_nombre']; ?></td>
                                    <td>
                                        <span class="badge badge-secondary">
                                            <?php echo $materia['semestre']; ?>° Semestre
                                        </span>
                                    </td>
                                    <td>
                                        <span class="badge badge-success">
                                            <?php echo $materia['modalidad_nombre']; ?>
                                        </span>
                                    </td>
                                    <td>
                                        <span class="badge badge-accent">
                                            <?php echo $materia['turno_nombre']; ?>
                                        </span>
                                    </td>
                                    <td>
                                        <div class="tooltip">
                                            <span class="text-sm text-gray-600 truncate block max-w-[150px]">
                                                <?php echo $materia['horarios']; ?>
                                            </span>
                                            <span class="tooltiptext"><?php echo $materia['horarios']; ?></span>
                                        </div>
                                    </td>
                                    <td>
                                        <a href="editar_materia.php?id=<?php echo $materia['materia_id']; ?>"
                                        class="btn btn-warning text-white px-4 py-2 rounded-full inline-flex items-center">
                                            <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" viewBox="0 0 20 20" fill="currentColor">
                                                <path d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z" />
                                            </svg>
                                            Editar
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="7" class="text-center py-8 text-gray-500">
                                    <div class="flex flex-col items-center justify-center">
                                        <svg xmlns="http://www.w3.org/2000/svg" class="h-12 w-12 text-gray-400 mb-3" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.172 16.172a4 4 0 015.656 0M9 10h.01M15 10h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                                        </svg>
                                        <p class="text-lg font-medium">No se encontraron materias</p>
                                        <p class="text-sm">Intenta con otro filtro o agrega una nueva materia</p>
                                    </div>
                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
    // Función para añadir un nuevo horario con animación
    function addHorario() {
        const container = document.getElementById('horarios-container');
        const div = document.createElement('div');
        div.classList.add('horario-item', 'animated-bg', 'scale-in');
        div.innerHTML = `
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div class="input-container">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <rect x="3" y="4" width="18" height="18" rx="2" ry="2"></rect>
                        <line x1="16" y1="2" x2="16" y2="6"></line>
                        <line x1="8" y1="2" x2="8" y2="6"></line>
                        <line x1="3" y1="10" x2="21" y2="10"></line>
                    </svg>
                    <label for="dia">Día</label>
                    <select name="dia[]" required class="form-select w-full">
                        <option value="" disabled selected></option>
                        <option value="Lunes">Lunes</option>
                        <option value="Martes">Martes</option>
                        <option value="Miércoles">Miércoles</option>
                        <option value="Jueves">Jueves</option>
                        <option value="Viernes">Viernes</option>
                        <option value="Sábado">Sábado</option>
                    </select>
                </div>
                <div class="input-container">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <circle cx="12" cy="12" r="10"></circle>
                        <polyline points="12 6 12 12 16 14"></polyline>
                    </svg>
                    <label for="hora_ingreso">Hora de Ingreso</label>
                    <input type="time" name="hora_ingreso[]" required class="form-input w-full">
                </div>
                <div class="input-container">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <circle cx="12" cy="12" r="10"></circle>
                        <polyline points="12 6 12 12 8 14"></polyline>
                    </svg>
                    <label for="hora_salida">Hora de Salida</label>
                    <input type="time" name="hora_salida[]" required class="form-input w-full">
                </div>
            </div>
        `;
        container.appendChild(div);
        
        // Crear efecto de confeti
        createConfetti(container);
    }
    
    // Función para crear confeti
    function createConfetti(container) {
        const colors = ['#6366f1', '#06b6d4', '#f43f5e', '#10b981', '#f59e0b'];
        for (let i = 0; i < 30; i++) {
            const confetti = document.createElement('div');
            confetti.classList.add('confetti');
            confetti.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
            confetti.style.left = Math.random() * 100 + '%';
            confetti.style.top = Math.random() * 100 + '%';
            confetti.style.transform = `rotate(${Math.random() * 360}deg)`;
            confetti.style.animation = `confetti 1s ease forwards ${Math.random() * 0.5}s`;
            container.appendChild(confetti);
            
            // Eliminar confeti después de la animación
            setTimeout(() => {
                confetti.remove();
            }, 1500);
        }
    }

    // Función para mostrar/ocultar tabs con animación
    function showTab(tabName) {
        // Ocultar todos los tabs
        document.getElementById('tab-agregar').style.display = 'none';
        document.getElementById('tab-listar').style.display = 'none';
        
        // Mostrar el tab seleccionado con animación
        const selectedTab = document.getElementById('tab-' + tabName);
        selectedTab.style.display = 'block';
        selectedTab.classList.add('fade-in');
        
        // Actualizar clases activas
        const tabs = document.querySelectorAll('.tab');
        tabs.forEach(tab => {
            tab.classList.remove('active');
        });
        
        // Encontrar el tab que se hizo clic y marcarlo como activo
        event.target.closest('.tab').classList.add('active');
    }

    // Animación para las filas de la tabla
    document.addEventListener('DOMContentLoaded', function() {
        const rows = document.querySelectorAll('tbody tr');
        rows.forEach((row, index) => {
            row.style.animation = `fadeIn 0.3s cubic-bezier(0.4, 0, 0.2, 1) ${index * 0.1}s forwards`;
            row.style.opacity = '0';
        });
    });
    </script>

</body>

</html>

