<?php
use Intervention\Image\ImageManagerStatic as Image;

$contatosDao = new \Dao\Contatos();
$contatosDao->getContatoPorId($conecta, filter_input(INPUT_POST, 'id'));
$contatosDao->data = implode('-', array_reverse(explode('/', filter_input(INPUT_POST, 'data'))));
$contatosDao->nome = $conecta->real_escape_string(filter_input(INPUT_POST, 'nome'));
$contatosDao->telefone = addslashes(filter_input(INPUT_POST, 'telefone'));
$contatosDao->empresa = addslashes(filter_input(INPUT_POST, 'empresa'));
$contatosDao->fk_id_categoria = $conecta->real_escape_string(filter_input(INPUT_POST, 'fk_id_categoria'));
$contatosDao->texto = addslashes(filter_input(INPUT_POST, 'texto'));
$contatosDao->slug = \Dao\Funcoes::removeCharacters($contatosDao->nome, '-');
$contatosDao->data_cadastro = date('Y-m-d H:i:s');
$contatosDao->usuario_cadastrou = $_SESSION['id_usuario'];
$contatosDao->data_alteracao = date('Y-m-d H:i:s');
$contatosDao->usuario_alterou = $_SESSION['id_usuario'];
if (!empty($_FILES['image']['name'])) {
    if ($contatosDao->image != '' &&file_exists($pasta . $contatosDao->image))
        unlink($pasta . $contatosDao->image);
    
    $contatosDao->image = \Dao\Funcoes::randFileName($_FILES['image']['name']);
    $image = Image::make($_FILES['image']['tmp_name'])->resize(800, null, function ($constraint) {
        $constraint->aspectRatio();
    });

    $image->save($pasta . $contatosDao->image, 85);
}
$res = json_decode($contatosDao->salvar($conecta));
if ($res->data == 'Success') 
    \Dao\Funcoes::displayMessage('Confirmação', $res->message, 'contatos.php');
else 
    \Dao\Funcoes::displayMessage('Erro', $res->message, 'contatos.php', 'red');