<?php
	
	require 'conexion.php';
    
    $type = $_POST['type'];
	$company = $_POST['company'];
	$cpf_cnpj = $_POST['cpf_cnpj'];
	$popular_name = $_POST['popular_name'];
	$materiais = $_POST['material'];
    $address = $_POST['address'];
	$hood = $_POST['hood'];
	$zip_code = $_POST['zip_code'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$ie = $_POST['ie'];
	$contact1 = $_POST['contact1'];
	$email1 = $_POST['email1'];
	$phone1 = $_POST['phone1'];
	$cell1 = $_POST['cell1'];
	$contact2 = $_POST['contact2'];
	$email2 = $_POST['email2'];
	$phone2 = $_POST['phone2'];
	$cell2 = $_POST['cell2'];
	$contact3 = $_POST['contact3'];
	$email3 = $_POST['email3'];
	$phone3 = $_POST['phone3'];
	$cell3 = $_POST['cell3'];
	$obs = $_POST['obs'];
	$ativo = isset($_POST['ativo']) ? $_POST['ativo'] : 0;
    
    $material = '';
    
    foreach($materiais as $k=>$v){
        $material .= $v . ", ";
    }
    $material = trim($material, ', '); 
    
    //echo $material;

    $duplicado = "SELECT * FROM companies WHERE companies.type = 'F' AND companies.cpf_cnpj = '$cpf_cnpj'";
    $resultadox = $mysqli->query($duplicado);
    $simnao = $resultadox->num_rows;
?>
<?php    
    IF ($simnao > 0) {
?>
        <div class="alert alert-warning">
            <strong>ATENÇÃO!</strong> Este Fornecedor com este tipo de Artigo escolhido ja existe na base de dados.
        </div>
<?php
        } ELSE {
        $sql = "INSERT INTO companies (material, type, company, cpf_cnpj, popular_name, address, hood, zip_code, city, state, ie, contact1, email1, phone1, cell1, contact2, email2, phone2, cell2, contact3, email3, phone3, cell3, obs, ativo) VALUES ( (UPPER('$material')), (UPPER('$type')), (UPPER('$company')), (UPPER('$cpf_cnpj')), (UPPER('$popular_name')), (UPPER('$address')), (UPPER('$hood')), (UPPER('$zip_code')), (UPPER('$city')), (UPPER('$state')), (UPPER('$ie')), (UPPER('$contact1')), (UPPER('$email1')), (UPPER('$phone1')), (UPPER('$cell1')), (UPPER('$contact2')), (UPPER('$email2')), (UPPER('$phone2')), (UPPER('$cell2')), (UPPER('$contact3')), (UPPER('$email3')), (UPPER('$phone3')), (UPPER('$cell3')), (UPPER('$obs')), '$ativo' )";
        $resultado = $mysqli->query($sql);
    
        }
?>
<META HTTP-EQUIV=REFRESH CONTENT = '0;URL=companies_index.php'>
