<?php
session_start();
if( $_SESSION["user"] == "" || $_SESSION["passwd"] == "" || $_SESSION["admin"] == false ) {
//   mysqli_close($conn);
   header("Location: index.php");
}

include_once('db_chat.php');

ini_set('max_execution_time', 240);


$Year     = isset($_POST['year']) ? $_POST["year"]  : "";
$Month  = isset($_POST['month']) ? $_POST['month'] : "";
$Fecha = "";
$Total_Llamados = 0;
$Total_Minutos = 0;
$Importe_Total = 0;
$Total_Dia_Llamados = 0;
$Total_Dia_Minutos = 0;
$Importe_Dia_Total = 0;
$tramas = 20;

$YS2015 = "";
$YS2016 = "";

if( empty($Year) ) {        
   if( $_SESSION["Year"] != "" ) $Year = $_SESSION["Year"];
   else {
      $Year = date("Y");
   }
}

if( empty($Month) ) {       
   if( $_SESSION["Month"] != "" ) $Month = $_SESSION["Month"];
   else {
     $Month = date("n");
   }
}

$_SESSION["Year"] = $Year;
$_SESSION["Month"] = $Month;


$sm = array("", "", "", "", "", "", "", "", "", "", "", "");
$sm[$Month-1] = "selected";

if( $Year == "2015")  $YS2015 = "selected";
if( $Year == "2016")  $YS2016 = "selected";
$admin = false;
$Movistar = 0.9225;
$Personal = 1.476;
$Claro =  0.738;
$Nextel = 1.107;

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

include 'header.php'; 
?>
<style>
.dropdown-content {
  overflow-x: hidden;
}

.channel {
   width: 3%;
}
</style>
  <div id="content">
  <div class="container" >
    <div style="height: 15px;"></div>
    <div class="col l9 m12 s12">
      <div class="page-content">
     </div>  
        <table id="detail">
                <thead>
                  <tr class="blue-grey lighten-4">
               <th class="col s1" data-field="t0">Canal</td> 
                    <td class="col s1 channel" data-field="t1">1</td>
                    <td class="col s1 channel" data-field="t2">2</td>
                    <td class="col s1 channel" data-field="t3">3</td>
                    <td class="col s1 channel" data-field="t4">4</td>
                    <td class="col s1 channel" data-field="t5">5</td>
                    <td class="col s1 channel" data-field="t6">6</td>
                    <td class="col s1 channel" data-field="t7">7</td>
                    <td class="col s1 channel" data-field="t8">8</td>
                    <td class="col s1 channel" data-field="t9">9</td>
                    <td class="col s1 channel" data-field="t10">10</td>
                    <td class="col s1 channel" data-field="t11">11</td>
                    <td class="col s1 channel" data-field="t12">12</td>
                    <td class="col s1 channel" data-field="t13">13</td>
                    <td class="col s1 channel" data-field="t14">14</td>
                    <td class="col s1 channel" data-field="t15">15</td>
                    <td class="col s1 channel" data-field="t16">16</td>
                    <td class="col s1 channel" data-field="t17">17</td>
                    <td class="col s1 channel" data-field="t18">18</td>
                    <td class="col s1 channel" data-field="t19">19</td>
                    <td class="col s1 channel" data-field="t20">20</td>
                    <td class="col s1 channel" data-field="t21">21</td>
                    <td class="col s1 channel" data-field="t22">22</td>
                    <td class="col s1 channel" data-field="t23">23</td>
                    <td class="col s1 channel" data-field="t24">24</td>
                    <td class="col s1 channel" data-field="t25">25</td>
                    <td class="col s1 channel" data-field="t26">26</td>
                    <td class="col s1 channel" data-field="t27">27</td>
                    <td class="col s1 channel" data-field="t28">28</td>
                    <td class="col s1 channel" data-field="t29">29</td>
                    <td class="col s1 channel" data-field="t30">30</td>
                  </tr>
                </thead>
                <tbody>

<script type="text/javascript" charset="utf-8">
  var Month = "";
  var Year = "";
  function btnClick() {

  } 
</script>
<style>
.table-row {
   height: 20px;
   padding-top: 8px;
   padding-bottom: 8px;
   border-style: solid;
   border-top: 0px;
   border-left: 0px;
   border-bottom-width: 1px;
   border-right-width: 1px;
   border-color: #cfd8dc;
}

.desc-row {
   height: 20px;
   padding-top: 8px;
   padding-bottom: 8px;
}

</style>
<?php


     $sql = "select sql_no_cache line, dnis, cid, mensaje from log_pantalla where active > 0 order by line";
     $result = $conn->query($sql);

     for( $i = 0; $i < $tramas; $i++ ) {
      for( $x = 0; $x < 30; $x++ ) {
         $Lines[$i][$x] = 0;
         $DNIS[$i][$x] = 0;
         $CID[$i][$x] = 0;
         $Mensajes[$i][$x] = "";
      }
     }

     $actual_line = 0;
     $lines_Count = 0;
     $green = 0;
     $amber = 0;
     $red = 0;
     $purple = 0;
     $blue = 0;
     if ($result->num_rows > 0) {

         while($row = $result->fetch_assoc()) {

            ++$lines_Count;
            $Channel = intval( $row["line"] ) - 1;
            $Mensaje = $row["mensaje"];

            $Trunk = intval( intval( $Channel ) / 30 );
            $Canal = intval( $Channel ) - ( $Trunk * 30 );

            $Lines[$Trunk][$Canal] = 1;
            $Mensajes[$Trunk][$Canal] = $Mensaje;
            $DNIS[$Trunk][$Canal] = $row["dnis"];
            $CID[$Trunk][$Canal] = $row["cid"];
         }
         mysql_free_result( $result );
      }

      for( $i = 0; $i < $tramas; $i++ ) {
         echo "<tr class='green lighten-3 table-row'>\n";
         echo "<td class='col s1 blue-grey lighten-4 right-align table-row'>" . intval( $i ) * 30 . "</td>\n";

         for( $x = 0; $x < 30; $x++ ) {
            switch( $Lines[$i][$x] ) {
               default:
               case 0: $color = "teal lighten-5"; break;
               case 1: 
                  switch( $DNIS[$i][$x] ) {
                     case 1140004444: 
                     case 1140008900: 
                     case 1140008989: 
                     case 1155000045: 
                        $color = "light-blue darken-2"; 
                        ++$blue;
                        break; 

                     case 1140106634: 
                     case 1151720074: 
                     case 1151720075: 
                        $color = "amber";        
                        ++$amber;
                        break; 

                     case 2215150018: 
                     case 2214101818: 
                        $color = "purple";        
                        ++$purple;
                        break; 

                     case 1140008966: 
                     case 1140008967: 
                     case 1140008969: 
                     case 1140008970: 
                     case 1151720080: 
                     case 1151720081: 
                     case 1151720082: 
                     case 1151720083: 
                        ++$red;
                        $color = "red darken-1"; 
                        break; 

                     case 1155000044: 
                     default:         
                        $color = "green";        
                        ++$green;
                        break;      
                  }
                  break;                        
            }

            if( $Mensajes[$i][$x] != '' ) {
               echo "<td id='a' class='col s1 " . $color. " table-row tooltip'><span class='tooltiptext'>&nbsp;&nbsp;" . substr( $DNIS[$i][$x], 6, 4 ) . ": " . $Mensajes[$i][$x] .  "</span></td>\n"; 
            }
            else echo "<td id='a' class='col s1 " . $color. " table-row'></td>\n"; 
         }
         echo "</tr>\n";
      }
      mysqli_close($conn);

//    if( $General_Total_Llamados == 0 ) $Promedio = 0;
//    else $Promedio = $General_Total_Minutos / $General_Total_Llamados;

//    $Horas = intval($Promedio / 60);
//    $Promedio_Min = intval( $Promedio - $Horas );
//    $Promedio_Sec = ($Promedio - $Horas - $Promedio_Min ) * 60;
//    $Sec = sprintf("%d:%02d", $Promedio_Min, $Promedio_Sec);

    echo "<tfoot>";
    echo "</tfoot>";
?>        
    </tbody>
    </table>
    </div>
<?php
    echo "<div style='height: 55px;'></div>";
    echo "<table id='desc'>";
    echo "<tr class='desc-row'>";
    echo "<td class='col s1 teal lighten-5 channel desc-row'></td>";
    echo "<td class='col s1 desc-row'>Disponible</td>";
    echo "<td class='col s1 light-blue darken-2 channel desc-row'></td>";
    echo "<td class='col s1 desc-row'>5500-0045 (" . $blue . ")</td>";
    echo "<td class='col s1 amber channel desc-row'></td>";
    echo "<td class='col s1 desc-row'>*1015 (" . $amber . ")</td>";
    echo "<td class='col s1 purple channel desc-row'></td>";
    echo "<td class='col s1 desc-row'>515-0018 (" . $purple . ")</td>";
    echo "<td class='col s1 red darken-1 channel desc-row'></td>";
    echo "<td class='col s2 desc-row'>*951&nbsp;(" . $red . ")&nbsp;</td>";
    echo "<td class='col s1 green channel desc-row'></td>";
    echo "<td class='col s1 desc-row'>Otros (" . $green . ")</td>";
    echo "</tr>";
    echo "</table>";
    echo "<div style='height:10px;'></div>";
    echo "<div class='row'>";
    echo "<div class='input-field col s12'>";
    echo "<div class='center-align' style='font-size: 1.2em;'>L&iacute;neas Activas: " . $lines_Count . "</div>";
    echo "</div>";
    echo "</div>";


?>
    <div style="height:100px;"></div>
  </div>
  </div>
  <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="js/materialize.min.js"></script>
<script>
   // Post to the provided URL with the specified parameters.
   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);
       });

       // The form needs to be a part of the document in
       // order for us to be able to submit it.
       $(document.body).append(form);
      form.submit();
   };

  $(document).ready(function() {
              // bind 'myForm' and provide a simple callback function 
              /*
              $('#asterisco').ajaxForm(function() { 
                    alert("Thank you for your comment!"; 
              }); */ 

    $('select').material_select();

    $('#month').on('change', function() {
        Month = this.value;
        console.log( this.value )
    });   
    $('#year').on('change', function() {
        Year = this.value;
        console.log( this.value )
    });   

//  $('#detail tr').click(function() {

/*
    $('#detail').on('click', 'tr', function(e) {
         post("detalle.php", {dia: e.currentTarget.cells[0].innerHTML }); 
    });
    */
  });   
</script>
</body>
</html>

