<?php


namespace backend\controllers;


use backend\base\CommonController;
use backend\base\Help;
use common\models\PushDiscount;
use Yii;


class PushaController extends CommonController
{
    /**
     * PC列表页-优惠推荐&品质美宅
     * @return string
     */
    public function actionIndex()
    {
        return $this->render($this->action->id, ['type' => \Yii::$app->request->get('type')]);
    }

    /**
     * PC列表页-优惠推荐&品质美宅
     * @return mixed
     */
    public function actionIndexform()
    {
        $post = Yii::$app->request->post();
        $model = new PushDiscount();
        $list = $model->getList($post);
        if ($list) {
            foreach ($list as &$val) {
                $val['create_at'] = date('Y-m-d H:i:s', $val['create_at']);
            }
            return Help::JsonData(0, '获取数据成功', $model->countList($post), $list);
        }
        return Help::JsonCode(Help::ERROR, '获取数据失败');
    }

    /**
     * 添加&编辑页面
     * @return string
     */
    public function actionEdit()
    {
        if (!empty(Yii::$app->request->get('status')) && Yii::$app->request->get('status') == 'edit') {
            $row = PushDiscount::findOne(Yii::$app->request->get('id'));
            if ($row) {
                return $this->render($this->action->id, ['row' => $row]);
            }
        }
        return $this->render($this->action->id);
    }

    /**
     * 添加&编辑数据
     * @return mixed
     */
    public function actionEditform()
    {
        $post = Yii::$app->request->post();
        if (!empty($post) && empty($post['id'])) {
            $model = new PushDiscount();
            if ($model->load($post, '') && $model->save()) {
                return Help::JsonCode(Help::SUCCESS, '添加成功');
            }
            return Help::JsonCode(Help::ERROR, '添加失败', $model->errors);

        } else if (!empty($post) && !empty($post['id'])) {
            $row = PushDiscount::findOne($post['id']);
            if ($row->load($post, '') && $row->save()) {
                return Help::JsonCode(Help::SUCCESS, '修改成功');
            }
            return Help::JsonCode(Help::ERROR, '修改失败', $row->errors);
        }
    }

    /**
     * 排序&审核&删除-数据
     * @return mixed
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionSetrow()
    {
        $input = Yii::$app->request->post();
        $row = PushDiscount::findOne($input['id']);
        if ($row != null) {
            switch ($input['type']) {
                case 'del':
                    if ($row->delete()) return Help::JsonCode(Help::SUCCESS, '删除成功');
                    break;
                case 'sort':
                    if (is_numeric($input['sort'])) {
                        $row->sort = $input['sort'];
                    }
                    break;
                case 'show':
                    if ($row['is_show'] == 1) {
                        $row->is_show = 2;
                    } else if ($row['is_show'] == 2) {
                        $row->is_show = 1;
                    }
                    break;
            }
            if ($row->save(false)) return Help::JsonCode(Help::SUCCESS, '操作成功');
        }
        return Help::JsonCode(Help::ERROR, '操作失败', $row->errors);
    }

}