 <?php
// DEBUG (quitar en producci車n)
ini_set('display_errors',1);
error_reporting(E_ALL);

/* --------------------------
   Funci車n ConsultarProducto
   -------------------------- */
function ConsultarProducto($cod)
{
    // Ajusta la ruta si tu conexion.php est芍 en otra carpeta
    include 'conexion.php';

    // seguridad m赤nima
    $cod = $conexion->real_escape_string($cod);

    $sentencia = "SELECT * FROM bienes WHERE cod='" . $cod . "' ";
    $resultado = $conexion->query($sentencia) or die("Error consultar bienes: " . $conexion->error);
    $filas = $resultado->fetch_assoc();

    return [
        $filas['cod'],          //0
        $filas['producto'],     //1
        $filas['cantidad'],     //2
        $filas['precio'],       //3
        $filas['imagen'],       //4
               
        $filas['estado'],       //5
        $filas['nombre'],       //6
        $filas['hora'],         //7
        $filas['rematado'],     //8
        $filas['lote'],         //9
        $filas['descripcion'],  //10
    ];
}

/* --------------------------
   Handler AJAX (devuelve solo fragmento)
   -------------------------- */
if (isset($_GET['ajax']) && isset($_GET['cod'])) {
    $consulta = ConsultarProducto($_GET['cod']);

    header('Content-Type: text/html; charset=utf-8');
    // SOLO el HTML interno que ir芍 dentro del div #oferta
    echo '<h2><label for="precio" class="col-md-15 control-label">Ultima Oferta Recibida:</label></h2>';
    echo '<h1><label style="color:#FF0000" for="precio" class="col-md-15 control-label">$ ' . htmlspecialchars($consulta[3]) . '</label></h1>';
    echo '<h1><label style="color:#FF0000" for="nombre" class="col-md-15 control-label">' . htmlspecialchars($consulta[6]) . '</label></h1>';
    exit;
}

/* --------------------------
   C車digo normal de la p芍gina
   -------------------------- */
$cod = isset($_GET['cod']) ? $_GET['cod'] : '';
$consulta = ConsultarProducto($cod);


session_start();
  require '../funcs/conexion.php';
  include '../funcs/funcs.php';
	
	if(!isset($_SESSION['id'])){
		header("Location: index.php");
	}
	
	$usuario = $_SESSION['usuario'];
	$nombre = $_SESSION['nombre'];
	$tipo_usuario = $_SESSION['id_tipo'];
	
	
	
	if (isset($_GET['ajax']) && $_GET['ajax'] === 'botones' && isset($_GET['cod'])) {
    $consulta = ConsultarProducto($_GET['cod']);
    include "conexion.php";
    ob_start();
    ?>
    <h3><label>Incremente para ganar:</label></h3>
    <?php if ($consulta[8]==1) {
        $sentencia="SELECT * FROM buttom WHERE status=1";
        $resultado= $conexion->query($sentencia);
        while($filas=$resultado->fetch_assoc()){
            $montoTotal = $consulta[3] + $filas['monto'];
            echo '<button type="button" class="btn btn-warning" onclick="enviarOferta(' . $montoTotal . ')">
                    OFERTAR $' . number_format($montoTotal, 0, ',', '.') . '
                  </button> ';
        }
    } ?>
    <?php
    echo ob_get_clean();
exit;
}
?>

<!DOCTYPE html>
<html dir="ltr" lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Title -->
<title>Remates Global</title>
<!-- Favicon -->
<link href="../images/" sizes="128x128" rel="shortcut icon" />
<link href="../images/" sizes="128x128" rel="shortcut icon" type="image/x-icon" />
<!-- css file -->
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" href="../css/style.css">
<link rel="stylesheet" href="../css/theme-color.css">
<!-- Responsive stylesheet -->
<link rel="stylesheet" href="../css/responsive.css">

<?php
// Si se llama con ?ajax=1, solo devolvemos el contenido din芍mico


?>



<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    
    <style>
    .thumbnail {
      width: 320px;
      cursor: pointer;
    }
  </style>
</head>
<body>
<div class="wrapper">
	
  	<div class="ulockd-header-topped">
  		<div class="container">
  			<div class="row">
  				<div class="col-md-4">
  					<div class="ulockd-social-linked">
  						<ul class="list-inline">
  							<li><a href="#"><i class="fa fa-facebook"></i></a></li>
  							<li><a href="#"><i class="fa fa-rss"></i></a></li>
  							<li><a href="#"><i class="fa fa-google-plus"></i></a></li>
  							<li><a href="#"><i class="fa fa-pinterest"></i></a></li>
  							<li><a href="#"><i class="fa fa-instagram"></i></a></li>
  						</ul>
  					</div>
  				</div>
  			</div>
  		</div>
  	</div>
  	<div class="ulockd-header-middle">
  		<div class="container">
  			<div class="row">
  				<div class="col-xs-12 col-sm-4 col-md-4">
					<div class="ulockd-contact-info text-right">
						<div class="ulockd-icon pull-right"><span class="flaticon-web-page-home"></span></div>
						<div class="ulockd-info">
							<h3></h3>
							<p class="ulockd-cell"></p>
						</div>
					</div>
  				</div>
  				<div class="col-xs-12 col-sm-4 col-md-4">
  					<div class="ulockd-welcm-hmddl text-center">
						<a href="index.html" class="ulockd-factr-main-logo"><img src="images/header-logo.png" alt=""></a>
  					</div>  					
  				</div>
  				<div class="col-xs-12 col-sm-4 col-md-4">
					<div class="ulockd-ohour-info">
						<div class="ulockd-icon pull-left"><span class="flaticon-clock-1"></span></div>
						<div class="ulockd-info">
							<h3>Abierto</h3>
							<p class="ulockd-addrss">9:00-20:00</p>
						</div>
					</div>
  				</div>
  			</div>
  		</div>
  	</div>
	<!-- Header Styles -->
	<header class="header-nav">
		<div class="main-header-nav navbar-scrolltofixed">
			<div class="container">
			    <nav class="navbar navbar-default bootsnav ulockd-menu-style-one light-red">
					<!-- Start Top Search -->
			        <div class="top-search">
			            <div class="container">
			                <div class="input-group">
			                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
			                    <input type="text" class="form-control" placeholder="Search">
			                    <span class="input-group-addon close-search"><i class="fa fa-times"></i></span>
			                </div>
			            </div>
			        </div>
			        <!-- End Top Search -->

			        <div class="container ulockd-pad-zero">
			            
			            <!-- End Atribute Navigation -->

			            <!-- Start Header Navigation -->
			            <div class="navbar-header">
			                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-menu">
			                    <i class="fa fa-bars"></i>
			                </button>
			            </div>
			            <!-- End Header Navigation -->

			            <!-- Collect the nav links, forms, and other content for toggling -->
			            <div class="collapse navbar-collapse" id="navbar-menu">
			                <ul class="nav navbar-nav navbar-left">
			                    <li class="dropdown">
			                       	<?php   echo "<td><a href='subastadm.php?idremate=".$consulta[7]."'> VOLVER A SALA</a></td>"; ?>
			                    </li>
			                </ul>
			            </div><!-- /.navbar-collapse -->
			        </div>

			        <!-- Start Side Menu -->
			        <div class="side ulockd-bgthm">
			            <a href="#" class="close-side"><i class="fa fa-times"></i></a>
			            <div class="widget">
			                <h4 class="title">Custom Pages</h4>
			                <ul class="link">
			                    <li><a href="#">About</a></li>
			                    <li><a href="#">Services</a></li>
			                    <li><a href="#">Blog</a></li>
			                    <li><a href="#">Portfolio</a></li>
			                    <li><a href="#">Contact</a></li>
			                </ul>
			            </div>
			            <div class="widget">
			                <h4 class="title">Additional Links</h4>
			                <ul class="link">
			                    <li><a href="#">Retina Homepage</a></li>
			                    <li><a href="#">New Page Examples</a></li>
			                    <li><a href="#">Parallax Sections</a></li>
			                    <li><a href="#">Shortcode Central</a></li>
			                    <li><a href="#">Ultimate Font Collection</a></li>
			                    <li><img title="Facebook Feed With Client File" class="img-responsive ulockd-mrgn1210" src="images/resource/image3.png" alt="image3.png"></li>
			                </ul>
			            </div>
			        </div>
			        <!-- End Side Menu -->
			        
			    </nav>
			</div>
		</div>		
	</header>
	
    <!-- Our About -->
	<section class="ulockd-about-one">
		<div class="container">
		   <!--<H1>CLAVE PARA INGRESAR A SALA DE ZOOM: 7HVR4T</H1>-->
			            <form id="booking_form" class="booking_form text-center" name="booking_form"  action="oferta2bienes.php" method="POST" enctype= "multipart/form-data" novalidate="novalidate">
			              <input type="hidden" name="cod" value="<?php echo $_GET['cod']?>" >
			<div class="row">
				<div class="col-sm-12 col-md-12 ">
					<div class="booking_form_style1 text-center">
		                <!-- Booking Form Sart-->
		                <div class="messages"></div>
							<div class="row">
							    	<?php   echo "<td><a href='subastadm.php?idremate=".$consulta[7]."'> <button type='button' class='btn btn-success'><h3>VOLVER A LA SALA</h3></button> </a> </td>";?>
							     <!--<a href="https://zoom.us/j/8454632543" target="_blank"> <button type='button' class='btn btn-info'><h3>ZOOM</h3></button> </a> --></td>
							     
							     <div class="col-sm-12">
				                	<h3>SUBASTA DE VEHICULO.</h3>
				                </div>
				                <div class="panel-body" >
                        <div class="form-group">
                            <div class="col-md-4">
                          <h4><label for="nombre" class="col-md-1 control-label  ">USUARIO:</label></h4>
                          
                          <h4> <select class="form-group col-md-12" name="nombre">
                            <?php
                              $query = $mysqli -> query ("SELECT * FROM palo");
                              while ($valores = mysqli_fetch_array($query)) {
                                echo '<option value="'.$valores[nombre].'">'.$valores[nombre].'</option>';
                              }
                            ?>
      </select></h4>
                          </div>
                      </div>

                       <!-- NUEVO BOTÓN HABILITAR / INHABILITAR REMATADO -->
        <button 
            type="button" 
            class="btn <?php echo $consulta[8] == 1 ? 'btn-success' : 'btn-danger'; ?>" 
            onclick="toggleRematado('<?php echo $consulta[0]; ?>', this)">
            <?php echo $consulta[8] == 1 ? 'Habilitado' : 'Inhabilitado'; ?>
        </button>

                </div>
                 
				                
				                 <div class="form-group col-md-6">
				                     <?php
      include "conexion.php";
      $sentencia="SELECT *  FROM bienes A, remate R WHERE A.hora=R.idremate && A.cod='$consulta[0]' ";
      $resultado= $conexion->query($sentencia) or die (mysqli_error($conexion));
      while($filas=$resultado ->fetch_assoc())
      { ?>
 <div style="position: relative; width: 100%; max-width: 400px; height: 0; padding-bottom: 56.25%;">
 	<a class="navbar-brand" target="_blank" href="<?php echo $filas['link']; ?>"><img src="../images/meet.jpg"   width="100%" height="250" alt=""></a>
			         </div><?
        }
        ?>
                  <div class="form-group col-md-9">
                  <img src="../ars/<?php echo $consulta[4] ?>" class="img-thumbnail" >
              </div>
               </div>
               <div class="form-group col-md-6">
                   

              <div id="oferta" class="form-group col-md-9 has-warning">
      <h2><label for="precio" class="col-md-15 control-label">Ultima Oferta Recibida:</label></h2>
      <h1><label style="color:#FF0000" for="precio" class="col-md-15 control-label">$ <?php echo htmlspecialchars($consulta[3]); ?></label></h1>
      <h1><label style="color:#FF0000" for="nombre" class="col-md-15 control-label"><?php echo htmlspecialchars($consulta[6]); ?> </label></h1>
  </div>

              <div id="botones"> 
                <h3><label for="precio" class="col-md-15 control-label">Incremente para ganar:</label></h3>
              
                 
                
          
  <button style="margin: 30px" type="button" class="btn btn-primary" onclick="enviarOferta(100000)">+100.000</button>
  <button style="margin: 30px" type="button" class="btn btn-warning" onclick="enviarOferta(200000)">+200.000</button>
  <button style="margin: 30px" type="button" class="btn btn-danger" onclick="enviarOferta(300000)">+300.000</button>
  <button style="margin: 10px" type="button" class="btn btn-success" onclick="enviarOferta(400000)">+400.000</button>
  <button style="margin: 20px" type="button" class="btn btn-info" onclick="enviarOferta(500000)">+500.000</button>
 </div>

                </div>
                <h4><b>Detalles:</b></h4>
<p><b>CÓDIGO:</b> <?php echo $consulta[0] ?> |
<b>PRODUCTO:</b> <?php echo $consulta[1] ?> |
<b>CANTIDAD:</b> <?php echo $consulta[2] ?> |
<b>PRECIO:</b> <?php echo $consulta[3] ?> |
<b>ESTADO:</b> <?php echo $consulta[5] ?> |
<b>LOTE:</b> <?php echo $consulta[9] ?> |
<b>DESCRIPCIÓN:</b> <?php echo $consulta[10] ?></p>
      
				                
				                
				               
				                    
			                </div>
					</div>
				</div>
			</div>
			 </form>
		</div>
		
	</section>
	

<!-- Our Footer -->
	<section class="ulockd-footer">
		<div class="container">
			<div class="row">
				<div class="col-xs-12 col-sm-6 col-md-3 col-lg-4">
					<div class="ulockd-footer-fst-col">
						<div class="ulockd-footer-logo"><img src="" alt=""></div>
						<p>Remates 100% confibables, responsables, seguros, online. Para todo lo que buscas y mas  .</p>
						<ul class="list-inline ulockd-footer-font-icon">
							<li><a href="#"><i class="fa fa-facebook"></i></a></li>
							<li><a href="#"><i class="fa fa-feed"></i></a></li>
							<li><a href="#"><i class="fa fa-google-plus"></i></a></li>
							<li><a href="#"><i class="fa fa-pinterest"></i></a></li>
							<li><a href="#"><i class="fa fa-instagram"></i></a></li>
						</ul>						
					</div>
				</div>
				<div class="col-xs-12 col-sm-6 col-md-3 col-lg-2">
					<div class="ulockd-footer-qlink">
						<h3 class="text-uppercase">NAVEGA<span class="text-thm1">CION</span></h3>
						<ul class="list-unstyled">
							<li><a href="#">Inicio</a></li>
							<li><a href="#">Usuario</a></li>
							<li><a href="#">Cerrar</a></li>
						</ul>
					</div>
				</div>
				<div class="col-xs-12 col-sm-6 col-md-3 col-lg-3">
					<div class="ulockd-footer-contact">
						<h3 class="text-uppercase">CONTACT<span class="text-thm1">ANOS</span></h3>
						<?php
						 include "../ars/conexion.php";
      
							      $sentencia="SELECT * FROM dcontact where iddatos=1 ";
							      $resultado= $conexion->query($sentencia) or die (mysqli_error($conexion));
							      while($row=$resultado ->fetch_assoc())
							      {
						  ?>
						<p><?php echo $row["direccion"]; ?>.</p>
						<div class="ulockd-ftr-fax"><span>Telefono </span>: <a href="#"><?php echo $row["telefono"]; ?>.</a></div>
						<div class="ulockd-ftr-mail"><span>Email </span>: <a href="#"><?php echo $row["correo"]; ?>.</a></div>
						<?php } ?> 
					</div>
				</div>
			</div>
		</div>
	</section>

	<!-- Our CopyRight Section -->
	<section class="ulockd-copy-right">
		<div class="container">
			<div class="row">
				<div class="col-md-12">
					<p class="color-white">穢Remates Global. <a class="color-white" href="#" target="_blank"></a> </p>
				</div>
			</div>
		</div>
	</section>

<a class="scrollToTop" href="#"><i class="fa fa-home"></i></a>
</div>
<!-- Wrapper End -->
<script>
  (function(){
    const cod = encodeURIComponent('<?php echo isset($_GET["cod"]) ? addslashes($_GET["cod"]) : ""; ?>');
    const base = location.pathname; // misma ruta
    async function refreshOferta(){
      try{
        const resp = await fetch(base + '?cod=' + cod + '&ajax=1&t=' + Date.now(), { cache: 'no-store' });
        if(!resp.ok) throw new Error('HTTP ' + resp.status);
        const html = await resp.text();
        const el = document.getElementById('oferta');
        if(el) el.innerHTML = html;
      }catch(err){
        console.error('Error refrescando precio:', err);
      }
    }
    // primer refresh r芍pido y luego interval
    setTimeout(refreshOferta, 500);
    setInterval(refreshOferta, 2000);
  })();
  </script>
  
 <script>
function enviarOferta(incremento) {
  const cod = '<?php echo addslashes($consulta[0]); ?>';
  const nombre = document.querySelector('select[name="nombre"]').value; // obtiene el nombre seleccionado

  fetch('oferta2bienes.php', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: 'cod=' + encodeURIComponent(cod) +
          '&monto=' + encodeURIComponent(incremento) +
          '&nombre=' + encodeURIComponent(nombre)
  })
  .then(resp => resp.text())
  .then(nuevoMonto => {
    console.log('Nuevo monto:', nuevoMonto);
  })
  .catch(err => console.error('Error al enviar precio:', err));
}
</script>

<script>
    function toggleRematado(cod, btn) {
  fetch('actualizar2.php', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: 'cod=' + encodeURIComponent(cod)
  })
  .then(resp => resp.json())
  .then(data => {
    if(data.success){
      btn.textContent = data.rematado == 1 ? 'Habilitado' : 'Inhabilitado';
      btn.className = data.rematado == 1 ? 'btn btn-success' : 'btn btn-danger';
    } else {
      alert('Error: ' + data.message);
    }
  })
  .catch(err => console.error('Error:', err));
}
    
</script>


<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../js/bootsnav.js"></script>
<script type="text/javascript" src="../js/scrollto.js"></script>
<script type="text/javascript" src="../js/jquery-scrolltofixed-min.js"></script>
<script type="text/javascript" src="../js/jquery.counterup.js"></script>
<script type="text/javascript" src="../js/jquery.magnific-popup.min.js"></script>
<script type="text/javascript" src="../js/fancybox.js"></script>
<script type="text/javascript" src="../js/wow.min.js"></script>
<script type="text/javascript" src="../js/jquery.masonry.min.js"></script>
<script type="text/javascript" src="../js/owl.carousel.min.js"></script>
<script type="text/javascript" src="../js/jquery.fitvids.js"></script>
<script type="text/javascript" src="../js/timepicker.js"></script>
<script type="text/javascript" src="../js/tweetie.js"></script>
<!-- Custom script for all pages --> 
<script type="text/javascript" src="../js/script.js"></script>

	

</body>
</html>

