<?php
session_start();
include('conexion.php');

$art_id = $_GET['art_id'];
?>
<!doctype html>
<html lang="en" onclick="cerrar();cerrar2()">

<head>
    <?php
    include('./include/sesiones.php');
    include('include/head.php');
    ?>
    <div id="div_buscador"></div>
    <?php
    include('php_menu/menu_categorias.php');
    include('php_menu/menu_izquierdo.php');
    ?>
</head>

<body>
    <section class="slider_section mt-20">
        <div class="product_details mt-50 mb-50">
            <div class="container">
                <div class="row">
                    <div class="col-lg-6 col-md-6">
                        <div class="product_d_right">
                            <div class="zoomWrapper single-zoom">
                                <?php
                                $nombre_archivo = '../img/articulos/' . $art_id . '.jpg';
                                if (file_exists($nombre_archivo)) {
                                    //$nu = rand(1, 1000);
                                    $nu = time();
                                    $nu; ?>
                                    <a>
                                        <img id="zoom1" src="<?php echo $emp_url_sub; ?>/img/articulos/<?php echo $art_id; ?>.jpg" data-zoom-image="<?php echo $emp_url_sub; ?>/img/articulos/<?php echo $art_id . '.jpg?' . $nu . ''; ?>" alt="big-1">
                                    <?php } else { ?>
                                        <img src="<?php echo $emp_url_sub; ?>/img/articulos/sin_imagen.jpg" alt="#">
                                    <?php   }   ?>
                                    </a>
                            </div>
                        </div>
                    </div>
                    <?php

                    $can3 = mysqli_query($con, "SELECT *,a.art_id as art_id_art FROM articulos a, articulos_has_bodegas ab 
                    where a.art_id=ab.articulos_art_id  and ab.art_cantidad>0  and a.empresa='$emp_id' and ab.empresa='$emp_id' and 
                    a.art_id='$art_id'") or die(mysqli_error($con));

                    $load1 = mysqli_fetch_array($can3);
                    $art_nombre = ucfirst($load1['art_nombre']);
                    $art_id = ($load1['art_id_art']);
                    $fam_id = ($load1['familias_fam_id']);
                    $art_referencia = ($load1['art_referencia']);
                    number_format($load1['art_pvp'], 0, ",", ".");
                    $art_nombre_largo = $load1['art_nombre_largo'];
                    $art_nombre_online = ($load1['art_nombre_online']);
                    $art_atributo_estado = ($load1['art_atributo_estado']);
                  
                    ?>
                    <div class="col-lg-6 col-md-6">
                        <h4><?php echo $art_nombre_online; ?></h4>
                        <div class="price_box">
                        <?php include("php_tienda/funcion_precio.php") ?>

                        </div>
                        <div class="product_desc">
                            <p><?php echo $art_nombre; ?></p>
                        </div>
                        <?php
                        if ($art_atributo_estado == 'si') { ?>
                            <form id="form_atributos" name="form_atributos" method="POST">
                                <div class="product_variant color">

                                    <h6><?php echo strtoupper($load1['art_atributo1']); ?></h6>
                                    <select class="form-control-sm" id="atributo1<?php echo $load1['art_id_art']; ?>" name="atributo1">
                                        <?php $casa = mysqli_query($con, "SELECT * FROM atributos1 WHERE atri_art_id='" . $load1["art_id_art"] . "'
                                        AND empresa='$emp_id' ") or die(mysqli_error($con));
                                        while ($load = mysqli_fetch_array($casa)) {
                                            $atri_nombre = ucfirst($load['atri_nombre']);
                                            $atri_id = ($load['atri_id']); ?>
                                            <option value="<?php echo $atri_id; ?>"><?php echo $atri_nombre; ?></option> <?php
                                                                                                                        } ?>
                                    </select>
                                    <p></p>
                                    <h6><?php echo strtoupper($load1['art_atributo2']); ?></h6>

                                    <select class="form-control-sm" id="atributo2<?php echo $load1['art_id_art']; ?>" name="atributo2">
                                        <?php $casa2 = mysqli_query($con, "SELECT * FROM atributos2 WHERE atri_art_id2='" . $load1["art_id_art"] . "'
                                        AND empresa='$emp_id' ") or die(mysqli_error($con));
                                        while ($load9 = mysqli_fetch_array($casa2)) {
                                            $atri_nombre2 = ucfirst($load9['atri_nombre2']);
                                            $atri_id2 = ($load9['atri_id2']); ?>
                                            <option value="<?php echo $atri_id2; ?>"><?php echo $atri_nombre2; ?></option> <?php
                                                                                                                        } ?>
                                    </select>
                                </div>
                                <input name="art_id" type="hidden" id="art_id<?php echo $load1["art_id_art"]; ?>"
                                    value="<?php echo $load1["art_id_art"]; ?>" />
                                <input name="cantidad" type="hidden" id="cantidad<?php echo $load1["art_id_art"]; ?>" value="1" class="pl-2" />
                                <div class="action_links product-quantity-containe">
                                    <ul>
                                        <button class="add_to_cart" type="submit" name="btn_atributos" id="btn_atributos"><i class="zmdi zmdi-shopping-cart-plus"></i> Agregar al Carrito</button>

                                        <input name="art_id" type="hidden" id="art_id<?php echo $load1["art_id_art"]; ?>"
                                            value="<?php echo $load1["art_id_art"]; ?>" />
                                        <?php if (isset($_SESSION['clie_id'])) {
                                            $sqll = mysqli_query($con, "SELECT * FROM cotizacion_detalle_deseo WHERE cotd_art_id='" .
                                                $load1['art_id_art'] . "' and clie_id='" . $_SESSION["clie_id"] . "'") or die(mysqli_error($con));

                                            if ($load2 = mysqli_fetch_array($sqll)) { ?>
                                                <li id="div1<?php echo $load1['art_id_art']; ?>" class="wishlist" onclick="eliminar_deseo(<?php echo $load1['art_id_art']; ?>);cambiar1(<?php echo $load1['art_id_art']; ?>);">
                                                    <a class="active" title="Eliminar de deseos">
                                                        <i class="fa fa-heart-o" aria-hidden="true"></i>
                                                    </a>
                                                </li>

                                                <li id="div2<?php echo $load1['art_id_art']; ?>" style="display:none;" class="wishlist"
                                                    onclick="envia_deseos(<?php echo $load1['art_id_art']; ?>);cambiar2(<?php echo $load1['art_id_art']; ?>);">
                                                    <a title="Enviar deseos">
                                                        <i class="fa fa-heart-o" aria-hidden="true"></i>
                                                    </a>
                                                </li> <?php
                                                    } else {  ?>

                                                <li style="display:none;" id="div1<?php echo $load1['art_id_art']; ?>" class="wishlist" onclick="eliminar_deseo(<?php echo $load1['art_id_art']; ?>);cambiar1(<?php echo $load1['art_id_art']; ?>);">
                                                    <a class="active" title="Eliminar de deseos">
                                                        <i class="fa fa-heart-o" aria-hidden="true"></i>
                                                    </a>
                                                </li>

                                                <li id="div2<?php echo $load1['art_id_art']; ?>" class="wishlist" onclick="envia_deseos(<?php echo $load1['art_id_art']; ?>);cambiar2(<?php echo $load1['art_id_art']; ?>);">
                                                    <a title="Enviar deseos">
                                                        <i class="fa fa-heart-o" aria-hidden="true"></i>
                                                    </a>
                                                </li><?php
                                                    }
                                                } else { ?>
                                            <li class="wishlist"><a href="login.php?art_id=<?php echo $load1["art_id_art"]; ?>" title="Agregar a deseos"><i class="fa fa-heart-o" aria-hidden="true"></i></a></li>

                                        <?php } ?>
                                    </ul>
                                </div>
                            </form>
                        <?php
                        } else {
                            $atri_id = '0';
                            $atri_id2 = '0';

                            $casa5 = mysqli_query($con, "SELECT *,sum(art_cantidad) as art_cantidad  FROM articulos_has_bodegas WHERE articulos_art_id='$art_id' 
                        AND empresa='$emp_id' ") or die(mysqli_error($con));
                            $load6 = mysqli_fetch_array($casa5);
                            $art_cantidad = ($load6['art_cantidad']); ?>
                            <div class="row">
                                <div class="col-12">
                                    <div class="breadcrumb_content">
                                        <ul>
                                            <li> <?php echo $art_cantidad; ?> disponibles</li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            <div class="action_links product-quantity-containe">
                                <ul>
                                    <select class="form-control-sm" id="cantidad<?php echo $load1['art_id_art']; ?>" name="cantidad">
                                        <?php
                                        for ($a = 1; $a <= $art_cantidad; $a++) { ?>
                                            <option value="<?php echo $a; ?>"><?php echo $a; ?></option>
                                        <?php } ?>
                                    </select>

                                    <li onclick="envia_carrito($('#art_id<?php echo $load1['art_id_art']; ?>').val(),$('#cantidad<?php echo $load1['art_id_art']; ?>').val());">
                                        <a style="border-radius: 50px; padding: 3px 25px;">
                                            <i class="zmdi zmdi-shopping-cart-plus"></i> Agregar al Carrito
                                        </a>
                                    </li>

                                    <input name="art_id" type="hidden" id="art_id<?php echo $load1["art_id_art"]; ?>" value="<?php echo $load1["art_id_art"]; ?>" />

                                    <?php
                                    if (isset($_SESSION['clie_id'])) {
                                        $sqll = mysqli_query($con, "SELECT * FROM cotizacion_detalle_deseo WHERE cotd_art_id='" . $load1['art_id_art'] . "' and clie_id='" . $_SESSION["clie_id"] . "'") or die(mysqli_error($con));

                                        if ($load2 = mysqli_fetch_array($sqll)) { ?>
                                            <li id="div1<?php echo $load1['art_id_art']; ?>" class="wishlist" onclick="eliminar_deseo(<?php echo $load1['art_id_art']; ?>);cambiar1(<?php echo $load1['art_id_art']; ?>);">
                                                <a class="active" title="Eliminar de deseos">
                                                    <i class="fa fa-heart-o" aria-hidden="true"></i>
                                                </a>
                                            </li>

                                            <li id="div2<?php echo $load1['art_id_art']; ?>" style="display:none;" class="wishlist" onclick="envia_deseos(<?php echo $load1['art_id_art']; ?>);cambiar2(<?php echo $load1['art_id_art']; ?>);">
                                                <a title="Enviar deseos">
                                                    <i class="fa fa-heart-o" aria-hidden="true"></i>
                                                </a>
                                            </li>

                                        <?php } else {  ?>

                                            <li style="display:none;" id="div1<?php echo $load1['art_id_art']; ?>" class="wishlist" onclick="eliminar_deseo(<?php echo $load1['art_id_art']; ?>);cambiar1(<?php echo $load1['art_id_art']; ?>);">
                                                <a class="active" title="Eliminar de deseos">
                                                    <i class="fa fa-heart-o" aria-hidden="true"></i>
                                                </a>
                                            </li>

                                            <li id="div2<?php echo $load1['art_id_art']; ?>" class="wishlist" onclick="envia_deseos(<?php echo $load1['art_id_art']; ?>);cambiar2(<?php echo $load1['art_id_art']; ?>);">
                                                <a title="Enviar deseos">
                                                    <i class="fa fa-heart-o" aria-hidden="true"></i>
                                                </a>
                                            </li>

                                        <?php }
                                    } else { ?>
                                        <li class="wishlist">
                                            <a data-id="" data-bs-toggle="modal" data-bs-target="#modal_box_sesion">
                                                <i class="fa fa-heart-o" aria-hidden="true"></i>
                                            </a>
                                        </li>
                                    <?php } ?>
                                </ul>
                            </div>
                        <?php }
                        $can4 = mysqli_query($con, "SELECT * FROM familias where fam_id='$fam_id'") or die(mysqli_error($con));
                        $dato5 = mysqli_fetch_array($can4);
                        $nombre = ucfirst($dato5['fam_nombre']);
                        $id = ($dato5['fam_id']);
                        ?>
                        <hr>
                        <div class="product_meta">
                            <span style="padding-right:50px;"><strong>SKU:</strong>&nbsp;&nbsp;&nbsp;<?php echo $art_referencia; ?></span>
                            <span><strong>Categoria:</strong><a href="articulos_categoria.php?fam_id=<?php echo $id; ?>"><?php echo $nombre; ?></a></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        </div>
        <div class="product_d_info">
            <div class="container">
                <div class="row">
                    <div class="col-12">
                        <div class="product_d_inner">
                            <div class="product_info_button">
                                <ul class="nav" role="tablist">
                                    <li>
                                        <a class="active" data-bs-toggle="tab" role="tab" aria-controls="info" aria-selected="false">Descripción</a>
                                    </li>
                                </ul>
                            </div>
                            <div class="tab-content">
                                <div class="tab-pane fade show active" id="info" role="tabpanel">
                                    <div class="product_info_content">
                                        <p><?php echo $art_nombre_largo; ?></p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <section class="product_area related_products mb-47">
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <div class="section_title">
                        <h2>Productos Relacionados</h2>
                    </div>
                    <div class="shop_area shop_fullwidth mt-47 mb-50">
                        <div class="row">
                            <div class="col-12">
                                <div class="row no-gutters shop_wrapper">
                                    <div id="mis_articulos"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <input name="fam_id" type="hidden" id="fam_id" value="<?php echo $dato5['fam_id']; ?>" />
    <input name="art_id" type="hidden" id="art_id" value="<?php echo $_GET['art_id']; ?>" />
    <?php include('include/footer.php') ?>
    <script src="main.js"></script>
    <script src="assets/js/plugins.js"></script>
    <script src="assets/js/main.js"></script>
    <script>
        $("#form_atributos").submit(function(event) {
            ;
            $('#btn_atributos').attr("disabled", true);
            var parametros = $(this).serialize();
            $.ajax({
                type: "POST",
                url: './php_tienda/sessiones_cart.php',
                data: parametros,
                beforeSend: function(objeto) {
                    $('#resultados').html('');
                },
                success: function(datos) {
                    $("#resultados").html(datos);
                    $('#btn_atributos').attr("disabled", false);
                    buscador();
                }
            });
            event.preventDefault();
        })



        function buscador() {
            $.get("./php_menu/buscador.php", "", function(data) {
                $("#div_buscador").html(data);
            })
        }
        buscador();

        function loadProductos() {
            var fam_id = $("#fam_id").val();
            var art_id = $("#art_id").val();
            $.get("php_tienda/tabla_articulos_relacionados.php?fam_id=" + fam_id + '&art_id=' + art_id,
                function(data) {
                    $("#mis_articulos").html(data);
                })
        }
        loadProductos();
        buscador();
        cambiar1();
        cambiar2();
        function envia_carrito(art_id, cantidad) {
           
            var parametros = {
                "art_id": art_id,
                "cantidad": cantidad
            };
            $.ajax({
                data: parametros,
                url: './php_tienda/sessiones_cart.php',
                type: 'POST',
                beforeSend: function() {
                    $('#resultados').html('');
                },
                success: function(datos) {
                    $("#resultados").html(datos);
                    buscador();
                }
            });
        }


        function envia_deseos(art_id) {
            var parametros = {
                "art_id": art_id
            };
            $.ajax({
                data: parametros,
                url: './php_tienda/registrar_deseos.php',
                type: 'POST',
                beforeSend: function() {
                    $('#resultados').html('');
                },
                success: function(datos) {
                    $("#resultados").html(datos);
                    cambiar1();
                    buscador();
                }
            });
        }



        function eliminar_deseo(art_id) {
            var parametros = {
                "art_id": art_id
            };
            $.ajax({
                data: parametros,
                url: './php_tienda/eliminar_deseo.php',
                type: 'POST',
                beforeSend: function() {
                    $('#resultados').html('');
                },
                success: function(datos) {
                    $("#resultados").html(datos);
                    cambiar2();
                    buscador();
                }
            });
        }

        function cambiar1(art_id) {
            $('#div1' + art_id).hide();
            $('#div2' + art_id).show();
        }

        function cambiar2(art_id) {
            $('#div1' + art_id).show();
            $('#div2' + art_id).hide();
        }
    </script>
    <div id="resultados"></div>
</body>

</html>