<?php
	require 'conexion.php';
	require_once('functions.php');

  	$where = "LEFT JOIN pilotos ON pilotos.id_pedido = pedidos.id LEFT JOIN grade ON grade.id_pedido AND pedidos.id LEFT JOIN companies on companies.id = pedidos.nr_company GROUP BY pedidos.id, pedidos.id_piloto";
    $sql = "SELECT pedidos.obs as obsPedido, pilotos.id as pilotoID, pedidos.id as pedidoID, pedidos.enviado_producao, pedidos.grade_gerada, pilotos.approvedby, pilotos.nr_company, pedidos.nr_company, pilotos.reference, pilotos.modelagem, pilotos.composition, companies.popular_name, pedidos.nr_pedido, pedidos.referencia, pedidos.revista, pedidos.quantidade FROM `pedidos` $where";
    //$resultado = $mysqli->query($sql);
	//$row3 = $resultado->fetch_array(MYSQLI_ASSOC);
    //$idx = $row3['pedidoID'];
    //print_r($row3);        
    
    // seleciona registros para a caixa de selecao de clientes    
    //$query_clientes = "SELECT id, popular_name FROM companies WHERE type = 'C' and ativo = '1' ORDER BY popular_name";
	//$resultado_clientes=$mysqli->query($query_clientes);
    
    //print_r($gradePedido);        
    include(HEADER_TEMPLATE);
?>
			<div class="row">
				<h3 style="text-align:center">Pedidos</h3>
			</div>

			<div class="row table-responsive">
            
				<table class="table table-hover" id="mitabla" >
					<thead>
						<tr>
							<th>Pedido</th>
							<th>Lacre</th>
							<th>Cliente</th>
							<th>Ped.Cliente</th>
							<th>Referência</th>
							<th>Modelagem</th>
							<th>Revista</th>
							<th>QTD</th>
							<th></th>
							<th></th>
						</tr>
					</thead>
					
					<tbody>
                            <?php 
                            $resultado = $mysqli->query($sql);
                            while($row = $resultado->fetch_array(MYSQLI_ASSOC)) { ?>
                                <tr>
                                    <td><?php echo $row['pedidoID']; ?></td>
                                    <td><?php echo $row['pilotoID']; ?></td>
                                    <td><?php echo $row['popular_name']; ?></td>
                                    <td><?php echo $row['nr_pedido']; ?></td>
                                    <td><?php echo $row['reference']; ?></td>
                                    <td><?php echo $row['modelagem']; ?></td>
                                    <td><?php echo $row['revista']; ?></td>
                                    <td><?php echo $row['quantidade']; ?></td>
                                    
                                    <?php if ( ( $row['enviado_producao'] == 0 ) && ( $row['grade_gerada'] == 0 ) ): ?>
                                        <td><a href="pedido_view.php?id=<?php echo $row['pedidoID']; ?>" data-toggle="tooltip" data-placement="top" title="Editar Pedido" ><span class="glyphicon glyphicon-eye-open"></span></a></td>
                                    <?php else : ?>
                                    <td><a href="pedido_view.php?id=<?php echo $row['pedidoID']; ?>" data-toggle="tooltip" data-placement="top" title="Editar Grade" ><span class="glyphicon glyphicon-th-list"></span></a></td>
                                    <?php endif ?>

                                    <?php switch ($row['pedidoID'] > 0 ) { ?>
                                    <?php case  ( ( $row['enviado_producao'] == 0 ) && ( $row['grade_gerada'] == 0 ) ) :?>
                                                    <td><a<button type="button" class="btn btn-xs btn-danger disabled" data-toggle="modal" data-target="#exampleModal" data-whatever="<?php echo $row['pedidoID']; ?>" data-whatevernome="<?php echo $row['popular_name']; ?>"data-whateverdetalhes="<?php echo $row['obsPedido']; ?>">Falta Grade</button></a></td>
                                    <?php break; ?>

                                    <?php case  ( ( $row['enviado_producao'] == 0 ) && ( $row['grade_gerada'] > 0 ) ) :?>
                                                    <td><a<button type="button" class="btn btn-xs btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="<?php echo $row['pedidoID']; ?>" data-whatevernome="<?php echo $row['popular_name']; ?>"data-whateverdetalhes="<?php echo $row['obsPedido']; ?>">Mover O.C.</button></a></td>
                                    <?php break; ?>

                                    <?php case  ( ( $row['enviado_producao'] > 0 ) && ( $row['grade_gerada'] > 0 ) ) :?>
                                                    <td><a<button type="button" class="btn btn-xs btn-success disabled" data-toggle="modal" data-target="#exampleModal" data-whatever="<?php echo $row['pedidoID']; ?>" data-whatevernome="<?php echo $row['popular_name']; ?>"data-whateverdetalhes="<?php echo $row['obsPedido']; ?>">Enviado O.C.!</button></a></td>
                                    <?php break; ?>

                                    <?php case  ( ( $row['enviado_producao'] > 0 ) && ( $row['grade_gerada']== 0 ) ) :?>
                                                    <td><a<button type="button" class="btn btn-xs btn-danger disabled" data-toggle="modal" data-target="#exampleModal" data-whatever="<?php echo $row['pedidoID']; ?>" data-whatevernome="<?php echo $row['popular_name']; ?>"data-whateverdetalhes="<?php echo $row['obsPedido']; ?>">O.C.SemGrade!</button></a></td>
                                    <?php break; ?>

                                    <?php } ?>
                                    
                                </tr>
                            <?php } ?>

                    </tbody>
				</table>
				<a href="pedido_novo.php?id=0&nr_company=0" class="btn btn-primary">Adicionar Pedido</a>
			</div>
            
      <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 mover para Produção (cortes)</h4>
                </div>
                <div class="modal-body">
                    <form method="POST" action="processa.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 do Pedido:</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">Mover para Ordem de Corte</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('Pedido Nr.: ' + recipient)
          modal.find('#pedidoID').val(recipient)
          modal.find('#recipient-name').val(recipientnome)
          modal.find('#detalhes').val(recipientdetalhes)
        })
    </script>

