From 27d8970f208576cec4b449ccd1ce23320db8a796 Mon Sep 17 00:00:00 2001
From: TheSilva7 <alexsilvasanchez777@gmail.com>
Date: Wed, 26 Mar 2025 23:48:49 -0600
Subject: [PATCH] Status

se hizo el edit para mostrar si un docente esta activo o no en la base de datos
---
 app/Http/Controllers/DocentesController.php   | 29 ++++++++++++++-----
 app/Models/Docentes.php                       |  3 +-
 ...27_054337_add_status_to_docentes_table.php | 28 ++++++++++++++++++
 resources/views/docentes.blade.php            | 26 +++++++----------
 routes/web.php                                |  1 +
 5 files changed, 63 insertions(+), 24 deletions(-)
 create mode 100644 database/migrations/2025_03_27_054337_add_status_to_docentes_table.php

diff --git a/app/Http/Controllers/DocentesController.php b/app/Http/Controllers/DocentesController.php
index 26dc233..ecefb71 100644
--- a/app/Http/Controllers/DocentesController.php
+++ b/app/Http/Controllers/DocentesController.php
@@ -19,11 +19,12 @@ class DocentesController extends Controller
 
         if ($busqueda) {
             // Busca docentes que coincidan con el término de búsqueda
-            $docentes = Docentes::where('nombre', 'LIKE', "%{$busqueda}%")
-                ->orWhere('correo', 'LIKE', "%{$busqueda}%")
-                ->orWhere('tipo_licencia', 'LIKE', "%{$busqueda}%")
-                ->orWhere('materia', 'LIKE', "%{$busqueda}%")
-                ->get();
+            $docentes = Docentes::where(function($query) use ($busqueda) {
+                $query->where('nombre', 'LIKE', "%{$busqueda}%")
+                    ->orWhere('correo', 'LIKE', "%{$busqueda}%")
+                    ->orWhere('tipo_licencia', 'LIKE', "%{$busqueda}%")
+                    ->orWhere('materia', 'LIKE', "%{$busqueda}%");
+            })->get();
 
             if ($docentes->isEmpty()) {
                 return redirect()->route('docentes.index')
@@ -79,7 +80,8 @@ class DocentesController extends Controller
     public function update(Request $request, $id)
     {
         $docente = Docentes::find($id);
-        $docente->update($request->all());
+        $docente->fill($request->all());
+        $docente->save();
         return redirect()->route('docentes.index')->with('success', 'Docente actualizado correctamente');
     }
 
@@ -89,8 +91,19 @@ class DocentesController extends Controller
     public function destroy($id)
     {
         $docente = Docentes::find($id);
-        $docente->delete();
-        return redirect()->route('docentes.index')->with('success', 'Docente eliminado correctamente');
+        $docente->status = false;
+        $docente->save();
+        return redirect()->route('docentes.index')->with('success', 'Docente desactivado correctamente');
+    }
+
+    public function toggleStatus($id)
+    {
+        $docente = Docentes::find($id);
+        $docente->status = !$docente->status;
+        $docente->save();
+
+        $mensaje = $docente->status ? 'Docente activado correctamente' : 'Docente desactivado correctamente';
+        return redirect()->route('docentes.index')->with('success', $mensaje);
     }
 
     public function export($format)
diff --git a/app/Models/Docentes.php b/app/Models/Docentes.php
index 8bd5954..ff46d73 100644
--- a/app/Models/Docentes.php
+++ b/app/Models/Docentes.php
@@ -14,6 +14,7 @@ class Docentes extends Model
         'telefono',
         'correo',
         'tipo_licencia',
-        'materia'
+        'materia',
+        'status'
     ];
 }
diff --git a/database/migrations/2025_03_27_054337_add_status_to_docentes_table.php b/database/migrations/2025_03_27_054337_add_status_to_docentes_table.php
new file mode 100644
index 0000000..921a4d6
--- /dev/null
+++ b/database/migrations/2025_03_27_054337_add_status_to_docentes_table.php
@@ -0,0 +1,28 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::table('docentes', function (Blueprint $table) {
+            $table->boolean('status')->default(true)->after('materia');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::table('docentes', function (Blueprint $table) {
+            $table->dropColumn('status');
+        });
+    }
+};
diff --git a/resources/views/docentes.blade.php b/resources/views/docentes.blade.php
index 470628c..c1c0ebc 100644
--- a/resources/views/docentes.blade.php
+++ b/resources/views/docentes.blade.php
@@ -82,6 +82,7 @@
                         <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Correo</th>
                         <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Tipo de Licencia</th>
                         <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Materia</th>
+                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Estado</th>
                         <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Acciones</th>
                     </tr>
                 </thead>
@@ -97,25 +98,20 @@
                         <td class="px-6 py-4 whitespace-nowrap text-sm">{{ $docente->correo }}</td>
                         <td class="px-6 py-4 whitespace-nowrap text-sm">{{ $docente->tipo_licencia }}</td>
                         <td class="px-6 py-4 whitespace-nowrap text-sm">{{ $docente->materia }}</td>
+                        <td class="px-6 py-4 whitespace-nowrap text-sm">
+                            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full {{ $docente->status ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800' }}">
+                                {{ $docente->status ? 'Activo' : 'Inactivo' }}
+                            </span>
+                        </td>
                         <td class="px-6 py-4 whitespace-nowrap text-sm">
                             <div class="flex gap-2">
-                                <a href="#"
-                                   onclick="confirmarEdicion('{{ route('docentes.edit', $docente->id) }}')"
-                                   class="text-blue-600 hover:text-blue-900">
+                                <a href="{{ route('docentes.edit', $docente->id) }}" class="text-blue-600 hover:text-blue-900">
                                     <i class="fas fa-edit"></i>
                                 </a>
-                                <form action="{{ route('docentes.destroy', $docente->id) }}"
-                                      method="POST"
-                                      class="inline"
-                                      onsubmit="return false;">
-                                    @csrf
-                                    @method('DELETE')
-                                    <button type="button"
-                                            onclick="confirmarEliminacion(this)"
-                                            class="text-red-600 hover:text-red-900">
-                                        <i class="fas fa-trash"></i>
-                                    </button>
-                                </form>
+                                <a href="{{ route('docentes.toggle-status', $docente->id) }}"
+                                   class="{{ $docente->status ? 'text-red-600 hover:text-red-900' : 'text-green-600 hover:text-green-900' }}">
+                                    <i class="fas {{ $docente->status ? 'fa-ban' : 'fa-check' }}"></i>
+                                </a>
                             </div>
                         </td>
                     </tr>
diff --git a/routes/web.php b/routes/web.php
index 91c4d56..4b10bad 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -42,6 +42,7 @@ Route::get('marcas/excel', [MarcaController::class, 'exportExcel'])->name('marca
 Route::get('marcas/pdf', [MarcaController::class, 'exportPDF'])->name('marcas.pdf');
 
 Route::get('/docentes/export/{format}', [DocentesController::class, 'export'])->name('docentes.export');
+Route::get('/docentes/{id}/toggle-status', [DocentesController::class, 'toggleStatus'])->name('docentes.toggle-status');
 
 // Rutas protegidas que requieren autenticación
 Route::middleware(['auth'])->group(function () {