<?php
    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';
	
	$id = $_POST['id'];
	
    // seleciona todos os registros da base com inner join cliente
	$sql = "SELECT pilotos.*, companies.popular_name FROM `companies` INNER JOIN pilotos ON companies.id = pilotos.nr_company WHERE pilotos.id = '$id'";
	$resultado = $mysqli->query($sql);
	$row = $resultado->fetch_array(MYSQLI_ASSOC);
	//print_r($row);

    // 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);
	
    $query_tecidos = "SELECT * from tecidos ORDER BY tecido";
	$resultado_tecidos=$mysqli->query($query_tecidos);	
	

    require_once('functions.php');
	include(HEADER_TEMPLATE);
?>

			<div class="row">
				<h3 style="text-align:center">Edição dos dados do Piloto</h3>
			</div>
			
   			<form method="POST" action="piloto_update.php" autocomplete="off">
				
				<input type="hidden" id="id" name="id" value="<?php echo $row['id']; ?>" />
                <input type="hidden" id="type" name="type" value="P" />			
                
                <div class="row">
                    <div class="form-group col-md-1">
                        <label for="lacre">Lacre</label>
                        <input type="text" onkeyup="somenteNumeros(this);" class="form-control" id="lacre" name="lacre" value="<?php echo $row['lacre']; ?>">
                    </div>                        
                    
                    <div class="form-group col-md-2">
                        <label for="stdate">Data de Cadastro</label>
                        <input type="date" class="form-control" name="stdate" id="stdate" value="<?php echo $row['stdate'] ?>">
                    </div>
                    
                    <div class="form-group col-md-3">
                      <label for="dlvdate">Cliente</label>
                        <input type="text" class="form-control" id="nr_company" name="nr_company" value="<?php echo $row['popular_name']; ?>" readonly="readonly">                      
                    </div>
                    
                    <div class="form-group col-md-4">
                        <label for="nr_company">Altera cliente</label>
                        <select class="form-control" id="nr_company" name="nr_company">
                            <option value="<?php echo $row['nr_company']; ?>">muda cliente</option>
                            <?php
                             $resultado_clientes2=$mysqli->query($query_clientes);
                             while($row2 = $resultado_clientes2->fetch_assoc()) { ?>
                                <option value="<?php echo $row2['id']; ?>"><?php echo $row2['popular_name']; ?></option>
                            <?php } ?>
                        </select>
                    </div>
                    
                    <div class="form-group col-md-2">
                        <label for="reference">Referência</label>
                        <input type="text" class="form-control" id="reference" name="reference" style='text-transform:uppercase' value="<?php echo $row['reference']; ?>" autofocus>
                    </div>                        

                </div>

                <div class="row">
                   
                    <div class="form-group col-md-2">
                      <label for="model">Modelo</label>
                        <input type="text" class="form-control" id="model" name="model" value="<?php echo $row['model']; ?>"  style='text-transform:uppercase'>                      
                    </div>
                   
                    <div class="form-group col-md-2">
                      <label for="modelagem">Modelagem</label>
                        <input type="text" class="form-control" id="modelagem" name="modelagem" value="<?php echo $row['modelagem']; ?>" style='text-transform:uppercase'>                      
                    </div>

                    <div class="form-group col-md-4">
                        <label for="tecido">Selecione Tecido</label>
                        <select class="form-control" id="tecido" name="tecido"  required>
                            <option value="<?php echo $row['tecido']; ?>"><?php echo $row['tecido']; ?></option>
                            <?php
                             $resultado_tecidos=$mysqli->query($query_tecidos);
                             while($row2 = $resultado_tecidos->fetch_assoc()) { ?>
                                <option value="<?php echo $row2['id_tecido']; ?> - <?php echo $row2['tecido']; ?> - <?php echo $row2['composicao']; ?>"><?php echo $row2['id_tecido']; ?> - <?php echo $row2['tecido']; ?> - <?php echo $row2['fornecedor']; ?> - <?php echo $row2['composicao']; ?> - <?php echo $row2['cor']; ?></option>
                            <?php } ?>
                        </select>
                    </div>


                    <div class="form-group col-md-2">
                        <label for="colors">Cores</label>
                        <input type="text" class="form-control" id="colors" name="colors" value="<?php echo $row['colors']; ?>" style='text-transform:uppercase'>
                    </div>

                    <div class="form-group col-md-2">
                        <label for="linha">Linha</label>
                        <input type="text" class="form-control" id="linha" name="linha" value="<?php echo $row['linha']; ?>"  style='text-transform:uppercase'>
                    </div>
                    
                    
                </div> 
                <div class="row">
					<div class="form-group col-md-2">
                        <label for="values">Cintura</label>
                        <input type="number" step="1" class="form-control" id="cintura" name="cintura" value="<?php echo $row['cintura']; ?>" style='text-transform:uppercase'>
                    </div>
					
                    <div class="form-group col-md-2">
                        <label for="valued">Tamanho</label>
                        <input type="number" step="1" class="form-control" id="tamanhoPiloto" name="tamanhoPiloto" value="<?php echo $row['tamanhoPiloto']; ?>" style='text-transform:uppercase'>
                    </div>				
                    <div class="form-group col-md-2">
                      <label for="dlvdate">Data de Entrega</label>
                        <input type="date" class="form-control" id="dlvdate" name="dlvdate" value="<?php echo $row['dlvdate']; ?>">                      
                    </div>
                   
                    <div class="form-group col-md-2">
                        <label for="approvedby">Aprovado por</label>
                        <input type="text" class="form-control" id="approvedby" name="approvedby" value="<?php echo $nome_user;?>" readonly="readonly">
                    </div>
                    
                    <div class="form-group col-md-2">
                      <label for="dtapproved">Data da Aprovação</label>
                        <input type="date" class="form-control" id="dtapproved" name="dtapproved" value="<?php echo date("Y-m-d") ?>">                          
                    </div>

                    <div class="form-group col-md-2">
                        <label for="value">Valor</label>
                        <input type="number" step="0.01" class="form-control" id="value" name="value" value="<?php echo $row['value']; ?>">
                    </div>
                                     
                    <div class="form-group col-md-2">
                        <label for="ativo">Piloto ATIVO?</label>
                        
                        <div class="form-group">
                            
                            <label class="radio-inline  col-md-3">
                                <input type="radio" id="ativo" name="ativo" value="1" <?php if($row['ativo']=='1') echo 'checked'; ?>> SIM
                            </label>
                            
                            <label class="radio-inline">
                                <input type="radio" id="ativo" name="ativo" value="0" <?php if($row['ativo']=='0') echo 'checked'; ?>> NÃO
                            </label>
                        </div> 
                    </div>
                </div>

                <div class="row">

                    <div class="form-group col-sm-12 control-label">
                        <label for="obs">Observações do PILOTO</label>
                        <div class="">
                            <textarea name="obs" class="form-control" rows="4" cols="70" style='text-transform:uppercase'><?php echo $row['obs']?></textarea>
                        </div>
                    </div>

                </div>
               
                <div class="row">                
                    <div class="form-group">
                        <div class="col-xs-6 col-xs-offset-10 col-sm-8 col-sm-offset-0">
                            <a href="piloto_index.php" class="btn btn-primary">Voltar</a>
                            <button type="submit" class="btn btn-primary">Gravar</button>
                        </div>
                    </div>
                </div>
            </form>
		</div>
    <?php include(FOOTER_TEMPLATE); ?>        
 <script>
    function somenteNumeros(num) {
        var er = /[^0-9.]/;
        er.lastIndex = 0;
        var campo = num;
        if (er.test(campo.value)) {
          campo.value = "";
        }
    }
 </script>
