<?php 
require_once $_SERVER['DOCUMENT_ROOT'].'/controller/admin/admin_controller.php';


if(!isset($_POST['acao']) OR empty($_POST['acao'])){
    die_error("Não foi possível concluir esta ação.");
}

if ($_POST['acao'] == 'site-smtp') {
    if (
        !isset($_POST['smtp_user']) OR 
        !isset($_POST['smtp_senha']) OR
        !isset($_POST['smtp_email']) OR
        !isset($_POST['smtp_porta']) OR
        !isset($_POST['smtp_host'])) {
        die_error("Preencha todos os campos.");
    }


    $smtp_user  = !empty($_POST['smtp_user'])   ? trim($_POST['smtp_user'])         : NULL;
    $smtp_senha = !empty($_POST['smtp_senha'])  ? $_POST['smtp_senha']              : NULL;
    $smtp_email = !empty($_POST['smtp_email'])  ? trim($_POST['smtp_email'])        : NULL;
    $smtp_porta = !empty($_POST['smtp_porta'])  ? trim($_POST['smtp_porta'])        : NULL;
    $smtp_host  = !empty($_POST['smtp_host'])   ? trim($_POST['smtp_host'])         : NULL;

    //ADICIONAR / EDITAR 
    if (!site_smtp_editar($smtp_user,$smtp_senha,$smtp_email,$smtp_porta,$smtp_host)) {
        die_error("Não foi possível salvar.");
    }

    die_success_reload("Informações salvas.");
}


if($_POST['acao'] == 'site-perfil') {
    if (!isset($_POST['site_nome']) OR empty($_POST['site_nome']) OR
        !isset($_POST['site_keywords']) OR empty($_POST['site_keywords']) OR
        !isset($_POST['site_descricao']) OR empty($_POST['site_descricao']) OR 
        !isset($_POST['site_whatsapp']) OR !isset($_POST['site_telegram']) OR !isset($_POST['site_instagram']) OR 
        !isset($_POST['site_cache']) OR !is_numeric($_POST['site_cache'])) {
        die_error("Preencha todos os campos.");
    }

    $site_nome               = trim(ucwords($_POST['site_nome']));
    $site_keywords           = trim($_POST['site_keywords']);
    $site_descricao          = trim($_POST['site_descricao']);
    $site_whatsapp           = !empty($_POST['site_whatsapp']) ? (valida_whatsapp_telegram($_POST['site_whatsapp']) ? $_POST['site_whatsapp']  : die_error("O número do whatsapp é inválido.")) : NULL;
    $site_telegram           = !empty($_POST['site_telegram']) ? (valida_whatsapp_telegram($_POST['site_telegram']) ? $_POST['site_telegram']  : die_error("O número do telegram é inválido.")) : NULL;
    $site_instagram          = !empty($_POST['site_instagram']) ? (filter_var($_POST['site_instagram'], FILTER_VALIDATE_URL) ? $_POST['site_instagram'] :  die_error("A url do instagram é inválida.")) : NULL;
    $site_cache              = trim($_POST['site_cache']);


    if(!empty($_FILES['site_pdf_crianca']['tmp_name'])){
        $up = upload_arquivo($_FILES['site_pdf_crianca']);
        $site_pdf_crianca = $up['name'];
    }


    if(!site_perfil_editar($site_nome, $site_descricao, $site_keywords, 
                            $site_whatsapp, $site_telegram, $site_instagram, $site_cache)){
         die_error("Não foi possível salvar"); 
    }

    die_success_reload("Informações Salvas.");
}

if($_POST['acao'] == 'site-imagens'){
    if(!isset($_FILES['site_logo']) OR !isset($_FILES['site_favicon'])){
        die_error("Preencha todos os campos.");
    }

    if(!empty($_FILES['site_logo']['tmp_name'])){

        $img = upload_imagem($_FILES['site_logo']);
        if(!atualizar_images_site($img['name'], "site_logo")){
            die_error("Não foi possível salvar.");
        }
        if(is_dir(BASE_IMAGES_PATCH.'system/')){
           @move_uploaded_file($img['tmp_name'], BASE_IMAGES_PATCH.'system/'.$img['name']);
        }
        if(file_exists(BASE_IMAGES_PATCH.'system/'.SITE_LOGO)){
            @unlink(BASE_IMAGES_PATCH.'system/'.SITE_LOGO);
        }
        die_success_reload("A logo do site foi alterada.");

    }

    if(!empty($_FILES['site_favicon']['tmp_name'])){

        $img = upload_imagem($_FILES['site_favicon']);
        if(!atualizar_images_site($img['name'], "site_favicon")){
            die_error("Não foi possível salvar.");
        }
        if(is_dir(BASE_IMAGES_PATCH.'system/')){
            @move_uploaded_file($img['tmp_name'], BASE_IMAGES_PATCH.'system/'.$img['name']);
         }
         if(file_exists(BASE_IMAGES_PATCH.'system/'.SITE_FAVICON)){
             @unlink(BASE_IMAGES_PATCH.'system/'.SITE_FAVICON);
         }

         die_success_reload("O favicon do site foi alterado.");

    }
}
