<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <link rel="shortcut icon" type="image/x-icon" href="/farmmedi/favicon.png">
        <title>FARMMEDI</title>
        <!-- Tell the browser to be responsive to screen width -->
        <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
        <?php
        include "../conexion.php";
        session_start(); /* Reanudar sesion */
        require '../menu/css_lte.ctp';
        ?>
    </head>
    <body class="hold-transition skin-blue sidebar-mini">  <!--Cabecera y menu izquierda-->
        <div class="wrapper">
            <?php require '../menu/cabecera.ctp'; ?><!--CABECERA PRINCIPAL-->
            <?php require '../menu/izquierda.ctp'; ?><!--MENU PRINCIPAL-->
            <div class="content-wrapper "> <!--Contenedor-->
                <div class="content"> 
                    <!--row titulo-->
                    <div class="row">
                        <!--impresion del titulo de la pagina-->
                        <div class="col-lg-12 col-md-12 col-xs-12">
                            <h3 class="page-header">Listado de Articulos
                                <a data-toggle="modal" data-target="#registrar" 
                                   class="btn btn-primary btn-circle pull-right" 
                                   rel="tooltip" data-title="Registrar">
                                    <i class="fa fa-plus"></i> AGREGAR
                                </a>                     
                            </h3>
                        </div> 
                        <!--impresion del titulo de la pagina-->
                        <!--Buscador-->
                        <div class="col-lg-12">
                            <div class="panel-heading">
                                <div class="input-group custom-search-form">
                                    <input id="filtrar" type="text" class="form-control" placeholder="Buscar...">
                                    <span class="input-group-btn">
                                        <button class="btn btn-primary" type="button" rel="tooltip" title="Buscar">
                                            <i class="fa fa-search"></i>
                                        </button>
                                    </span>
                                </div>
                            </div>                      
                        </div>
                        <!--fin Buscador-->
                    </div> 
                    <!--fin de row titulo-->

                    <!-- /.row consultas -->
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    Articulos Registrados
                                </div>
                                <?php
                                $articulos = consultas::get_datos("select * from v_articulos order by id_articulo asc");
                                if (!empty($articulos)) {
                                    ?>                         
                                    <!-- /.panel-heading -->
                                    <div class="panel-body">
                                        <div>
                                            <table id="example1" width="100%" 
                                                   class="table table-striped table-bordered table-hover tablas">
                                                <thead>
                                                    <tr>
                                                        <th class="text-center">#</th>
                                                        <th class="text-center">Cod. Barra</th>
                                                        <th class="text-center">Tipo</th>
                                                        <th class="text-center">Presentación</th>
                                                        <th class="text-center">Marca</th>
                                                        <th class="text-center">Descripción</th>
                                                         <th class="text-center">% IVA</th>
                                                        <th class="text-center">Imagen</th>
                                                        <th class="text-center">Acciones</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="buscar">
                                                    <?php foreach ($articulos as $articulo) { ?> 
                                                        <tr>
                                                            <td class="text-center"><?php echo $articulo['id_articulo']; ?></td>
                                                            <td class="text-center"><?php echo $articulo['cod_barra']; ?></td>
                                                            <td class="text-center"><?php echo $articulo['tipo']; ?></td>
                                                            <td class="text-center"><?php echo $articulo['modelo']; ?></td>
                                                            <td class="text-center"><?php echo $articulo['mar_descrip']; ?></td>
                                                            <td class="text-left"><?php echo $articulo['art_descri']; ?></td>
                                                            <td class="text-center"><?php echo $articulo['art_iva']; ?></td>
                                                            <td class="text-center">
                                                                <img height="45px" src="/farmmedi/img/articulos/<?php echo (!empty($articulo['art_imagen'])?$articulo['art_imagen']:"no-image.jpg") ?>"/>
                                                            </td>
                                                            <td class="text-center">
                                                                <a onclick="editar(this)" data-datos2='<?= json_encode($articulo); ?>'
                                                                   class="btn btn-sm btn-warning" rel='tooltip' data-title="Editar" 
                                                                   data-toggle="modal" data-target="#editar">
                                                                    <i class="fa fa-edit"></i>
                                                                </a>
                                                                <a onclick="borrar(<?php
                                                                echo "'" . $articulo['id_articulo'] . "_" . $articulo['art_descri'] . "'";
                                                                ?>)"
                                                                   data-toggle="modal" data-target="#delete" class="btn btn-sm btn-danger" rel='tooltip' data-title="Borrar">
                                                                    <i class="fa fa-trash"></i>
                                                                </a>
                                                            </td>
                                                        </tr>
                                                    <?php } ?>
                                                </tbody>
                                            </table>                         
                                        </div>
                                        <?php } else { ?>
                                        <div class="alert alert-info alert-dismissable">
                                            <strong>Aún no se registraron articulos...</strong>
                                        </div>
                                        <?php } ?>  
                                    </div>
                            </div>
                        </div>
                    </div>
                    <!--Final row consultas-->   

                    <!--Boton de Registrar-->
                    <div id="registrar" class="modal fade" role="dialog">
                        <div class="modal-dialog">
                            <!-- Modal content-->
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" arial-label="Close">x</button>
                                    <h4 class="modal-title"><i class="fa fa-plus"></i> <strong>Registrar Articulos</strong></h4>
                                </div>
                                <form action="articulos_control.php" method="post" accept-charset="utf-8" class="form-horizontal">
                                    <div class="modal-body">
                                        <input type="hidden" name="accion"  value="1">
                                        <input type="hidden" name="id_articulo1" id="id_articulo1" value="0"/> 
                                        <input type="hidden" name="pagina" value="articulos_index.php" id="cod">

                                        <div class="form-group">
                                            <label class="col-md-2 control-label">Cod. Barra</label>
                                            <div class="col-lg-4 col-sm-4 col-md-4">
                                                <input type="text" class="form-control" name="cod_barra1" autofocus="" maxlength="13" 
                                                       id="codigob" placeholder="Cod. Barra" onkeyup="reemplazar()" pattern="[A-Za-z0-9-.]+">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-2 control-label" >Descripción</label>
                                            <div class="col-lg-10 col-sm-10 col-md-10">
                                                <input type="text" class="form-control" name="art_descri1" 
                                                       required="" autofocus="" id="descripcion" placeholder="Ingrese la Descripcion">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-2 control-label ">Tipo:</label>
                                            <div class="col-lg-6 col-sm-6 col-md-6">
                                                <?php $tipos = consultas::get_datos("select * from tipo_articulo order by tarti_cod"); ?>                                 
                                                <select name="tarti1" class="select2 form-control" required="" style="width: 100%">
                                                    <?php if (!empty($tipos)) {
                                                        foreach ($tipos as $tipo) { ?>
                                                            <option value="<?php echo $tipo['tarti_cod']; ?>">
                                                                <?php echo $tipo['descripcion']; ?></option>
                                                            <?php
                                                        }
                                                    } else {
                                                        ?>
                                                        <option value="0">Debe insertar un tipo</option>
                                                    <?php } ?>
                                                </select> 
                                            </div>
                                        </div>
                                        <div class="form-group">                                      
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Marca:</label>
                                            <div class="col-lg-6 col-sm-6 col-md-6">
                                                <?php $marcas = consultas::get_datos("select * from marcas order by mar_cod"); ?>                                 
                                                <select name="mar_cod1" class="form-control select2" style="width: 100%">
                                                    <?php if (!empty($marcas)) {
                                                        foreach ($marcas as $marca) { ?>
                                                            <option value="<?php echo $marca['mar_cod']; ?>">
                                                                <?php echo $marca['mar_descrip']; ?></option>
                                                            <?php
                                                        }
                                                    } else {
                                                        ?>
                                                        <option value="0">Debe insertar una Marca</option>
                                                    <?php } ?>
                                                </select>
                                            </div>

                                        </div>
                                         <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label ">Presentación</label>
                                            <div class="col-lg-6 col-sm-6 col-md-6">
                                                <?php $modelos = consultas::get_datos("select * from modelo order by cod_modelo"); ?>                                 
                                                <select name="modelo" class="form-control select2" style="width: 100%">
                                                    <?php if (!empty($modelos)) {
                                                        foreach ($modelos as $modelo) { ?>
                                                            <option value="<?php echo $modelo['cod_modelo']; ?>">
                                                                <?php echo $modelo['descripcion']; ?></option>
                                                            <?php
                                                        }
                                                    } else {
                                                        ?>
                                                        <option value="0">Debe insertar un registro</option>
                                                    <?php } ?>
                                                </select> 
                                            </div>                    
                                        </div>                                                                                
                                        <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">P/Compra</label>
                                            <div class="col-lg-4 col-md-4 col-sm-4">
                                                <input type="number" class="form-control" name="pre_compra1" 
                                                       required="" id="precompra" min="0" value="0">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">P/Venta</label>
                                            <div class="col-lg-4 col-md-4 col-sm-4">
                                                <input type="number" class="form-control" name="pre_venta1" 
                                                       required="" id="preventa" min="0" value="0">
                                            </div>
                                        </div>                                        
                                        <div class="form-group">
                                            <label class="col-md-2 control-label">IVA</label>
                                            <div class="col-md-4">
                                                <label class="radio-inline">
                                                    <input type="radio" value="5"  class="radio1" name="art_iva1" required="">5%
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="radio" value="10" class="radio2" name="art_iva1">10%
                                                </label>
                                            </div>
                                        </div> 

                                        <div class="form-group">
                                            <label class="col-md-2 control-label">Imagen</label>
                                            <div class="col-md-8">
                                                <input type="file" placeholder="Ingrese Imagen"  class="form-control" name="art_imagen1" >
                                            </div>
                                        </div>     
                                        <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Stock Min.</label>
                                            <div class="col-lg-3 col-md-3 col-sm-3">
                                                <input type="number" class="form-control" name="stock_min" 
                                                       required="" min="0" value="0">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Stock Max.</label>
                                            <div class="col-lg-3 col-md-3 col-sm-3">
                                                <input type="number" class="form-control" name="stock_max" 
                                                       required="" min="0" value="0">
                                            </div>
                                        </div>                                        
                                    </div>
                                    
                                    <div class="modal-footer">
                                        <button type="reset" data-dismiss="modal" class="btn btn-default pull-left">
                                            <i class="fa fa-close"></i> Cerrar</button>
                                        <button type="submit" class="btn btn-primary pull-right">
                                            <i class="fa fa-floppy-o"></i> Guardar</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>  
                    <!--fin registrar-->

                    <!--editar-->
                    <div id="editar" class="modal fade" role="dialog">
                        <div class="modal-dialog">
                            <!-- Modal content-->
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" arial-label="Close">x</button>
                                    <h4 class="modal-title"><i class="fa fa-edit"></i> <strong>Editar Articulos</strong></h4>
                                </div>
                                <form action="articulos_control.php" method="post" accept-charset="utf-8" class="form-horizontal">
                                    <div class="modal-body">
                                        <input type="hidden" name="accion"  value="2">
                                        <input type="hidden" name="id_articulo1" id="id_articulo_edi" value="0"/> 
                                        <input type="hidden" name="pagina" value="articulos_index.php" id="cod">

                                        <div class="form-group">
                                            <label class="col-md-2 control-label">Cod. Barra</label>
                                            <div class="col-lg-4 col-sm-4 col-md-4">
                                                <input type="text" class="form-control" name="cod_barra1" autofocus="" maxlength="13" 
                                                       id="codigo" placeholder="Cod. Barra" onkeyup="reemplazar()" pattern="[A-Za-z0-9-.]+">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-2 control-label" >Descripción</label>
                                            <div class="col-lg-10 col-sm-10 col-md-10">
                                                <input type="text" class="form-control" name="art_descri1" 
                                                       required="" autofocus="" id="descri" placeholder="Ingrese la Descripcion">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-2 control-label ">Tipo:</label>
                                            <div class="col-lg-6 col-sm-6 col-md-6">
                                                <?php $tipos = consultas::get_datos("select * from tipo_articulo order by tarti_cod"); ?>                                 
                                                <select name="tarti1" id="tipo" class="select2 form-control" required="" style="width: 100%">
                                                    <?php if (!empty($tipos)) {
                                                        foreach ($tipos as $tipo) { ?>
                                                            <option value="<?php echo $tipo['tarti_cod']; ?>">
                                                                <?php echo $tipo['descripcion']; ?></option>
                                                            <?php
                                                        }
                                                    } else {
                                                        ?>
                                                        <option value="0">Debe insertar un tipo</option>
                                                    <?php } ?>
                                                </select> 
                                            </div>
                                        </div>
                                        <div class="form-group">                                      
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Marca:</label>
                                            <div class="col-lg-6 col-sm-6 col-md-6">
                                                <?php $marcas = consultas::get_datos("select * from marcas order by mar_cod"); ?>                                 
                                                <select name="mar_cod1" id="marca" class="form-control select2" style="width: 100%">
                                                    <?php if (!empty($marcas)) {
                                                        foreach ($marcas as $marca) { ?>
                                                            <option value="<?php echo $marca['mar_cod']; ?>">
                                                                <?php echo $marca['mar_descrip']; ?></option>
                                                            <?php
                                                        }
                                                    } else {
                                                        ?>
                                                        <option value="0">Debe insertar una Marca</option>
                                                    <?php } ?>
                                                </select>
                                            </div>

                                        </div>
                                         <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label ">Presentación</label>
                                            <div class="col-lg-6 col-sm-6 col-md-6">
                                                <?php $modelos = consultas::get_datos("select * from modelo order by cod_modelo"); ?>                                 
                                                <select name="modelo" id="modelo" class="form-control select2" style="width: 100%">
                                                    <?php if (!empty($modelos)) {
                                                        foreach ($modelos as $modelo) { ?>
                                                            <option value="<?php echo $modelo['cod_modelo']; ?>">
                                                                <?php echo $modelo['descripcion']; ?></option>
                                                            <?php
                                                        }
                                                    } else {
                                                        ?>
                                                        <option value="0">Debe insertar un registro</option>
                                                    <?php } ?>
                                                </select> 
                                            </div>                    
                                        </div>                                                                                
                                        <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">P/Compra</label>
                                            <div class="col-lg-4 col-md-4 col-sm-4">
                                                <input type="number" class="form-control" name="pre_compra1" 
                                                       required="" id="precioc_edi" min="0" value="0">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">P/Venta</label>
                                            <div class="col-lg-4 col-md-4 col-sm-4">
                                                <input type="number" class="form-control" name="pre_venta1" 
                                                       required="" id="preciov_edi" min="0" value="0">
                                            </div>
                                        </div>                                        
                                        <div class="form-group">
                                            <label class="col-md-2 control-label">IVA</label>
                                            <div class="col-md-4">
                                                <label class="radio-inline">
                                                    <input type="radio" value="5"  class="radio1" name="art_iva1" required="">5%
                                                </label>
                                                <label class="radio-inline">
                                                    <input type="radio" value="10" class="radio2" name="art_iva1">10%
                                                </label>
                                            </div>
                                        </div> 

                                        <div class="form-group">
                                            <label class="col-md-2 control-label">Imagen</label>
                                            <div class="col-md-8">
                                                <input type="file" placeholder="Ingrese Imagen"  class="form-control" name="art_imagen1" >
                                            </div>
                                        </div>     
                                        <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Stock Min.</label>
                                            <div class="col-lg-3 col-md-3 col-sm-3">
                                                <input type="number" class="form-control" name="stock_min" 
                                                       required="" id="stock_min" min="0" value="0">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Stock Max.</label>
                                            <div class="col-lg-3 col-md-3 col-sm-3">
                                                <input type="number" class="form-control" name="stock_max" 
                                                       required="" id="stock_max" min="0" value="0">
                                            </div>
                                        </div> 
                                    </div>
                                    
                                    <div class="modal-footer">
                                        <button type="reset" data-dismiss="modal" class="btn btn-danger pull-left">
                                            <i class="fa fa-close"></i> Cerrar</button>
                                        <button type="submit" class="btn btn-warning pull-right">
                                            <i class="fa fa-edit"></i> Actualizar</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div> 
                    <!--fin editar-->

                    <!--borrar-->
                    <div class="modal fade" id="delete" tabindex="-1" role="dialog">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                    <h4 class="modal-title custom_align" id="Heading">Atenci&oacute;n!!!</h4>
                                </div>
                                <div class="modal-body">
                                    <div class="alert alert-danger" id="confirmacion"></div>
                                </div>
                                <div class="modal-footer">
                                    <a id="si" role="button" class="btn btn-primary" ><span class="glyphicon glyphicon-ok-sign"></span> Si</a>
                                    <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> No</button>
                                </div>
                            </div>
                        </div>
                    </div> 
                    <!--fin borrar-->
                </div>
            </div> <!--Final de Contenedor-->     
        </div> <!--Final de id wrapper-->
        <?php require '../menu/js_lte.ctp'; ?><!--archivos js--> 
        <!--script-->
        <script>
            function editar(datos) {
                var dat = $(datos).data("datos2");
                $('#id_articulo_edi').val(dat.id_articulo);
                $('#codigo').val(dat.cod_barra);
                $('#tipo').val(dat.tarti_cod).trigger('change');
                $('#marca').val(dat.mar_cod).trigger('change');
                $('#modelo').val(dat.cod_modelo).trigger('change');
                $('#descri').val(dat.art_descri);
                $('#precioc_edi').val(dat.pre_compra);
                $('#preciov_edi').val(dat.pre_venta);
                $('#stock_min').val(dat.stock_min);
                $('#stock_max').val(dat.stock_max);

                /*console.log(dat.pre_venta);
                console.log(dat.pre_compra);*/

                if (dat.art_iva == 5) {
                    $('.radio1').prop("checked", true);
                } else {
                    $('.radio2').prop("checked", true);
                }
            };

            function borrar(datos) {
                var dat = datos.split("_");
                console.log(dat[1]);
                $('#si').attr('href', 'articulos_control.php?id_articulo1=' + dat[0] +
                        '&tarti1=null' +
                        '&mar_cod1=null' +
                        '&modelo=null' +
                        '&calce=null' +
                        '&art_descri1=null' +
                        '&pre_compra1=null' +
                        '&art_imagen1=null' +
                        '&art_iva1=null' +
                        '&cod_barra1=null' +
                        '&pre_venta1=null' +
                        '&stock_min=null' +
                        '&stock_max=null' +
                        '&accion=3' +
                        '&pagina=articulos_index.php');
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                    Desea Borrar el Articulo <i><strong>' + dat[1] + '</strong></i>?');
            }
            /*PARA TIEMPO DE MENSAJE*/
            $("#mensaje").delay(2000).slideUp(200, function () {
                $(this).alert('close');
            });

            /*PARA VALIDAR CAMPOS*/
            function reemplazar() {
                var res = document.getElementById("codigob").value.replace("'", "");
                document.getElementById("codigob").value = res;
            }
            function reemplazar1() {
                var res = document.getElementById("descripcion").value.replace("'", "");
                document.getElementById("descripcion").value = res;
            }
        </script>
        <!--script-->
    </body>
</html>
