<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/4/3
 * Time: 下午5:14
 * 邮箱设置
 */
namespace backend\controllers;
use Yii;
use backend\base\Help;
use backend\base\CommonController;
use common\models\Email;
    class OthermailController extends CommonController
    {
        /*
         * 其他管理-邮箱设置-发件人页面
         * */
        public function actionFhome()
        {
            $url = Yii::$app->basePath . '/config/email.json';
            if (is_file($url)) {
                $data = json_decode(file_get_contents($url), true);
                return $this->render('fhomesave', ['model' => $data]);
            }

            return $this->render('fhome');
        }

        /*
         * 其他管理-邮箱设置-修改and添加数据
         * */
        public function actionHomemailform()
        {
            $url = Yii::$app->basePath . '/config/email.json';
            $input = Yii::$app->request->post('data');
            $data = json_encode([
                    'class' => 'Swift_SmtpTransport',
                    'host' => $input['host'],  //每种邮箱的host配置不一样
                    'username' => $input['username'],
                    'password' => $input['password'],
                    'port' => $input['port'],
//                    'encryption' => 'tls'
                    'encryption' => 'ssl'
                ]
            );
            if (file_put_contents($url, $data) > 0) {
                return Help::JsonCode(Help::SUCCESS, '操作成功');
            }
        }

        /*
         * 其他管理-邮箱设置-邮件发送测试
         * */
        public function actionTestemail()
        {
            $url = Yii::$app->basePath . '/config/email.json';
            $input = Yii::$app->request->post('data');
            if (is_file($url)) {
                $mail = Yii::$app->mailer->compose();
                $mail->setTo($input['collect']);
                $mail->setSubject($input['title']);
                $mail->setTextBody($input['content']);
                if ($mail->send() == true) return Help::JsonCode(Help::SUCCESS, '邮件发送成功');
            }
            return Help::JsonCode(Help::ERROR, '邮件发送失败,请查看邮箱配置是否正确。');

        }

        public function actionAa()
        {
            phpinfo();
        }

        /*
         * 其他管理-邮箱设置-收件人首页
         * */
        public function actionCollecthome()
        {
            return $this->render('collecthome');
        }

        /*
         * 其他管理-邮箱设置-收件人数据列表
         * */
        public function actionCollecthomeform()
        {
            $model = new Email();
            $rows = $model->getList(Yii::$app->request->post());
            if($rows != null)
            {
                return Help::JsonData(0,'成功',$model->Total(),$rows);
            }
                return Help::JsonCode(Help::ERROR,'暂无数据');
        }

        /*
         * 其他管理-邮箱设置-收件人添加页面
         * */
        public function actionCollectadd()
        {
            $model = new \common\models\CategoryCity();
            $rows = $model->getList([]);
            return $this->render('collectadd',['city'=>$rows]);
        }

        /*
         * 其他管理-邮箱设置-收件人添加数据
         * */
        public function actionCollectaddform()
        {
            $input = Yii::$app->request->post('data');
            $model = new \common\models\Email();
            $model->scenario = 'add';
            $models = $model->Authenticator($input);
            if(is_array($models)) return Help::JsonCode(Help::ERROR,'添加失败',$model->errors);
            if($models->save() == true) return Help::JsonCode(Help::SUCCESS,'添加成功');

        }

        /*
         * 其他管理-邮箱设置-收件人删除数据
         * */
        public function actionCollectdel()
        {
            $model = new Email();
            $row = $model->FindById(Yii::$app->request->post('id'));
            if($row != null)
            {
                $row->del = 2;
                if($row->save() == true) return Help::JsonCode(Help::SUCCESS,'操作成功');
            }
                return Help::JsonCode(Help::ERROR,'操作失败');
        }

        /*
         * 其他管理-邮箱设置-收件人修改页面
         * */
        public function actionCollectedit()
        {
            $model = new Email();
            $row = $model->FindById(Yii::$app->request->get('id'));
            if($row != null)
            {
                $cityModel = new \common\models\CategoryCity();
                $city = $cityModel->getList([]);
                return $this->render('collectedit',['model'=>$row,'city'=>$city]);
            }

        }

        /*
         * 其他管理-邮箱设置-收件人修改数据
         * */
        public function actionCollecteditform()
        {
            $input = Yii::$app->request->post('data');
            $model = new Email();

            $model->scenario = 'edit';
            $model->id = $input['id'];
            $auth = $model->Authenticator($input);
            if(is_object($auth))
            {
                $row = $model->FindById($input['id']);
                if($row != null)
                {
                    $row = Help::SetAttr($input,$auth,$row);
                    if($row->save(false) == true) return Help::JsonCode(Help::SUCCESS,'操作成功');
                }
            }
            return Help::JsonCode(Help::ERROR,'操作失败',$auth);
        }


}