<?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 backend\server\UploadFile;
use common\models\Link;
use common\models\About;

class OthersitesetController extends CommonController
{
    /*
     * 其他管理-网站设置-首页
     * */
    public function actionHome()
    {
        $model = new \common\models\Config();
        $row = $model->FindById();
        if ($row != null) {
            $row['logo'] = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['setsite'] . $row['logo'];
            $row['wechat'] = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['setsite'] . $row['wechat'];
            return $this->render('sethome', ['model' => $row]);
        }
        return $this->render('home');
    }

    /*
     * 其他管理-网站设置-修改网站数据
     * */
    public function actionEditform()
    {

        $model = new \common\models\Config();
        $row = $model->FindById();
        $input = Yii::$app->request->post();
        $url = Yii::$app->params['img_url']['setsite'];
        $model->load($input, '');
        if (!$model->validate()) return Help::JsonCode(Help::ERROR, '失败', $model->errors);
        if ($row == null) {

            $model->wechat = UploadFile::InstanceImgName('wechat', $url);
            $model->logo = UploadFile::InstanceImgName('logo', $url);
            if ($model->save(false) == true) return Help::JsonCode(Help::SUCCESS, '成功');

        } else {

            $wechat = UploadFile::InstanceImgName('wechat', $url);
            if ($wechat != false) {
                UploadFile::delImg($url, $row->wechat);
                $row->wechat = $wechat;
            }

            $logo = UploadFile::InstanceImgName('logo', $url);

            if ($logo != false) {
                UploadFile::delImg($url, $row->logo);
                $row->logo = $logo;
            }
            $row = Help::SetAttr($input, $model, $row);
            if ($row->save(false) == true) return Help::JsonCode(Help::SUCCESS, '成功');
        }

    }


    //==========================友情链接============================

    /*
     * 友情链接首页页面
     * */
    public function actionFriendshiplink()
    {

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

    /*
     * 友情链接首页数据
     * */
    public function actionFriendshiplinkform()
    {
        $model = new Link();
        $rows = $model->getList(Yii::$app->request->post());
        if ($rows != null) {
            return Help::JsonData(0, '成功', $model->Total(), $rows);
        }
    }

    /*
     * 友情链接添加数据页面
     * */
    public function actionFriendshiplinkadd()
    {
        $model = new \common\models\CategoryCity();
        $city = $model->getList([]);

        $frontMenu = new \common\models\SysmenuQ();
        $menu = $frontMenu->getList([]);

        return $this->render('friendshiplinkadd', ['city' => $city, 'menu' => $menu]);
    }


    /*
     * 友情链接添加数据
     * */
    public function actionFriendshiplinkaddform()
    {
        $model = new Link();

        $input = Yii::$app->request->post('data');
        $link = $model->Authenticator($input);
        if (is_object($link)) {
            if ($link->save() == true) return Help::JsonCode(Help::SUCCESS, '添加成功');
        }
        return Help::JsonCode(Help::ERROR, '添加失败');
    }

    /*
     * 友情链接修改数据页面
     * */
    public function actionFriendshiplinkedit()
    {
        $model = new Link();
        $row = $model->FindById(Yii::$app->request->get('id'));
        if ($row != null) {
            $model = new \common\models\CategoryCity();
            $city = $model->getList([]);

            $frontMenu = new \common\models\SysmenuQ();
            $menu = $frontMenu->getList([]);

            return $this->render('friendshiplinkedit', ['model' => $row, 'city' => $city, 'menu' => $menu]);
        }
    }

    /*
     * 友情链接修改数据
     * */
    public function actionFriendshiplinkeditform()
    {
        $model = new Link();
        $input = Yii::$app->request->post('data');
        $link = $model->Authenticator($input);
        if (is_object($link)) {
            $row = $model->FindById($input['id']);
            if ($row != null) {
                $setAttr = Help::SetAttr($input, $link, $row);
                if ($setAttr->save() == true) return Help::JsonCode(Help::SUCCESS, '成功');
            }

        }
        return Help::JsonCode(Help::ERROR, '失败', $link);
    }

    /*
     * 删除友情链接
     * */
    public function actionFriendshiplinkdel()
    {
        $model = new Link();
        $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 actionHomewatermark()
    {
        $query = \common\models\CategoryWatermark::find()->asArray()->one();
        $positionArr = [
            1 => '#1',
            2 => '#2',
            3 => '#3',
            4 => '#4',
            5 => '#5',
            6 => '#6',
            7 => '#7',
            8 => '#8',
            9 => '#9',
        ];
        if ($query != null) {

            $query['template'] = Yii::$app->params['watermark_img']['url'] . $query['template'];
            return $this->render('homewatermark', ['model' => $query, 'position' => $positionArr]);
        } else {
            return $this->render('homewatermarkadd', ['position' => $positionArr]);

        }
    }

    /*
     * 其他管理-添加 and 修改,水印设置
     * */
    public function actionEditwatermarkform()
    {

        $model = new \common\models\CategoryWatermark();
        $query = $model::find()->one();
        $input = Yii::$app->request->post();
        $url = Yii::$app->params['watermark_img']['url'];
        $model->load($input, '');
        $model->validate();
        $img = \backend\server\UploadFile::InstanceImgName('template', $url);
        if ($query == null) {
            if ($img != false) {
                $model->template = $img;
            }
            $result = $model->save(false);
        } else {
            $setAtt = Help::SetAttr($input, $model, $query);
            if ($img != false) {
                $setAtt->template = $img;
            }
            if (!isset($input['status'])) {
                $setAtt->status = 2;
            }
            $result = $setAtt->update(false);
        }


        if ($result == true) return Help::JsonCode(Help::SUCCESS, '操作成功');
        return Help::JsonCode(Help::ERROR, '操作失败', $model->errors);
    }

    //==========================关于我们============================

    public function actionAbout()
    {
        $model = new About();
        $about = $model->getList();
        if (!empty($about)) {
            return $this->render('about', ['about' => $about]);
        }
        return $this->render('about');
    }

    /*
    * 其他管理-关于我们-添加关于我们
    * */
    public function actionAddform()
    {
        $model = new About();
        $input = Yii::$app->request->post();
        $rows = $model->Authenticator($input);

        if (is_object($rows)) {
            if (isset($input['id']) && !empty($input['id'])) {
                $row = $model->FindById($input['id']);
                $authMoel = Help::SetAttr($input, $rows, $row);
                if ($authMoel->update() == true) return Help::JsonCode(Help::SUCCESS, '修改成功');
            } else {
                if ($rows->save() == true) return Help::JsonCode(Help::SUCCESS, '添加成功');
            }
        }
    }

}