<?php
session_start();

if( $_SESSION["user"] == "" || $_SESSION["passwd"] == "" ) {
   mysqli_close($conn);
   header("Location: index.php");
}

$admin = $_SESSION["admin"];

include_once('db_config.php');


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

$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";

$Importe = array( 0,0,0,0);
if( $admin == true )
   $Movistar = 0.9225;
else $Movistar = 1.476; 
$Personal = 1.476;
$Claro =  0.738;
$Nextel = 1.107;
include 'header.php';  
?>
<style>
.dropdown-content {
  overflow-x: hidden;
}
</style>
   <div id="content">
   <div class="container" >
      <div style="height: 15px;"></div>
      <div class="col l9 m12 s12">
         <div class="page-content">
            <div style="height:20px;"></div>
            <form id="asterisco" action="operadora.php" method="post"> 
            <div class="row">
            <div class="input-field col m3"> 
    				<select name="month">
 <?php
  					echo "<option value='' disabled>Seleccione el Mes</option>";				
      					echo "<option value='1' " . $sm[0] . ">Enero</option>";
      					echo "<option value='2' " . $sm[1] . ">Febrero</option>";
      					echo "<option value='3' " . $sm[2] . ">Marzo</option>";
      					echo "<option value='4' " . $sm[3] . ">Abril</option>";
      					echo "<option value='5' " . $sm[4] . ">Mayo</option>";
      					echo "<option value='6' " . $sm[5] . ">Junio</option>";
      					echo "<option value='7' " . $sm[6] . ">Julio</option>";
      					echo "<option value='8' " . $sm[7] . ">Agosto</option>";
      					echo "<option value='9' " . $sm[8] . ">Septiembre</option>";
      					echo "<option value='10' " . $sm[9] . ">Octubre</option>";
      					echo "<option value='11' " . $sm[10] . ">Noviembre</option>";
      					echo "<option value='12' " . $sm[11] . ">Diciembre</option>";
    					echo "</select>";
               echo "<label>Mes</label>";
   				echo "</div>";
  				echo "<div class='input-field col m3'>";
    					echo "<select  name='year' size='2'>";
                					echo "<option value='' disabled selected>Seleccione el A&ntilde;o</option>";
      					echo "<option value='2015' " . $YS2015 . ">2015</option>";
      					echo "<option value='2016' " . $YS2016 . ">2016</option>"; 
?>     					
     					</select>
               <label>A&ntilde;o</label>
  				</div>		
  			 	<button class="btn waves-effect waves-light btn col s1" style="margin-top:25px; margin-left: 30px;" type="submit" name="action" >Go!</button>
  				</row>	
  				</form>
  				 <div style="height:70px;"></div>
  			</div>	
  			<table class="highlight">
        				<thead>
          				<tr class="red lighten-3">
              				<th class="col s1" data-field="id">Fecha</th>
             				<th class="col s3" data-field="price">Aplicacion</th>
             				<th class="col s2 right-align" data-field="movistar" class="col s1 right-align">Movistar</th>
             				<th class="col s2 right-align" data-field="personal" class="col s2 right-align">Personal</th>
             				<th class="col s2 right-align" data-field="claro" class="col s2 right-align">Claro</th>
                                 <th class="col s2 right-align" data-field="nextel" class="col s2 right-align">Nextel</th>
              				<th class="col s2 right-align" data-field="price" class="col s2 right-align">Total</th>
          				</tr>
        				</thead>
        				<tbody>

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

   }	
</script>
<?php
//	$sql = "select dnis, date(fecha) as dia, dnis, aplicacion, count(id) as llamados, sum(duracion) as segundos, sum(duracion_facturada) as minutos, sum( duracion_facturada ) * Tarifa as importe from tickets where year(fecha) ='" . $Year . "' and Month(fecha)='" . $Month . "' and dnis  >= '1140008966' and dnis <= '1140008970' and dnis <> '1140008968' and fecha >= '2016-02-01' group by date(fecha), dnis";
   $sql = "select sql_no_cache dnis, date(fecha) as dia, dnis, aplicacion, ( sum( case when dnis = '1140008966' then duracion else 0 end ) ) as movistar, (sum( case when dnis = '1140008967' then duracion else 0 end ) ) as personal, (sum( case when dnis = '1140008969' then duracion else 0 end ) ) as claro,  ( sum( case when dnis = '1140008970' then duracion else 0 end ) )as nextel  from tickets where year(fecha) ='" . $Year . "' and Month(fecha)='" . $Month . "' and dnis  >= '1140008966' and dnis <= '1140008970' and dnis <> '1140008968' and fecha >= '2016-02-01' group by date(fecha) desc";
   $result = $conn->query($sql);

      $Fecha = "";
      $Total_Llamados = 0;
      $Total_Minutos = 0;
      $Importe_Total = 0;
      $Importe_Total_General = 0;
      $Importe[0] = $Importe[1] = $Importe[2] = $Importe[3] = 0;        
      $Totales[0] = $Totales[1] = $Totales[2] = $Totales[3] = 0;        
      $first = true;

   if ($result->num_rows > 0) {
    	
    		while($row = $result->fetch_assoc()) {

         $date = date_create($row["dia"]);
         $formatted = date_format($date, 'd-m-Y');
 
                
                $Importe[0] = $row["movistar"] / 60 * $Movistar;
                $Importe[1] = $row["personal"] / 60 * $Personal;
                $Importe[2] = $row["claro"] / 60 * $Claro;
                $Importe[3] = $row["nextel"] / 60 * $Nextel;
                
                $Totales[0] += $Importe[0];
                $Totales[1] += $Importe[1];
                $Totales[2] += $Importe[2];
                $Totales[3] += $Importe[3];

                $Importe_Total = $Importe[0] + $Importe[1] + $Importe[2] + $Importe[3];
                $Importe_Total_General += $Importe[0] + $Importe[1] + $Importe[2] + $Importe[3];

    			echo "<tr>";
            	echo "<td class='col s1' >" . $formatted . "</td>";
                if( $first == true )
            	   echo "<td class='col s3' >Chat *951</td>";
                 else echo "<td class='col s3' ></td>";
            	echo "<td class='col s2 right-align'>" . number_format ( $Importe[0] , 2, ',' ,  '.' ) . "</td>";
            	echo "<td class='col s2 right-align'>" . number_format ( $Importe[1] , 2, ',' ,  '.' ) . "</td>";
                echo "<td class='col s2 right-align'>" . number_format ( $Importe[2] , 2, ',' ,  '.' ) . "</td>";
                echo "<td class='col s2 right-align'>" . number_format ( $Importe[3] , 2, ',' ,  '.' ) . "</td>";
                echo "<td class='col s2 right-align'>" . number_format ( $Importe_Total , 2, ',' ,  '.' ) . "</td>";
        		echo "</tr>";
                 $first = false;
    		}
   } 
   mysqli_close($conn);


         		echo "<tfoot>";
    			echo "<tr>";
    		echo "<td class='col s1' ></td>";
           	echo "<td class='col s3' ></td>";
           	echo "<td class='col s2  orange lighten-2 right-align'>" . number_format ( $Totales[0] , 2, ',' ,  '.' ) . "</td>";
                echo "<td class='col s2  orange lighten-2 right-align'>" . number_format ( $Totales[1] , 2, ',' ,  '.' ) . "</td>";
                echo "<td class='col s2  orange lighten-2 right-align'>" . number_format ( $Totales[2] , 2, ',' ,  '.' ) . "</td>";
                echo "<td class='col s2  orange lighten-2 right-align'>" . number_format ( $Totales[3] , 2, ',' ,  '.' ) . "</td>";
          		echo "<td class='col s2  orange lighten-2 right-align'>" . number_format ( $Importe_Total_General , 2, ',' ,  '.' ) . "</td>";
     			echo "</tr>";
          		echo "</tfoot>";
?>					
              	</tbody>
      			</table>
         	</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>
   $(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 )
      });		

      console.log( "Ready")
   });		
</script>
</body>
</html>
