<?php


namespace frontend\modules\api\controllers;


use common\components\AjaxException;
use common\models\AccidentCasesScoreComment;
use common\services\AdminLogService;
use frontend\modules\api\components\BaseAdminController;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\Exception;
use yii\web\Response;

class AccidentCasesScoreCommentController extends BaseAdminController
{
    /**
     * 评价用语列表
     * @param int $current
     * @param int $page
     * @return Response
     */
    public function actionList(int $current = 1, int $page = 10)
    {
        //重新整合数据
        $query = AccidentCasesScoreComment::find()
            ->limit($page)
            ->offset(($current - 1) * $page);
        //排序
        $this->initSequence($query);
        //主要筛选条件
        $this->addConditionToQuery("type", $query);
        $this->addConditionToQuery("type_two", $query);
        $this->addConditionToQuery("content", $query, true);
        $this->addConditionToQuery("status", $query);
        //组合数据
        $data['data'] = $query->all();
        $data['total'] = $query->count();
        return $this->asJson($data);
    }

    /**
     * 评价用语显示列表
     * @return Response
     */
    public function actionShowList()
    {
        //重新整合数据
        $query = AccidentCasesScoreComment::find()->where(["status" => 1]);
        //排序
        $this->initSequence($query);
        //主要筛选条件
        $this->addConditionToQuery("type", $query);
        $this->addConditionToQuery("type_two", $query);
        $this->addConditionToQuery("content", $query, true);
        //组合数据
        $data['data'] = $query->all();
        return $this->asJson($data);
    }

    /**
     * 评价用语修改
     * @return Response
     * @throws AjaxException
     * @throws Exception
     * @throws InvalidConfigException
     */
    public function actionUpdate()
    {
        $post = Yii::$app->request->post();
        $model = AccidentCasesScoreComment::findOne($post["id"]);
        if (!$model) {
            throw new AjaxException("评价用语不存在");
        }
        $origin = AdminLogService::getOrigin($model, $this->getAllParams());
        $model->type = $post["type"];
        $model->type_two = $post["type_two"];
        $model->content = $post["content"];
        $model->status = $post["status"];
        if (!$model->save()) {
            throw new Exception($model->getErrorSummary(true)[0]);
        }
        AdminLogService::saveLogWithUpdateHistory($origin, $model, 0, "", $this->getAllParams());
        $data = [];
        return $this->asJson($data);
    }

    /**
     * 评价用语添加
     * @return Response
     * @throws Exception
     * @throws InvalidConfigException
     */
    public function actionAdd()
    {
        $post = Yii::$app->request->post();
        $param = [
            "type" => $post["type"],
            "type_two" => $post["type_two"],
            "content" => $post["content"],
            "status" => $post["status"],
        ];
        $model = new AccidentCasesScoreComment($param);
        $origin = AdminLogService::getOrigin($model, $this->getAllParams(), true);
        if (!$model->save()) {
            throw new Exception($model->getErrorSummary(true)[0]);
        }
        AdminLogService::saveLogWithUpdateHistory($origin, $model, 0, "", $this->getAllParams());
        $data = [];
        return $this->asJson($data);
    }
}