<?php

Include("includes/con_cliente.php");
require_once('connection.php');




$P_nota=$_GET['id'];

$result_nota = "SELECT * FROM tbnf WHERE nota = '".$P_nota."'";
$resultado_nota = mysqli_query($con,$result_nota);
$row_nota = mysqli_fetch_assoc($resultado_nota);

$result_custo = "SELECT Sum(tbitensnf.customedtot) AS custo FROM tbnf INNER JOIN tbitensnf ON tbnf.nota = tbitensnf.nota WHERE tbnf.nota = '".$P_nota."'";
$resultado_custo = mysqli_query($con,$result_custo);
$row_custo = mysqli_fetch_assoc($resultado_custo);


?>


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Sistema SCA - Grupo BRO</title>
    <link rel="stylesheet" href="node_modules/bootstrap/compiler/bootstrap.css">
    <link rel="stylesheet" href="node_modules/bootstrap/compiler/styler.css">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>   

</head>
<body>
  
  <script>

    $('.txtvalinstru').mask('#.##0,00', {reverse: true});

    function convertToCurrency(value) {
      return value.toLocaleString("pt-BR", {
        style: "currency",
        currency: "BRL",
        minimumFractionDigits: 2
      })
    }

    jQuery(document).ready(function(){
      jQuery('input').on('keyup',function(){
        if(jQuery(this).attr('name') === 'result'){
        return false;
        }
      
        var qtdinstrumentador = (jQuery('#txtinstrumentador').val() == '' ? 0 : jQuery('#txtinstrumentador').val());
        var vlrinstrumentador = (jQuery('#txtvalinstru').val() == '' ? 0 : jQuery('#txtvalinstru').val());
        var resultinstru = (parseInt(qtdinstrumentador) * parseInt(vlrinstrumentador));
        jQuery('#txtcinstru').val(convertToCurrency(resultinstru));
      });
    });
  </script>


    <nav class="navbar fixed-top navbar-expand-lg navbar-dark" style="background-color: #3333ff;">
        <a class="navbar-brand" href="menu.php">
            <img src="img/logomarca.png" width="50" height="40" class="d-inline-block align-center" alt="">
            SCA - Orçamentos - Apuração das Margens de Comercialização
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
            <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
            </ul>
            <form class="form-inline my-2 my-lg-0">
                <button class="btn btn-primary btn-sm my-2 my-sm-0" type="submit">
                    <a href="pesquisanotas.php" style="color:inherit"> Nova Pesquisa </a>
                </button>
                <button class="btn btn-primary btn-sm my-2 my-sm-0" type="submit">
                    <a href="m.php" style="color:inherit"> Sair </a>
                </button>
            </form>
        </div>
    </nav>
      <div style="margin-top: 5rem;" >
        <div class="row mt-2 mx-5" >
          <div class="card-body border border-dark rounded" style="background-color:#FFFFE0">
            <h4 class="card-title">Parâmetros</h4>
              <div class="form-row">
                    <div class="col-md-1 mb-4">
                      <label for="txthospital">Hospital:</label>
                      <select class="custom-select " id="txthospital" required name="txthospital">
                          <option value="">%...</option>
                          <option value="0.0">0,0%</option>
                          <option value="0.08">8,0%</option>
                          <option value="0.1">10,0%</option>
                          <option value="0.15">15,0%</option>
                          <option value="0.20">20,0%</option>
                      </select>
                  </div>
                  <div class="col-md-1 mb-4">
                      <label for="txtimportacao">Médico:</label>
                      <select class="custom-select " id="txthospital" required name="txthospital">
                          <option value="">%...</option>
                          <option value="0.0">0,0%</option>
                          <option value="0.16">16,0%</option>
                          <option value="0.20">20,0%</option>
                          <option value="0.25">25,0%</option>
                          <option value="0.28">28,0%</option>
                          <option value="0.30">30,0%</option>
                          <option value="0.35">35,0%</option>
                          <option value="0.40">40,0%</option>
                          <option value="0.45">45,0%</option>
                      </select>
                      
                  </div>
                  <div class="col-md-2 mb-4">
                      <label for="txtinstrumentador">Instrumentador:</label>
                      <select class="custom-select " id="txtinstrumentador" required name="txtinstrumentador">
                          <option value="">Qtd...</option>
                          <option value="0">0</option>
                          <option value="1">1</option>
                          <option value="2">2</option>
                          <option value="3">3</option>
                          <option value="4">4</option>
                      </select>
                  </div>
                  <div class="col-md-2 mb-4">
                      <label for="txtvalinstru">R$ Instrumentador:</label>
                      <input type="text" class="txtvalinstru form-control" id="txtvalinstru" name="txtvalinstru" placeholder="Valor R$" required style="text-align:right; display:inline-block">
                  </div>
                  <div class="col-md-2 mb-4">
                      <label for="txtimposto">Imposto:</label>
                      <select class="custom-select " id="txtimposto" required name="txtimposto">
                          <option value="">%...</option>
                          <option value="0.10">10,0%</option>
                          <option value="0.15">15,0%</option>
                          <option value="0.20">20,0%</option>
                          <option value="0.25">25,0%</option>
                          <option value="0.30">30,0%</option>
                      </select>
                  </div>
                  <div class="col-md-2 mb-4">
                      <label for="txtvendedor">Comis.Vendas:</label>
                      <select class="custom-select " id="txtvendedor" required name="txtvendedor">
                          <option value="">%...</option>
                          <option value="0.00">0,0%</option>
                          <option value="0.02">2,0%</option>
                          <option value="0.03">3,0%</option>
                          <option value="0.04">4,0%</option>
                      </select>
                  </div>
                  <div class="col-md-2 mb-4">
                      <label for="txtadm">Des.Adm.Financ.%:</label>
                      <select class="custom-select " id="txtadm" required name="txtadm">
                          <option value="">%...</option>
                          <option value="0.0">0,0%</option>
                          <option value="0.04">4,0%</option>
                          <option value="0.06">06,0%</option>
                          <option value="0.08">08,0%</option>
                          <option value="0.10">10,0%</option>
                          <option value="0.15">15,0%</option>
                          <option value="0.20">20,0%</option>
                          <option value="0.30">30,0%</option>
                          <option value="0.40">40,0%</option>
                      </select>
                  </div>
                  <div class="col-md-2 mb-4">
                      <label for="txtfrete">Fretes R$:</label>
                      <input type="text" class="text-info form-control" id="txtfrete" name="txtfrete" placeholder="Valor R$" required style="text-align:right">
                  </div>
                  <div class="col-md-2 mb-4">
                      <label for="txtperdas">Perdas R$:</label>
                      <input type="text" class="text-info form-control" id="txtperdas" name="txtperdas" placeholder="Valor R$" required style="text-align:right">
                  </div>
                  <div class="col-md-2 mb-4">
                      <label for="txtmonito">Monitorização R$:</label>
                      <input type="text" class="text-info form-control" id="txtmonito" name="txtmonito" placeholder="Valor R$" required style="text-align:right">
                  </div>
              </div>
              <button class="btn btn-outline-info" type="submit" name="btnReload" >Calcular</button> 
            </div>
      </div>
        
        <div class="row mt-2 mx-5">
          <div class="card-body border border-dark rounded" style="background-color:#F0FFF0">
            <h4 class="card-title">Cálculo da Simulação</h4>
            <form method="POST" action="listasimula.php" enctype="multipart/form-data">
            <div class="form-row">
              <div class="col-md-2 mb-3">
                  <label for="txtempresa">Custo total da Nota R$</label>
                  <input type="text" class="text-success form-control" id="txtcusto" name="txtcusto" value="<?php echo number_format(($row_custo['custo']),2, ',', '.'); ?>" readonly style="text-align:right; font-weight: bold;">
              </div>
              <div class="col-md-2 mb-3">
                  <label for="txtvlr">Valor da Nota R$:</label>
                  <input type="text" class="text-success form-control" id="txtvlr" name="txtvlr" value="<?php echo number_format(($row_nota['vlrnota']),2, ',', '.'); ?>" readonly style="text-align:right; font-weight: bold;">
              </div>
              <div class="col-md-2 mb-3">
                  <label for="txtcinstru">Custo da Instrum.R$:</label>
                  <input type="text" class="text-success form-control" id="txtcinstru" name="txtcinstru" value="" placeholder="R$ 0,00" readonly style="text-align:right; font-weight: bold">
              </div>
              <div class="col-md-2 mb-3">
                  <label for="txtmkp">Outros Valores R$:</label>
                  <input type="text" class="text-info form-control" id="txtmkp" name="txtmkp" value="<?php echo number_format(($row_nota['vlrnota'] - $row_custo['custo']),2, ',', '.');?>" readonly style="text-align:right">
              </div>
              <div class="col-md-2 mb-3">
                  <label for="txtempresa">Margem Geral %:</label>
                  <input type="text" class="text-info form-control" id="txtcusto" name="txtcusto" value="<?php echo number_format(($row_custo['custo']),2, ',', '.'); ?>" readonly style="text-align:right">
              </div>
              <div class="col-md-2 mb-3">
                  <label for="txtempresa">Valor da Margem R$:</label>
                  <input type="text" class="text-info form-control" id="txtcusto" name="txtcusto" value="<?php echo number_format(($row_custo['custo']),2, ',', '.'); ?>" readonly style="text-align:right; font-weight: bold;">
              </div>
            </div> 
            </form>
          </div>
        </div>

        <!-- Scripr para mostrar/ocultar div -->
         <script>
            function Mudarestado(el) {
            var display = document.getElementById(el).style.display;
            if (display == "none")
              document.getElementById(el).style.display = 'block';
            else
              document.getElementById(el).style.display = 'none';
            }
          </script>
        <!-- Scripr para mostrar/ocultar div -->
        <div class="row mt-2 mx-5">
          <button type="button" class="btn btn-warning btn-sm btn-block" style="margin-bottom: 0.1rem" onclick="Mudarestado('divDados')">Dados NF</button>
        </div>


        <div class="row mt-2 mx-5">

          <div class="card-body border border-dark rounded" id="divDados" style="background-color:#E0FFFF; display: none">
            <h4 class="card-title">Dados da Nota</h4>
            <form method="POST" action="listasimula.php" enctype="multipart/form-data">
            <div class="form-row">
              <div class="col-md-3 mb-3">
                  <label for="txtempresa">Empresa de Faturamento</label>
                  <input type="text" class="text-info form-control" id="txtempresa" name="txtempresa" value="<?php echo $row_nota['empresa']; ?>" readonly style="text-align:right">
              </div>
              <div class="col-md-1 mb-3">
                  <label for="txtnf">Número da NF:</label>
                  <input type="text" class="text-info form-control" id="txtnf" name="txtnf" value="<?php echo $P_nota;?>" readonly style="text-align:right">
              </div>
              <div class="col-md-2 mb-3">
                  <label for="txtvlr">Valor NF R$:</label>
                  <input type="text" class="text-info form-control" id="txtvlr" name="txtvlr" value="<?php echo number_format(($row_nota['vlrnota']),2, ',', '.'); ?>" readonly style="text-align:right">
              </div>
              <div class="col-md-2 mb-3">
                  <label for="txtcusto">Custo NF R$:</label>
                  <input type="text" class="text-info form-control" id="txtcusto" name="txtcusto" value="<?php echo number_format(($row_custo['custo']),2, ',', '.'); ?>" readonly style="text-align:right">
              </div>
              <div class="col-md-2 mb-3">
                  <label for="txtmkp">Markup NF R$:</label>
                  <input type="text" class="text-info form-control" id="txtmkp" name="txtmkp" value="<?php echo number_format(($row_nota['vlrnota'] - $row_custo['custo']),2, ',', '.');?>" readonly style="text-align:right">
              </div>
            </div> 
            </form>
          </div>
        </div>
        <div class="row mt-2 mx-5">
          <button type="button" class="btn btn-success btn-sm btn-block" style="margin-bottom: 0.5rem" onclick="Mudarestado('divDetalhes')">Detalhes NF</button>
          <div class="card-body border border-dark rounded" id="divDetalhes" style="background-color:#FFFFE0; display:none">
            <h4 class="card-title">Detalhes da Nota</h4>
            <div class="form-row">
              <div class="col-md-3 mb-4">
                  <label for="txtcliente">Cliente:</label>
                  <input type="text" class="text-info form-control" id="txtcliente" name="txtcliente" value="<?php echo $row_nota['cliente']; ?>" readonly>
              </div>
              <div class="col-md-2 mb-4">
                  <label for="txtemissao">Emissão:</label>
                  <input type="text" class="text-info form-control" id="txtemissao" name="txtemissao" value="<?php echo date("d/m/y",strtotime($row_nota['emissao']));?>"readonly>
              </div>
              <div class="col-md-3 mb-4">
                  <label for="txtpaciente">Paciente:</label>
                  <input type="text" class="text-info form-control" id="txtpaciente" name="txtpaciente" value="<?php echo $row_nota['paciente'];?>" readonly>
              </div>
              <div class="col-md-4 mb-4">
                  <label for="txthospital">Hospital:</label>
                  <input type="text" class="text-info form-control" id="txthospital" name="txthospital" value="<?php echo $row_nota['hospital'];?>" readonly>
              </div>
              <div class="col-md-2 mb-4">
                  <label for="txtcidade">Cidade:</label>
                  <input type="text" class="text-info form-control" id="txtcidade" name="txtcidade" value="<?php echo $row_nota['cidhosp'];?>" readonly>
              </div>
              <div class="col-md-2 mb-4">
                  <label for="txtplano">Plano de Saúde:</label>
                  <input type="text" class="text-info form-control" id="txtplano" name="txtplano" value="<?php echo $row_nota['convenio'];?>" readonly>
              </div>
              <div class="col-md-2 mb-4">
                  <label for="txtmedico">Médico:</label>
                  <input type="text" class="text-info form-control" id="txtmedico" name="txtmedico" value="<?php echo $row_nota['medico'];?>" readonly>
              </div>
              <div class="col-md-2 mb-4">
                  <label for="txtcirurgia">Cirurgia:</label>
                  <input type="text" class="text-info form-control" id="txtcirurgia" name="txtcirurgia" value="<?php echo date("d/m/y",strtotime($row_nota['dtcirurgia']));?>"readonly>
              </div>
              <div class="col-md-2 mb-4">
                  <label for="txtgrupo">Grupo:</label>
                  <input type="text" class="text-info form-control" id="txtgrupo" name="txtgrupo" value="<?php echo $row_nota['grupo'];?>" readonly>
              </div>
              <div class="col-md-2 mb-4">
                  <label for="txttipo">Tipo:</label>
                  <input type="text" class="text-info form-control" id="txttipo" name="txttipo" value="<?php echo $row_nota['tipo'];?>" readonly>
              </div>
              <div class="col-md-2 mb-4">
                  <label for="txtvendedor">Vendedor:</label>
                  <input type="text" class="text-info form-control" id="txtvendedor" name="txtvendedor" value="<?php echo $row_nota['representante'];?>" readonly>
              </div>
            </div>
          </div>
        </div>

        <div class="row mt-2 mx-5">
          <button type="button" class="btn btn-info btn-sm btn-block" style="margin-bottom: 0.5rem" onclick="Mudarestado('divProdutos')">Produtos da NF</button>
        </div>
        
        <div class="row mt-2 mx-5">
          <?php
            $connect = mysqli_connect('127.0.0.1', 'root', 'fcf2778@', 'administrativo');
            if(mysqli_connect_errno($connect)){
            echo 'Falha ao conectar com o Banco de Dados !'.mysqli_connect_error();}


            $result= mysqli_query($connect, "SELECT * FROM tbitensnf WHERE nota = '".$P_nota."'");

            //$result= mysqli_query($connect, "SELECT tbnf.empresa, tbnf.nota, tbnf.cliente, tbitensnf.produto ,tbitensnf.descricao ,tbitensnf.qtditem, tbitensnf.vlrunit ,tbitensnf.vlrtotal ,tbitensnf.toticms, tbitensnf.customeduni, tbitensnf.customedtot from tbnf INNER JOIN tbitensnf ON tbnf.nota = tbitensnf.nota WHERE tbnf.nota = '".$P_nota."'");
          ?>
          
          <div class="card-body border border-dark rounded" id="divProdutos" style="background-color:#F5FFFA; display:none">
            <h4 class="card-title">Produtos da Nota Fiscal</h4>
            <div class="container">
              <div class="table-responsive">
                <table style="overflow: auto;" class="table table-striped table-responsive-md btn-table table-hover table-bordered">
                  <thead class="table-warning">
                      <tr>
                          
                          <th style="text-align: center;vertical-align:middle">Codigo</th>
                          <th style="text-align: center;vertical-align:middle">Descrição</th>
                          <th style="text-align: center;vertical-align:middle">Qtd</th>
                          <th style="text-align: center;vertical-align:middle">R$ Unit</th>
                          <th style="text-align: center;vertical-align:middle">R$ Total</th>
                          <th style="text-align: center;vertical-align:middle">R$ Icms</th>
                          <th style="text-align: center;vertical-align:middle">Cust.Unit</th>
                          <th style="text-align: center;vertical-align:middle">Cust.Tot</th>
                      </tr>
                  </thead>
                  <?php while($row = mysqli_fetch_array($result)): ?>
                  <tr>
                      
                      <td style="text-align: left;vertical-align:middle"><?php echo $row['produto']; ?></td>
                      <td style="text-align: left;vertical-align:middle"><?php echo $row['descricao']; ?></td>
                      <td style="text-align: center;vertical-align:middle"><?php echo $row['qtditem']; ?></td>
                      <td style="text-align: right;vertical-align:middle"><?php echo number_format(($row['vlrunit']),2, ',', '.'); ?></td>
                      <td style="text-align: right;vertical-align:middle"><?php echo number_format(($row['vlrtotal']),2, ',', '.'); ?></td>
                      <td style="text-align: right;vertical-align:middle"><?php echo number_format(($row['toticms']),2, ',', '.'); ?></td>
                      <td style="text-align: right;vertical-align:middle"><?php echo number_format(($row['customeduni']),2, ',', '.'); ?></td>
                      <td style="text-align: right;vertical-align:middle"><?php echo number_format(($row['customedtot']),2, ',', '.'); ?></td>
                  </tr>
                  <?php endwhile; ?>
                </table>
              </div>
            </div>
          </div>
        </div>
        </div>
      </body>   
        <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
        <script src="js/jquery.js"></script> 
        <script src="js/bootstrap.min.js"></script>

</html>