<?php

Include("includes/con_cliente.php");

 require_once('connection.php');
    session_start();

    if(isset($_SESSION['User']))
    {
        $query="select * from tbusuario where nomeuser='".$_SESSION['User']."'";
        $result=mysqli_query($con,$query);
        $resultado = mysqli_fetch_assoc($result);
        $r_resultado = $resultado;
        //echo $resultado["nome"] .'<br>';
    }
    else
    {
        header("location:index.php");
    }

$c_fornecedor="select nomefor from tbfornecedor order by nomefor asc";
$r_fornecedor= mysql_query($c_fornecedor);

$id = $_GET['id'];
//$id=11;
//echo $id;


$result_veiculo = "SELECT * FROM tbservicos WHERE codigo = '".$id."'";
//$result_veiculo = "SELECT * FROM tbveiculos WHERE id = '12'";
$resultado_veiculo = mysqli_query($con,$result_veiculo);
$row_veiculo = mysqli_fetch_assoc($resultado_veiculo);


?>



<html lang="pt-br">

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

        <!--<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" /> -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-datetimepicker.css" />

<!--        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
        <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/src/js/bootstrap-datetimepicker.js"></script> -->


        <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="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
    <script src="jquery.maskMoney.js" type="text/javascript"></script>


    </head>
    <body>
        
        <nav class="navbar fixed-top navbar-expand-lg navbar-dark" style="background-color: #3333ff;">
          <!--<a class="navbar-brand" href="#">SCA</a> -->
          <a class="navbar-brand" href="menu.php">
                <img src="img/logomarca.png" width="50" height="40" class="d-inline-block align-center" alt="">
                SCA - Controle de Veícuos - Agendamento de Serviços para Veículo
          </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="m.php" style="color:inherit"> Sair </a></button>
              </button>
            </form>
          </div>
        </nav>

        
        <div style="margin-top: 4rem;" >
          <div class="row mx-5">
            <div class="card-body border border-dark rounded" style="background-color:#EEE8AA">
              <h4 class="card-title">Dados do Veículo</h4>
              <h6 class="card-subtitle mb2 text-muted ">Placa: <?php echo $row_veiculo['srvplaca']; ?></h6>
              <p>Km Atual: <?php echo $row_veiculo['srvkmatual']; ?>
              
              </p>
            </div>
          </div>
          <div class="row mt-2 mx-5">
            <div class="card-body border border-dark rounded">
              <h4 class="card-title">Dados do Serviço</h4>
              <form method="POST" action="srvatualiza.php" enctype="multipart/form-data">
                <div class="form-row">
                  <div class="col-md-2 mb-3">
                    <input type="hidden" name="txtplaca" value="<?php echo $row_veiculo['srvplaca']; ?>">
                    <input type="hidden" name="txtkmatual" value="<?php echo $row_veiculo['srvkmatual']; ?>">
                    <input type="hidden" name="txtcodigo" value="<?php echo $row_veiculo['codigo']; ?>">
                    <input type="hidden" id ="vlrorcd" name="txtvlrorcado" value="<?php echo $row_veiculo['srvvlrorcado']; ?>">

                    <label for="txtfornecedor">Fonecedor do Serviço:</label>
                    <p style="color:#27408B"><?php echo $row_veiculo['srvfornecedor']; ?></p>
                    

                  </div>
                  <div class="col-md-3 mb-3">
                    <label for="txttipomanu">Tipo de Manutenção:</label>
                    <p style="color:#27408B"><?php echo $row_veiculo['srvtipo']; ?></p>
                    
                </div>
                <div class="col-md-3 mb-3">
                    <label for="txtsituacao">Situação:</label>
                    <p style="color:#27408B"><?php echo $row_veiculo['srvsituacao']; ?></p>
                    
                </div>
                <div class="col-md-3 mb-3">
                    <label for="txttipo">Tipo do Serviço:</label>
                    <p style="color:#27408B"><?php echo $row_veiculo['srvservico']; ?></p>
                    
                </div>
                  <label for="txtdescricao">Descrição do Serviços:</label>
                  <textarea class="form-control" type="text" disabled name="txtdescricao" cols="75" style="color:#27408B"><?php echo $row_veiculo['srvdescricao']; ?></textarea>
                  <div class="col-md-2 mb-3">
                    <label for="txtvlrorcado">Valor Orçado R$:</label>
                    <p style="color:#27408B"><?php echo $row_veiculo['srvvlrorcado']; ?></p>
                    
                  </div>
                  <div class="col-md-2 mb-3">
                    <label for="txtdatarealiza">Data Prevista do Serviço:</label>
                    <p id="vlrorc"style="color:#27408B"><?php echo $row_veiculo['srvdtservico']; ?></p>
                   
                  </div>
                  <div class="col-md-3 mb-3">
                    <label for="txtaprova">Liberação:</label>
                    <p style="color:#27408B"><?php echo $row_veiculo['srvaprova']; ?></p>
                    
                </div>
                  
                </div>
              </form>
            </div>
          </div>

          <?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();}
              $resultpecas= mysqli_query($connect, "SELECT * FROM tbservicospecas WHERE codigo='".$id."' ORDER BY idpeca");
          ?>

          <div class="row mt-2 mx-5">
            <div class="card-body border border-dark rounded" style="background-color:#E8E8E8">
              <h4 class="card-title">Preças e Componentes</h4>
              <button type="button" class="btn btn-info btn-sm mb-2 data" data-toggle="modal"  data-target="#MymodalCad">+ Peça</button>
              <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">Descrição</th>
                              <th style="text-align: center;vertical-align:middle">Qtd</th>
                              <th style="text-align: center;vertical-align:middle">R$ Unit</th>
                          </tr>
                      </thead>
                      <?php while($rowsp = mysqli_fetch_array($resultpecas)): ?>
                      <tr>
                          
                          <td style="text-align: left;vertical-align:middle"><?php echo $rowsp['srvpeca']; ?></td>
                          <td style="text-align: center;vertical-align:middle"><?php echo $rowsp['srvqtdpeca']; ?></td>
                          <!--<td style="text-align: right;vertical-align:middle"><?php echo $rowsp['srvqvlrunitpeca']; ?></td> -->
                          <td style="text-align: right;vertical-align:middle"><?php echo $rowsp['srvqvlrunitpeca']; ?></td>
                         <!-- <td style="text-align: center;vertical-align:middle"><a href="srvaltera.php?id=<?php echo $rows['codigo']; ?>">Editar</a> </td> -->

                      </tr>
                      <?php endwhile; ?>
                  </table>
                </div>
              </div>
            </div>

          </div>

          <!-- Início da Janela Modal -->

          <div class="modal fade" tabindex="-1" role="dialog" id="MymodalCad">
            <div class="modal-dialog" role="document">
              <div class="modal-content">
                <div class="modal-header">
                  <h4 class="modal-title text-center" id="myModalLabel">Inclusão de Peças em OS</h4>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
                    <span aria-hidden="true">&times;</span>
                  </button>
                </div>
                <div class="modal-body">
                <form method="POST" action="srvitemcadastrado.php" enctype="multipart/form-data">
                  <div class="form-group">  
                      <label for="txtdescricao">Descrição:</label>
                      <input type="hidden" name="txtcodigoos" value="<?php echo $row_veiculo['codigo']; ?>">
                      <input type="text" class="form-control" id="txtdescricaoitem" name="txtdescricaoitem" placeholder="Descriçao da Peça" required>
                  </div>
                   <div class="form-group">  
                      <label for="txtdescricao">Quantidade:</label>
                      <input type="text" class="form-control" id="txtqtditem" name="txtqtditem" placeholder="1" required>
                  </div>
                   <div class="form-group">  
                      <label for="txtdescricao">Valor Unitário R$:</label>
                      <input type="text" class="form-control" id="txtvlrunit" name="txtvlrunit" placeholder="1,00" required>
                  </div>
                  <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
                  <button type="submit" class="btn btn-primary">Incluir Peça</button>
                </div>
                </form>
                </div>
                
              </div>
            </div>
          </div>

          <!-- Fim da Janela Modal -->
          <?php
          $result_pecas_veiculo = "SELECT SUM((srvqtdpeca * srvqvlrunitpeca)) AS total FROM tbservicospecas WHERE codigo='".$id."'";
          //$result_veiculo = "SELECT * FROM tbveiculos WHERE id = '12'";
          $resultado_pecas = mysqli_query($con,$result_pecas_veiculo);
          $rstotal = mysqli_fetch_assoc($resultado_pecas);

          ?>


          <div class="row mx-5 mt-2">
            <form method="POST" action="srvencerrado.php" enctype="multipart/form-data">
              <div class="card-body border border-dark rounded" style="background-color:#C6E2FF">
                <h4 class="card-title">Encerramento da OS</h4>
                <div class="form-row">
                    <div class="col-md-2 mb-3">
                      <input type="hidden" name="txtcodigoos" value="<?php echo $row_veiculo['codigo']; ?>"/>
                      <label for="txtsituacao">Situação do Serviço:</label>
                      <select class="custom-select" id="txtsituacao" name="txtosituacao">
                        <option value="Realizado">Realizado</option>
                        <option value="Nao Realizado">Não Realizado</option>
                      </select>
                    </div>
                    <div class="col-md-2 mb-3">
                      <label for="txtvlrorcado">Valor da Mão de Obra R$:</label>
                      <!--<p style="color:#27408B"><?php echo $rstotal['total']; ?></p> -->
                      <input type="text" class="form-control" id="vlrServ" name="txtosvlrorcado"  placeholder="0,00" required onblur="calcular();calcular1();" />
                    </div>

                    <div class="col-md-2 mb-3">
                      <label for="txtvlrorcado">Valor das Peças R$:</label>

                      <input type="text" class="form-control" id="vlrPc" name="txtospc" value="<?php echo $rstotal['total']; ?>" placeholder="0,00"  required onkeypress="$(this).mask('###.##0,00');" onblur="calcular();calcular1()" />
                    </div>
                    <div class="col-md-3 mb-3">
                      <label for="txtaprova">Valor Total:</label>
                      <h5 id="resultado"></h5>




                    </div>
                    <div class="col-md-3 mb-3">
                      <label for="txtaprova">Valor Total - Orçado:</label>
                      <h5 id="resultado1"></h5>
                    </div>
                    <label for="txtdescricao">Observações:</label>
                    <textarea class="form-control" type="text" name="txtosobs" cols="75" ></textarea>

                    <script>
                      function calcular() {
                          var num1 = Number(document.getElementById("vlrServ").value.replace( ',', '.' ));
                          var num2 = Number(document.getElementById("vlrPc").value.replace( ',', '.' ));
                          var elemResult = document.getElementById("resultado");

                          if (elemResult.textContent === undefined) {
                             elemResult.textContent = "R$ " + String(num1 + num2).replace('.',',');
                          }
                          else { // IE
                             elemResult.innerText = "R$ " + String(num1 + num2).replace('.',',');
                          }

                      }
                    </script>
                     <script>
                      function calcular1() {
                          var numa = Number(document.getElementById("vlrServ").value.replace( ',', '.' ));
                          var numb = Number(document.getElementById("vlrPc").value.replace( ',', '.' ));
                          var numc = Number(document.getElementById("vlrorcd").value.replace( ',', '.' ));
                          var elemResult1 = document.getElementById("resultado1");

                          if (elemResult1.textContent === undefined) {
                             elemResult1.textContent = "R$ " + String(numc-(numa + numb)).replace('.',',');

                          }
                          else { // IE
                             elemResult1.innerText = "R$ " + String(numc-(numa + numb)).replace('.',',');
                          }

                      }
                    </script>
                    <div class="col-md-2 mb-3 mt-4">
                      <button class="btn btn-success mt-2" type="submit">Finalizar</button>
                      <button class="btn btn-warning mt-2" type="reset" disableValidation="true">Limpar</button>
                    </div>     
                  </div>
                </div>
              </form>  
            </div>
          </div>
        </div>
        <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">

    </body>

    <footer class="page-footer font-small text-primary "style="margin-top: 6rem">
          <div class="footer-copyright text-center py-3">© 2019 Copyright: Brasil Ortopedia
          </div>
    </footer>

</html>
