<!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>
        <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">
                <div class="content">   
                    <div class="row"><!--Titulo y botones-->
                        <div class="col-lg-12 col-md-12 col-xs-12">
                            <h3 class="page-header">Listado de Marcas
                                <a data-toggle="modal" data-target="#registrar" 
                                   class="btn btn-primary btn-circle pull-right" 
                                   rel="tooltip" data-title="Agregar">
                                    <i class="fa fa-plus"></i> AGREGAR
                                </a>                     
                            </h3>

                            <!--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>
                    </div> <!--Titulo y botones-->

                    <div class="row"> <!--Tablas y consultas-->
                        <div class="col-lg-12 col-md-12 col-xs-12">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    Marcas registradas
                                </div>
                                <?php
                                $marcas = consultas::get_datos("select * from marcas where mar_cod > 0 order by mar_cod asc ");
                                if (!empty($marcas)) {
                                    ?>                         
                                    <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">Descripción</th>                                         
                                                        <th class="text-center">Acciones</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="buscar">
                                                    <?php foreach ($marcas as $marca) { ?> 
                                                        <tr>
                                                            <td class="text-center"><?php echo $marca['mar_cod']; ?></td>
                                                            <td class="text-left"><?php echo $marca['mar_descrip']; ?></td>
                                                            <td class="text-center">
                                                                <a onclick="editar(this)" data-datos='<?= json_encode($marca) ?>' 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 "'" . $marca['mar_cod'] . "_" .$marca['mar_descrip'] . "'";?>)" 
                                                                   class="btn btn-sm btn-danger" rel='tooltip' data-title="Borrar"
                                                                   data-toggle="modal" data-target="#delete">
                                                                    <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 marcas...</strong>
                                        </div>
                                    <?php } ?>  
                                </div>

                            </div>
                        </div>
                    </div><!--Tablas y 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 Marcas</strong></h4>
                                </div>
                                <form action="marcas_control.php" method="post" accept-charset="utf-8" class="form-horizontal">
                                    <div class="panel-body se">
                                        <input type="hidden" name="accion"  value="1">
                                        <input type="hidden" name="codigo" value="0"/> 
                                        <input type="hidden" name="pagina" value="marcas_index.php" id="codigo">

                                        <label class="col-lg-2 control-label">Descripción:</label>
                                        <div class="col-lg-10">
                                            <input type="text" class="form-control" name="descripcion" 
                                                   required="" autofocus="" maxlength="15" 
                                                   id="descripcion" onkeyup="reemplazar()" pattern="[A-Za-z0-9 &]+">
                                        </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> Registrar</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>   
                    <!--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 Marcas</strong></h4>
                                </div>
                                <form action="marcas_control.php" method="post" accept-charset="utf-8" class="form-horizontal">
                                    <div class="panel-body">
                                        <input name="accion" value="2" type="hidden"/>
                                        <input type="hidden" name="pagina" value="marcas_index.php">
                                        <input id="codigo1" type="hidden" name="codigo"/>

                                        <label class="col-lg-2 control-label" style="left: 100px">Descripción:</label>
                                        <div class="col-lg-10" style="width: 250px; left: 110px;">
                                            <input type="text" class="form-control" name="descripcion" 
                                                   required="" autofocus="" maxlength="15" 
                                                   id="descripcion1" onkeyup="reemplazar1()" pattern="[A-Za-z0-9 ]+" >
                                        </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-primary pull-right">
                                            <i class="fa fa-refresh"></i> Actualizar</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <!--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-danger" ><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>

                </div>     
            </div> 
        </div> 
        <!--borrar-->

        <?php require '../menu/js_lte.ctp'; ?><!--ARCHIVOS JS-->
        <script>
            $('.modal').on('shown.bs.modal', function() {
                $(this).find('input:text:visible:first').focus();
            });
        </script>
        <script>
            function editar(datos) {
                var dat = $(datos).data("datos");
                $('#codigo1').val(dat.mar_cod);
                console.log(dat.mar_cod);
                $('#descripcion1').val(dat.mar_descrip);
                console.log(dat.mar_descrip);
            };
            function borrar(datos) {
                var dat = datos.split("_");
                $('#si').attr('href', 'marcas_control.php?codigo=' + dat[0]+ '&descripcion=' + dat[1]+ '&accion=3 &pagina=marcas_index.php');
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
             Desea Borrar el Registro <i><strong>' + dat[1] + '</strong></i>?');
            };
            $("#mensaje").delay(1000).slideUp(200, function () {
                $(this).alert('close');
            })

            function sololetras() {
                var numero = document.getElementById("descripcion").value;
                if (numero.match(/^[a-zA-Z-0-9]+$/))
                {

                } else {
                    alert("No se permiten caracteres especiales");
                    document.getElementById("descripcion").value = "";
                }
            }

            function reemplazar() {
                var res = document.getElementById("descripcion").value.replace("'", "");
                document.getElementById("descripcion").value = res;
            }
            function reemplazar1() {
                var res = document.getElementById("descripcion1").value.replace("'", "");
                document.getElementById("descripcion1").value = res;
            }
        </script>

    </body>
</html>
