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

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

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