<?php

/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace qcbase\controllers;

use Yii;
use qcbase\models\Config;
use yii\console\Request;

/**
 * 后台配置控制器
 *
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class ConfigController extends Controller
{
    public function beforeAction($action)
    {
        $this->enableCsrfValidation = false;
        
        if (parent::beforeAction($action)) {
            return true;
        }
        else {
            return false;
        }

    }

    public function actions()
    {
        return [];
    }

    private function renderConfig($name)
    {
        $data = Config::getConfig($name);

        $model = new $this->model;
        $model->attributes = $data;
        return $this->render($name, [
                    'model' => $model,
        ]);
    }

    /**
     * 基本配置
     */
    public function actionBasic()
    {
        return $this->renderConfig('basic');
    }

    /**
     * 系统配置
     */
    public function actionSystem()
    {
        return $this->renderConfig('system');
    }

    /**
     * 任务配置
     */
    public function actionTask()
    {
        return $this->renderConfig('task');
    }

    /**
     * 任务配置
     */
    public function actionProject()
    {
        return $this->renderConfig('project');
    }

    public function actionReimbursement()
    {
        return $this->renderConfig('reimbursement');
    }

    public function actionApplication()
    {
        return $this->renderConfig('application');
    }

    /**
     * 配置更新
     */
    public function actionUpdate()
    {
        $name = Yii::$app->request->post('config_name');
        $config = Yii::$app->request->post('Config');
        //从数组中去掉id值
        $content = json_encode($config);

        $model = Config::find()
                ->where(['name' => $name])
                ->one();

        if($model === null){
            return $this->flash('配置不存在', 'error');
        } else {
            $model->content = $content;
            if($model->save(false)){
                return $this->flash('更新配置成功', 'success', ['config/' . $model->name]);
            }
        }
    }

    public function actionTestSendMail()
    {
//        $cache_system = \Yii::$app->cache->get('system_config');
//        if(!empty($cache_system)){
//
//            \Yii::$app->mailer->transport = [
//                'class' => 'Swift_SmtpTransport',
//                'host' => $cache_system['mail_host'],
//                'username' => $cache_system['mail_user'],
//                'password' => $cache_system['mail_pwd'],
//                'port' => $cache_system['mail_post'],
//                'encryption' => 'ssl',
//            ];
//            return \Yii::$app->mailer->compose()
//                            ->setFrom($cache_system['mail_user'])
//                            ->setTo('1161635760@qq.com')
//                            ->setSubject('Message subject')
//                            ->setTextBody('Plain text content')
//                            ->setHtmlBody('<b>HTML content</b>')
//                            ->send();
//        }
        \common\models\Email::sendTaskEmail();
    }

}
