<?php
require_once "../config/conexion.php";

if (isset($_POST)) {
    if (!empty($_POST)) {
        $nombre = $_POST['nompublicidad'];
        $hipervin1 = $_POST['hipervin1'];
        $hipervin2 = $_POST['hipervin2'];
        $idserviciof = $_POST['idserviciof'];
		$img = $_FILES['foto'];
        $name = $img['name'];
        $tmpname = $img['tmp_name'];
        $fecha = date("YmdHis");
        $foto = $fecha . ".jpg";
        $destino = "../assets/img/" . $foto;
        $query = mysqli_query($conexion, "INSERT INTO publicidad(nompublicidad, hipervin1, hipervin2, imagen, idservicio) VALUES ('$nombre', '$hipervin1', '$hipervin2', '$foto', $idserviciof)");
        if ($query) {
			if (move_uploaded_file($tmpname, $destino)) {
                header('Location: mantenpublicidad.php');
            }echo $idserviciof;
        }
    }
}
include("includes/header.php"); ?>
<meta charset="utf-8">
<meta namw="viewport" content="width=device-width, inicial-scale-1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="http://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

  <!-- Bootstrap core JS-->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <!-- Core theme JS-->
    <script src="assets/js/jquery-3.6.0.min.js"></script>
    <script src="assets/js/scripts.js"></script>
	
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">Publicidad</h1>
    <a href="#new" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm" id="abrirProducto"><i class="fas fa-plus fa-sm text-white-50"></i> Nuevo</a>
	<a href="#new2" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm" id="abrirServicio"><i class="fas fa-plus fa-sm text-white-50"></i> Modificar</a>
</div>
<!-- css -->
<link rel="stylesheet" type="text/css" href="/assets/css/estilos.css">

<div class="row">
    <div class="col-md-12">
         <div class="table-responsive"> 
		
            <table class="table table-hover table-bordered table-fixed">
                <thead class="thead-dark">
                    <tr>
					    <th>Codigo</th>
						<th>Imagen</th>
					    <th>Nombre</th>
					    <th>Servicio</th>
					    <th>Enlace1</th>
                        <th>Enlace2</th>
						<th>Registro</th>
					    <th></th>
                    </tr>
                </thead>
                <tbody>
	               <?php  
                    $query = mysqli_query($conexion, "SELECT p.*, c.idserviciof AS id_cat, c.nomservicio, hipervin1, hipervin2, p.imagen,  (Select t.nomservicio From servicio t Where t.idserviciof=p.idservicio) As nomservicio
                    FROM publicidad p 
                    INNER JOIN servicio c ON c.idserviciof = p.idservicio ORDER BY p.idpublicidad desc");
                    while ($data = mysqli_fetch_assoc($query)) { ?>
                        <tr>
						    <td><?php echo $data['idpublicidad']; ?></td>
                            <td><img class="img-thumbnail" src="../assets/img/<?php echo $data['imagen']; ?>" width="50"></td>
							<td><?php echo $data['nompublicidad']; ?></td>
							<td><?php echo $data['nomservicio']; ?></td>
                            <td><?php echo $data['hipervin1']; ?></td>
                            <td><?php echo $data['hipervin2']; ?></td>
                          	<td><?php echo $data['registro']; ?></td>
                            <td>
                                <form method="post" action="eliminarpublicidad.php?accion=<?php echo $data['idpublicidad']; ?>" class="d-inline eliminar">
                                    <button class="btn btn-danger" type="submit name="button">Eliminar</button>
							    </form>
							   <form method="post" action="modificar1.php?accion=<?php echo $data['idpublicidad']; ?>" class="d-inline modificar">
                                 	<button class="btn btn-danger" type="submit name="button">Modificar</button>
							   </form>
							</td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<a name="new">
<div id="productos" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="my-modal-title" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header bg-gradient-primary text-white">
                <h5 class="modal-title" id="title">Nuevo Producto</h5>
                <button class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="" method="POST" enctype="multipart/form-data" autocomplete="off">
                    <div class="row">
					<div class="col-md-3">
                        <div class="form-group">
                                <label for="nombre">Codigo</label>
                                <input id="idservicio" class="form-control" type="text" name="Codigo" placeholder="Codigo" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="nombre">Nombre</label>
                                <input id="nompublicidad" class="form-control" type="text" name="nompublicidad" placeholder="Nombre" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="cantidad">Enlace1</label>
                                <input id="hipervin1" class="form-control" type="text" name="hipervin1" placeholder="correo electronico" required>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="descripcion">Enlace2</label>
                                <textarea id="hipervin2" class="form-control" name="hipervin2" placeholder="pagina web" rows="3" required></textarea>
                            </div>
                        </div>
					    <div class="col-md-6">
                            <div class="form-group">
                                <label for="subservicio">SubServicio</label>
                                <select id="idsubservicio" class="form-control" name="idsubservicio" required>
                                    <?php
									session_start();
                                    ob_start();
									$catego=$GLOBALS['catego'];
									unset($subservicio);
									$subservicio = mysqli_query($conexion, "SELECT *, UCASE(nomsubservicio) AS nomsubservicio FROM subservicio");
                                    foreach ($subservicio as $catsub) { ?>
                                        <option value="<?php echo $catsub['idsubservicio']; ?>"><?php echo $catsub['nomsubservicio']; ?></option>
                                    <?php } ?>
                                </select>
								<div id="idserviciof"></div>
                            </div>
                        </div>	
                   	<div class="col-md-6">
                            <div class="form-group">
                                <label for="subservicio">Servicio</label>
                                <select id="idserviciof" class="form-control" name="idserviciof" required>
                                    <?php
									session_start();
                                    ob_start();
									$catego=$GLOBALS['catego'];
									unset($servicio);
									$servicio = mysqli_query($conexion, "SELECT *, UCASE(nomservicio) AS nomservicio FROM servicio");
                                    foreach ($servicio as $catsub) { ?>
                                        <option value="<?php echo $catsub['idserviciof']; ?>"><?php echo $catsub['nomservicio']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>	
					    <div class="col-md-6">
                            <div class="form-group">
                                <label for="imagen">Foto</label>
                                <input type="file" class="form-control" name="foto" required>
                            </div>
                        </div>
                    </div>
                    <button class="btn btn-primary" type="submit">Registrar</button>
                </form>
            </div>
        </div>
    </div>
</div>
</a>

<?php include("includes/footer.php"); ?>
 
  <!-- Bootstrap core JS-->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <!-- Core theme JS-->
    <script src="assets/js/jquery-3.6.0.min.js"></script>
    <script src="assets/js/scripts.js"></script>

<script type="text/javascript">
     $(document).ready(function(){
		 //$('#idsubservicio').val(2);
		 recargarLista();
		    $('#idsubservicio').change(function(){
			 recargarLista();
		});
	})
</script>
<script type="text/javascript">
    function recargarLista(){
		$.ajax({
			type:"POST",
			url:"datos2.php",
			data:"publicidad=" + $('#idsubservicio').val(),
			success:function(r){
				$('#idserviciof').html(r);
		}
	});
  }
</script>
