<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Emailmng_admin extends \Ciext\Controller\AbstractAdminController
{

    public function __construct()
    {
        parent::__construct();
        $this->load->config('emailmng/config');

        // Load language file
        $this->load->library('gettext_language');
        $this->gettext_language->load(__DIR__ . '/../language/' . $this->config->item('language') . '.po');
    }

    public function index()
    {
        $this->load->model('emailmng/crud_emailmng');

        return array('content' => $this->crud_emailmng->render(), 'title' => $this->lang->line('Templates'));
    }

    public function configuration()
    {
        $this->load->model('emailmng/emailmng_config');
        $content = $this->emailmng_config->render();
        $content .= '<a href="#" class="btn btn-primary" id="check_btn">' . $this->lang->line('Send test email') . '</a>';
        $this->js_css->parse_js('emailmng/js/config.js', ['admin_email' => $this->load->library('ciext/configurator')->set_resource('system')->item('admin_email')]);

        return array('content' => $content, 'title' => $this->lang->line('Configuration'));
    }

    public function check()
    {
        $this->load->library('emailmng/emailmng')->initialize();
        $this->emailmng->to($this->input->post('email'));
        $this->emailmng->message('test email');

        return $this->output->set_content_type('application/json')->set_output(json_encode(['sent' => $this->emailmng->send()]));
    }

}
