<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/12/012
 * Time: 11:19
 */

namespace frontend\modules\cp\controllers;

use frontend\models\ArticleCategoryApi;
use frontend\models\behaviors\NoCSRFBehavior;
use frontend\models\ArticleprojectApi;

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

    /**
     * 文章管理
     */
    public function actionArticleSet()
    {
        $api = new ArticleCategoryApi();
        $rs = $api->getArticleCategoryList();
        return $this->renderPartial('article_set', [
            'data' => $rs['data'],
        ]);
    }

    /**
     * 首页管理
     */
    public function actionIndexSet()
    {
        return $this->renderPartial('index_set');
    }

    /**
     * Xcoq金融产品列表
     */
    public function actionList()
    {
        $request = \Yii::$app->request->get();
        $data['id'] = isset($request['id']) ? (int)$request['id'] : '';
        $data['pageNumber'] = isset($request['pageNumber']) ? (int)($request['pageNumber']) : 1;
        $data['pageSize'] = isset($request['pageSize']) ? (int)($request['pageSize']) : 20;
        $data['orderBy'] = isset($request['orderBy']) ? trim($request['orderBy']) : 'create_date';
        $data['order'] = isset($request['order']) ? trim($request['order']) : 'desc';

        $api = new ArticleprojectApi();
        $rs = $api->getProjectList($data);
        return $this->renderPartial('list', [
            'data' => $rs['data'],
            'vars' => $data,
        ]);
    }

    /**
     * 添加页面
     */
    public function actionAdd()
    {
        $isAddAction = true;
        return $this->renderPartial('xcoqproject_add', [
            'xcoqproject' => [],
            'isAddAction' => $isAddAction,
        ]);
    }

    /**
     * 编辑页面
     */
    public function actionEdit()
    {
        $request = \Yii::$app->request->get();
        $data['id'] = isset($request['id']) ? (int)$request['id'] : '';
        $isAddAction = false;
        $api = new ArticleprojectApi();
        $rs = $api->getProjectDetail($data);
        return $this->renderPartial('xcoqproject_add', [
            'xcoqproject' => $rs['data'],
            'isAddAction' => $isAddAction,
        ]);
    }

    /**
     * 更新XCOQ金融产品信息
     */
    public function actionUpdate()
    {
        $request = \Yii::$app->request->post();
        $data['id'] = isset($request['id']) ? (int)$request['id'] : '';
        $data['title'] = isset($request['title']) ? $request['title'] : '';
        $data['content'] = isset($request['content']) ? $request['content'] : '';

        $api = new ArticleprojectApi();
        $rs = $api->updateProject($data);
        if ($rs['code'] == 1) {
            return $this->redirect(['list']);
        }
    }

    /**
     * 删除XCOQ金融产品
     */
    public function actionDelete()
    {
        if (\Yii::$app->request->isAjax) {
            $data['ids'] = \Yii::$app->request->post('ids');
            $api = new ArticleprojectApi();
            $rs = $api->deleteProject($data);
            return $this->asJson($rs);
        }
    }
}
