<?php
session_start();
$servername = "localhost";
$username = "itladevprubycom_enma";
$password = "53gNpj4t.xDc";
$database = "itladevprubycom_enma1";

$conexion = new mysqli($servername, $username, $password, $database);

$query = $conexion->prepare("SELECT * FROM produc");
$query->execute();
$data = $query->get_result();

$id = $_SESSION["id"];

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>agregar</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.18.1/dist/css/uikit.min.css" />
    <script src="https://cdn.jsdelivr.net/npm/uikit@3.18.1/dist/js/uikit.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/uikit@3.18.1/dist/js/uikit-icons.min.js"></script>
</head>
<body>
    <?php include "navbar.php";?>
    <div uk-filter="target: .js-filter">
    <div class="uk-margin-medium-top uk-flex uk-flex-center">
        <ul class="uk-subnav uk-subnav-pill">
            <li uk-filter-control=""><a href="#">Todo</a></li>
            <li><a href="edtienda0.php">Editar producto</a></li>
            <li uk-filter-control=".producto"><a href="#">Agregar producto</a></li>
            <li uk-filter-control=".eliminar"><a href="#">Eliminar producto</a></li>
        </ul>
    </div>
    <ul class="js-filter">
        <li class="producto">
            <div class="uk-card uk-card-body">
                <div class="uk-container">
                    <h2 class="uk-text-center">Agregar Productos</h2>
                    <div class="uk-grid uk-child-width-1-1 uk-child-width-1-2@s" uk-grid>
                        <form action="" onsubmit="return validaciond();" class="" method="post" enctype="multipart/form-data">
                            <div class="uk-margin">
                                <label class="uk-form-label" for="form-stacked-text">Nombre del producto:</label>
                                <input class="uk-input uk-form-medium" id="nombre" type="text" placeholder="Agregar Usuario" name="nombre">
                            </div>
                            <div class="uk-margin">
                                <label class="uk-form-label" for="form-stacked-text">Precio:</label>
                                <input class="uk-input uk-form-medium" id="precio" type="number" placeholder="Agregar precio" name="precio">
                            </div>
                            <div class="uk-margin">
                                <label class="uk-form-label" for="form-stacked-text">Descripción:</label>
                                <input class="uk-input uk-form-medium" id="descripcion" type="text" placeholder="Agregar descripcion" name="descripcion">
                            </div>
                            <div class="uk-margin">
                                <label class="uk-form-label" for="form-stacked-text">Imagen:</label>
                                <input class="uk-input uk-form-medium" id="img" type="file" placeholder="Agregar Usuario" name="img">
                            </div>
                            <div class="uk-margin uk-flex uk-flex-center uk-margin-small">
                                <button type="submit" class="uk-button uk-button-primary">Submit</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </li>
        <li class="eliminar">
            <div class="uk-card uk-card-body">
                <div class="uk-container">
                    <h2 class="uk-margin-large-top uk-text-center">Eliminar Producto</h2>
                    <div class="uk-margin">
                        <form action="" onsubmit="return confirm();" method="post">
                            <div class="uk-form-controls">
                                <select class="uk-select uk-form-medium" name="tipo" id="form-stacked-select">
                                    <?php foreach($data as $value): ?>
                                        <option value="<?php echo $value['id']; ?>"><?php echo $value['producto']; ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="uk-margin uk-flex uk-flex-center uk-margin-small">
                                <button type="submit" class="uk-button uk-button-danger">Eliminar</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </li>
    </ul>
</div>
<div class="uk-container uk-flex uk-flex-center uk-margin-large-top">
    <a href="index.php" class="uk-button uk-button-default">Volver al inicio</a>
</div>
<?php


if(isset($_FILES['img']) || isset($_POST['nombre']) || isset($_POST['precio']) || isset($_POST['descripcion'])){
    $nombre = $_POST["nombre"];
    $precio = $_POST["precio"];
    $descripcion = $_POST["descripcion"];
    $archivotmp = $_FILES['img']['tmp_name'];
    $nombrefile = $_FILES['img']['name'];
    $carpetafate = '../img/';

    if(move_uploaded_file($archivotmp, $carpetafate . $nombrefile)){
    }

    $query = "INSERT INTO produc (producto, precio, descripcion, img) VALUES ('$nombre','$precio','$descripcion','$carpetafate$nombrefile')";
    $execute = $conexion->prepare($query);
    $execute->execute();
}
?>
<script>
    function validaciond() {
        let nombre = document.getElementById('nombre').value;
        let precio = document.getElementById('precio').value;
        let descripcion = document.getElementById('descripcion').value;
        let img = document.getElementById('img').value;

        if (nombre.length === 0) {
            alert('Nombre esta vacio');
            return false;
        }
        if (precio.length === 0) {
            alert('Precio esta vacio');
            return false;
        }
        if (descripcion.length === 0) {
            alert('No ha descrito el producto');
            return false;
        }
        if (img.length == 0) {
            alert('no hay ninguna imagen seleccionada');
            return false;
        }

        alert('Se agrego el producto correctamente');
        return true;
    }
    function confirm(){
        if (confirm("¿Estás seguro de que quieres eliminar esto?")) {
            <?php if ($_SERVER["REQUEST_METHOD"] === "POST") {
                if (isset($_POST["tipo"])) {
                    $id_eliminar = $_POST["tipo"];
                    $eliminar = $conexion->prepare("DELETE FROM produc WHERE id = ?");
                    $eliminar->bind_param("i", $id_eliminar);
                    $eliminar->execute();
                    header("Location: edtienda.php?id=".$id_eliminar);
                    exit();
                    
                }
            }?>
            
        }

    }
</script>
</body>
</html>

