casoBase

 <?php

require_once "../cabesa.php";
?>
<?php

class Conexion
{
    static public function conectar()
    {
        return new PDO("mysql:host=localhost;dbname=despacho;charset=utf8", "root", "Juzgado-01");
    }
}


class CasosModel extends Conexion
{
    static public function obtenerCasos()
    {
        $stmt = Conexion::conectar()->prepare("SELECT * FROM casos");
        $stmt->execute();
        return $stmt->fetchAll();
    }

    static public function crearCaso($datos)
    {
        $stmt = Conexion::conectar()->prepare("INSERT INTO casos (numero_expediente, cliente_id, tipo_caso, juzgado, fecha_apertura, estado, descripcion) VALUES (:numero_expediente, :cliente_id, :tipo_caso, :juzgado, :fecha_apertura, :estado, :descripcion)");
        $stmt->bindParam(":numero_expediente", $datos['numero_expediente'], PDO::PARAM_STR);
        $stmt->bindParam(":cliente_id", $datos['cliente_id'], PDO::PARAM_STR);
        $stmt->bindParam(":tipo_caso", $datos['tipo_caso'], PDO::PARAM_STR);
        $stmt->bindParam(":juzgado", $datos['juzgado'], PDO::PARAM_STR);
        $stmt->bindParam(":fecha_apertura", $datos['fecha_apertura'], PDO::PARAM_STR);
        $stmt->bindParam(":estado", $datos['estado'], PDO::PARAM_STR);
        $stmt->bindParam(":descripcion", $datos['descripcion'], PDO::PARAM_STR);
        if($stmt->execute()){
            return "success";
        } else {
            return "error";
        }
    }

    static public function actualizarCaso($datos)
    {
        $stmt = Conexion::conectar()->prepare("UPDATE casos SET numero_expediente = :numero_expediente, cliente_id = :cliente_id, tipo_caso = :tipo_caso, juzgado = :juzgado, fecha_apertura = :fecha_apertura, estado = :estado, descripcion = :descripcion WHERE id = :id");
        $stmt->bindParam(":id", $datos['id'], PDO::PARAM_INT);
        $stmt->bindParam(":numero_expediente", $datos['numero_expediente'], PDO::PARAM_STR);
        $stmt->bindParam(":cliente_id", $datos['cliente_id'], PDO::PARAM_INT);
        $stmt->bindParam(":tipo_caso", $datos['tipo_caso'], PDO::PARAM_STR);
        $stmt->bindParam(":juzgado", $datos['juzgado'], PDO::PARAM_STR);
        $stmt->bindParam(":fecha_apertura", $datos['fecha_apertura'], PDO::PARAM_STR);
        $stmt->bindParam(":estado", $datos['estado'], PDO::PARAM_STR);
        $stmt->bindParam(":descripcion", $datos['descripcion'], PDO::PARAM_STR);
        if($stmt->execute()){
            return "success";
        } else {
            return "error";
        }
    }

    static public function eliminarCaso($id)
    {
        $stmt = Conexion::conectar()->prepare("DELETE FROM casos WHERE id = :id");
        $stmt->bindParam(":id", $id, PDO::PARAM_INT);
        if($stmt->execute()){
            return "success";
        } else {
            return "error";
        }
    }
}


// Manejo de solicitudes POST para CRUD
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    switch ($_POST['action']) {
        case 'crear':
            $datos = [
                "numero_expediente" => $_POST["numero_expediente"],
                "cliente_id" => $_POST["cliente_id"],
                "tipo_caso" => $_POST["tipo_caso"],
                "juzgado" => $_POST["juzgado"],
                "fecha_apertura" => $_POST["fecha_apertura"],
                "estado" => $_POST["estado"],
                "descripcion" => $_POST["descripcion"]
            ];
            $resultado = CasosModel::crearCaso($datos);
            if ($resultado === true) {
                echo '<script>
                    if (window.history.replaceState) {
                        window.history.replaceState(null, null, window.location.href);
                    }
                 
                         window.location = "agenda/casoBase.php";
                </script>';
            } else {
                echo '<script>
                    if (window.history.replaceState) {
                        window.history.replaceState(null, null, window.location.href);
                    }
                   
                                  window.location.reload();
                </script>';
            }
            break;
        case 'actualizar':
            $datos = [
                "id" => $_POST["id"],
                "numero_expediente" => $_POST["numero_expediente"],
                "cliente_id" => $_POST["cliente_id"],
                "tipo_caso" => $_POST["tipo_caso"],
                "juzgado" => $_POST["juzgado"],
                "fecha_apertura" => $_POST["fecha_apertura"],
                "estado" => $_POST["estado"],
                "descripcion" => $_POST["descripcion"]
            ];
            $resultado = CasosModel::actualizarCaso($datos);
            if ($resultado === true) {
                echo '<script>
                    if (window.history.replaceState) {
                   
                        window.history.replaceState(null, null, window.location.href);
                       
                    }
                window.location = "agenda/casoBase.php";
                </script>';
            } else {
                echo '<script>
                    if (window.history.replaceState) {
                        window.history.replaceState(null, null, window.location.href);
                    }
                    window.location.reload();
                </script>';
            }
            break;
        case 'eliminar':
            $resultado = CasosModel::eliminarCaso($_POST['id']);
            if ($resultado === true) {
                echo '<script>
                    if (window.history.replaceState) {
                       
                        window.history.replaceState(null, null, window.location.href);
                    }
                window.location = "agenda/casoBase.php";
                </script>';
            } else {
                echo '<script>
                    if (window.history.replaceState) {
                        window.history.replaceState(null, null, window.location.href);
                    }
                    window.location.reload();
                </script>';
            }
            break;
    }
}

// Obtener todos los casos
$casos = CasosModel::obtenerCasos();
?>

<div class="container mt-5">
    <h3>Gestión de Casos</h3>
    <button class="btn btn-success mb-3" data-bs-toggle="modal" data-bs-target="#crearModal">Crear Caso</button>
    <table class="table">
        <thead class="table-dark">
            <tr>
                <th>ID</th>
                <th>Expediente</th>
                <th>Cliente</th>
                <th>Tipo de Caso</th>
                <th>Juzgado</th>
                <th>Fecha de Apertura</th>
                <th>Estado</th>
                <th>Descripción</th>
                <th>Acciones</th>
            </tr>
        </thead>
        <tbody>
    <?php foreach ($casos as $caso) { ?>
        <tr>
            <td><?php echo htmlspecialchars($caso['id']); ?></td>
            <td>
                <a href="expediente.php?id=<?php echo htmlspecialchars($caso['id']); ?>">
                    <?php echo htmlspecialchars($caso['numero_expediente']); ?>
                </a>
            </td>
            <td><?php echo htmlspecialchars($caso['cliente_id']); ?></td>
            <td><?php echo htmlspecialchars($caso['tipo_caso']); ?></td>
            <td><?php echo htmlspecialchars($caso['juzgado']); ?></td>
            <td><?php echo htmlspecialchars($caso['fecha_apertura']); ?></td>
            <td><?php echo htmlspecialchars($caso['estado']); ?></td>
            <td><?php echo htmlspecialchars($caso['descripcion']); ?></td>
            <td>
                <div class="btn-group">
                    <a href="index.php?pagina=exp/expBase&id=<?php echo htmlspecialchars($caso['id']); ?>&carpeta=<?php echo htmlspecialchars($caso['numero_expediente']); ?>" class="btn btn-success active" aria-current="page">Consultar</a>
                    <a href="index.php?pagina=solicitud/expsolicitud&carpeta=<?php echo htmlspecialchars($caso['numero_expediente']); ?>" class="btn btn-success active" aria-current="page">Solicitudes</a>
                    <a href="index.php?pagina=notificacion/expnotificacion&carpeta=<?php echo htmlspecialchars($caso['numero_expediente']); ?>" class="btn btn-success active" aria-current="page">Notificaciones</a>
                    <a href="index.php?pagina=audiencias/expaudiencia&carpeta=<?php echo htmlspecialchars($caso['numero_expediente']); ?>" class="btn btn-success active" aria-current="page">Audiencias</a>
                    <a href="index.php?pagina=audiencias/audiencia&id=<?php echo htmlspecialchars($caso['id']); ?>&carpeta=<?php echo htmlspecialchars($caso['numero_expediente']); ?>" class="btn btn-success active" aria-current="page">Historial</a>
                </div>
                <form method="post" style="display: inline-block;">
                    <input type="hidden" name="action" value="eliminar">
                    <input type="hidden" name="id" value="<?php echo htmlspecialchars($caso['id']); ?>">
                    <button type="submit" class="btn btn-danger" onclick="return confirm('¿Está seguro de eliminar este caso?')">Eliminar</button>
                </form>
            </td>
        </tr>
    <?php } ?>
</tbody>

    </table>
</div>

<!-- Modal Crear -->
<div class="modal fade" id="crearModal" tabindex="-1" aria-labelledby="crearModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="crearModalLabel">Crear Caso</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form method="post">
                    <input type="hidden" name="action" value="crear">
                    <div class="row">
                        <div class="col-6 mb-3">
                            <label for="numero_expediente" class="form-label">Numero de Expediente</label>
                            <input type="text" class="form-control" id="numero_expediente" name="numero_expediente" required>
                        </div>
                        <div class="col-6 mb-3">
                            <label for="tipo_caso" class="form-label">Tipo de Caso</label>
                            <select class="form-select" id="tipo_caso" name="tipo_caso" required>
                                <option value="Control Penal">Control Penal</option>
                                <option value="Civil">Civil</option>
                                <option value="Familiar">Familiar</option>
                                <option value="Laboral">Laboral</option>
                            </select>
                        </div>
                        <div class="col-6 mb-3">
                            <label for="cliente_id" class="form-label">Cliente ID</label>
                            <input type="text" class="form-control" id="cliente_id" name="cliente_id" required>
                        </div>
                        <div class="col-6 mb-3">
                            <label for="juzgado" class="form-label">Juzgado</label>
                            <input type="text" class="form-control" id="juzgado" name="juzgado" required>
                        </div>
                        <div class="col-6 mb-3">
                            <label for="fecha_apertura" class="form-label">Fecha de Apertura</label>
                            <input type="date" class="form-control" id="fecha_apertura" name="fecha_apertura" required>
                        </div>
                        <div class="col-6 mb-3">
                            <label for="estado" class="form-label">Estado</label>
                            <select class="form-select" id="estado" name="estado" required>
                                <option value="Activo">Activo</option>
                                <option value="Cerrado">Cerrado</option>
                                <option value="Archivado">Archivado</option>
                            </select>
                        </div>
                        <div class="col-12 mb-3">
                            <label for="descripcion" class="form-label">Descripción</label>
                            <textarea class="form-control" id="descripcion" name="descripcion"></textarea>
                        </div>
                    </div>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cerrar</button>
                    <button type="submit" class="btn btn-success">Crear</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js"></script>


<?php
require_once "../pie.php";
?>

Comentarios