<?php
    require 'conexion.php';
    require_once('functions.php');
	include(HEADER_TEMPLATE); 
    
	$id_pedido = $_GET['id'];
	$route = $_GET['route'];
	
	if (($route) == 'riscos') {
		$titulo = "RISCOS";
	} else {
		$titulo = "FORRO";
	}
    $sqlTotRiscos = "SELECT titulo, obs_grade, ajuste, SUM(tda) as stda, SUM(tqa) as stqa, SUM(tsa) as stsa, SUM(toa) as stoa, SUM(qua) as squa, SUM(qda) as sqda, SUM(qqa) as sqqa, SUM(qsa) as sqsa, SUM(qoa) as sqoa,SUM(cia) as scia,SUM(cda) as scda, (SUM(tda) + SUM(tqa) + SUM(tsa) + SUM(toa) + SUM(qua) + SUM(qda) + SUM(qqa) + SUM(qsa) + SUM(qoa) + SUM(cia) + SUM(cda) ) as subtotal FROM grade WHERE grade.id_pedido = '$id_pedido' AND grade.titulo = 'RISCOS'";
    $resultadoTotRiscos = $mysqli->query($sqlTotRiscos);
    $row = $resultadoTotRiscos->fetch_array(MYSQLI_ASSOC);

?>
<script> 
function soma() 
{
    
formPedido.subtotal.value = (formPedido.tda.value*1) + (formPedido.tqa.value*1) + (formPedido.tsa.value*1) + (formPedido.toa.value*1) + (formPedido.qua.value*1) + (formPedido.qda.value*1) + (formPedido.qqa.value*1) + (formPedido.qsa.value*1) + (formPedido.qoa.value*1) + (formPedido.cia.value*1) + (formPedido.cda.value*1)

}
</script>

            <div class="row">
				<h3 style="text-align:center">Adiciona Grade - ORDEM DE CORTE</h3>
			</div>

			<br>
			
        <form name="formPedido" method="POST" action="grade_grava.php" autocomplete="off">

            <div class="row">
                   
                <input type="hidden" id="ativo" name="ativo" value="1" />
                <input type="hidden" id="id_pedido" name="id_pedido" value="<?php echo $id_pedido; ?>" />
                <input type="hidden" id="dir" name="dir" value="2" />
                
                <div class="row">
                    <div class="form-group col-sm-1">
                        <label for="xtda">ID Pedido</label>
                        <input type="text" class="form-control" id="id_pedido" name="id_pedido" value="<?php echo $id_pedido; ?>" readonly="readonly">
                    </div>
			
                    <div class="form-group col-md-2">
                        <label for="nr_company">Grade</label>
						<input type="text" class="form-control" id="titulo" name="titulo" value="<?php echo $titulo; ?>" readonly="readonly">						
                    </div>
					
                    <div class="form-group col-sm-7">
                        <label for="obs_grade">Nome do Corte</label>
                        <input type="text" class="form-control" id="obs_grade" name="obs_grade" required style='text-transform:uppercase' required autofocus>
                    </div>
					
                </div>

                <div class="row">
                
                    <h4 style="text-align:center">Grade Cortada - RISCOS</h4>
                    <table class="table table-condensed  table-bordered">
                        <thead>
                          <tr bgcolor="#d6d6d6">
                            <th><h4 style="text-align:center"><?php echo $row['stda'] ?></h4></th>
                            <th><h4 style="text-align:center"><?php echo $row['stqa'] ?></h4></th>
                            <th><h4 style="text-align:center"><?php echo $row['stsa'] ?></h4></th>
                            <th><h4 style="text-align:center"><?php echo $row['stoa'] ?></h4></th>
                            <th><h4 style="text-align:center"><?php echo $row['squa'] ?></h4></th>
                            <th><h4 style="text-align:center"><?php echo $row['sqda'] ?></h4></th>
                            <th><h4 style="text-align:center"><?php echo $row['sqqa'] ?></h4></th>
                            <th><h4 style="text-align:center"><?php echo $row['sqsa'] ?></h4></th>
                            <th><h4 style="text-align:center"><?php echo $row['sqoa'] ?></h4></th>
                            <th><h4 style="text-align:center"><?php echo $row['scia'] ?></h4></th>
                            <th><h4 style="text-align:center"><?php echo $row['scda'] ?></h4></th>
                            <th><h4 style="text-align:center"><?php echo $row['subtotal'] ?></h4></th>
                          </tr>
                        </thead>
                    </table>

                </div>
                <div class="row">
                    <div class="form-group col-md-1">
                        <label for="tda">32</label>
                        <input type="number" class="form-control" id="tda" name="tda" onBlur="soma();">
                    </div>
                    <div class="form-group col-md-1">
                        <label for="tqa">34</label>
                        <input type="number" class="form-control" id="tqa" name="tqa" onBlur="soma();">
                    </div>
                    <div class="form-group col-md-1">
                        <label for="tsa">36</label>
                        <input type="number" class="form-control" id="tsa" name="tsa" onBlur="soma();">
                    </div>
                    <div class="form-group col-md-1">
                        <label for="toa">38</label>
                        <input type="number" class="form-control" id="toa" name="toa" onBlur="soma();">
                    </div>
                    <div class="form-group col-md-1">
                        <label for="qua">40</label>
                        <input type="number" class="form-control" id="qua" name="qua" onBlur="soma();">
                    </div>
                    <div class="form-group col-md-1">
                        <label for="qda">42</label>
                        <input type="number" class="form-control" id="qda" name="qda" onBlur="soma();">
                    </div>
                    <div class="form-group col-md-1">
                        <label for="qqa">44</label>
                        <input type="number" class="form-control" id="qqa" name="qqa" onBlur="soma();">
                    </div>
                    <div class="form-group col-md-1">
                        <label for="qsa">46</label>
                        <input type="number" class="form-control" id="qsa" name="qsa" onBlur="soma();">
                    </div>
                    <div class="form-group col-md-1">
                        <label for="qoa">48</label>
                        <input type="number" class="form-control" id="qoa" name="qoa" onBlur="soma();">
                    </div>
                    <div class="form-group col-md-1">
                        <label for="cia">50</label>
                        <input type="number" class="form-control" id="cia" name="cia" onBlur="soma();">
                    </div>
                    <div class="form-group col-md-1">
                        <label for="cda">52</label>
                        <input type="number" class="form-control" id="cda" name="cda" onBlur="soma();">
                    </div>
                    <div class="form-group col-md-1">
                        <label for="subtotal">Subtotal</label>
                        <input type="number" class="form-control" id="subtotal" name="subtotal" readonly="readonly">
                    </div>
                </div>
                
                <div class="row">                
                    <div class="form-group">
                        <div class="col-xs-6 col-xs-offset-3 col-sm-4 col-sm-offset-0">
                            <a href="javascript:window.history.go(-1)" class="btn btn-info">Voltar</a>
                            <button type="submit" class="btn btn-danger">GRAVA GRADE</button>
                        </div>
                    </div>
                </div>
            </div>
                
        </form>
            

<?php include(FOOTER_TEMPLATE); ?>        
  
