<?php
/**
 * Created by PhpStorm.
 * User: chenkuan
 * Date: 2017/12/14
 * Time: 下午4:48
 */

namespace frontend\modules\cp\controllers;


use frontend\models\ArticleCategoryApi;
use frontend\models\behaviors\NoCSRFBehavior;
use yii\helpers\Url;

class ArticleCategoryController extends BaseController
{
    public function behaviors()
    {
        return [
            'csrf' => [
                'class' => NoCSRFBehavior::className(),
                'controller' => $this,
                'actions' => [
                    'delete'
                ],
            ],
        ];
    }

    /**
     * 获取文章分类列表
     */
    public function actionList()
    {
        $api = new ArticleCategoryApi();
        $rs = $api->getArticleCategoryList();
        return $this->renderPartial('list', [
            'data' => $rs['data'],
        ]);
    }

    /**
     * 新增文章分类
     */
    public function actionAdd()
    {
        $api = new ArticleCategoryApi();
        $articleCategoryTreeList = $api->getArticleCategoryTreeList();
        $isAddAction = true;

        $data = [
            'articleCategory' => [],
            'isAddAction' => $isAddAction,
            'articleCategoryTreeList' => $articleCategoryTreeList['data'],
        ];
        return $this->renderPartial('add', $data);
    }

    /**
     * 新增文章保存
     */
    public function actionSave()
    {
        $request = \Yii::$app->request->post();
        $request['id'] = isset($request['id']) ? $request['id'] : '';

        if ($request['parent_id']) {
            // 不是顶级分类
            $request['grade'] = 1;
        } else {
            $request['grade'] = 0;
        }

        if ($request['language'] == 'china') {
            $request['language'] = 0;
        } elseif ($request['language'] == 'english') {
            $request['language'] = 1;
        }

        $api = new ArticleCategoryApi();
        $rs = $api->articleCategorySave($request);
        if ($rs['code'] == 1) {
            return $this->redirect(['list']);
        } else {
            if (empty($request['id'])) {
                $redirectUrl = Url::to(['/cp/article-category/add']);
            } else {
                $redirectUrl = Url::to(['/cp/article-category/edit?id='.$request['id']]);
            }
            return $this->redirect(['error/index?redirectUrl='.$redirectUrl.'&tip='.$rs['message']]);
        }
    }

    /**
     * check-sign
     */
    public function actionCheckSign()
    {
        $request = \Yii::$app->request->get();
        $data['oldValue'] = isset($request['oldValue']) ? $request['oldValue'] : '';
        $data['sign'] = isset($request['sign']) ? $request['sign'] : '';

        $api = new ArticleCategoryApi();
        $rs = $api->checkSign($data);
        if ($rs['code'] == 1) {
            return $this->asJson($rs['data']['isSuccess']);
        }
    }

    /**
     * 编辑文章分类
     */
    public function actionEdit()
    {
        $request = \Yii::$app->request->get();
        $data = [];
        $data['id'] = isset($request['id']) ? (int)$request['id'] : '';
        $isAddAction = false;
        $api = new ArticleCategoryApi();
        $detail = $api->articleCategoryDetail($data);

        return $this->renderPartial('add', [
            'isAddAction' => $isAddAction,
            'articleCategory' => $detail['data'],
        ]);
    }

    /**
     * 删除文章分类
     */
    public function actionDelete()
    {
        $data['id'] = \Yii::$app->request->get('id');
        $api = new ArticleCategoryApi();
        $rs = $api->articleCategoryDelete($data);
        if ($rs['code'] == 1) {
            return $this->redirect(['list']);
        } else {
            $redirectUrl = Url::to(['/cp/article-category/list']);
            return $this->redirect(['error/index?redirectUrl='.$redirectUrl.'&tip='.$rs['message']]);
        }
    }
}
