<?php

include_once "includes/constantes.php";
include_once "includes/DBConnector.php";
include_once "includes/dSolicitudes.php";
include_once "includes/gestionActivos.php";
session_name("Activos");
session_start();
$rol = $_SESSION['rol'];
$id_activo = $_GET['ID_A'];
$departamento = $_GET['dep'];
$id_s = $_GET['ID'];
$db = new dSolicitudes(__HOST__, __USUARIODB__, __PASSDB__, __DATABASE__);
$dbs = new gestionActivos(__HOST__, __USUARIODB__, __PASSDB__, __DATABASE__);
$result = $dbs->actualizarEstado($id_activo, $departamento, 1);
$res = $db->eliminarSolicitud($id_s);
if ($result && $res) {
    echo "Se ha eliminado correctamente";
} else {
    echo "Error al eliminar";
}
?>

<html>
    <head>
        <meta HTTP-EQUIV="REFRESH" CONTENT="0;URL=activosSolicitados.php">
        <title>Aceptar</title>
        <link rel="stylesheet" type="text/css" href="../css/cambAplicados.css">
    </head>

    <body>
            </header>
            <div class="main">
                <main>

                    <h1>Modificación de Activos</h1>


                    <?php 
if (isset($_POST['mod'])) {
    $ID = $_POST['ID'];
    $ID_departamento = $_POST['ID_departamento'];
    $activo = $_POST['activo'];
    $descripcion = $_POST['descripcion'];
    $estado = $_POST['estado'];
    $db = new gestionActivos(__HOST__, __USUARIODB__, __PASSDB__, __DATABASE__);
    $resultado = $db->modificarActivo($ID, $ID_departamento, $activo, $descripcion, $estado);
    if ($resultado) {
        header("Location:adminActivos.php");
        exit;
    }
} else {
    $ID = $_GET['ID'];
    $ID_departamento = $_GET['ID_departamento'];
    $activo = $_GET['activo'];
    $descripcion = $_GET['descripcion'];
    $estado = $_GET['estado'];
}
?>

    if (isset($_GET['action']) == "eliminar") {
        $db = new gestionActivos(__HOST__, __USUARIODB__, __PASSDB__, __DATABASE__);
        $resultado = $db->eliminarActivo($_GET['ID'], $rol);
        if ($resultado) {
            header("Location: adminActivos.php");
            echo "El activo se borró con éxito";
        } else {
            echo "Error al eliminar el activo";
        }
    }
}
?>

<?php 
echo "\n<div class='formulario'>\n\n\n<table border=1 class='table table-striped table-hover table-responsive tabla'>\n\n<thead>\n\n<th>ID</th>\n<th>Departamento</th>\n<th>Activo</th>\n<th>Descripcion</th>\n\n</thead>\n\n<tbody>";
$db = new gestionActivos(__HOST__, __USUARIODB__, __PASSDB__, __DATABASE__);
$datos = $db->consultarActivos($rol);
if ($datos !== false) {
    while ($fila = mysqli_fetch_assoc($datos)) {
        echo "\n<tr id='activos'>\n<td><a href='adminActivos.php?ID=" . $fila['ID'] . "'>" . $fila['ID'] . "</a></td>\n<td>" . $fila['ID_departamento'] . "</td>\n<td>" . $fila['activo'] . "</td>\n<td>" . $fila['descripcion'] . "</td>\n\n<td><a href='formModActivo.php?\n&ID=" . $fila['ID'] . "\n&ID_departamento=" . $fila['ID_departamento'] . "\n&activo=" . $fila['activo'] . "\n&descripcion=" . $fila['descripcion'] . "\n'>Modificar</a><t/d>\n\n<td><a href='adminActivos.php?action=eliminar&ID=" . $fila['ID'] . "'><button class='btn-delete'>Eliminar</button></a></td>\n\n</tr>";
        $depart = $fila['ID_departamento'];
    }
}
?>

<form action='adminActivos.php' method='POST'>
<tr>
  <td><input type="text" name="ID"  required></td>
  <td><input type="text" name="ID_Departamento" value="<?php 
echo $depart;
?>
                            <thead>

                                <th><a href="">ID</a></th>
                                <th>Departamento</th>
                                <th>Activo</th>
                                <th>Descripcion</th>
                                <th>Estado</th>

                            </thead>


                            <tbody>

                                <?php 
$departamento = $_GET['departamento'];
$conexion = new gestionActivos(__HOST__, __USUARIODB__, __PASSDB__, __DATABASE__);
$datos = $conexion->obtenerActivos($departamento);
if ($datos !== false) {
    while ($fila = mysqli_fetch_assoc($datos)) {
        if ($fila['estado'] == 1) {
            echo "<tr>\n                                                    <td><a href='activosDpt.php?departamento=" . $departamento . "&ID=" . $fila['ID'] . "&estado=" . $fila['estado'] . "'>" . $fila['ID'] . "</a></td>\n                                                    <td>" . $fila['ID_departamento'] . "</td>\n                                                    <td>" . $fila['activo'] . "</td>\n                                                    <td>" . $fila['descripcion'] . "</td>\n                                                    <td>disponible</td>";
        } elseif ($fila['estado'] == 2 or $fila['estado'] == 3) {
            echo "<tr>\n                                                    <td><a href=''>" . $fila['ID'] . "</a></td>\n                                                    <td>" . $fila['ID_departamento'] . "</td>\n                                                    <td>" . $fila['activo'] . "</td>\n                                                    <td>" . $fila['descripcion'] . "</td>\n                                                    <td>en revisión</td>";
        } elseif ($fila['estado'] == 4) {
            echo "<tr>\n                                                    <td><a href=''>" . $fila['ID'] . "</a></td>\n                                                    <td>" . $fila['ID_departamento'] . "</td>\n                                                    <td>" . $fila['activo'] . "</td>\n                                                    <td>" . $fila['descripcion'] . "</td>\n                                                    <td>no disponible</td>";
        }
        echo "</tr>";
    }
}
?>