<?php
session_start();
if( $_SESSION["user"] == "" || $_SESSION["passwd"] == "" ) {
   header("Location: traficoIndex.php");
}


if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height'])){
   $screen_width =  $_SESSION['screen_width'];
   $screen_height =  $_SESSION['screen_height']; 
} else if(isset($_REQUEST['width']) AND isset($_REQUEST['height'])) {
    $_SESSION['screen_width'] = $_REQUEST['width'];
    $_SESSION['screen_height'] = $_REQUEST['height'];
    header('Location: ' . $_SERVER['PHP_SELF']);
} else {
    echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?width="+screen.width+"&height="+screen.height;</script>';
}

$width = "98%";
$IP = $_SERVER['REMOTE_ADDR'];
//$informes = $_SESSION["informes"];
$trama = isset($_POST['t']) ? $_POST["t"]  : "";
if( $trama == "" ) {
   if( ( $trama = $_SESSION['t'] ) == "" ) $trama = "0";
}
$hide = 0;
$_SESSION['t'] = $trama;
if( ($hide = $_SESSION["hide"]) == "" ) {
   $_SESSION["hide"] = 1;
   $hide = 1;
}
$hide = 0;

include_once('db_ouichat.php');

ini_set('max_execution_time', 120);

$page = $_SERVER['PHP_SELF'];
$sec = "10";
header("Refresh: $sec; url=$page");

include 'traficoHeader.php'; 
?>
<style type="text/css">
  .btn, .btn-large, .btn-small {
     text-decoration: none;
     color: #fff;
     background-color: #d50000;
     background-color: #b71c1c;
     height: 2.5em;
     border-radius: 1.50em;
     text-align: center;
     letter-spacing: .5px;
     -webkit-transition: background-color .2s ease-out;
     transition: background-color .2s ease-out;
     cursor: pointer;
     line-height:2.5em;
   }
   
   .btn:hover, .btn-large:hover, .btn-small:hover {
     background-color: #dd2c00;
      }
  p {
      display: block;
      -webkit-margin-before: 0em;
      -webkit-margin-after: 0.3em;
      -webkit-margin-start: 0px;
      -webkit-margin-end: 0px;
  }

   #toast-container {
       min-width: 10%;
       top: 50%;
       right: 50%;
       transform: translateX(50%) translateY(50%);
    }   

    a {
        text-decoration: none;
    }

   .btn, .btn-large, .btn-small {
     text-decoration: none;
     color: #fff;
     background-color: #b71c1c;
     height: 2.5em;
     border-radius: 1.25em;
     text-align: center;
     letter-spacing: .5px;
     -webkit-transition: background-color .2s ease-out;
     transition: background-color .2s ease-out;
     cursor: pointer;
     line-height:2.2em;
   }

   .btn:hover, .btn-large:hover, .btn-small:hover {
     background-color: #dd2c00 ;
   }

   .butn {
      color:black;
   }

   .butn:hover {
     color: red;
   }


.tabs {
  height: 40px !important;
}

.tabs.tabs-transparent {
  background-color: transparent;
}

.tabs.tabs-transparent .tab a,
.tabs.tabs-transparent .tab.disabled a,
.tabs.tabs-transparent .tab.disabled a:hover {
  color: rgba(255, 255, 255, 0.7);
}

.tabs.tabs-transparent .tab a:hover,
.tabs.tabs-transparent .tab a.active {
  color: #fff;
}

.tabs.tabs-transparent .indicator {
  background-color: #fff;
}

.tabs.tabs-fixed-width {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
}

.tabs.tabs-fixed-width .tab {
  -webkit-box-flex: 1;
  -webkit-flex-grow: 1;
      -ms-flex-positive: 1;
          flex-grow: 1;
}

.tabs .tab {
  line-height: 40px !important;
  height: 40px !important;
/*  text-transform: uppercase; */
  text-transform: none !important;
}

.tabs .tab a {
  color: #16a2d6 !important;
  -webkit-transition: color .28s ease, background-color .28s ease;
  transition: color .28s ease, background-color .28s ease;
}

.tabs .tab a:focus, .tabs .tab a:focus.active {
  background-color: rgba(246, 178, 181, 0.2);
  background-color: #16a2d6 !important;
  background-color: rgba(22, 162, 214, 0.9) !important; 
  color: white !important;
  outline: none;
}

.tabs .tab a:hover, .tabs .tab a.active {
  color: #16a2d6 !important;
}

.tabs .tab.disabled a,
.tabs .tab.disabled a:hover {
  color: #16a2d6 !important;
}

.tabs .indicator {
  position: absolute;
  bottom: 0;
  height: 2px;
  background-color: #16a2d6;
  will-change: left, right;
}

.active {
   border: none !important;

}

.card {
-webkit-box-shadow: 0px 12px 15px -5px rgba(0,0,0,0.59); 
box-shadow: 0px 12px 15px -5px rgba(0,0,0,0.59);

}

th, td {
   padding: 0px;
}

</style>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script> 
<script type="text/javascript" src="materialize/1.0.0/js/materialize.js"></script>
<script type="text/javascript">

   var width;
   var trama;
   var hide = 1;

   $(document).ready(function() {

      trama = <?php echo json_encode($trama); ?>;
      hide = <?php echo json_encode($hide); ?>;

      console.log("Ready!");

      width = $(window).width();
      console.log( width );

      $('.sidenav').sidenav();
      $('.dropdown-trigger').dropdown({
         "hover": true,
         "constrainWidth": false,
         "coverTrigger": false
      });

      $('select').formSelect();
      $('.collapsible').collapsible();
      $('.modal-trigger').modal({
          dismissible: false,
          preventScrolling: true,
          opacity: .7, // Opacity of modal background
          in_duration: 300, // Transition in duration
          out_duration: 200, // Transition out duration
          starting_top: '40%'
      });      
      

      $('.tabs').tabs();
      $('#tabs_menu').tabs();
      $('ul.tabs').tabs();

      $('.tooltiped').tooltip({exitDelay:0,enterDelay:0,inDuration:10,outDuration:10});
      console.log( trama );
   });   

   function onClick(e) {

      switch( e.id ) {
         case "0":
            post("traficoPanel.php", {t:0});
            break;

         case "1":
            post("traficoPanel.php", {t:1});
            break;

         case "2":
            post("traficoPanel.php", {t:2});
//            post("traficoGateways.php", {t:2});
            break;

         case "3":
            post("traficoPanel.php", {t:3});
//            post("traficoGateways.php", {t:2});
            break;

         case "4":
            post("traficoLlamados.php", {t:4});
            break;

         case "5":
            post("traficoConference.php", {t:5});
            break;

         case "6":
            post("traficoOpciones.php", {t:6});
            break;


         case "7":
            post("traficoAni.php", {t:7});
            break;
      }
   }


   function post(path, parameters) {
      var form = $('<form></form>');

      form.attr("method", "post");
      form.attr("action", path);

      $.each(parameters, function(key, value) {
          var field = $('<input></input>');
          field.attr("type", "hidden");
          field.attr("name", key);
          field.attr("value", value);
          form.append(field);
      });
      $(document.body).append(form);
      form.submit();
   };

   function destacar() {

      hide ^= 1;
      console.log( "DESTACAR " + hide );

      if( hide == 1 )
         post("traficoPanel.php", {t:trama,hide:"1"});
      else post("traficoPanel.php", {t:trama,hide:"0"});

   }
</script>

<?php  
      $mensaje = array();
      $dnis = array();
      $cid = array();
      $app = array();
      $groups = array();
      for ($i = 0; $i < 720; $i++) {
          $mensaje[$i] = "";
          $dnis[$i] = "";
          $cid[$i] = "";
          $app[$i] = "";
          $groups[$i] = "";
      }
      $Total_Lines = 0;
      $Total_0045 = 0;
      $Total_0074 = 0;
      $Total_12 = 0;
      $Total_13 = 0;
      $total_mod_singles = 0;
      $total_mod_directo = 0;
      $total_mod_otros = 0;
      $total_discador = 0;
      $total_conference = 0;
      $total_conf1 = 0;
      $total_conf2 = 0;
      $total_diego = 0;
      $equipo1 = 0;
      $equipo2 = 0;
      $equipo3 = 0;
      $din4 = 0;
      $conf1 = 0;
      $conf2 = 0;
      $conf3 = 0;
      $synway1 = 0;
      $synway1a = 0;
      $synway2 = 0;
      $synway3 = 0;
      $synway4 = 0;
      $synway5 = 0;
      $synway6 = 0;
      $synway7 = 0;
      $david = 0;
      $diego16 = 0;
      $diego32 = 0;
      $syn1_conf = 0;
      $syn2_conf = 0;
      $syn3_conf = 0;
      $syn4_conf = 0;
      $syn5_conf = 0;
      $syn6_conf = 0;
      $syn7_conf = 0;
      $din4_conf = 0;

      $tc = 0;
      $chat = 0;
      $stra = 0;
      $movi = 0;
      $movi2 = 0;
      $movi3 = 0;
      $movi4 = 0;

      $no_digits = 0;
      $no_digits_1 = 0;
    
      $q = "select sql_no_cache * from log_pantalla left join aplicaciones on aplicaciones.dnis=log_pantalla.dnis where Active = 1";
      $result = mysqli_query( $conn, $q );
      if( mysqli_num_rows( $result ) > 0 ) {

         while($row = mysqli_fetch_assoc( $result )) {
            $line = $row["Line"];
            $dde = $row["Dnis"];
            $msg = $row["Mensaje"];

            $module = $row["app"];
            $conference = $row["conference"];

            $dnis_full[$line] = $dde;
            $dde = substr( $dde, 6, 4 );
            $dnis[$line] = $dde;

            $grupo = $row["grupo"];

            if( substr($msg,0,9) == "NO DIGITS" ) {
                 ++$no_digits;
            }

            if( substr($msg,0,13) == "APP NO DIGITS" ) {
                 ++$no_digits_1;
            }


            if( $grupo == "TC" ) ++$tc; 
            if( $grupo == "CHAT" || $grupo == "CHAT24F" ) ++$chat; 
            if( $grupo == "STRA" ) ++$stra; 
            if( $grupo == "MOVI"  || $grupo =="1" ) ++$movi; 
            if( $grupo == "MOVI2" || $grupo =="2" ) ++$movi2; 
            if( $grupo == "MOVI3" || $grupo =="3" ) ++$movi3; 
            if( $grupo == "4" ) ++$movi4; 

            if( $dnis_full[$line] == "1151723963" || $dnis_full[$line] == "1151723964" || $dnis_full[$line] == "1150784909" || $dnis_full[$line] == "1150783756" ) ++$equipo1; 
            if( $dnis_full[$line] == "1150780059" || $dnis_full[$line] == "1151717149" ) ++$equipo1; 
            if( $dnis[$line] == "0582" || $dnis[$line] == "1157" || $dnis[$line] == "0023" ) ++$equipo1;


            if( $dnis_full[$line] == "1151728162" || $dnis_full[$line] == "1151728163" || $dnis_full[$line] == "1151720092" || $dnis_full[$line] == "1151720094" || $dnis_full[$line] == "1151720056" || $dnis_full[$line] == "1151720075" ) ++$synway4;

            ++$Total_Lines;
            switch( $dde ) {
               case "0045": ++$Total_0045; break;
               case "0074": ++$Total_0074; break; 
               case "0012": ++$Total_12; break;
               case "0013": ++$Total_13; break;
            }

            if( $module == "mod_singles" ) $total_mod_singles++;
            else if( $module == "mod_directo" ) $total_mod_directo++;
                 else if( $module == "dialer" ) $total_discador++;
                      else if( $module == "conference" ) $total_conference++;
                           else $total_mod_otros++;
         }
      }
      if( is_resource($result) ) mysql_free_result( $result );


      $q = "select sql_no_cache * from log_pantalla left join aplicaciones on aplicaciones.dnis=log_pantalla.dnis where Active = 1 " . $filter;
      $result = mysqli_query( $conn, $q );
      if( mysqli_num_rows( $result ) > 0 ) {
         while($row = mysqli_fetch_assoc( $result )) {

            $line = $row["Line"];
            $msg = $row["Mensaje"];
            $dde = $row["Dnis"];
            $ani = $row["Cid"];
            $module = $row["app"];
            $grupo = $row["grupo"];
            $conference = $row["conference"];
            if( $ani == '' ) $ani = "Privado";


            $dde = substr( $dde, 6, 4 );
            $dnis[$line] = $dde;
            $app[$line] = $module;
            $groups[$line] = $grupo;

            if( substr( $msg, 0, 7 ) == "Privado" ) $msg = substr( $msg, 8, strlen( $msg ) - 8 );  
            if( $conference == "1" ) $mensaje[$line] = "<span style='color:MEDIUMORCHID;'>$dde: $msg</span>";  
            else {
               if( $module == "mod_validar" ) $mensaje[$line] = $msg; 
               else $mensaje[$line] = $dde . ": " . $msg;
            }
            $cid[$line] = $ani;
         }
      }
      if( is_resource($result) ) mysql_free_result( $result );

            echo "<div class='card white' style='margin:0px;'>"; 
               echo "<div class='card-content black-text' style='border-top:none; !important'>"; 
                  echo "<table>";

                     for( $i = 1; $i < 31; ++$i ) {
                        echo "<tr style='line-height:2em; font-size:8pt; border:none; padding-left:0;'>";
                           for( $x = 0; $x < $c; ++$x ) {
                              $n = $i + ( $x * 30 ) + $index;
                              echo "<td style='width:20px; text-align: right; font-size:8pt;'>$n.</td>";
                              echo "<td style='width:5px;'>&nbsp;</td>";
                              $color = "blue";
                              if( $dnis[$n] == "0074" ) $color = "SALMON";
                              else if( $dnis[$n] == "00018" ) $color = "green";

                              if( $dnis[$n] == "1157" || $dnis[$n] == "1323" || $dnis[$n] == "2615" || $dnis[$n] == "2846" ) $color = "green";
                              else if( $dnis[$n] == "7182" || $dnis[$n] == "0121" || $dnis[$n] == "1204" || $dnis[$n] == "2054" ) $color = "#64b5f6";
                                   else if( $dnis[$n] == "0046" || $dnis[$n] == "0051" || $dnis[$n] == "0058" || $dnis[$n] == "0062" || $dnis[$n] == "0076" ) $color = "#ef6c00";

                              if( $dnis[$n] == "1157" || $dnis[$n] == "0023" || $dnis[$n] == "0582" || $dnis[$n] == "0077" ) $color = "green";


                              $background = ""; 
                              if( $hide == 0 ) {


                              /*
                                 if( $dnis[$n] == "2054" || $dnis[$n] == "0857" ) {
                                    $background = "background-color: gray !important;";
                                    $color = "white";
                                 }

                                 if( $dnis[$n] == "1743" || $dnis[$n] == "3682") {
                                    $background = "background-color: navy !important;";
                                    $color = "white";
                                 }

                                 if( $dnis[$n] == "0426" || $dnis[$n] == "0626" ) {
                                    $background = "background-color: yellow !important;";
                                    $color = "black";
                                 }


                                 if( $dnis[$n] == "1323" || $dnis[$n] == "2615" ) {
                                    $background = "background-color: green !important;";
                                    $color = "white";
                                 }

                                 if( $dnis[$n] == "1317" || $dnis[$n] == "1425" ) {
                                    $background = "background-color: green !important;";
                                    $color = "white";
                                 }
                              */


                                 if( $groups[$n] == "CHAT" || $groups[$n] == "CHAT24F") {
                                    $background = "background-color: blue !important;";
                                    $color = "white";
                                 }

                                 if( $groups[$n] == "STRA" ) {
                                    $background = "background-color: green !important;";
                                    $color = "white";
                                 }

                                 if( $groups[$n] == "TC") {
                                    $background = "background-color: purple !important;";
                                    $color = "white";

                                 }

//                                 if( strncasecmp( substr($mensaje[$n],6,15), "MUST HANGUP", 15 ) ) {
                                 if( substr($mensaje[$n],6,11) == "MUST HANGUP" ) {
                                    $color = "red";
                                 }

                                 if( substr($mensaje[$n],6,9) == "NO DIGITS" ) {
                                    $color = "green";
                                 }

                                 if( substr($mensaje[$n],6,13) == "APP NO DIGITS" ) {
                                      $color = "red";
                                 }


                                 if( $cid[$n] == "1172215490" || $cid[$n] == "1121902872" ) {
                                    $background = "background-color: green !important; font-size:9pt !important;";
                                    $color = "white";
                                 }


                                 if( $cid[$n] == "1130547201" || $cid[$n] == "1158514264" || $cid[$n] == "1134903823" || $cid[$n] == "1171882311" || $cid[$n] == "1124718044") {
                                    $background = "background-color: red !important; font-size:9pt !important;";
                                    $color = "white";
                                 }

                                 if( $dnis[$n] == "0009" ) {
                                    $background = "background-color: purple !important;";
                                    $color = "white";
                                 }

                              }
                              else $background = ""; 
                              echo "<td style='width:16%; border-bottom:1px solid rgba(0, 0, 0, 0.12); color:$color !important; $background'><span class='tooltiped' data-position='bottom' data-tooltip='$dnis_full[$n]: $cid[$n]' style='cursor:pointer;'>$mensaje[$n]</span></td>";
                              echo "<td style='width:2px; border: none;'>";
                              echo "</td>"; 
                              echo "</td>";
                           }
                        echo "</tr>";
                     }
                  echo "</table>";

                  $Otras = $Total_Lines - $Total_0045 - $Total_0074;
                  $Discador = $Total_12 + $Total_13;
                  echo "<div style='height:10px;'></div>";
                  echo "<div class='row' style='line-height:0.2em !important;'>";
                     echo "<table style='width:100%;'>";
                        echo "<tr style='line-height:3em; font-size:10pt; border:none;'>";
                            echo "<td style='width:1%;'>&nbsp;</td>";
                            echo "<td style='width:10%;'>Activas: ($Total_Lines)</td>";
                            echo "<td style='width:1%;'>&nbsp;</td>";
                            echo "<td style='width:10%;'>5500-0045: ($Total_0045)</td>";
                            echo "<td style='width:1%;'>&nbsp;</td>";
                            echo "<td style='width:10%;'>5172-0074: ($Total_0074)</td>";
                            echo "<td style='width:1%;'>&nbsp;</td>";

                            echo "<td style='width:10%;'>Discador: ($Total_12/$Total_13)</td>";
                            echo "<td style='width:1%;'>&nbsp;</td>";

                            echo "<td style='width:10%;'>Otras: ($Otras)</td>";
                            echo "<td style='width:1%;'>&nbsp;</td>";

//                            echo "<td style='width:10%;'>mod_singles: ($total_mod_singles)</td>";

//                            echo "<td style='width:1%;'>&nbsp;</td>";
//                          echo "<td style='width:12%;'>mod_directo: ($total_mod_directo)</td>";

                            $dinstar1 = $equipo1 + $conf1;  
                            $dinstar2 = $equipo2 + $conf2;  
                            $dinstar3 = $equipo3 + $conf3;  
                            $dinstar4 = $din4 + $din4_conf;  
                            $total_syn1 = $synway1 + $syn1_conf + $synway1a;  
                            $total_syn2 = $synway2 + $syn2_conf;  
                            $total_syn3 = $synway3 + $syn3_conf;  
                            $total_syn4 = $synway4 + $syn4_conf;  
                            $total_syn5 = $synway5 + $syn5_conf;  
                            $total_syn6 = $synway6 + $syn6_conf;  
                            $total_syn7 = $synway7 + $syn7_conf;  

//                            $total_syn1 = $synway1;  
//                            $total_syn2 = $synway2;  
//                            $total_syn3 = $synway3;  

//                            echo "<td style='width:30%;'>(Dinstar:$dinstar1/$dinstar2/$dinstar3/$dinstar4-Synway:$total_syn1/$total_syn2/$total_syn3/$total_syn4/$total_syn5/$total_syn6/$total_syn7)</td>";
                            echo "<td style='width:1%;'>&nbsp;</td>";
//                            echo "<td style='width:25%;'>(Chat/Stracalli/Movi: $chat/$stra/$movi/$movi2/$movi3/$movi4)</td>";
                            $total = $no_digits+$no_digits_1;
                            echo "<td style='width:25%;'>NO DIGITS: ($no_digits/$no_digits_1:$total)</td>";
//                            echo "<td style='width:10%;'>(Synway-A:$synway1a)</td>";
//                            echo "<td style='width:10%;'>(David:$david/Diego16:$diego16)</td>";


//                            echo "<td style='width:15%;'>($equipo1-$conf1/$equipo2-$conf2/$equipo3-$conf3/Synway:$total_sinway-$sinway/$synway2)</td>";
//                            echo "<td style='width:15%;'>(David:$david/Diego32:$diego32/Diego16:$total_diego)</td>";

//                            echo "<td style='width:12%;'>($equipo1/$equipo2/$david/$diego32/$total_diego/Synway:$total_sinway-$sinway)</td>";
//                            echo "<td style='width:12%;'>($equipo1/$equipo2/$david/$diego32/$total_diego/Synway:$total_sinway-$sinway)</td>";

//                            echo "<td style='width:12%;'>Discador: ($total_discador/$total_conference  $total_conf1/$total_conf2/$total_diego)</td>";
                            echo "<td style='width:1%;'>&nbsp;</td>";
                            echo "<td style='width:35%; text-align:right; padding-right:20px;'><a class='butn' href='#' onclick='destacar();'>DESTACAR</a></td>";
                            echo "<td style='width:35%; text-align:right; padding-right:20px;'><a class='butn' href='traficoLogout.php'>LOGOUT</a></td>";
                        echo "</tr>";
                     echo "</table>";
                  echo "</div>";
               echo "</div>"; 
            echo "</div>"; 
         echo "</div>"; 
      echo "</div>"; 

      mysqli_close($conn);

//        echo "<td id='a' class='col s1 " . $color. " table-row tooltip'><span class='tooltiptext'>&nbsp;&nbsp;" . substr( $DNIS[$i][$x], 6, 4 ) . ": " .

?>

<!--    <div id='ip'></div> -->
</body>
</html>

