<?php
include '../session.php';
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

        <?php
        include "../conexion.php";
        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 Proveedores
                                <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>    

                        <!--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">
                                    Datos
                                </div>
                                <?php
                                $proveedores = consultas::get_datos("select * from v_proveedores order by codigo asc ");
                                if (!empty($proveedores)) {
                                    ?>                         
                                    <!-- /.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">RUC</th>
                                                        <th class="text-center">Razon Social</th>
                                                        <th class="text-center">Ciudad</th>
                                                        <th class="text-center">Direccion</th>
                                                        <th class="text-center">Telefono</th>
                                                        <th class="text-center">Correo</th>
                                                        <th class="text-center">Acciones</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="buscar">
                                                    <?php foreach ($proveedores as $prv) { ?> 
                                                        <tr>
                                                            <td class="text-center"><?php echo $prv['codigo']; ?></td>
                                                            <td class="text-center"><?php echo $prv['prv_ruc']; ?></td>
                                                            <td class="text-left"><?php echo $prv['nombre']; ?></td>
                                                            <td class="text-left"><?php echo $prv['ciudad']; ?></td>
                                                            <td class="text-left"><?php echo $prv['prv_direccion']; ?></td>
                                                            <td class="text-center"><?php echo $prv['prv_telefono']; ?></td>
                                                            <td class="text-left"><?php echo $prv['prv_correo']; ?></td>
                                                            <td class="text-center">
                                                                <?php if($prv['prv_estado']==="ACTIVO"){ ?>
                                                                <a onclick="editar(this)" data-datos2='<?= json_encode($prv); ?>'
                                                                   class="btn btn-sm btn-warning" rel='tooltip' data-title="Editar" 
                                                                   data-toggle="modal" data-target="#editar">
                                                                    <span class="glyphicon glyphicon-pencil"></span>
                                                                </a>                                                            
                                                                <?php } ?>
                                                                <a onclick="borrar(<?php echo "'" . $prv['codigo'] . "_" . $prv['nombre'] . "'"; ?>)" class="btn btn-sm btn-<?php echo (($prv['prv_estado']==="ACTIVO")?"danger":"success") ?>" 
                                                                   role="button" data-title="<?php echo (($prv['prv_estado']==="ACTIVO")?"Inactivar":"Activar") ?>" data-placement="top" rel="tooltip" data-toggle="modal" data-target="#delete"><i class="<?php echo (($prv['prv_estado']==="ACTIVO")?"fa fa-remove":"fa fa-check") ?>"></i></a>                                            
                                                            </td>
                                                        </tr>
                                                    <?php } ?>
                                                </tbody>
                                            </table>                         
                                        </div>
                                    <?php } else { ?>
                                        <div class="alert alert-info alert-dismissable">
                                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">
                                                &times;</button>
                                            <strong>No se encontraron registro....!</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 Proveedores</strong></h4>
                                </div>
                                <form action="prv_control.php" method="post" accept-charset="utf-8" class="form-horizontal">
                                    <div class="modal-body">  <!--inicio panel-->
                                        <input type="hidden" name="accion"  value="1">
                                        <input type="hidden" name="prv_cod1" id="" value="0"/> 
                                        <input type="hidden" name="pagina" value="prv_index.php">

                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label">RUC:</label>
                                            <div class="col-lg-4 col-md-4 col-sm-4">
                                                <input type="text" class="form-control" name="prv_ruc1" required="" 
                                                       id="ruc" placeholder="Ingrese el RUC" onkeypress="return numeros(event)">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label" >Razón Social:</label>
                                            <div class="col-lg-9 col-md-9 col-sm-9">
                                                <input type="text" class="form-control" name="prv_razonsoc1" 
                                                       required="" id="razon" placeholder="Ingrese la Razon Social"
                                                       pattern="[A-Za-z0-9áÁéÉíÍóÓúÚ\u00f1\u00d1 .-()]+">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label ">Ciudad:</label>
                                            <div class="col-lg-6 col-md-6 col-sm-6">
                                                <?php $tipos = consultas::get_datos("select * from ciudad order by id_ciudad"); ?>                                 
                                                <select name="id_ciudad1" class="select2 form-control" style="width:100%">
                                                    <?php if (!empty($tipos)) {
                                                        foreach ($tipos as $tipo) { ?>
                                                            <option value="<?php echo $tipo['id_ciudad']; ?>">
                                                                <?php echo $tipo['ciu_descri']; ?></option>
                                                            <?php
                                                        }
                                                    } else {
                                                        ?>
                                                        <option value="0">Debe insertar un registro</option>
                                                    <?php } ?>
                                                </select> 
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label">Dirección:</label>
                                            <div class="col-md-8 col-sm-8 col-lg-8">
                                                <textarea class="form-control" name="prv_direccion1" id="direccion" 
                                                          pattern="[A-Za-z0-9áÁéÉíÍóÓúÚ\u00f1\u00d1 .,/º]+"></textarea>
                                            </div>
                                        </div> 
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label" >Telefono:</label>
                                            <div class="col-lg-4 col-md-4 col-sm-4">
                                                <input type="tel" class="form-control" name="prv_telefono1" 
                                                       required="" id="telefono" placeholder="Ingrese el telefono">
                                            </div>
                                        </div>          
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label" >Correo:</label>
                                            <div class="col-lg-6 col-md-6 col-sm-6">
                                                <input type="email" class="form-control" name="prv_correo1" 
                                                       required="" id="prv_correo" placeholder="Ingrese el correo">
                                            </div>
                                        </div>                                         
                                    </div>  <!--fin panel-->
                                    <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>  
                    <!--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 Proveedores</strong></h4>
                                </div>                                
                                <form action="prv_control.php" method="post" accept-charset="utf-8" class="form-horizontal">
                                    <div class="modal-body">  <!--inicio panel-->
                                        <input type="hidden" name="accion"  value="2">
                                        <input type="hidden" name="prv_cod1" id="prv_cod_e"/> 
                                        <input type="hidden" name="pagina" value="prv_index.php">

                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label">RUC:</label>
                                            <div class="col-lg-4 col-md-4 col-sm-4">
                                                <input type="text" class="form-control" name="prv_ruc1" required="" 
                                                       id="prv_ruc_e" placeholder="Ingrese el RUC" onkeypress="return numeros(event)">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label" >Razón Social:</label>
                                            <div class="col-lg-9 col-md-9 col-sm-9">
                                                <input type="text" class="form-control" name="prv_razonsoc1" 
                                                       required="" id="prv_razon_e" placeholder="Ingrese la Razon Social"
                                                       pattern="[A-Za-z0-9áÁéÉíÍóÓúÚ\u00f1\u00d1 .-()]+">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label ">Ciudad:</label>
                                            <div class="col-lg-6 col-md-6 col-sm-6">
                                                <?php $tipos = consultas::get_datos("select * from ciudad order by id_ciudad"); ?>                                 
                                                <select name="id_ciudad1" id="ciudad" class="select2 form-control" style="width:100%">
                                                    <?php if (!empty($tipos)) {
                                                        foreach ($tipos as $tipo) { ?>
                                                            <option value="<?php echo $tipo['id_ciudad']; ?>">
                                                                <?php echo $tipo['ciu_descri']; ?></option>
                                                            <?php
                                                        }
                                                    } else {
                                                        ?>
                                                        <option value="0">Debe insertar un registro</option>
                                                    <?php } ?>
                                                </select> 
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label">Dirección:</label>
                                            <div class="col-md-8 col-sm-8 col-lg-8">
                                                <textarea class="form-control" name="prv_direccion1" id="prv_direc_e" 
                                                          pattern="[A-Za-z0-9áÁéÉíÍóÓúÚ\u00f1\u00d1 .,/º]+"></textarea>
                                            </div>
                                        </div> 
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label" >Telefono:</label>
                                            <div class="col-lg-4 col-md-4 col-sm-4">
                                                <input type="tel" class="form-control" name="prv_telefono1" 
                                                       required="" id="prv_tel_e" placeholder="Ingrese el telefono">
                                            </div>
                                        </div>          
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label" >Correo:</label>
                                            <div class="col-lg-6 col-md-6 col-sm-6">
                                                <input type="email" class="form-control" name="prv_correo1" 
                                                       required="" id="prv_correo_e" placeholder="Ingrese el correo">
                                            </div>
                                        </div>                                         
                                    </div>  <!--fin panel-->
                                    <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-warning pull-right">
                                            <i class="fa fa-floppy-o"></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-info" 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>
    $('.modal').on('shown.bs.modal', function() {
        $(this).find('input:text:visible:first').focus();
    });
</script>
        <!--script-->
        <script>
            function editar(datos) {
                var dat = $(datos).data("datos2");
                $('#prv_cod_e').val(dat.codigo);
                $('#prv_ruc_e').val(dat.prv_ruc);
                $('#prv_razon_e').val(dat.nombre);
                //console.log(dat.nombre);
                $('#ciudad').val(dat.id_ciudad).trigger('change');
                $('#prv_direc_e').val(dat.prv_direccion);
                $('#prv_tel_e').val(dat.prv_telefono);
                $('#prv_correo_e').val(dat.prv_correo);

            }

            function borrar(datos) {
                var dat = datos.split("_");
                console.log(dat[1]);
                $('#si').attr('href', 'prv_control.php?prv_cod1=' + dat[0] +
                        '&id_ciudad1=null' +
                        '&prv_ruc1=null' +
                        '&prv_razonsoc1=null' +
                        '&prv_direccion1=null' +
                        '&prv_telefono1=null' +
                        '&accion=4' +
                        '&pagina=prv_index.php');
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                    Desea cambiar el estado del registro <i><strong>' + dat[1] + '</strong></i>?');
            }
            /*PARA TIEMPO DE MENSAJE*/
            $("#mensaje").delay(1500).slideUp(150, function () {
                $(this).alert('close');
            });
            function numeros(e) {
                key = e.keyCode || e.which;
                tecla = String.fromCharCode(key).toLowerCase();
                letras = "0123456789-";
                especiales = [8, 37, 39, 46];

                tecla_especial = false
                for (var i in especiales) {
                    if (key == especiales[i]) {
                        tecla_especial = true;
                        break;
                    }
                }

                if (letras.indexOf(tecla) == -1)
                    return false;
            }            
        </script>
        <!--script-->
    </body>
</html>
