<?php

include('conexion.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nombre = $con->real_escape_string(htmlentities($_POST['nombre']));
    $rut = $con->real_escape_string(htmlentities($_POST['rut']));
    $dpto = $con->real_escape_string(htmlentities($_POST['dpto']));
    $ppu = $con->real_escape_string(htmlentities($_POST['ppu']));
    $tipo = $con->real_escape_string(htmlentities($_POST['tipo']));
    $fono = $con->real_escape_string(htmlentities($_POST['fono']));
    $fecin = $con->real_escape_string(htmlentities($_POST['fecin']));
    $fecout = $con->real_escape_string(htmlentities($_POST['fecout']));
    $prop = $con->real_escape_string(htmlentities($_POST['prop']));
    $direccion = $con->real_escape_string(htmlentities($_POST['direccion']));
    $correo = $con->real_escape_string(htmlentities($_POST['correo']));
    $familia1 = $con->real_escape_string(htmlentities($_POST['familia1']));
    $rut_fam1 = $con->real_escape_string(htmlentities($_POST['rut_fam1']));
    $familia2 = $con->real_escape_string(htmlentities($_POST['familia2']));
    $rut_fam2 = $con->real_escape_string(htmlentities($_POST['rut_fam2']));
    $familia3 = $con->real_escape_string(htmlentities($_POST['familia3']));
    $rut_fam3 = $con->real_escape_string(htmlentities($_POST['rut_fam3']));
    $familia4 = $con->real_escape_string(htmlentities($_POST['familia4']));
    $rut_fam4 = $con->real_escape_string(htmlentities($_POST['rut_fam4']));
    $familia5 = $con->real_escape_string(htmlentities($_POST['familia5']));
    $rut_fam5 = $con->real_escape_string(htmlentities($_POST['rut_fam5']));
    $horaIn = $con->real_escape_string(htmlentities($_POST['horaIn']));
    $horaOut = $con->real_escape_string(htmlentities($_POST['horaOut']));


    $file_name = $_FILES['file']['name'];

    $new_name_file = null;

    if ($file_name != '' || $file_name != null) {
        $file_type = $_FILES['file']['type'];
        list($type, $extension) = explode('/', $file_type);
        if ($extension == 'pdf') {
            $dir = 'files/';
            if (!file_exists($dir)) {
                mkdir($dir, 0777, true);
            }
            $file_tmp_name = $_FILES['file']['tmp_name'];
            //$new_name_file = 'files/' . date('Ymdhis') . '.' . $extension;
            $new_name_file = $dir . file_name($file_name) . '.' . $extension;
            if (copy($file_tmp_name, $new_name_file)) {
                
            }
        }
    }
 $ins = $con->query("INSERT INTO files(nombre,rut,dpto,ppu,tipo,fono, prop, fecin, fecout, direccion, correo, familia1, rut_fam1, familia2, rut_fam2, familia3, rut_fam3, familia4, rut_fam4, familia5, rut_fam5, horaIn, horaOut, url) VALUES ('$nombre','$rut','$dpto','$ppu','$tipo', '$fono', '$prop','$fecin','$fecout','$direccion','$correo','$familia1','$rut_fam1', '$familia2','$rut_fam2', '$familia3','$rut_fam3', '$familia4','$rut_fam4', '$familia5','$rut_fam5', '$horaIn', '$horaOut','$new_name_file')");

    if ($ins) {
        echo 'Registro Ingresado';
    } else {
        //echo 'failHH';
    }
} else {
    //echo 'failF2';
}

