<?php

session_start();

require_once 'gerador/SqlServer.php';
require_once 'script_php/conexaoSQL.php';

$sql = new Sqlserver;

if (isset($_GET['chave'])){

$chave = filter_var($_GET['chave']);

$produtos = $sql->analiseDePrecos($chave);
$boletos = $sql->boletosNota($chave);
$dadosNota = $sql->dadosNotaEntrada($chave);

} ?>

<!DOCTYPE html>
<html>
<head>

<?php include_once 'head.html' ?>

<title></title>
</head>

<body>

<?php include_once 'header.php'; ?>

<h5>

<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="currentColor" class="bi bi-arrow-clockwise" viewBox="0 0 16 16">
  <path fill-rule="evenodd" d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2z"/>
  <path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466"/>
</svg>

ANALISAR PREÇOS DA ENTRADA DE UMA NOTAS</h5>
<p></p>

<form action="#" method="get">

    <input type="text" name="chave" placeholder="chave"  class="form-control form_forn" autofocus> <p></p>
    <input type="submit" name="" id=""  class="form-control form_forn btn btn-warning" value="CONSULTAR"> 
</form>

<p></p>
<h5>

<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" class="bi bi-cart4 text-success" viewBox="0 0 16 16">
  <path d="M0 2.5A.5.5 0 0 1 .5 2H2a.5.5 0 0 1 .485.379L2.89 4H14.5a.5.5 0 0 1 .485.621l-1.5 6A.5.5 0 0 1 13 11H4a.5.5 0 0 1-.485-.379L1.61 3H.5a.5.5 0 0 1-.5-.5M3.14 5l.5 2H5V5zM6 5v2h2V5zm3 0v2h2V5zm3 0v2h1.36l.5-2zm1.11 3H12v2h.61zM11 8H9v2h2zM8 8H6v2h2zM5 8H3.89l.5 2H5zm0 5a1 1 0 1 0 0 2 1 1 0 0 0 0-2m-2 1a2 2 0 1 1 4 0 2 2 0 0 1-4 0m9-1a1 1 0 1 0 0 2 1 1 0 0 0 0-2m-2 1a2 2 0 1 1 4 0 2 2 0 0 1-4 0"/>
</svg>

INFORMAÇÃO DE RECEBIMENTO DE NOTA </h5>

<p></p>

<hr>

<table class="table">

 <tr class="table-warning">
    <th>NUMERO NOTA</th>
    <th> DATA EMISSAO</th>
    <th>DATA ENTRADA</th>
    <th>VALOR</th>
    <th>DESCONTO</th>
    <th>CFOP ENTRADA</th>
    <th>LOJA</th>
  </tr>

  <?php
  
  if(isset($_REQUEST['chave'])){

  foreach ($dadosNota as $nota){ ?>

  <tr>
    <td><?= $nota[0] ?></td>
 <td><?php $date = new DateTime($nota[1]); echo $date->format('d/m/Y') ?></td>
 <td><?php $date = new DateTime($nota[2]); echo $date->format('d/m/Y') ?></td>
    <td><?php echo number_format($nota[3],2,',', '.'); ?></td>
    <td><?= $nota[4] ?></td>
    <td><?= $nota[5] ?></td>
    <td><?= $nota[6] ?></td>
  </tr>

  <?php } } ?>

</table>
<p></p>

<p></p>
<table class="table">

<tr class="table-warning" >
  <!-- <td>SELECIONAR</td> -->
    <td>COD</td>
    <td>PRODUTO</td>
    <td>VENDA</td>
    <td>SUGERIDO</td>
    <td>DIFERENÇA</td>
    <td>COMPRA</td>    
</tr>

<form action="" method="">

<?php if (isset($produtos)){

foreach($produtos as $prod){

  $diferencaPreco = ($prod[2] - $prod[3]);
  
if (($prod[2] < $prod[3]) && $prod[2] > 0){?>

<tr class= 'text-danger'>

<?php }else if ($prod[2] < 0.01){ ?>
  
<tr class= 'p-5 bg-danger'>

<?php } else if(($prod[2] > $prod[3]) && $prod[2] > 0){
  
  if ($diferencaPreco > 0.30){
  
  ?>

  <tr class= 'text-success'>

  <?php }else{ ?>

  <tr>

<?php } }?>

    <!-- <td> <input type="checkbox" name="selecao[]" > </td> -->
    <td><?= $prod[0]?></td>
    <td><?= $prod[1]?></td>
    <td align="center"><?= number_format($prod[2], 2, ',', '.');?></td>
    <td align="center"><?= number_format($prod[3], 2, ',', '.');?></td>
    <td align="center"><?= number_format($prod[4], 2, ',', '.');?></td>
    <td align="center"><?= number_format($prod[5], 2, ',', '.');?></td>
</tr>

<?php }} ?>

</table>


<!-- <input type="submit" value= "ENVIAR" class="btn btn-danger"> -->

</form>

<P></P>
<h5>CONTAS A PAGAR</h5>

<p></p>
<hr>
<p></p>

<table class="table">

  <tr>
    <th>VENCIMENTO</th>
    <th>VALOR</th>
    <th>STATUS</th>
    <th>OBSERVAÇÃO</th>
    <th>PENDENTE</th>
  </tr>

  <?php 

  if (isset($_REQUEST['chave'])){

  foreach ($boletos as $boleto){

    if ($boleto[2] == 0){

      $status = "ABERTO";

    }else{

      $status = "PAGO";
    }
  ?>
  <tr>
    <td><?php $date = new DateTime($boleto[0]); echo $date->format('d/m/Y') ?></td>
    <td><?php echo number_format($boleto[1],2,',', '.'); ?></td>

    <?php if ($status == 'ABERTO'){ ?>

    <td><?= $status ?></td>

    <?php }else{ ?>

    <td class= 'text-danger'><?= $status ?></td>

    <?php }?>
      <td><?= $boleto[3]?></td>
      <td><?= $boleto[4]?></td><?php } }?>
  </tr>

</table>

<?php include_once 'footer.php' ?>