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

namespace frontend\modules\cp\controllers;


use frontend\models\AdvantageRuleApi;
use frontend\models\behaviors\NoCSRFBehavior;

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

    /**
     * 首页优势广告列表
     */
    public function actionList()
    {
        $request = \Yii::$app->request->get();
        $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 AdvantageRuleApi();
        $rs = $api->getRuleList($data);
        return $this->renderPartial('list', [
            'data' => $rs['data'],
            'vars' => $data,
        ]);
    }

    /**
     * 编辑首页优势广告页面
     */
    public function actionEdit()
    {
        $request = \Yii::$app->request->get();
        $data['id'] = isset($request['id']) ? (int)$request['id'] : '';
        $isAddAction = false;
        $api = new AdvantageRuleApi();
        $rs = $api->getRuleDetail($data);
        return $this->renderPartial('advantage_edit', [
            'advantageRule' => $rs['data'],
            'isAddAction' => $isAddAction,
        ]);
    }

    /**
     * 更新首页优势广告信息
     */
    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 AdvantageRuleApi();
        $rs = $api->updateRule($data);
        if ($rs['code'] == 1) {
            return $this->redirect(['list']);
        }
    }

    /**
     * 删除首页优势广告
     */
    public function actionDelete()
    {
        if (\Yii::$app->request->isAjax) {
            $data['ids'] = \Yii::$app->request->post('ids');
            $api = new AdvantageRuleApi();
            $rs = $api->deleteRule($data);
            return $this->asJson($rs);
        }
    }
}
