<?php
	require 'conexion.php';
	$tdat = 0;
	$tqat = 0;
	$tsat = 0;
	$toat = 0;
	$quat = 0;
	$qdat = 0;
	$qqat = 0;
	$qsat = 0;
	$qoat = 0;
	$ciat = 0;
	$cdat = 0;
	$totalg = 0;

	$id = $_GET['id'];

    // seleciona todos os registros da base com inner join cliente
    $sql = "SELECT ordem_corte.*, companies.popular_name, grade.*, pedidos.id, pedidos.modelo, pedidos.modelagem, pedidos.nr_company, pedidos.id_piloto, pedidos.revista, pedidos.dtcadastro, pedidos.data_pre, pedidos.data_pedido,pedidos.data_entrega,pedidos.nr_pedido, pedidos.referencia, pedidos.descricao, pedidos.descricao, pedidos.quantidade, pedidos.tecido, pedidos.dt_baixado, pedidos.obs AS obsPed, pedidos.ativo FROM grade RIGHT OUTER JOIN pedidos ON pedidos.id = grade.id_pedido LEFT JOIN companies on companies.id = pedidos.nr_company INNER JOIN ordem_corte ON ordem_corte.id_pedido = pedidos.id WHERE pedidos.id = '$id' AND grade.dir = '4'   ";
    //print_r($sql);
    $resultado = $mysqli->query($sql);
    $row = $resultado->fetch_array(MYSQLI_ASSOC);


    require_once('functions.php');
	include(HEADER_TEMPLATE);
?>
			<div class="row">
				<h3 style="text-align:center">Grade Finalização Produção | Ordem de Corte: <?php echo $row['id_oc']; ?> | Pedido: <?php echo $row['id_pedido']; ?> </h3>
            </div>

   			<form name="formPedido" method="POST" action="grade_adiciona.php" autocomplete="off">
				
				<input type="hidden" id="id" name="id" value="<?php echo $row['id']; ?>" />
				<input type="hidden" id="id_grade" name="id_grade" value="<?php echo $row['id_grade']; ?>" />
                <input type="hidden" id="type" name="type" value="O" />
                <input type="hidden" id="ativo" name="ativo" value="1" />
                                
                <a href="#collapse1" data-toggle="collapse">recolher detalhes do Pedido</a>
                <div id="collapse1" class="collapse">
                    <div class="row">    
                        <div class="form-group col-md-2">
                            <label for="dtcadastro">Cadastro Pedido</label>
                            <input type="date" class="form-control" name="dtcadastro" id="dtcadastro" value="<?php echo $row['dtcadastro'] ?>" readonly="readonly">
                        </div>

                        <div class="form-group col-md-1">
                            <label for="idpiloto">Lacre</label>
                            <input type="text" class="form-control" name="id_piloto" id="id_piloto" value="<?php echo $row['id_piloto'] ?>" readonly="readonly">
                        </div>

                        <div class="form-group col-md-2">
                            <label for="nr_pedido">Nr Pedido</label>
                            <input type="number" class="form-control" id="nr_pedido" name="nr_pedido" value="<?php echo $row['nr_pedido'] ?>" readonly="readonly">
                        </div>
                        
                        <div class="form-group col-md-3">
                            <label for="revista">Revista</label>
                            <input type="text" class="form-control" id="revista" name="revista" value="<?php echo $row['revista'] ?>" readonly="readonly">
                        </div>

                        <div class="form-group col-md-4">
                            <label for="popular_name">Cliente</label> 
                            <input type="text" class="form-control" id="popular_name" name="popular_name" value="<?php echo $row['popular_name'] ?>" readonly="readonly">
                        </div>
                    </div>
                    <div class="row">
                           
                        <div class="form-group col-md-2">
                            <label for="referencia">Referência</label>
                            <input type="text" class="form-control" id="referencia" name="referencia" value="<?php echo $row['referencia'] ?>" readonly="readonly">
                        </div>

                        <div class="form-group col-md-2">
                            <label for="descricao">Descrição</label>
                            <input type="text" class="form-control" id="descricao" name="descricao" value="<?php echo $row['descricao'] ?>" readonly="readonly">
                        </div>

                        <div class="form-group col-md-3">
                            <label for="modelo">Modelo</label>
                            <input type="text" class="form-control" id="modelo" name="modelo" value="<?php echo $row['modelo'] ?>" readonly="readonly">
                        </div>

                        <div class="form-group col-md-3">
                            <label for="modelagem">Modelagem</label>
                            <input type="text" class="form-control" id="modelagem" name="modelagem" value="<?php echo $row['modelagem'] ?>" readonly="readonly">
                        </div>

                        <div class="form-group col-md-2">
                            <label for="tecido">Tecido</label>
                            <input type="text" class="form-control" id="tecido" name="tecido" value="<?php echo $row['tecido'] ?>" readonly="readonly">
                        </div>
                                            
                    </div>

                    <div class="row">

                            <div class="form-group col-md-2">
                                <label for="data_pre">Data Pré</label>
                                <input type="date" class="form-control" id="data_pre" name="data_pre" value="<?php echo $row['data_pre'] ?>" readonly="readonly">
                            </div>                        
                        
                            <div class="form-group col-md-2">
                              <label for="dtpedido">Data do Pedido</label>
                                <input type="date" class="form-control" id="data_pedido" name="data_pedido" value="<?php echo $row['data_pedido'] ?>" readonly="readonly">                          
                            </div>
                            
                            <div class="form-group col-md-2">
                              <label for="dtentrega">Data da Entrega</label>
                                <input type="date" class="form-control" id="data_entrega" name="data_entrega" value="<?php echo $row['data_entrega'] ?>" readonly="readonly">                          
                            </div>
                            
                            <div class="form-group col-md-2">
                                <label for="qtd">Quantidade</label>
                                <input type="number" class="form-control" id="quantidade" name="quantidade" value="<?php echo $row['quantidade'] ?>" readonly="readonly">
                            </div>

         
                            <div class="form-group col-sm-4 control-label">
                                <label for="obs">Observações do PEDIDO</label>
                                <div class="">
                                    <textarea name="obs" class="form-control" rows="1" cols="48" readonly="readonly" ><?php echo $row['obsPed']?></textarea>
                                </div>
                            </div>
                            
                        </div>

                    </div>

                    <table class="table table-condensed  table-bordered">
                        <thead>
                          <tr>
                            <th><h4 style="text-align:center">Grade</h4></th>
                            <th><h4 style="text-align:center">Nome / Obs</h4></th>
                            <th><h3 style="text-align:center">32</h3></th>
                            <th><h3 style="text-align:center">34</h3></th>
                            <th><h3 style="text-align:center">36</h3></th>
                            <th><h3 style="text-align:center">38</h3></th>
                            <th><h3 style="text-align:center">40</h3></th>
                            <th><h3 style="text-align:center">42</h3></th>
                            <th><h3 style="text-align:center">44</h3></th>
                            <th><h3 style="text-align:center">46</h3></th>
                            <th><h3 style="text-align:center">48</h3></th>
                            <th><h3 style="text-align:center">50</h3></th>
                            <th><h3 style="text-align:center">52</h3></th>
                            <th><h3 style="text-align:center">Subtotal</h3></th>
                            <th><a href="grade_adiciona_refugo.php?id_grade=<?php echo $row['id_grade']; ?>&id=<?php echo $row['id']; ?>&dir=2" data-toggle="tooltip" data-placement="top" type="button" class="btn btn-info btn-xs">Refugo</button></a></th>
                          </tr>
                        </thead>

                        <tbody>
                            <?php 
                            $resultado = $mysqli->query($sql);
                            while($row = $resultado->fetch_array(MYSQLI_ASSOC)) { ?>
                                
                                <input type="hidden" id="id_grade" name="id_grade" value="<?php echo $row['id_grade']; ?>" />
                                <input type="hidden" id="id" name="id" value="<?php echo $row['id']; ?>" />
                                <tr>
                                    <td><?php echo $row['titulo'] ?></td>
                                    <td><?php echo $row['obs_grade'] ?></td>

                                    <td align="center"><?php echo $row['tda'] ?></td>
                                        <?php $tdat += $row['tda']; ?>

                                    <td align="center"><?php echo $row['tqa'] ?></td>
                                        <?php $tqat += $row['tqa'];?>

                                    <td align="center"><?php echo $row['tsa'] ?></td>
                                        <?php $tsat += $row['tsa'];?> 

                                    <td align="center"><?php echo $row['toa'] ?></td>
                                        <?php $toat += $row['toa'];?> 

                                    <td align="center"><?php echo $row['qua'] ?></td>
                                        <?php $quat += $row['qua'];?> 

                                    <td align="center"><?php echo $row['qda'] ?></td>
                                        <?php $qdat += $row['qda'];?> 

                                    <td align="center"><?php echo $row['qqa'] ?></td>
                                        <?php $qqat += $row['qqa'];?> 

                                    <td align="center"><?php echo $row['qsa'] ?></td>
                                        <?php $qsat += $row['qsa'];?> 

                                    <td align="center"><?php echo $row['qoa'] ?></td>
                                        <?php $qoat += $row['qoa'];?> 

                                    <td align="center"><?php echo $row['cia'] ?></td>
                                        <?php $ciat += $row['cia'];?> 

                                    <td align="center"><?php echo $row['cda'] ?></td>
                                        <?php $cdat += $row['cda'];?> 

                                    <td align="center"><?php echo $row['subtotal'] ?></td>
                                        <?php $totalg += $row['subtotal']; ?>
                                    <td align="center">
                                        <?php if (empty($row['obs_grade']) ): ?> 
                                               <a href="grade_edita_linha_fim.php?id_grade=<?php echo $row['id_grade']; ?>&id=<?php echo $row['id']; ?>&dir=2" data-toggle="tooltip" data-placement="top" type="button" class="btn btn-primary btn-xs">Adiciona</button></a>
                                         <?php endif ?>
                                    </td>

                                </tr>

                            <?php } ?>
                        </tbody>
                        <hr>
                <tr>

                    <td>
                        <a href="apontamento_index.php" class="btn btn-info">Retornar</a>
                    </td>
                    <td>
                    <?php   $resultado2 = $mysqli->query($sql);
                            $row2 = $resultado2->fetch_array(MYSQLI_ASSOC);?>

                            <a<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal" data-whatever="<?php echo $row2['id_oc']; ?>" data-whatevernome="<?php echo $row2['popular_name']; ?>"data-whateverdetalhes="<?php echo $row2['obsPed']; ?>">Fechar Produção</button></a>

                    </td>
                    <td align="center">
                        <input type="text" class="form-control" id="tdat" name="tdat" value="<?php echo $tdat ?>" readonly="readonly" style="min-width:55px;">
                    </td>
                    <td>
                        <input type="text" class="form-control" id="tqat" name="tqat" value="<?php echo $tqat ?>" readonly="readonly" disabled style="min-width:55px;">
                    </td>
                    <td>
                        <input type="text" class="form-control" id="tsat" name="tsat" value="<?php echo $tsat ?>" readonly="readonly" style="min-width:55px;">
                    </td>
                    <td>
                        <input type="text" class="form-control" id="toat" name="toat" value="<?php echo $toat ?>" readonly="readonly" style="min-width:55px;">
                    </td>
                    <td>
                        <input type="text" class="form-control" id="quat" name="quat" value="<?php echo $quat ?>" readonly="readonly" style="min-width:55px;">
                    </td>
                    <td>
                        <input type="text" class="form-control" id="qdat" name="qdat" value="<?php echo $qdat ?>" readonly="readonly" style="min-width:55px;">
                    </td>
                    <td>
                        <input type="text" class="form-control" id="qqat" name="qqat" value="<?php echo $qqat ?>" readonly="readonly" style="min-width:55px;">
                    </td>
                    <td>
                        <input type="text" class="form-control" id="qsat" name="qsat" value="<?php echo $qsat ?>" readonly="readonly" style="min-width:55px;"> 
                    </td>
                    <td>
                        <input type="text" class="form-control" id="qoat" name="qoat" value="<?php echo $qoat ?>" readonly="readonly" style="min-width:55px;">
                    </td>
                    <td>
                        <input type="text" class="form-control" id="ciat" name="ciat" value="<?php echo $ciat ?>" readonly="readonly" style="min-width:55px;">
                    </td>
                    <td>
                        <input type="text" class="form-control" id="cdat" name="cdat" value="<?php echo $cdat ?>" readonly="readonly" style="min-width:55px;">
                    </td>
                    <td>
                        <input type="text" class="form-control" id="totalg" name="totalg" value="<?php echo $totalg ?>" readonly="readonly" style="min-width:60px;">
                    </td>
                </tr>
            </table>
        </form>

          <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="exampleModalLabel">Aprovação para fechar para Produção</h4>
                    </div>
                    <div class="modal-body">
                        <form method="POST" action="fim_producao_modal.php" enctype="multipart/form-data">
                            <div class="form-group">
                                <label for="recipient-name" class="control-label">Cliente :</label>
                                <input name="nome" type="text" class="form-control" id="recipient-name">
                            </div>
                            <div class="form-group">
                                <label for="message-text" class="control-label">Observação final:</label>
                                <textarea name="detalhes" class="form-control" id="detalhes"></textarea>
                            </div>
                            <input name="pedidoID" type="hidden" class="form-control" id="pedidoID" value="">
                        
                            <button type="button" class="btn btn-success" data-dismiss="modal">Cancelar</button>
                            <button type="submit" class="btn btn-danger">Confirmar encerrar Produção ?</button>
                        </form>
                    </div>
                  
                </div>
              </div>
            </div>
            
    <?php include(FOOTER_TEMPLATE); ?>      

    <script type="text/javascript">
        $('#exampleModal').on('show.bs.modal', function (event) {
          var button = $(event.relatedTarget) // Button that triggered the modal
          var recipient = button.data('whatever') // Extract info from data-* attributes
          var recipientnome = button.data('whatevernome')
          var recipientdetalhes = button.data('whateverdetalhes')
          // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
          // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
          var modal = $(this)
          modal.find('.modal-title').text('Ordem de Corte Nr.: ' + recipient)
          modal.find('#pedidoID').val(recipient)
          modal.find('#recipient-name').val(recipientnome)
          modal.find('#detalhes').val(recipientdetalhes)
        })
    </script>       
