using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using SistemaGestionAcademias.Data; using SistemaGestionAcademias.Models; namespace SistemaGestionAcademias.Controllers { [Authorize] public class CategoriasController : Controller { private readonly ApplicationDbContext _context; public CategoriasController(ApplicationDbContext context) { _context = context; } // GET: Categorias public async Task Index() { return View(await _context.Categorias.ToListAsync()); } // GET: Categorias/Details/5 public async Task Details(int? id) { if (id == null) return NotFound(); var categoria = await _context.Categorias.FirstOrDefaultAsync(m => m.Id == id); if (categoria == null) return NotFound(); return View(categoria); } // GET: Categorias/Create public IActionResult Create() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public async Task Create([Bind("Id,Nombre,Descripcion,Estado")] Categoria categoria) { if (ModelState.IsValid) { _context.Add(categoria); await _context.SaveChangesAsync(); TempData["AlertTipo"] = "success"; TempData["AlertMensaje"] = "Categoría creada correctamente."; return RedirectToAction(nameof(Index)); } return View(categoria); } // GET: Categorias/Edit/5 public async Task Edit(int? id) { if (id == null) return NotFound(); var categoria = await _context.Categorias.FindAsync(id); if (categoria == null) return NotFound(); return View(categoria); } [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(int id, [Bind("Id,Nombre,Descripcion,Estado")] Categoria categoria) { if (id != categoria.Id) return NotFound(); if (ModelState.IsValid) { try { _context.Update(categoria); await _context.SaveChangesAsync(); TempData["AlertTipo"] = "success"; TempData["AlertMensaje"] = "Categoría actualizada."; } catch (DbUpdateConcurrencyException) { if (!CategoriaExists(categoria.Id)) return NotFound(); else throw; } return RedirectToAction(nameof(Index)); } return View(categoria); } // --- NUEVO MÉTODO: CAMBIAR ESTADO RÁPIDO (TOGGLE) --- public async Task ToggleStatus(int? id) { if (id == null) return NotFound(); var categoria = await _context.Categorias.FindAsync(id); if (categoria == null) return NotFound(); // Invertimos el estado categoria.Estado = !categoria.Estado; _context.Update(categoria); await _context.SaveChangesAsync(); // Mensaje de feedback TempData["AlertTipo"] = "success"; TempData["AlertMensaje"] = categoria.Estado ? $"La categoría '{categoria.Nombre}' ha sido ACTIVADA." : $"La categoría '{categoria.Nombre}' ha sido DESACTIVADA."; return RedirectToAction(nameof(Index)); } // Mantenemos Delete solo para administradores (Opcional, pero seguro) [Authorize(Roles = "Admin")] public async Task Delete(int? id) { if (id == null) return NotFound(); var categoria = await _context.Categorias.FirstOrDefaultAsync(m => m.Id == id); if (categoria == null) return NotFound(); return View(categoria); } [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] [Authorize(Roles = "Admin")] public async Task DeleteConfirmed(int id) { var categoria = await _context.Categorias.FindAsync(id); if (categoria != null) _context.Categorias.Remove(categoria); await _context.SaveChangesAsync(); TempData["AlertTipo"] = "success"; TempData["AlertMensaje"] = "Categoría eliminada permanentemente."; return RedirectToAction(nameof(Index)); } private bool CategoriaExists(int id) { return _context.Categorias.Any(e => e.Id == id); } } }