<?php
session_start();
require_once("../conexion.php");
require_once('../include/sesiones.php'); 

$est = mysqli_query($con, "SELECT * FROM modulo_permiso WHERE mod_id='19' and modp_nombre='Modificar Precio' and usuario='$usu_id'") or die(mysqli_error($con));
$est = mysqli_fetch_array($est);

if ($est['estado'] == 'Activo') {
    $readonly = '';
} else {
    $readonly = 'readonly';
}

$action = (isset($_REQUEST['action']) && $_REQUEST['action'] != NULL) ? $_REQUEST['action'] : '';

if ($action == 'ajax') {
    $tarifa = mysqli_real_escape_string($con, (strip_tags($_REQUEST['tarifa'], ENT_QUOTES)));
    $q = mysqli_real_escape_string($con, (strip_tags($_REQUEST['q'], ENT_QUOTES)));
    $tipo = mysqli_real_escape_string($con, (strip_tags($_REQUEST['tipo'], ENT_QUOTES)));
    $buscar = mb_strtoupper($q);

    include '../include/pagination.php';

    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page'])) ? $_REQUEST['page'] : 1;
    $per_page = 6;
    $adjacents = 4;
    $offset = ($page - 1) * $per_page;
?>

<div class="table-responsive">
    <table class="table">
        <tr class="warning">
            <th width="10%">CODIGO</th>
            <th width="">NOMBRE</th>
            <th width="8%"><span class="pull-center">STOCK</span></th>
            <th width="10%"><span class="pull-left">CANTIDAD</span></th>
            <th width="10%"><span class="pull-right">DESCUENTO</span></th>
            <th width="16%"><span class="pull-right">PRECIO</span></th>
            <th width="10%"></th>
        </tr>

        <?php
        if ($tipo == 'articulos') {
            $count_query = mysqli_query($con, "SELECT count(*) AS numrows FROM articulos a, articulos_has_bodegas b
                WHERE a.art_id=b.articulos_art_id AND a.empresa='$empresa' AND b.sucursal='$sucursal' AND a.art_estado='activo' AND a.art_tipo='articulo' 
                AND (a.art_referencia LIKE '%$q%' OR a.art_nombre LIKE '%$q%' OR a.art_barras LIKE '%$q%' OR a.art_referencia2 LIKE '%$q%')");

            $row = mysqli_fetch_array($count_query);
            $numrows = $row['numrows'];
            $total_pages = ceil($numrows / $per_page);
            $reload = '../index.php';
            $sql = mysqli_query($con, "SELECT *, a.art_id AS art_id, b.art_id AS art_id_bod FROM articulos a, articulos_has_bodegas b
                WHERE a.art_id=b.articulos_art_id AND a.empresa='$empresa' AND b.sucursal='$sucursal' AND a.art_estado='activo' AND a.art_tipo='articulo' 
                AND (a.art_referencia LIKE '%$q%' OR a.art_nombre LIKE '%$q%' OR a.art_barras LIKE '%$q%' OR a.art_referencia2 LIKE '%$q%') 
                ORDER BY b.art_cantidad DESC, a.art_referencia ASC LIMIT $offset, $per_page");

            if ($tarifa == 0) {
                $sql_tar = mysqli_query($con, "SELECT tarifas_tar_id FROM cliente c, cliente_cotizacion_temp cc 
                    WHERE c.clie_id=cc.cct_clie_id AND cc.usuario='$usu_id' AND c.empresa='$empresa'");
                
                if ($rw_tar = mysqli_fetch_array($sql_tar)) {
                    $tarifas_tar_id = $rw_tar['tarifas_tar_id'];
                } else {
                    $clim = mysqli_query($con, "SELECT * FROM cliente where clie_destacado='si' AND empresa='$empresa'") or die(mysqli_error($con));
                    $clim = mysqli_fetch_array($clim);
                    $tarifas_tar_id = $clim['tarifas_tar_id'];
                }
            } else {
                $tarifas_tar_id = $tarifa;
            }

            while ($row = mysqli_fetch_array($sql)) {
                $art_id = $row['art_id'];
                $art_id_bod = $row['art_id_bod'];
                $art_tipo = $row['art_tipo'];
                $art_referencia = $row['art_referencia'];
                $art_nombre = mb_strtoupper($row['art_nombre']);
                $art_pvp = $row['art_pvp'];
                $art_iva = $row['art_iva'];
                $art_precio_costo = $row['art_precio_costo'];
                $art_cantidad = $row['art_cantidad'];
                $art_venta_min = $row['art_venta_min'];

                $sql_pre = mysqli_query($con, "SELECT * FROM precio WHERE articulos_art_id='$art_id' AND tarifas_tar_id='$tarifas_tar_id'");
                if ($rw_pre = mysqli_fetch_array($sql_pre)) {
                    $pre_precio = $rw_pre['pre_precio'];
                } else {
                    $pre_precio = $row['art_pvp'];
                }
        ?>

        <tr>
            <td>
                <input type="hidden" id="cambio<?php echo $art_id_bod; ?>" value="ven">
                <input type="hidden" id="art_id<?php echo $art_id_bod; ?>" value="<?php echo $art_id; ?>">
                <input type="hidden" id="art_referencia<?php echo $art_id_bod; ?>" value="<?php echo $art_referencia; ?>">
                <input type="hidden" id="art_iva<?php echo $art_id_bod; ?>" value="<?php echo $art_iva; ?>" >
                <input type="hidden" id="precio_costo<?php echo $art_id_bod; ?>" value="<?php echo $art_precio_costo; ?>">
                <?php echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $art_referencia)); ?>
            </td>
            <td>
                <input type="hidden" id="nombre<?php echo $art_id_bod; ?>" value="<?php echo $art_nombre; ?>">
                <?php echo mb_strtoupper(str_replace($buscar, "<span style='font-weight: bold;color:".$resaltador."'>".$buscar."</span>", $art_nombre)); ?>
                <span style="color: red;">
                    <?php if ($row["art_atributo_estado"] == 'si') {
                        $atributo1 = $row['atributo1'];
                        $atri1 = mysqli_query($con, "SELECT * FROM atributos1 WHERE atri_art_id='$art_id' AND atri_id='$atributo1'");
                        $fila1 = mysqli_fetch_array($atri1);
                        if (isset($fila1['atri_nombre'])) {
                            echo $atri_nombre1 = $fila1['atri_nombre'];
                        }
                        $atributo2 = $row['atributo2'];
                        $atri2 = mysqli_query($con, "SELECT * FROM atributos2 WHERE atri_art_id2='$art_id' AND atri_id2='$atributo2'");
                        $fila2 = mysqli_fetch_array($atri2);
                        if (isset($fila2['atri_nombre2'])) {
                            echo ' / ' . $atri_nombre2 = $fila2['atri_nombre2'];
                        }
                    } ?>
                </span>
            </td>
            <td><p style="text-align:right"><?php echo $art_cantidad; ?></p></td>
            <td><input type="text" class="form-control input-sm" style="text-align:left" id="cantidad<?php echo $art_id_bod; ?>" value="1"></td>
            <td><input type="text" class="form-control input-sm" style="text-align:left" id="descuento<?php echo $art_id_bod; ?>" value="0"></td>
            <td><input type="text" class="form-control input-sm" style="text-align:right" id="precio_venta<?php echo $art_id_bod; ?>" value="<?php echo $pre_precio; ?>" <?php echo $readonly; ?> ></td>
            <td>
                <a data-id="<?php echo $art_id; ?>" class="btn btn-edit btn-xs btn-warning" title="Tarifas de Precio"> <i class="btn-icon-only icon-usd"></i></a>
                <span class="pull-right">
                    <a href="#" class="btn btn-xs btn-info" onclick="agregar('<?php echo $art_id_bod ?>')">
                        <i class="btn-icon-only icon-plus"></i>
                    </a>
                </span>
            </td>
        </tr>

        <?php } ?>

        <tr class="danger">
            <td><input type="text" class="form-control input-sm" id="art_referencia" value="" placeholder="Codigo"></td>
            <td colspan="2"><input type="text" class="form-control input-sm" id="nombre" placeholder="Nombre"></td>
            <td>
                <input type="text" id="cantidad" class="form-control input-sm" value="1">
                <input type="hidden" id="art_iva" value="00">
            </td>
            <td><input type="text" class="form-control input-sm" style="text-align:right" id="descuento" value="0"></td>
            <td>
                <input type="hidden" id="precio_costo_" value="0">
                <input type="text" id="precio_venta" class="form-control input-sm" placeholder="precio" autocomplete="off" required value="">
            </td>
            <td>
                <span class="pull-right">
                    <a href="#" class="btn btn-xs btn-info" onclick="agregarnuevo()">
                        <i class="btn-icon-only icon-plus"></i>
                    </a>
                </span>
            </td>
        </tr>

        <tr>
            <td colspan="7">
                <span class="pull-right">
                    <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
                </span>
            </td>
        </tr>

        <?php }

        if ($tipo == 'servicio') {
            $count_query = mysqli_query($con, "SELECT count(*) AS numrows FROM articulos 
                WHERE empresa='$empresa' AND art_tipo='servicio' 
                AND (art_referencia LIKE '%$q%' OR art_nombre LIKE '%$q%' OR art_barras LIKE '%$q%')");

            $row = mysqli_fetch_array($count_query);
            $numrows = $row['numrows'];
            $total_pages = ceil($numrows / $per_page);
            $reload = '../I_articulos.php';

            $sql = mysqli_query($con, "SELECT * FROM articulos 
                WHERE empresa='$empresa' AND art_tipo='servicio' 
                AND (art_referencia LIKE '%$q%' OR art_nombre LIKE '%$q%' OR art_barras LIKE '%$q%') 
                ORDER BY art_nombre DESC LIMIT $offset, $per_page");

            if ($numrows > 0) {
                while ($row = mysqli_fetch_array($sql)) {
                    $art_id = $row['art_id'];
                    $art_referencia = $row['art_referencia'];
                    $art_nombre = mb_strtoupper($row['art_nombre']);
                    $pre_precio = $row['art_pvp'];
                    $art_iva = $row['art_iva'];
                    $art_precio_costo = $row['art_precio_costo'];
                    $art_cantidad = 0;
        ?>

        <tr>
            <td>
                <input type="hidden" id="art_id<?php echo $art_id; ?>" value="<?php echo $art_id; ?>">
                <input type="hidden" id="art_referencia<?php echo $art_id; ?>" value="<?php echo $art_referencia; ?>">
                <input type="hidden" id="nombre<?php echo $art_id; ?>" value="<?php echo $art_nombre; ?>">
                <input type="hidden" id="art_iva<?php echo $art_id; ?>" value="<?php echo $art_iva; ?>" >
                <input type="hidden" id="precio_costo<?php echo $art_id; ?>" value="<?php echo $art_precio_costo;?>">
                <input type="hidden" id="tipo<?php echo $art_id; ?>" value="servicio">

                <?php echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $art_referencia)); ?>
            </td>
            <td><?php echo mb_strtoupper(str_replace($buscar, "<strong><span style='color:".$resaltador."'>".$buscar."</span></strong>", $art_nombre)); ?></td>
            <td><p style="text-align:right"><?php echo $art_cantidad; ?></p></td>
            <td><input type="text" class="form-control input-sm" style="text-align:left" id="cantidad<?php echo $art_id; ?>" value="1" ></td>
            <td><input type="text" class="form-control input-sm" style="text-align:right" id="descuento<?php echo $art_id;?>" value="0"></td>
            <td><input type="text" class="form-control input-sm" style="text-align:right" id="precio_venta<?php echo $art_id; ?>" value="<?php echo $pre_precio;?>"></td>
            <td>
                <a data-id="<?php echo $art_id;?>" class="btn btn-edit btn-xs btn-warning" title="Tarifas de Precio"> <i class="btn-icon-only icon-usd"></i></a>
                <span class="pull-right"><a href="#" class="btn btn-xs btn-info" onclick="agregarservicio_producto('<?php echo $art_id ?>')"><i class="btn-icon-only icon-plus"></i></a></span>
            </td>
        </tr>

        <?php } } ?>
        
        <tr>
            <td colspan="7" ><span class="pull-right">
                <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
            </span></td>
        </tr>
        <?php } ?>
    </table>
</div>

<script>
    $(".btn-edit").click(function(){
        id = $(this).data("id");
        $.get("./php_articulo/form_tarifas_precio.php", "id=" + id, function(data) {
            $("#form-tar").html(data);
        });
        $('#tarifa_cot').modal('show');
    });
</script>

<?php } ?>
