<?php
include 'sesion.php';

if ($cat_user == 9) {
    header("Location:index_usuario_tienda.php");
    exit;
}

include '../url_amigable.php';
include 'concha.php';
include '../concha_extras.php';

if (isset($_POST['delete_id'])) {
    $delete_id = $_POST['delete_id'];
    $deleteSQL = "DELETE FROM `users` WHERE `ID` = '$delete_id'";
    $result = mysql_db_query("$basedatos", $deleteSQL);

    if ($result) {
        echo "Operador eliminado correctamente.";
    } else {
        echo "Error al eliminar el operador.";
    }
}

if (isset($_POST['nombre']) && isset($_POST['apellido']) && isset($_POST['username']) && isset($_POST['pass'])) {
    $nombre = $_POST['nombre'];
    $apellido = $_POST['apellido'];
    $username = $_POST['username'];
    $pass = $_POST['pass'];

    $sSQL = "INSERT INTO `users` (`ID`, `username`, `password`, `email`, `nombre`, `apellido`, `nivel`, `token`)
             VALUES (NULL, '$username', '$pass', '', '$nombre', '$apellido', '1', '');";
    mysql_db_query("$basedatos", $sSQL);

    echo "Operador cargado correctamente.";
}

$operadoresSQL = "SELECT `ID`, `nombre`, `apellido`, `username` FROM `users`";
$operadores = mysql_db_query("$basedatos", $operadoresSQL);
?>

<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="utf-8">
  <meta name="description" content="Panel de Administracion">
  <meta name="author" content="<?php echo $nombresitio ?>">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title><?php echo $nombresitio ?> - Panel de Administracion</title>
  <link href="https://fonts.googleapis.com/css?family=Montserrat:400,500,600,700" rel="stylesheet">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" crossorigin="anonymous">
  <style>
    body {
        background-image: url(../fondeux.jpg);
        background-color: #EBEBEB;
        height: 100%;
    }
    .btn, h1, h2, h3, h4, h5, h6 {
        font-family: 'Montserrat', sans-serif;
        font-weight: 600;
    }
    .page-header {
        margin: 0;
        padding: 1em;
        background-color: #0066FF;
        text-align: center;
    }
  </style>
</head>

<body>
    <div class="container" style="background-color:#F1F2ED;">
        <?php include 'menu.php'; ?>
    </div>

    <div style="background-color:#fff;" class="container pt-3">
        <hr>
        <h3>Agregar un nuevo operador al sistema</h3>
        <hr>
        <form action="" method="post">
            <input type="text" class="form-control" name="nombre" placeholder="Nombre">
            <hr>
            <input type="text" class="form-control" name="apellido" placeholder="Apellido">
            <hr>
            <input type="text" class="form-control" name="username" placeholder="Usuario (sin espacios)">
            <hr>
            <input type="text" class="form-control" name="pass" placeholder="Contraseña (sin espacios)">
            <hr>
            <button class="btn btn-primary" type="submit">ENVIAR</button>
        </form>
        <hr>

        <h3>Listado de Operadores</h3>
        <table class="table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Nombre</th>
                    <th>Apellido</th>
                    <th>Usuario</th>
                    <th>Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($row = mysql_fetch_assoc($operadores)): ?>
                <tr>
                    <td><?php echo $row['ID']; ?></td>
                    <td><?php echo $row['nombre']; ?></td>
                    <td><?php echo $row['apellido']; ?></td>
                    <td><?php echo $row['username']; ?></td>
                    <td>
                        <form action="" method="post" onsubmit="return confirm('¿Seguro que deseas eliminar este operador?');">
                            <input type="hidden" name="delete_id" value="<?php echo $row['ID']; ?>">
                            <button type="submit" class="btn btn-danger">Eliminar</button>
                        </form>
                    </td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" crossorigin="anonymous"></script>
</body>
</html>
