<script>
     
                function myFunction(evento, evento1, evento2, evento3) {
        
                // alert(evento);
                // alert(evento1);
                // alert(evento2);
                // alert(evento3);
    
                var minha_var = evento;
                var minha_var1 = evento1;
                var minha_var2 = evento2;
                var minha_var3 = evento3;
    
                document.getElementById("myText").innerHTML = minha_var;
                
                document.getElementById("myText1").innerHTML = minha_var1;
                
                document.getElementById("myText2").innerHTML = minha_var2;
              
                document.getElementById("myText3").innerHTML = minha_var3;
                
                }
 </script>


 <script> 
function AdicionaSelect(){ 
 var i = document.form.select.selectedIndex; 
 
 var j = document.form.txtarea.value;
     j += document.form.select[i].text; 
     
  document.form.txtarea.value = j + "\n";
 
 
   alert(document.form.select[i].text);
  
  
  var retorno = document.form.select[i];
  
 return  retorno; 
  
  document.getElementById("myText5").innerHTML =  document.form.txtarea.value;
  
} 
</script> 
<?php
function Mask($mask,$str){

    $str = str_replace(" ","",$str);

    for($i=0;$i<strlen($str);$i++){
        $mask[strpos($mask,"#")] = $str[$i];
    }

    return $mask;

}

    echo "Versão 1.1";
    echo " - ";
    echo  $_SESSION['customercode'];  
    echo "-";
    echo  $_SESSION['ce'];  
    echo "-";
    echo  $_SESSION['cnpj'];  
    echo "-";
    echo  $_SESSION['tipo'];  
    echo "";
?>
<div class="well well-personalizado">
    
  
    <div class="page-header">
        <h1>RCE por Distribuidor Analítico</h1>
        
        
    </div>
    
    <?php
    
    
    
    $host1    = $_SESSION['HOST'];
    $user1    = $_SESSION['USER'];
    $pass1    = $_SESSION['PASS'];
    $dbname1  = $_SESSION['DBNAME'];
    
    $ccc=$_SESSION['customercode']; 
    $cnpj=$_SESSION['cnpj']; 
    
    //********************************* //
     $ce = 0;
     if(isset($_REQUEST['slug_ce'])) {
        $ce             = $_REQUEST['slug_ce'];
        $_SESSION['tipo'] = $ce;
        
     } else {
      // $ce = 0;
      // $_SESSION['ce'] = $ce;
     }
     
     
     // $ce       =  $_SESSION['ce'];
   
     //********************************* //
    
    
    if (isset($this->Dados[0])):
    $catArtigos = $this->Dados[0];
    //var_dump($catArtigos);
    endif;
   
   
  
?>    
    
    <!--<script>
    console.log("passou aqui - listar artigo");
    debugger; // quando estiver no modo de debug (F12) o cursor irá parar aqui
    alert("passou aqui - listar artigo");
    </script>-->



      <!--<div class="form-group">
            <label class="col-sm-4 control-label">Chamada de Encalhe:</label>
            <input type="text"  class="form-control" name="slug_ce" placeholder="Informe a Chamada de Encalhe"  value=""> 
            </div>-->
          
             <div class="form-group">
             <label class="col-sm-4 control-label">Resposta da Chamada de Encalhe:</label>
             <div class="col-sm-4">
             <select class="form-control" name="slug_ce" id="slug_ce">
                 <option value="0"> </option>    
      <?php          
               
               $servername = $host1;
               $username   = $user1;
               $password   = $pass1;
               $dbname     = $dbname1;
               
            // Create connection
               $conn = new mysqli($servername, $username, $password, $dbname);
               // Check connection
               if ($conn->connect_error) {
                  die("Connection failed: " . $conn->connect_error);
               }
       
                       $sql =  "select"
                         . " cde_number "
                         . "FROM carousels"
                         . "  group by cde_number" ;  
               
               // echo $sql; 
               
               if ($result = $conn->query($sql)) {

               /* fetch associative array */
               while ($row = $result->fetch_assoc()) {
                         $descricao =  $row["cde_number"];
                         if($descricao == $_REQUEST["slug_ce"]) {
                             $selectedField = "selected='selected'";
                         } else {
                             $selectedField = "";
                         }
         ?>
                        <option value="<?php echo $descricao; ?>" <?php echo $selectedField; ?>><?php echo $descricao; ?> </option> 
                 
         <?php          
              // echo "<br />" ; 
              // echo $row["description"];
              // echo " ";
              //$descricao =  $row["description"];
              //$precocapa =  $row["price"];
              //$packet    =  $row["packet"];
              //echo $packet; 
               } 
               }
               
               /* commit transaction */
               $conn->commit();
    
               $conn->close(); 
 
    
    // *************************************************************         
                 
      ?>           
                </select>
                 <label class="col-sm-10 control-label"></label>
     <!--                         
               <form name="form"> 
         <select name="select"> 
                    
                    <option value="">201815</option>
                    <option value="">201816</option>
                    <option value="">201817</option>
                    
          </select> 
         </br>
         <textarea name="txtarea"  cols="30" rows="1"></textarea> 
         
         </form> 
         <input name="add" type="button" onClick="AdicionaSelect()" value="Pesquisar">    
     -->           
            </div> 
    
    
<?php     
    
     $varcount = 0;
     
     //********************************* //
     // $ce = 0;
     // if(isset($_REQUEST['slug_ce'])) {
     //   $ce       = $_REQUEST['slug_ce'];
     // } 
     
     // $_SESSION['ce'] = $ce;
     //********************************* //
    
     $valor_venda = 0;
     $desconto    = 0;
     $valorce     = 0;
     
     // echo "Variavel de Sessão CE: " . $_SESSION['ce'];
    
     
    if (isset($_SESSION['msg'])):
        echo $_SESSION['msg'];
        unset($_SESSION['msg']);
    endif;
    if (isset($_SESSION['msgcad'])):
        echo $_SESSION['msgcad'];
        unset($_SESSION['msgcad']);
    endif;
    
    
     // echo $_SESSION['customercode'];    
    $paginacao = $this->Dados[1];
    $this->Dados = $this->Dados[0];
    
    ?>
    <div class="pull-right">
        
       <a href="<?php echo URL; ?>controle-artigo/gerar_excel_p_destrib_det"><button type="button" class="btn btn-danger">Gerar Excel</button></a> -->
      
    </div>


    <?php
    
    
    
    if (!empty($this->Dados)):
        
       
        
        ?>
        <table class="table table-striped table-bordered">
            <thead>
           
            
<?php
   

?>          
            
            <?php
           
           
            ?>
            
            
            <p id="myText5"></p>    
            
            <div class="form-group">
           
            
             <div class="col-sm-10" >
                  
                 <label class="col-sm-4 control-label">Quantidade de Documentos: :</label>
                              
                 <p id="myText"></p>
                 

                <!--<button onclick="myFunction()">Try it</button>-->
                
                 
                 
                 
               <?php
    
               

          
                        
    
                ?>
               
                 
                 
                 
            </div>
             
            </div>    
            
                
                <tr>
                    
                    <th>Id</th>
                    <th>Documento</th>
                  
                    <th>Descrição</th>
                    <th>Data de Envio</th>
                    <!--<th>Tipo Recolh.</th>
                    <th>Data Recolh.</th>
                   
                    <th>Reparte</th>
                    <th>Devolução</th>
                    <th>Venda</th>
                    <th>Preço Capa</th>
                    <th>Valor Venda</th>
                    <th>Pacote Padrão</th>
                    
                    <th>Ações</th> -->
                    
                    
                </tr>
            </thead>
            <tbody>
                
               
                
                
                <?php
                foreach ($this->Dados as $nivelAcesso) :
                    extract($nivelAcesso);
               
               // pesquisar tabela de produtos
               
               // Create connection
               $conn = new mysqli($servername, $username, $password, $dbname);
               // Check connection
               if ($conn->connect_error) {
                  die("Connection failed: " . $conn->connect_error);
               }

               $rce_id_prod_Planeta = $id_prod_Planeta;
               
               $sql     =  "SELECT"
                         . " id_product,"
                         . " id_collection ,"
                         . " id_edition, "
                         . " edition_number, "
                         . " id_edition, "
                         . " edition_number, "
                         . " id_prod_Planeta, "
                         . " description, "
                         . " price, "
                         . " reordable, "
                         . " returnable, "
                         . " packet "
                         . "FROM categorias_artigos"
                         . " where id_prod_Planeta = '" . $rce_id_prod_Planeta . "'"; 
               
               // echo $sql; 
               
               if ($result = $conn->query($sql)) {

               /* fetch associative array */
               while ($row = $result->fetch_assoc()) {
        
              
              // echo "<br />" ; 
              // echo $row["description"];
              // echo " ";
              // $descricao =  $row["description"];
              $precocapa =  $row["price"];
              $packet    =  $row["packet"];
              //echo $packet; 
               } 
               }
               
               
               /* commit transaction */
               $conn->commit();
    
               $conn->close(); 
     
                // ***********************************************************
                
               //if($cde_number == $ce) {
                   
                    $varcount++;
                    ?>               
                    <tr>
                        <!--<td><?php echo $id; ?></td>-->
                        <!--<td><?php echo $cde_number; ?></td> -->
                        
                        <td><?php echo $tipo; ?></td>
                        <?php
                        $link = "../ftp/remessa/doctos/" . $foto;
                        ?>
                        
                        <td> <a href=<?php echo $link  ?>   ><?php echo $foto; ?> </td>
                       
                        <td><?php echo $descricao; ?></td>
                         <td><?php echo $data; ?></td>
                        <!--<td><?php echo $recall_type; ?></td>
                        <td><?php echo $recall_date; ?></td>
                     
                        <td><?php echo $total_distributed; ?></td>
                        <td><?php echo $return2; ?></td>-->
                        
                        <?php 
                        
                       //IF (trim($recall_type) == "NORMAL") {
                       //       $sales = $total_distributed - $return2;  
                       // } else {
                       //       $sales = $sales - $return2; 
                       // }
                        
                        //************************
                 //        $sales = $sales - $return2; 
                 //       IF (trim($recall_type) == "NORMAL") {
                 //           $sales = $total_distributed-$return2; 
                 //        } else {
     
                 //          IF (trim($recall_type) == "NORMAL                                            ") {
                 //              $sales = $total_distributed-$return2; 
                 //         } else {
                  //         $sales = $sales - $return2; 
                  //         }                   
                  //          }
                        
                                                    
                        
                        // ***********************
                     
                          //$precocapa = number_format($precocapa, 2, ',', ' ');
                         
                        ?>
                        
                        
                        <!--<td><?php echo $sales; ?></td>-->
                        
                        <!-- Preço da Capa -->
                       
                        <!--<td><?php echo $precocapa ?></td>-->
                        
                        <?php 
                        $precovenda = $precocapa * ($sales);
                        $valor_venda=$valor_venda+$precovenda;
                        //$precovenda = number_format($precovenda, 2, ',', ' ');
                        ?>
                        
                        <!-- Valor da Venda -->
                        
                        <!--<td><?php echo $precovenda ?></td>-->
                        
                        <?php 
                        //$devolucao = $precocapa * $return2;
                        $precodesconto = ($precovenda * $discount) / 100;
                        $valor_desconto= $precodesconto;
                       
                        $desconto= $desconto+$valor_desconto;
                                               
                        ?>
                        
                        <!-- Pacote Padrão -->
                        
                        <!--<td><?php echo $packet ?></td>-->
                        
                        <?php 
                        $valorce=$valor_venda-$desconto;
                        ?>
                        
                        
                        
   
     
                        
                       
                        <!-- <td class="hidden-xs"><?php 
                        $imagem_artigo = URL . "assets/imagens/artigo/$id/$foto";
                        echo "<img src='$imagem_artigo' height='45' width='65' alt='$titulo_artigo'>";
                        ?></td>
                        <td><?php echo $titulo_artigo; ?></td>
                        <td class="hidden-xs"><?php echo $categorias_artigos; ?></td> -->
                        
                        <!--<td>
                           <!-- <a href="<?php echo URL; ?>controle-artigo/visualizar/<?php echo $id; ?>"><button type="button" class="btn btn-primary">Visualizar</button></a> -->

                           <!-- <a href="<?php echo URL; ?>controle-artigo/editar/<?php echo $id; ?>"><button type="button" class="btn btn-warning">Alterar</button></a> -->

                             <!-- <a href="<?php echo URL; ?>controle-artigo/visualizar/<?php echo $id; ?>"><button type="button" class="btn btn-danger">Fechar</button></a> --> 
                             <!-- <a href="<?php echo URL; ?>controle-artigo/apagar/<?php echo $id; ?>"><button type="button" class="btn btn-danger">Apagar</button></a> -->
                        </td-->
                    </tr>
                    
                    <?php
              // }
                
                endforeach;
               
                
                
                ?>
                    
                
                    
                    
            </tbody>
            
        </table>
    
        
        <!-- <div class="col-sm-10">
                 
                   <label class="col-sm-2 control-label">Total:</label>
                   
                    <?php
                   $valor_venda2 = number_format($valor_venda, 2, ',', ' ');
                   echo $valor_venda2;
                    ?>
                   <!-- <p id="myText1"></p> 
        </div>-->
        <!--<div class="col-sm-10">
                
                
                   <label class="col-sm-2 control-label">Desconto:</label>
                   <?php
                   $desconto2 = number_format($desconto, 2, ',', ' ');
                   echo $desconto2;
                   ?>
                   <!--  <p id="myText2"></p> 
                    
        </div>-->
        <!-- <div class="col-sm-10">
                
               
                   <label class="col-sm-2 control-label">Valor CE:</label>
                   <?php
                   $valorce2 = number_format($valorce, 2, ',', ' ');
                    echo $valorce2;
                   ?>
                   
                    <!-- <p id="myText3"></p> 
        </div> -->
        
             <?php
                         
             // echo " ";
             // $valor_venda2 = number_format($valor_venda, 2, ',', ' ');
             // echo "Total      : " . $valor_venda2;
             // echo " ";
             // $desconto2 = number_format($desconto, 2, ',', ' ');
             // echo "Desconto      : " . $desconto2;
             // echo " ";
             //  $valorce2 = number_format($valorce, 2, ',', ' ');
             // echo "Valor da CE : " . $valorce2;
             
             ?>
             
           <script>myFunction(<?php echo $varcount;?>, <?php echo $valor_venda;?> , <?php echo $desconto;?> ,<?php echo $valorce;?>  )</script> 
           
           
           
           
           
           
        <?php
    endif;
     
    
    //echo Mask("#.###.###.##",$desconto);
    
    
     //if ( $varcount!=0){
     // echo $paginacao;
     //}
    
    
    
    
   
    // var_dump($this->Dados);
    ?>
    
    
</div>