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

$faqsDao = new \Dao\Faqs();
$faqsDao->getFaqPorId($conecta, filter_input(INPUT_POST, 'id'));
$faqsDao->titulo = $conecta->real_escape_string(filter_input(INPUT_POST, 'titulo'));
$faqsDao->subtitulo = addslashes(filter_input(INPUT_POST, 'subtitulo'));
$faqsDao->texto = addslashes(filter_input(INPUT_POST, 'texto'));
$faqsDao->slug = \Dao\Funcoes::removeCharacters($faqsDao->titulo, '-');
$faqsDao->data_cadastro = date('Y-m-d H:i:s');
$faqsDao->usuario_cadastrou = $_SESSION['id_usuario'];
$faqsDao->data_alteracao = date('Y-m-d H:i:s');
$faqsDao->usuario_alterou = $_SESSION['id_usuario'];
if (!empty($_FILES['image']['name'])) {
    if ($faqsDao->image != '' &&file_exists($pasta . $faqsDao->image))
        unlink($pasta . $faqsDao->image);
    
    $faqsDao->image = \Dao\Funcoes::randFileName($_FILES['image']['name']);
    $image = Image::make($_FILES['image']['tmp_name'])->resize(800, null, function ($constraint) {
        $constraint->aspectRatio();
    });

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