<?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";
        //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 Clientes
                                <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
                                $clientes = consultas::get_datos("select * from v_clientes where id_cliente > 0 order by id_cliente asc ");
                                if (!empty($clientes)) {
                                    ?>                         
                                    <!-- /.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">C.I</th>
                                                        <th class="text-center">RUC</th>
                                                        <th class="text-center">Nombre y Apellido</th>
                                                        <th class="text-center">Direccion</th>
                                                        <th class="text-center">Telefono</th>
                                                        <th class="text-center">Correo</th>
                                                        <th class="text-center">Imagen</th>
                                                        <th class="text-center">Acciones</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="buscar">
                                                    <?php foreach ($clientes as $cliente) { ?> 
                                                        <tr>
                                                            <td class="text-center"><?php echo $cliente['id_cliente']; ?></td>
                                                            <td class="text-left"><?php echo number_format($cliente['per_ci'], 0, ',', '.'); ?></td>
                                                            <td class="text-left"><?php echo $cliente['per_ruc']; ?></td>
                                                            <td class="text-left"><?php echo $cliente['cliente']; ?></td>
                                                            <td class="text-left"><?php echo $cliente['per_direccion']; ?></td>
                                                            <td class="text-left"><?php echo $cliente['per_telefono']; ?></td>
                                                            <td class="text-left"><?php echo $cliente['cli_correo']; ?></td>
                                                            <td class="text-center">
                                                                <img height="45px" src="/farmmedi/img/personas/<?php echo (!empty($cliente['per_imagen'])?$cliente['per_imagen']:"no_disponible.jpg") ?>"/>
                                                            </td>
                                                            <td class="text-center">
                                                                <a onclick="editar(this)" data-datos='<?= json_encode($cliente); ?>'
                                                                   class="btn btn-sm btn-warning" rel='tooltip' data-title="Editar" 
                                                                   data-toggle="modal" data-target="#editar">
                                                                    <span class="glyphicon glyphicon-pencil"></span>
                                                                </a>
                                                                <a onclick="borrar(<?php echo "'" . $cliente['id_cliente'] . "_" . $cliente['cliente'] . "'"; ?>)" class="btn btn-sm btn-<?php echo (($cliente['cli_estado']==="ACTIVO")?"danger":"success") ?>" 
                                                                   role="button" data-title="<?php echo (($cliente['cli_estado']==="ACTIVO")?"Inactivar":"Activar") ?>" data-placement="top" rel="tooltip" data-toggle="modal" data-target="#delete"><i class="<?php echo (($cliente['cli_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">
                                            <strong>Aún no se registraron clientes...</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 Clientes</strong></h4>
                                </div>
                                <form action="clientes_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="idcliente" id="" value="0"/> 
                                        <input type="hidden" name="pagina" value="clientes_index.php" id="cod">

                                        <div class="form-group">                                            
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Persona</label>
                                                    <div class="col-lg-8 col-md-8 col-sm-8" >
                                                    <?php $personas = consultas::get_datos("SELECT id_persona, per_ci,per_nombre||' '||per_apellido as nombre from persona as per
                                                            where not exists (SELECT * from clientes as cli where per.id_persona=cli.id_persona);"); ?>                                 
                                                    <select name="idpersona" class="form-control select2" autofocus="" style="width: 100%;" required="">
                                                        <?php if (!empty($personas)) {
                                                            foreach ($personas as $persona) { ?>
                                                                <option value="<?php echo $persona['id_persona']; ?>">
                                                                    <?php echo $persona['nombre']; ?></option>
                                                                <?php
                                                            }
                                                        } else {
                                                            ?>
                                                            <option value="">Debe registrar una persona</option>
                                                        <?php } ?>
                                                    </select> 
                                            </div>
                                        </div>   
                                        <div class="form-group">                                               
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Correo Electronico</label>
                                            <div class="col-md-8 col-md-8 col-md-8">
                                                <input type="email" class="form-control" name="correo" 
                                                       required="" autofocus="" placeholder="Escriba el Correo">
                                            </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> Registrar</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>  
                    <!--fin registrar-->
                    <!--Boton de Registrar-->
                    <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 Clientes</strong></h4>
                                </div>
                                <form action="clientes_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="idcliente" id="idcliente" value="0"/> 
                                        <input type="hidden" name="pagina" value="clientes_index.php" id="cod">
                                        <div class="form-group">                                               
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Persona</label>
                                            <div class="col-md-8 col-md-8 col-md-8">
                                                <input type="text" class="form-control" id="persona" disabled="">
                                            </div>
                                        </div>   
                                        <div class="form-group">                                               
                                            <label class="col-lg-2 col-md-2 col-sm-2 control-label">Correo Electronico</label>
                                            <div class="col-md-8 col-md-8 col-md-8">
                                                <input type="email" class="form-control" name="correo" id="correo"
                                                       required="" autofocus="" placeholder="Escriba el Correo">
                                            </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-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("datos");
                $('#idcliente').val(dat.id_cliente);
                $('#persona').val(dat.cliente);
                $('#correo').val(dat.cli_correo);
            };            
            function borrar(datos) {
                var dat = datos.split("_");
                console.log(dat[1]);
                $('#si').attr('href', 'clientes_control.php?idcliente=' + dat[0] +'&idpersona=null&accion=3&pagina=clientes_index.php');
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                    Desea cambiar el estado al Cliente <i><strong>' + dat[1] + '</strong></i>?');
            }
 
            /*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>
