<?php
if( $_POST["inicia"] != "" && $_POST["termina"] != "" ){
    
     $data = array(
        'inicia'=> $_POST["inicia"], 
        'termina'=> $_POST["termina"]
    );
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://fora2020.com/admin/analitica_across_api.php");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $remote_server_output = curl_exec ($ch);
    // cerramos la sesión cURL
    curl_close ($ch);
                                  
    $estadisticasArray = json_decode($remote_server_output);
    
    function Total_Vista($arr, $ruta){
        $total = 0;
        foreach($arr as $item){
            if($ruta != ""){
                if($item[0] == $ruta){
                    $total += $item[1];
                }
            }
            else{
               $total += $item[1]; 
            }
            
            
        }
        return $total;
    }


    function Total_Stand($arr){
        $total_1 = 0;
        $total_2 = 0;
        $total_3 = 0;
        $total_4 = 0;
        $total_5 = 0;
        $total_6 = 0;
        $total_7 = 0;
        $total_8 = 0;
        $total_9 = 0;
        $total_10 = 0;
        $total_11 = 0;
        $total_12 = 0;
        $total_13 = 0;
        $total_14 = 0;

        foreach($arr as $item){
            if($item[0] == '/info/1'){
                $total_1 += $item[1];
            }
            if($item[0] == '/info/2'){
                $total_2 += $item[1];
            }
            if($item[0] == '/info/3'){
                $total_3 += $item[1];
            }
            if($item[0] == '/info/4'){
                $total_4 += $item[1];
            }
            if($item[0] == '/info/5'){
                $total_5 += $item[1];
            }
            if($item[0] == '/info/6'){
                $total_6 += $item[1];
            }
            if($item[0] == '/info/7'){
                $total_7 += $item[1];
            }
            if($item[0] == '/info/8'){
                $total_8 += $item[1];
            }
            if($item[0] == '/info/9'){
                $total_9 += $item[1];
            }
            if($item[0] == '/info/10'){
                $total_10 += $item[1];
            }
            if($item[0] == '/info/11'){
                $total_11 += $item[1];
            }
            if($item[0] == '/info/12'){
                $total_12 += $item[1];
            }
            if($item[0] == '/info/13'){
                $total_13 += $item[1];
            }
            if($item[0] == '/info/14'){
                $total_14 += $item[1];
            }
        }

        $arreglo = array( $total_1 , $total_2, $total_3, $total_4, $total_5, $total_6, $total_7, $total_8, $total_9, $total_10, $total_11, $total_12, $total_13, $total_14 );
        return $arreglo;
    }

    $total_dia = 0;
    $total_live = 0;
    $total_muestra = 0;

    $total_dia = Total_Vista($estadisticasArray, '' );
    $total_live = Total_Vista($estadisticasArray, '/live');
    $total_muestra = Total_Vista($estadisticasArray, '/muestra');
    $stands = Total_Stand($estadisticasArray);
}
?>


<div class="row" style="padding-top: 15px;">
 
    <!-- BUSCADOR -->
    <!-- FICHA -->
    <!-- FICHA -->
    <div class="col-md-12" >

        <div class="card" >
            <div class="card-header">Filtrar por rango de fechas</div>
            <div class="card-body">
                <form action="" method="post">
                    <input type="date" name="inicia" class="form-control" style=" width: 200px; display: inline-table;" value="<?php echo $_POST["inicia"] ?>">
                    <input type="date" name="termina" class="form-control" style=" width: 200px; display: inline-table;" value="<?php echo $_POST["termina"] ?>">
                    <input type="submit" value="Filtrar" class="btn btn-success">
                </form>
            </div>
        </div>
    </div>


         
    <!-- FICHA -->
    <!-- FICHA -->
    <!-- FICHA -->
    <div class="col-md-6" style="margin-top: 15px">
              <table width="100%" style="background-color: #ff5a23; color: #ffffff;">
              <tr>
                <td width="100" style="padding: 15px; background-color: rgba(0,0,0,0.2);">
                  <i class="fa fa-user btn-left" style=" font-size: 60px "></i>
                </td>
                <td align="center" style="font-size: 20px; line-height: 35px;">
                  <div style="line-height: 20px; margin-top: 15px;">
                    Live Streaming<br>
                    <span style="font-size: 12px">Personas que asistieron a los en vivo</span>
                  </div>
                  <b style="font-size: 50px;" id="en_vivo"></b>
                </td>
                  
               
                <td align="center" style="width: 100px">
                    <a href="<?php echo $url_admin; ?>/?pg=analitica/en_vivo&cant=<?php echo $total_live; ?>">
                    <button type="button" class="btn btn-light">Ver</button>
                    </a>
                    
                    <a href="<?php echo $url_admin; ?>/?pg=conectados_ahora">
                    <button type="button" class="btn btn-light" style="display: none">Ver</button>
                    </a>
                </td>
              </tr>
            </table>
    </div>
            
            
    <!-- FICHA -->
    <!-- FICHA -->
    <!-- FICHA -->
    <div class="col-md-6" style="margin-top: 15px">
              <table width="100%" style="background-color: #ff5a23; color: #ffffff;">
              <tr>
                <td width="100" style="padding: 15px; background-color: rgba(0,0,0,0.2);">
                  <i class="fa fa-user btn-left" style=" font-size: 60px "></i>
                </td>
                <td align="center" style="font-size: 20px; line-height: 35px;">
                  <div style="line-height: 20px; margin-top: 15px;">
                    Memorias<br>
                    <span style="font-size: 12px">Personas que vieron las memorias</span>
                  </div>
                  <b style="font-size: 50px;" ><?php echo $queryMemorias->num_rows ?></b>
                </td>
                  
               
                <td align="center" style="width: 100px">
                    <a href="<?php echo $url_admin; ?>/?pg=analitica/evidencias&cant=<?php echo $queryMemorias->num_rows; ?>&ini=<?php echo $_POST["inicia"]; ?>&ter=<?php echo $_POST["termina"]; ?>">
                    <button type="button" class="btn btn-light">Ver</button>
                    </a>
                </td>
              </tr>
            </table>
    </div>
            

    <div class="col-md-4" style="margin-top: 15px">
            <table width="100%" style="background-color: #28a745; color: #ffffff;">
              <tr>
                <td width="100" style="padding: 15px; background-color: rgba(0,0,0,0.2);">
                  <i class="fa fa-user btn-left" style=" font-size: 60px "></i>
                </td>
                <td align="center">
                    <div class="ti_ficha"> Total de interacciones en plataforma </div>
                    <div class="numero_ficha"><?php echo $total_dia; ?></div>
                </td>
              </tr>
            </table>
    </div>
            
            

    <div class="col-md-4" style="margin-top: 15px">
            <table width="100%" style="background-color: #03A9F4; color: #ffffff;">
              <tr>
                <td width="100" style="padding: 15px; background-color: rgba(0,0,0,0.2);">
                  <i class="fa fa-eye btn-left" style=" font-size: 60px "></i>
                </td>
                <td align="center">
                    <div class="ti_ficha"> Total de interacciones en transmisión en vivo </div>
                    <div class="numero_ficha"><?php echo $total_live; ?></div>
                </td>
              </tr>
            </table>
    </div>

    <div class="col-md-4" style="margin-top: 15px">
            <table width="100%" style="background-color: #673AB7; color: #ffffff;">
              <tr>
                <td width="100" style="padding: 15px; background-color: rgba(0,0,0,0.2);">
                  <i class="fa fa-shopping-bag btn-left" style=" font-size: 60px "></i>
                </td>
                <td align="center">
                    <div class="ti_ficha"> Total de interacciones en Muestra Comercial </div>
                    <div class="numero_ficha"><?php echo $total_muestra; ?></div>
                </td>
              </tr>
            </table>
    </div>

    <!-- FICHA -->
    <!-- FICHA -->
    <!-- FICHA -->
    <div class="col-md-12" style="margin-top: 15px">

            <div class="card" style="margin-top: 15px">
              <div class="card-header" align="center">
                <h3 align="center" style="margin-bottom: 0">Muestra Comercial</h3>
                <div>Visitas a los stands</div>
              </div>
            </div>
    </div>

    <?php
                $nod = 0;    
                $queryMuestra = mysqli_query($connect,"SELECT * FROM Stands WHERE id_evento = 1 ORDER BY id ASC ");
		        while($dataMuestra = mysqli_fetch_array($queryMuestra)){
                    
                    $nod = $dataMuestra["id"]-1;   
                    echo '
                    <div class="col-md-3" style="margin-top: 15px;">
                        <div class="card" align="center" style="line-height: 40px;">
                            <div class="card-body">
                                <div class="numero_ficha">'.$stands[$nod].'</div>
                                <div class="ti_ficha"> '.$dataMuestra["nombre"].' </div>

                                <a href="'.$url_admin.'/?pg=analitica/producto&id='.$dataMuestra["id"].'&ini='.$_POST["inicia"].'&ter='.$_POST["termina"].'&cant='.$stands[$nod].'">
                                    <button type="button" class="btn btn-outline-dark" style="margin-bottom: 15px;">Ver</button>
                                </a>
                            </div>
                          </div>
                      </div>
                    
                    ';
                    $nod++;
                }
    ?>
</div>

<input type="button" value="Guardar SnapShot" class="btn btn-secondary btn-sm" onclick="GuardarData()" style="width: 160px; margin-top: 25px;">


<style>
    .numero_ficha{
        font-size: 40px;
    }
</style>

<script src="https://www.gstatic.com/firebasejs/8.2.2/firebase.js"></script>
    <script>
      // Your web app's Firebase configuration
        var firebaseConfig = {
            apiKey: "AIzaSyBc4fFwumMcp1J9J2pejhpPQOfmPlFUp6o",
            authDomain: "ournorth-450c4.firebaseapp.com",
            databaseURL: "https://ournorth-450c4-default-rtdb.firebaseio.com",
            projectId: "ournorth-450c4",
            storageBucket: "ournorth-450c4.appspot.com",
            messagingSenderId: "281119170967",
            appId: "1:281119170967:web:17211ef88cdbaa080a64d2" 
        };
        // Initialize Firebase
        firebase.initializeApp(firebaseConfig);
        var database = firebase.database();
       
        
        //2. ESCUCHAMOS SI UN USUARIO SE CONECTA 
        var tb_live = database.ref('participants');
        tb_live.on('value', EnVivo);
        
        var snapShot = '';
        function EnVivo(data){
          traza = data.val() ;
          var keyCount  = Object.keys(traza).length;

            $("#en_vivo").html( keyCount );
            console.log( keyCount );
            snapShot = JSON.stringify( data.val() );
            //GuardarData( JSON.stringify( data.val() ) )
        }
        
        
        function GuardarData( ) {

          jQuery.ajax({
            url: "https://ournorth2021.com/admin/api/guardar_dato.php",
            type:'post',
            data: { dato: snapShot},
            }).done(function (resp){
              
            
            })
            .fail(function(resp) {
              console.log(resp);
            })
            .always(function(resp){
            }
          );


        }
        
        
    </script>

