<?php
$controller = 'not_login_required';
$action = '';
include $_SERVER["DOCUMENT_ROOT"].'/app/include/bootstrap.php';
?>
<!DOCTYPE html>
<html style="height: 100%;width: 100%">
  <head>
    <meta charset="UTF-8">
    <title>PCG</title> 
     <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--<link href="public/css/login.css" rel="stylesheet">-->
    <link href="http://pcgus.lunis.mx/public/css/menu.css" rel="stylesheet">
    <link href="http://pcgus.lunis.mx/public/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <script src="http://pcgus.lunis.mx/public/js/jquery-1.10.2.js"></script>
    <script src="http://pcgus.lunis.mx/public/bootstrap/js/bootstrap.min.js"></script>
  </head>
  <body style="height: 100%;width: 100%">   
 <?php 
    /*VARIABLES GLOBAL*/
    $categories = "[";
    $page = 0;
    /*END VARIABLES GLOBALES*/
    function listarMenu($data){
            $i = 1;
            $productosEnTabla = 1;
            $productosPorTabla = 5;
            $uniqueId = uniqid();
            $GLOBALS['categories'] .= "'".$uniqueId."',";
            $GLOBALS['page']++?>
            <div class="page__style <?php echo "page_".$GLOBALS['page']?>" id="<?php echo $uniqueId;?>">
                <img src="public/img/logo_login.png" width="10%" class="logo_fix"/>
                    <div class="content_menu">
                    <div style="width: 49%;float: left;margin-right: 1%;margin-top: 1px;border-right: 10px solid pink;border-left: 10px solid pink;height: 100%">
                    <table>
                    <tbody><?php 
                        foreach($data as $productName => $products){?>
                            <tr>
                            <td>
                                <span class="product_name"><?php echo $productName ?></span>
                                <br/>
                                <ul ><?php
                                    foreach($products as $id => $size){ ?>                               
                                        <li class="price" style="display: inline-block">$ <?php echo number_format($size['punitarioventa'],2) ?><br/><small><?php echo $size['tamano_name'] ?></small></li><?php  
                                    }
                                    unset($data[$productName]);?>  
                                </ul>    
                            </td>
                            </tr><?php
                            $i++; 
                            $productosEnTabla++;
                            if($productosEnTabla > $productosPorTabla){
                               $productosEnTabla = 1?>
                                </tbody>
                                </table>
                                </div>  
                                <div style="width: 50%;float: left;">
                                <table>
                                <tbody><?php
                            }
                            
                            if($i > $productosPorTabla*2 && count($data) > 0){?>
                                </tbody>
                                </table>
                                </div>
                                <div style="clear:both"></div>
                                </div>
                                </div><?php
                                listarMenu($data);
                                break; // para salir de foreach($products as $id => $product){
                            }
                        }?>
                    </tbody>
                    </table>
                    </div>
                     <div style="clear:both"></div>
                    </div>
            </div>                           
<?php    }
    
    function getLastCategorie(){
        $getLastCategorie = $GLOBALS['categories'];
        $getLastCategorie = trim($getLastCategorie, '[');
        $getLastCategorie = trim($getLastCategorie, ']');
        $getLastCategorie = trim($getLastCategorie, ',');
        $getLastCategorie = explode(',', $getLastCategorie);
        
        if(count($getLastCategorie) == 1){return 'never_equal';}
        
        foreach($getLastCategorie as $index => $value){
            $lastCategorie = $value;
        }

        return $lastCategorie;
    }
    
    function getSizeNameForMenu($size){
        $size = strtolower($size);
        $changeSizeName = array(
            'individual'=>'Invidual',
            'red. 5p'=>'5 Servers',
            'red. 8-10'=>'8-10 Servers'
        );
        
        if(key_exists($size, $changeSizeName)){
            return $changeSizeName[$size];
        }
        
        return $size;
    }
    
    $repoProduct = new ProductoRepository();
    $products = $repoProduct->getProductForMenu();
    
    foreach($products as $product){
        $product['tamano_name'] = getSizeNameForMenu($product['tamano_name']);
        $menuList[$product['nombre_para_menu']][] = $product;
    }
    
    if(count($menuList) >0){listarMenu($menuList);}       
    
    $GLOBALS['categories'] .= "]";
    
    $lastCategorie = getLastCategorie();
    
?>          
<script>
function showMenu(){

    var menu = <?php echo $GLOBALS['categories'] ?>;
    menu.forEach(function(menuCategory,index){
        setTimeout(function(){

            $('.page__style').addClass('animate_content');
            $('.page__description').fadeOut(100).delay(2800).fadeIn();

            setTimeout(function() {
              $('.page__style').removeClass('animate_content');
            }, 3200);

            //remove fadeIn class after 1500ms
            setTimeout(function() {
              $('.page__style').removeClass('fadeIn');
            }, 1500);

            setTimeout(function() {
                $('#'+menuCategory).addClass('fadeIn');
            }, 1500);
            
            if(menuCategory === <?php echo $lastCategorie?>){
                setTimeout(function() {
                    document.location.reload();
                },index * 30000);  
            }
            
        },index * 30000);
    });
}

    showMenu();

</script>

</body>
</html>
