<?php

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




$P_nota=$_GET['id'];
$P_empresa=$_GET['empresa'];

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

$result_custo = "SELECT Sum(tbitensnf.customedtot) AS custo FROM tbitensnf WHERE nota = '".$P_nota."' and empresa = '".$P_empresa."' ";
//$result_custo = "SELECT Sum(tbitensnf.customedtot) AS custo FROM tbnf INNER JOIN tbitensnf ON tbnf.nota = tbitensnf.nota WHERE tbnf.nota = '".$P_nota."' and tbnf.empresa = '".$P_empresa."' ";
//$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>

    <title>Sistema SCA - Grupo BRO</title>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <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>   

    <script type="text/javascript">
        function maskIt(w,e,m,r,a){
        // Cancela se o evento for Backspace
          if (!e) var e = window.event
          if (e.keyCode) code = e.keyCode;
          else if (e.which) code = e.which;
        // Variáveis da função
        var txt  = (!r) ? w.value.replace(/[^\d]+/gi,'') : w.value.replace(/[^\d]+/gi,'').reverse();
        var mask = (!r) ? m : m.reverse();
        var pre  = (a ) ? a.pre : "";
        var pos  = (a ) ? a.pos : "";
        var ret  = "";
        if(code == 9 || code == 8 || txt.length == mask.replace(/[^#]+/g,'').length) return false;
        // Loop na máscara para aplicar os caracteres
        for(var x=0,y=0, z=mask.length;x<z && y<txt.length;){
        if(mask.charAt(x)!='#'){
        ret += mask.charAt(x); x++; } 
        else {
        ret += txt.charAt(y); y++; x++; } }
        // Retorno da função
        ret = (!r) ? ret : ret.reverse()  
        w.value = pre+ret+pos; }
        // Novo método para o objeto 'String'
        String.prototype.reverse = function(){
        return this.split('').reverse().join(''); };
      </script>

      <script language="javascript">
        function number_format( number, decimals, dec_point, thousands_sep ) {
        var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
        var d = dec_point == undefined ? "," : dec_point;
        var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
        var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
        return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
        }
      </script>

      <script> 
        function calcula(operacion){ 
        var operando1 = parseFloat( document.calc.operando1.value.replace(/\./g, "").replace(",", ".") );
        var operando2 = parseFloat( document.calc.operando2.value.replace(/\./g, "").replace(",", ".") );
        var result = eval(operando1 + operacion + operando2);
        document.calc.resultado.value = number_format(result,2, ',', '.');
        } 
      </script> 




</head>
<body>
  
  <script>

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



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

    function CInstrum() {
      var vlrinstru = $('#txtvalinstru').val().replace(',', '.');
      var qtdinstru = $('#txtinstrumentador').val();
      var rins = eval(qtdinstru * vlrinstru);
      $('#txtcinstru').val(number_format(rins,2, ',', '.'));
    }

    function Chopital() {
      var phospital = $('#txthospital').val().replace(',', '.');
      var vnf = $('#vlNF').val();
      var rh = eval(phospital * vnf);
      $('#txtvlrhospital').val(number_format(rh,2, ',', '.'));
    }

    function Cmedico() {
      var pmedico = $('#txtmedico').val().replace(',', '.');
      var vnf = $('#vlNF').val();
      var rmed = eval(pmedico * vnf);
      $('#txtvlrmedico').val(number_format(rmed,2, ',', '.'));
    }

    function Cimposto() {
      var pimposto = $('#txtimposto').val().replace(',', '.');
      var vnf = $('#vlNF').val();
      var rimp =eval(pimposto * vnf);
      $('#txtcimposto').val(number_format(rimp,2, ',', '.'));
    }

    function Cvendedor() {
      var pvendedor = $('#txtvendedor').val().replace(',', '.');
      var vnf = $('#vlNF').val();
      var rven = eval(pvendedor * vnf);
      $('#txtcvendedor').val(number_format(rven,2, ',', '.'));
    }

    function Cdespesas() {
      var pdespesas = $('#txtadm').val().replace(',', '.');
      var vnf = $('#vlNF').val();
      var rdesp = eval(pdespesas * vnf);
      $('#txtcdespesas').val(number_format(rdesp,2, ',', '.'));
    }

    function Cfretes() {
      var pfretes = Number(document.getElementById("txtfrete").value.replace( ',', '.' ));
      var vnf = Number(document.getElementById("vlNF").value.replace( ',', '.' ));
      if (!(pfretes) == '' || pfretes == null) {
        $('#txtcfrete').val(String(pfretes).replace('.',','));
      }
      else {
        $('#txtcfrete').val(String(pfretes ).replace('.',','));
      }
    }

    function Cperdas() {
      var pperdas = Number(document.getElementById("txtperdas").value.replace( ',', '.' ));
      var vnf = Number(document.getElementById("vlNF").value.replace( ',', '.' ));
      if (!(pperdas) == '' || pperdas == null) {
        $('#txtcperdas').val(String(pperdas).replace('.',','));
      }
      else {
        $('#txtcperdas').val(String(pperdas ).replace('.',','));
      }
    }

     function Cmonitor() {
      var pmonitor = Number(document.getElementById("txtmonito").value.replace( ',', '.' ));
      var vnf = Number(document.getElementById("vlNF").value.replace( ',', '.' ));
      if (!(pmonitor) == '' || pmonitor == null) {
        $('#txtcmonitor').val(String(pmonitor).replace('.',','));
      }
      else {
        $('#txtcmonitor').val(String(pmonitor ).replace('.',','));
      }
    }

    function Ctotal() {

      var phospital = $('#txthospital').val().replace(',', '.');
      var vnf = $('#vlNF').val();
      var rh = eval(phospital * vnf);

      //var pmedico = $('#txtmedico').val().replace(',', '.');
      var vnf = $('#vlNF').val();
      var rmed = eval(pmedico * vnf);

      var vlrinstru = $('#txtvalinstru').val().replace(',', '.');
      var qtdinstru = $('#txtinstrumentador').val();
      var rins = eval(qtdinstru * vlrinstru);

      var pimposto = $('#txtimposto').val().replace(',', '.');
      var vnf = $('#vlNF').val();
      var rimp =eval(pimposto * vnf);

      var pvendedor = $('#txtvendedor').val().replace(',', '.');
      var vnf = $('#vlNF').val();
      var rven = eval(pvendedor * vnf);

      var pdespesas = $('#txtadm').val().replace(',', '.');
      var vnf = $('#vlNF').val();
      var rdesp = eval(pdespesas * vnf);

      //var vhospital = String(document.getElementById('txtvlrhospital').value);

      var vfrete = Number(document.getElementById("txtcfrete").value.replace( ',', '.' ));
      var vperdas = Number(document.getElementById("txtcperdas").value.replace( ',', '.' ));
      var vmonitor = Number(document.getElementById("txtcmonitor").value.replace( ',', '.' ));
      var rtot = rins;
      //var rtot = rh + rmed + rins + rimp + rven + rdesp + vfrete + vperdas + vmonitor;

      var perc = (rtot / vnf) * 100;
      var marg = (vnf - rtot)
      
      //document.getElementById('txtctotal').innerHTML = rtot;

      var total = number_format(rtot,2, ',', '.')
      var base = number_format(perc,2, ',', '.')
      var margem = number_format(marg,2, ',', '.')


      //document.getElementById('txtctotal').innerHTML = total;
      $('#txtctotal').val(String(total ).replace('.',','));
      $('#txtLO').val(String(base ).replace('.',','));
      $('#txtMargem').val(String(margem ).replace('.',','));
 
      //$('#txtctotal').val(String(rtot).replace('.',','));
      //$('#txtctotal').val(number_format(rtot,2, ',', '.'));
    
    }




    //function Ctotal() {

      //var hospital = Number(document.getElementById("txtvlrhospital").value.replace( ',', '.' ));
      //var frete = Number(document.getElementById("txtcfrete").value.replace( ',', '.' ));
      //var perda = Number(document.getElementById("txtcperdas").value.replace( ',', '.' ));
      //var monitor = Number(document.getElementById("txtcmonitor").value.replace( ',', '.' ));
      //var elemResult = document.getElementById("txtctotal");

      //if (elemResult.textContent === undefined) {
        // elemResult.textContent =(frete + perda + monitor + hospital);
      //}
      //else { // IE
       //  elemResult.innerText = (frete + perda + monitor + hospital);
      //}
   // }
  </script>

  <input type="hidden" id="vlNF" name="vlNF" value="<?php echo $row_nota['vlrnota']; ?>"/>

  <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 - Cálculo - 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="text-info form-control" id="txthospital" required name="txthospital" onblur="Chopital();" >
                        <option value="0.0">0,0%</option>
                        <option value="0.08">8,0%</option>
                        <option value="0.10">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="txtmedico">Médico:</label>
                    <select class="text-info form-control" id="txtmedico" required name="txtmedico" onblur="Cmedico();">
                        <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="text-info form-control" id="txtinstrumentador" required name="txtinstrumentador" onblur="CInstrum()">
                        <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="number" class="text-info form-control" id="txtvalinstru" name="txtvalinstru" step="1.00" min="1.00" placeholder="Valor R$" onblur="CInstrum()" required style="text-align:right; display:inline-block">
                </div>
                <div class="col-md-2 mb-4">
                    <label for="txtimposto">Imposto:</label>
                    <select class="text-info form-control" id="txtimposto" required name="txtimposto"  onblur="Cimposto()">
                        <option value="0.0">0,0%</option>
                        <option value="0.10">10,0%</option>
                        <option value="0.15">15,0%</option>
                        <option value="0.18">18,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="text-info form-control" id="txtvendedor" required name="txtvendedor" onblur="Cvendedor()">
                        <option value="0.0">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="text-info form-control" id="txtadm" required name="txtadm" onblur="Cdespesas()">
                        <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="number" class="text-info form-control" value="" id="txtfrete" placeholder="Valor R$" name="txtfrete" onblur="Cfretes(); "style="text-align:right">
                    <!--<input type="number" class="text-danger form-control" id="txtfrete" name="txtfrete" placeholder="Valor R$" onblur="Cfretes()" required style="text-align:right"> -->
                    
                </div>
                <div class="col-md-2 mb-4">
                    <label for="txtperdas">Perdas R$:</label>
                    <input type="number" step="1.00" min="1.00" class="text-info form-control" id="txtperdas" name="txtperdas" placeholder="Valor R$" onblur="Cperdas();" style="text-align:right">
                </div>
                <div class="col-md-2 mb-4">
                    <label for="txtmonito">Monitorização R$:</label>
                    <input type="number" step="1.00" min="1.00" class="text-info form-control" id="txtmonito" name="txtmonito" placeholder="Valor R$" onblur="Cmonitor()" style="text-align:right">
                </div>
            </div>
            <button class="btn btn-outline-info" type="submit" name="btnReload" onclick="Ctotal()">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-6 mb-3 ">
                <label for="txtempresa">Custo Total de Produtos 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-6 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>
          <div class="form-row">
            <!--<div class="col-md-2 mb-3">
                <label for="txtvlrhospital">Valor Hospital R$:</label>
                <input type="nummber" class="text-success form-control" id="txtvlrhospital" name="txtvlrhospital" value="" placeholder="R$ 0,00" readonly style="text-align:right; font-weight: bold; font-style: italic;">
            </div> -->
            <div class="col-md-2 mb-3">
                <label for="txtvlrmedico">Valor Médico R$:</label>
                <input type="text" class="text-success form-control" id="txtvlrmedico" name="txtvlrmedico" value="" placeholder="R$ 0,00" readonly style="text-align:right; font-weight: bold; font-style: italic; ">
            </div>
            <div class="col-md-2 mb-3">
                <label for="txtcinstru">Valor 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; font-style: italic;">
            </div>
            <div class="col-md-2 mb-3">
                <label for="txtcimposto">Valor Imposto R$:</label>
                <input type="text" class="text-success form-control" id="txtcimposto" name="txtcimposto" value="" placeholder="R$ 0,00" readonly style="text-align:right; font-weight: bold; font-style: italic;">
            </div>
            <div class="col-md-2 mb-3">
                <label for="txtcvendedor">Valor Vendedor R$:</label>
                <input type="text" class="text-success form-control" id="txtcvendedor" name="txtcvendedor" value="" placeholder="R$ 0,00" readonly style="text-align:right; font-weight: bold; font-style: italic;">
            </div>
            <div class="col-md-2 mb-3">
                <label for="txtcdespesas">Valor Desp.Adm R$:</label>
                <input type="text" class="text-success form-control" id="txtcdespesas" name="txtcdespesas"  value="" placeholder="R$ 0,00" readonly style="text-align:right; font-weight: bold; font-style: italic;">
            </div>
            <div class="col-md-2 mb-3">
                <label for="txtcfrete">Valor Fretes R$:</label>
                <input type="text" class="text-success form-control" id="txtcfrete" name="txtcfrete" value="" placeholder="R$ 0,00" readonly style="text-align:right; font-weight: bold; font-style: italic;">

            </div>
            <div class="col-md-2 mb-3">
                <label for="txtcperdas">Valor Perdas R$:</label>
                <input type="text" class="text-success form-control" id="txtcperdas" name="txtcperdas" value="" placeholder="R$ 0,00" readonly style="text-align:right; font-weight: bold; font-style: italic;">
            </div>
            <div class="col-md-2 mb-3">
                <label for="txtcmonitor">Valor Monitorizacao R$:</label>
                <input type="text" class="text-success form-control" id="txtcmonitor" name="txtcmonitor" value="" placeholder="R$ 0,00" readonly style="text-align:right; font-weight: bold; font-style: italic;">
            </div>
          </div>
           <div class="form-row">
            <div class="col-md-4 mb-3 border border-primary rounded">
                <label for="txtctotal" class="text-primary" style="font-weight: bold;">Custo Simulado Total R$:</label>
                <!--<h5 id="txtctotal"></h5> -->
                <input type="text" class="text-primary form-control" id="txtctotal" name="txtctotal" value="" placeholder="R$ 0,00" readonly style="text-align:center; font-weight: bold;">

                <!--<input type="number" class="text-success form-control" id="txtctotal" name="txtctotal" value="" placeholder="R$ 0,00" readonly style="text-align:right; font-weight: bold"> -->
            </div>
            <div class="col-md-4 mb-3 border border-primary rounded">
                <label for="txtLO" class="text-primary" style="font-weight: bold;">% Simulado :</label>
                <input type="text" class="text-primary form-control" id="txtLO" name="txtLO" value="" placeholder="0,00 %" readonly style="text-align:center; font-weight: bold;">
                <!--<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-4 mb-3 border border-primary rounded">
                <label for="txtMargem" class="text-primary" style="font-weight: bold;">Margem R$ :</label>
                <input type="text" class="text-primary form-control" id="txtMargem" name="txtMargem" value="" placeholder="R$ 0,00" readonly style="text-align:center; font-weight: bold;">
                <!--<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> 
          </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."' AND empresa = '".$P_empresa."'");
          //$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>