<?php if(isset($_printTicketApertura)){?>
        <script>
            $("#iframe").attr("src", "https://demo.pos.parisbakery.app/View/Tickets/printTicketApertura.php?idAperturaCaja=<?php echo $_idAperturaCaja ?>");
        </script>
<?php }

    if(isset($_printTicketCierre)){?>
        <script>
            $("#iframe").attr("src", "https://demo.pos.parisbakery.app/Tickets/printTicketCierre.php?idCierreCaja=<?php echo $_idCierreCaja ?>");
        </script>
<?php
    }?>        
<div class="row">
    <div class="col-lg-12">
        <ol class="breadcrumb">
            <?php      
            $currentAction = '';
            $agregarNuevo = '';
            if($action =='edit' || $action =='cerrar'){  
                if($action == 'edit'){$abrirCerrarCajaAction = 'Editar Abrir/Cerrar Caja';}
                elseif($action == 'cerrar'){$abrirCerrarCajaAction = 'Cerrar Caja';}
                $currentAction = "<li class='active'><i class='fa fa-pencil'></i> ".$_translator->_getTranslation('Editar')."</li>";
                $agregarNuevo = "<a href='Register.php' class='btn btn-sm btn-default'><i class='fa fa-plus'></i> ".$_translator->_getTranslation('Abrir caja')."</a>";
                $agregarNuevo .= '<hr>';                
            }elseif($action =='' || $action =='abrir'){
                $abrirCerrarCajaAction = 'Abrir Caja';
                $currentAction = "<li class='active'><i class='fa fa-plus'></i> ".$_translator->_getTranslation('Abrir')."</li>";
            }?>
            <li><i class="fa fa-dashboard"></i>  <a href="<?php echo ROOT_HOST?>/Controller/Home.php"><?php echo $_translator->_getTranslation('Home')?></a></li>
            <li class="active"><i class="fa fa-unlock-alt"></i> <?php echo $_translator->_getTranslation('Cajas')?></li>
            <?php echo $currentAction?>
        </ol>
    </div>
</div>
<div class="row">
    <div class="col-lg-12">
        <div id="flashmessenger"><?php $flashmessenger->showMessage();?></div>
        <div class="box">
            <?php include ROOT."/View/Modal/denominaciones.php";?>
            <?php include ROOT."/View/Modal/arqueoCaja.php";?>
            <?php include ROOT."/View/Modal/resumenDeVenta.php";?>
            <div class="box-header"> 
                <h3><i class='fa-fw fa fa-unlock-alt'></i><?php echo $_translator->_getTranslation($abrirCerrarCajaAction);?></h3>
                <div class='box-icon'><i class='icon fa fa-tasks'></i></div>
            </div>
            <div class='box-content'>
                <div class="row">
                    <div class="col-md-12 col-xs-12 text-right">
                        <?php echo $agregarNuevo;?>                        
                        <div class="clear"></div>
                    </div> 
                    <?php 
                    echo $_form->openForm();
                    echo $_form->showActionController();
                    echo $_form->showId();
                    ?>
                    <div class="col-md-11">
                        <?php echo $_form->showElement('idCaja');?>
                        <?php echo $_form->showElement('efectivoInicial');?>
                        <?php echo $_form->showElement('efectivoFinal');?>
                        <?php echo $_form->showElement('tarjetasFinal');?>
                        <?php echo $_form->showElement('electronicoFinal');?>
                        <?php echo $_form->showElement('doorDashFinal');?>
                        <?php echo $_form->showElement('chequesFinal');?>
                        <?php echo $_form->showElement('waste_lbs');?>
                        <?php echo $_form->showElement('waste_pza');?>
                        <div class="form-group">
                            <div class="col-md-3"></div>
                            <div class="col-md-8 p-r-0 text-right">
                                <?php $element = $_form->getElement('send'); echo $_form->createOnlyElement($element);?>
                                <?php $element = $_form->getElement('cancelar'); echo $_form->createOnlyElement($element);?>                               
                            </div>
                        </div>
                    </div>      
                    <?php echo $_form->closeForm();?>
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <h3><?php echo $_translator->_getTranslation('Lista cajas')?></h3>
                        <hr/>
                        <div class='table-responsive'>
                        <table id="tblCajas" class="table table-striped table-hover table-condensed">
                            <thead>
                            <th class="text-center col-md-1"><?php echo $_translator->_getTranslation('Accion');?></th>
                            <th class="text-center"><?php echo $_translator->_getTranslation('Nombre');?></th>
                            <th class="text-center"><?php echo $_translator->_getTranslation('Usuario');?></th>
                            <th class="text-center"><?php echo $_translator->_getTranslation('Fecha apertura');?></th>
                            <th class="text-center"><?php echo $_translator->_getTranslation('Fecha cierre');?></th>
                            <th class="text-right"><?php echo $_translator->_getTranslation('Apertura');?></th>
                            <th class="text-right"><?php echo $_translator->_getTranslation('Cierre');?></th>
                            <th class="text-center"><?php echo $_translator->_getTranslation('Status');?></th>
                            <th class="text-center"><?php echo $_translator->_getTranslation('Cerrar');?></th>
                            </thead>
                            <tbody>
                            <?php 
                                if($_listaCajas){
                                    $login = new Login();
                                    foreach($_listaCajas as $caja){
                                        $cerrar = "";
                                        if($caja['status']=='1'){                                            
                                            $cerrar = "<button class='btn btn-sm btn-success resumen' data-idregister='{$caja['id']}'><i class='fa fa-line-chart'></i> <span class='hidden-md hidden-sm hidden-xs hidden-phone'>".$_translator->_getTranslation('Resumen')."</span></button>";
                                            $cerrar .= " <a href='Register.php?action=cerrar&id={$caja['id']}' class='btn btn-sm btn-warning'><i class='fa fa-lock'></i><span class='hidden-md hidden-sm hidden-xs hidden-phone'> ".$_translator->_getTranslation('Cerrar')."</span></a>";
                                        }
                                        
                                        if($login->getRole() == 1){
                                           $onclick = "document.location = 'Register.php?action=edit&id={$caja['id']}'";    
                                        }else{
                                           $onclick = " confirmarAccion('editar_cierre',function(){ document.location = 'Register.php?action=edit&id={$caja['id']}';}) ";
                                        }      
                                        
                                        ?>
                                        <tr>
                                            <td class="text-center">
                                                <span class="btn btn-sm btn-default" onclick="<?php echo $onclick; ?>" ><i style="color:#000" class="fa fa-pencil-square-o"></i></span>                                                                                                                                        
                                            </td>
                                            <td class="text-center"><?php echo $caja['caja']?></td>
                                            <td class="text-center"><?php echo $caja['nombre'].' '.$caja['apellido'];?></td>
                                            <td class="text-center"><?php echo $caja['fechaAperturaFormateada']?></td>
                                            <td class="text-center"><?php echo $caja['fechaCierreFormateada']?></td>
                                            <td class="text-right"><?php echo number_format($caja['efectivoInicial'],2)?></td>                                    
                                            <td class="text-right"><?php if($caja['saldoFinal']!=='-'){echo number_format($caja['saldoFinal'],2);}else{echo $caja['saldoFinal'];} ?></td>
                                            <?php   $ventas = "-"; 
                                                    $arqueo = "";
                                                    if($caja['status']=='2'){
                                                        $ventas = $caja['saldoFinal'] - $caja['efectivoInicial'];
                                                        $arqueo = "<button onclick='setIdCheckRegister({$caja['id']})' class='btn btn-sm btn-success' data-toggle='modal' data-target='#modalArqueoCaja'><i class='fa fa-balance-scale'></i><span class='hidden-md hidden-sm hidden-xs hidden-phone'>".$_translator->_getTranslation('Arqueo')."</span></button>";
                                                    }?>
                                            <td class="text-center"><?php echo $caja['statusName']?></td>
                                            <td class="text-right">
                                                <div class='dropup' style="float:right;margin-left: 5px;">
                                                    <button class='btn btn-sm btn-primary' id="moreOptions" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><i class="fa fa-print"></i></button>
                                                    <ul class="dropdown-menu dropdown-menu-right" aria-labelledby="moreOptions">
                                                        <li><a href="javascript:printOpening(<?php echo $caja['id'] ?>)"><i class="fa fa-unlock"></i><?php echo $_translator->_getTranslation('Apertura')?></a></li>
                                                        <li><a href="javascript:printClosing(<?php echo $caja['id'] ?>)"><i class="fa fa-lock"></i> <?php echo $_translator->_getTranslation('Cierre')?></a></li>
                                                    </ul>
                                                </div>
                                                <?php echo $cerrar.$arqueo;?> 
                                            </td>
                                        </tr><?php
                                    }                   
                                }?>
                            </tbody>
                        </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" language="javascript">
        $('#idCaja').select2();
        $('.resumen').on('click',function(){
            var idregister = $(this).data('idregister');
            sessionStorage.idR = $(this).data('idregister');
           
            confirmarAccion('resumen_ventas',function(){getVentasByIdRegister(idregister);});
        });
        
	$('#tblCajas').DataTable({
        paginate:true,
        filter:true,
        pageLength: 100
        });

        $('#tblCajas')
                .removeClass( 'display' )
                .addClass('table table-striped table-bordered');    
</script>