<?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 Empleados
                                <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
                                $empleados = consultas::get_datos("select * from v_empleados order by id_empleado asc ");
                                if (!empty($empleados)) {
                                    ?>                         
                                    <!-- /.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-left">Nombre y Apellido</th>
                                                        <th class="text-left">Cargo</th>
                                                        <th class="text-left">F. Ingreso</th>
                                                        <th class="text-left">Correo</th>
                                                        <th class="text-center">Salario</th>
                                                        <th class="text-center">Imagen</th>
                                                        <th class="text-center">Acciones</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="buscar">
                                                    <?php foreach ($empleados as $empleado) { ?> 
                                                        <tr>
                                                            <td class="text-center"><?php echo $empleado['id_empleado']; ?></td>
                                                            <td class="text-center"><?php echo number_format($empleado['per_ci'], 0, ',', '.'); ?></td>
                                                            <td class="text-left"><?php echo $empleado['empleado']; ?></td>
                                                             <td class="text-left"><?php echo $empleado['car_descri']; ?></td>
                                                            <td class="text-center"><?php echo date("d/m/Y", strtotime($empleado['emp_fingreso'])); ?></td>
                                                            <td class="text-center"><?php echo $empleado['emp_correo']; ?></td>
                                                            <td class="text-center"><?php echo $empleado['emp_salario']; ?></td>
                                                            <td class="text-center">
                                                                <img height="45px" src="/farmmedi/img/personas/<?php echo (!empty($empleado['per_imagen'])?$empleado['per_imagen']:"no_disponible.jpg") ?>"/>
                                                            </td>                                                            
                                                            <td class="text-center">                                                     
                                                                <a onclick="editar(this)" data-datos='<?= json_encode($empleado); ?>'
                                                                   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 "'" . $empleado['id_empleado'] . "_" . $empleado['empleado'] . "_" . $empleado['id_persona'] . "_" . $empleado['id_cargo'] . "'"; ?>)" class="btn btn-sm btn-<?php echo (($empleado['emp_estado']==="ACTIVO")?"danger":"success") ?>" 
                                                                   role="button" data-title="<?php echo (($empleado['emp_estado']==="ACTIVO")?"Inactivar":"Activar") ?>" data-placement="top" rel="tooltip" data-toggle="modal" data-target="#delete"><i class="<?php echo (($empleado['emp_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 Empleado</strong></h4>
                                </div>
                                <form action="empleado_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="idempleado" value="0"/> 
                                        <input type="hidden" name="pagina" value="empleado_index.php">

                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label">Persona</label>
                                            <div class="col-lg-8 col-md-8 col-md-8" >
                                                <?php $personas = consultas::get_datos("SELECT id_persona, per_ci,per_nombre||' '||per_apellido as nombre from persona as per
                                                        where id_persona > 0 and not exists (SELECT * from empleado as emp where per.id_persona=emp.id_persona);"); ?>                                 
                                                <select name="idpersona" class="form-control select2"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>
                                        <?php $fecha = consultas::get_datos("select current_date as fecha");?>
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label">Fecha Ingreso:</label>
                                            <div class="col-lg-4 col-md-4 col-sm-4">
                                                <input type="date" class="form-control" name="fingreso" value="<?= $fecha[0]['fecha']?>"
                                                       required="">
                                            </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="correo" required="">
                                            </div>
                                        </div>                                         
                                        <div class="form-group">                                                 
                                                <label class="col-lg-3 col-md-3 col-sm-3 control-label">Cargo</label>
                                                <div class="col-lg-6 col-md-6 col-md-6" >
                                                <?php $cargos = consultas::get_datos("select * from cargo"); ?>                                 
                                                <select name="idcargo" class="select2" autofocus="" style="width: 100%;">
                                                    <?php
                                                    if (!empty($cargos)) {
                                                        foreach ($cargos as $cargo) {
                                                            ?>
                                                            <option value="<?php echo $cargo['id_cargo']; ?>">
                                                                <?php echo $cargo['car_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">Salario:</label>
                                            <div class="col-lg-6 col-md-6 col-sm-6">
                                                <input type="number" class="form-control" name="sueldo" min="0"
                                                       required="">
                                            </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 Empleado</strong></h4>
                                </div>
                                <form action="empleado_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="idempleado" id="idempleado" value="0"/> 
                                        <input type="hidden" name="pagina" value="empleado_index.php">

                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label">Persona:</label>
                                            <div class="col-lg-8 col-md-8 col-sm-8">
                                                <input type="text" class="form-control" id="persona" readonly="">
                                            </div>
                                        </div>                                         
                                        <?php $fecha = consultas::get_datos("select current_date as fecha");?>
                                        <div class="form-group">
                                            <label class="col-lg-3 col-md-3 col-sm-3 control-label">Fecha Ingreso:</label>
                                            <div class="col-lg-4 col-md-4 col-sm-4">
                                                <input type="date" class="form-control" name="fingreso" value="<?= $fecha[0]['fecha']?>"
                                                       required="" id="fingreso">
                                            </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="correo" required="" id="correo">
                                            </div>
                                        </div>                                         
                                        <div class="form-group">                                                 
                                                <label class="col-lg-3 col-md-3 col-sm-3 control-label">Cargo</label>
                                                <div class="col-lg-6 col-md-6 col-md-6" >
                                                <?php $cargos = consultas::get_datos("select * from cargo"); ?>                                 
                                                    <select name="idcargo" class="select2 form-control" id="cargo" style="width: 100%;">
                                                    <?php
                                                    if (!empty($cargos)) {
                                                        foreach ($cargos as $cargo) {
                                                            ?>
                                                            <option value="<?php echo $cargo['id_cargo']; ?>">
                                                                <?php echo $cargo['car_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">Salario:</label>
                                            <div class="col-lg-6 col-md-6 col-sm-6">
                                                <input type="number" class="form-control" name="sueldo" min="0"
                                                       required="" id="sueldo">
                                            </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");
                $('#idempleado').val(dat.id_empleado);
                $('#persona').val(dat.empleado);
                $('#fingreso').val(dat.emp_fingreso);
                $('#cargo').val(dat.id_cargo).trigger('change');
                $('#correo').val(dat.emp_correo);
                $('#sueldo').val(dat.emp_salario);
            };                
            function borrar(datos) {
                var dat = datos.split("_");
                console.log(dat[1]);
                $('#si').attr('href', 'empleado_control.php?idempleado=' + dat[0] +
                        '&idpersona=' + dat[2] +
                        '&idcargo='+ dat[3] +
                        '&accion=3' +
                        '&pagina=empleado_index.php');
                $('#confirmacion').html('<span class="glyphicon glyphicon-warning-sign"></span>\n\
                    Desea desactivar el Registro <i><strong>' + dat[1] + '</strong></i>?');
            }
            /*PARA TIEMPO DE MENSAJE*/
            $("#mensaje").delay(1500).slideUp(100, function () {
                $(this).alert('close');
            })
        </script>
        <!--script-->
    </body>
</html>
