<?php
session_start();
include('../conexion.php');
include('../include/sesiones.php');
$id = $_GET['id'];
$cano = 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='$id'") or die(mysqli_error($con));
$load1 = mysqli_fetch_array($cano);

$art_atributo_estado = ($load1['art_atributo_estado']);
$art_id_art = ($load1['art_id_art']);
$familias_fam_id = ($load1['familias_fam_id']);

$nombre_archivo = '../../img/articulos/' . $load1['art_id_art'] . '.jpg'; ?>
<div class="container">
    <div class="row">
        <div class="col-lg-5 col-md-5 col-sm-12">
            <div class="modal_tab">
                <div class="tab-content product-details-large">
                    <div class="tab-pane fade show active" id="tab1" role="tabpanel">
                        <div class="modal_tab_img">
                            <a>
                                <?php
                                if (file_exists($nombre_archivo)) {
                                    $nu = rand(1, 1000);
                                    $nu; ?>
                                    <img src="<?php echo $emp_url_sub; ?>/img/articulos/<?php echo $load1['art_id_art'] . '.jpg?' . $nu . ''; ?>" alt="">
                                <?php } else { ?>
                                    <img src="<?php echo $emp_url_sub; ?>/img/articulos/sin_imagen.jpg" alt="#">
                                <?php   }   ?>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-7 col-md-7 col-sm-12">
            <div class="modal_right">
                <div class="modal_title mb-10">
                    <h2><?php echo substr(utf8_encode($load1['art_nombre_online']), 0, 65); ?></h2>
                </div>
                <div class="modal_description mb-15">
                    <p><?php echo substr($load1['art_nombre'], 0, 500); ?></p>
                </div>
                <div class="modal_price mb-10">
                    <?php include("funcion_precio.php") ?>
                </div>
                <div class="row">
                    <div class="col-12">
                        <div class="modal_referencia mb-15">
                            <p> Cod: <?php echo $load1['art_referencia']; ?></p>
                        </div>
                        <?php
                        $casa4 = mysqli_query($con, "SELECT * FROM familias WHERE fam_id='$familias_fam_id' AND empresa='$emp_id' ") or die(mysqli_error($con));
                        $load8 = mysqli_fetch_array($casa4);

                        $nombre = ucfirst($load8['fam_nombre']);
                        $fam_id = ($load8['fam_id']);
                        ?>
                        <ul>
                            <div class="modal_title mb-10">
                                <li>
                                    <a href="articulos_categoria.php?fam_id=<?php echo $fam_id; ?>"> Categoria :
                                        <?php echo $nombre; ?></a>
                                </li>
                            </div>
                        </ul>
                    </div>
                </div>
                <?php
                if ($art_atributo_estado == 'si') { ?>
                    <form id="form_atributos<?php echo $art_id_art; ?>" name="form_atributos<?php echo $art_id_art; ?>"
                        method="POST">
                        <div class="col-12">

                            <div class="product_variant color col-12">
                                <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 $art_id_art; ?>" value="1" class="pl-2" />

                            <div class="modal_add_to_cart1">
                                <button id="btn_atributos" name="btn_atributos" type="submit">Agregar al Carrito</button>
                            </div>
                        </div>
                    </form> <?php
                        } else if ($art_atributo_estado == 'no') {
                            $atri_id = '0';
                            $atri_id2 = '0';

                            $casa4 = mysqli_query($con, "SELECT *,sum(art_cantidad) as art_cantidad  FROM articulos_has_bodegas where articulos_art_id='$art_id_art' AND atributo1='$atri_id' AND atributo2='$atri_id2' AND empresa='$emp_id'") or die(mysqli_error($con));

                            $load5 = mysqli_fetch_array($casa4);
                            $art_cantidad = $load5['art_cantidad']; ?>

                    <div class="variants_selects">
                        <div class="breadcrumb_content">
                            <ul>
                                <li><?php echo $art_cantidad; ?> disponibles</li>
                            </ul>
                        </div>
                        <div class="modal_add_to_cart">
                            <input name="art_id" type="hidden" id="art_id<?php echo $load1['art_id_art']; ?>"
                                value="<?php echo $load1['art_id_art']; ?>" />
                            <select class="form-control-sm" id="cantidad1<?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>
                            <button onclick="envia_carrito2($('#art_id<?php echo $load1['art_id_art']; ?>').val(),$('#cantidad1<?php echo $load1['art_id_art']; ?>').val());" type="submit">Agregar al Carrito
                            </button>
                        </div>
                    </div><?php
                        } ?>
            </div>
        </div>
    </div>
</div>
<script src="assets/js/main.js"></script>
<script>
    $("#form_atributos<?php echo $art_id_art; ?>").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 envia_carrito2(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();
            }
        });
    }
</script>