<?php
	session_cache_expire(60);
    session_start();
	ob_start();

	$id_users = isset($_SESSION['id_users']) ? $_SESSION['id_users']: "";	
	$nome_user = isset($_SESSION['nome']) ? $_SESSION['nome']: "";	
	$email_users = isset($_SESSION['email']) ? $_SESSION['email']: "";	
	$pass_users = isset($_SESSION['pass']) ? $_SESSION['pass']: "";	
	$logado = isset($_SESSION['logado']) ? $_SESSION['logado']: "N";	

	if ($logado == "N" && $id_users == ""){	    
		echo  "<script type='text/javascript'>
					location.href='login.php'
				</script>";	
		exit();
	}

	require 'conexion.php';
	require_once('functions.php');
            
    $sql = "SELECT producao.producao_fim, grade.obs_grade, grade.somageralc, grade.somagerala, grade.somageralb, ordem_corte.*, pedidos.*, companies.popular_name FROM ordem_corte INNER JOIN pedidos ON pedidos.id = ordem_corte.id_pedido INNER JOIN companies ON pedidos.nr_company = companies.id INNER JOIN grade ON pedidos.id = grade.id_pedido LEFT JOIN producao ON producao.id_oc = ordem_corte.id_oc AND grade.dir = '1' GROUP BY ordem_corte.id_oc";
     
    include(HEADER_TEMPLATE);
 
?>
 
			<div class="container-fluid">

				<table class="table table-hover table-bordered" id="tabelaOC">
					<thead>
						<tr>			
							<th>O.C.</th>
							<th>Ped</th>
							<th>Cliente</th>
							<th>Pedido Cliente</th>
							<th>Referência</th>							
							<th>Modelagem</th>
							<th>Modelo</th>
							<th>Riscos</th>
							<th>Forro</th>
							<th></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['id_oc']; ?></td>
                                    <td><?php echo $row['id_pedido']; ?></td>
                                    <td><?php echo $row['popular_name']; ?></td>
                                    <td><?php echo $row['nr_pedido']; ?></td>									
                                    <td><?php echo $row['referencia']; ?></td>									
                                    <td><?php echo $row['modelagem']; ?></td>
                                    <td><?php echo $row['modelo']; ?></td>
                                    <td><?php echo (int)$row['somagerala']; ?></td>
                                    <td><?php echo (int)$row['somageralb']; ?></td>

									<td><a href="pdf_oc_view.php?id=<?php echo $row['id_pedido']; ?>" target="_blank" <span class="glyphicon glyphicon-print"></span></a></td>
									
                                    <?php if ( (int)$row['enviado_producao'] == 0 && (int)$row['producao_fim'] == 0 ) : ?>
                                        <td align="center"><a href="oc_view.php?id=<?php echo $row['id_pedido']; ?>&route=prod_on" data-toggle="tooltip" data-placement="top" title="Editar Grade"><button type="button" class="btn btn-xs btn-primary" >Editar Grade</button></a></td>
                                    <?php else : ?>
										<?php if ( (int)$row['enviado_producao'] > 0 && (int)$row['producao_fim'] == 0 ) : ?>
												<td align="center"><a href="all2_view.php?id=<?php echo $row['id_pedido']; ?>&view=prod" <button type="button" class="btn btn-xs btn-danger" >em Produção</button></a></td>										
										<?php else : ?>	
												<td align="center"><a href="all2_view.php?id=<?php echo $row['id_pedido']; ?>&view=prod" <button type="button" class="btn btn-xs btn-success" >Concluída</button></a></td>										
										<?php endif ?>
                                    <?php endif ?>
									
                                    <?php if ( (int)$row['enviado_producao'] == 0 AND (int)$row['somagerala'] > 0 ) : ?>
                                        <td align="center"><button type="button" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#exampleModal" data-whatever2="<?php echo $row['id_oc']; ?>" data-whatever="<?php echo $row['id_pedido']; ?>"data-whatevernome="<?php echo $row['popular_name']; ?>"data-whateverdetalhes="<?php echo $row['obs_grade']; ?>">Mover Produção</button></td>
                                    <?php else : ?>
                                        <td align="center"><a href="all2_view.php?id=<?php echo $row['id_pedido']; ?>&view=oc" <span class="glyphicon glyphicon-sunglasses"></span></a></td>																																		
                                    <?php endif ?>      
                                </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</h4>
                            </div>
                        <div class="modal-body">
                            <form method="POST" action="oc_move_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 da Ordem de Corte:</label>
                                    <textarea name="detalhes" class="form-control" id="detalhes"></textarea>
                                </div>
                                <input name="pedidoID" type="hidden" class="form-control" id="pedidoID" value="">
                                <input name="pedidoID2" type="hidden" class="form-control" id="pedidoID2" value="">
                            
                                <button type="button" class="btn btn-success" data-dismiss="modal">Cancelar</button>
                                <button type="submit" class="btn btn-danger">Confirma mover para 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 recipient2 = button.data('whatever2') // 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 + '  |  O.C. Nr.: ' + recipient2)
      modal.find('#pedidoID').val(recipient)
      modal.find('#pedidoID2').val(recipient2)	  
      modal.find('#recipient-name').val(recipientnome)
      modal.find('#detalhes').val(recipientdetalhes)
    })
</script>  
	<script type="text/javascript">		
		$(document).ready(function() {
			$('#tabelaOC').DataTable( { 
			"stateSave": true,
				"order": [[1, "desc"]],
						"language":{
							"lengthMenu": "Mostrar _MENU_ registros por página",
							"info": "Mostrando pagina _PAGE_ de _PAGES_",
							"infoEmpty": "Sem registros para serem mostrados",
							"infoFiltered": "(filtrada de _MAX_ registros)",
							"loadingRecords": "Lendo...",
							"processing":     "Procesando...",
							"search": "Buscar:",
							"zeroRecords":    "Não se encontra registros com esta caracterísica",
							"paginate": {
								"next":       ">>",
								"previous":   "<<"
						}},                 
							
			"columnDefs": [
				{
					"targets": [ 1 ],
					"visible": false,
					"searchable": false
				}
				
				]
			} );
		} );
		</script>   