<?php


namespace frontend\modules\api\controllers;


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

class EventOverviewController extends BaseAdminController
{
    /**
     * 事件概览列表
     * @param int $accident_id
     * @return Response
     */
    public function actionList(int $accident_id)
    {
        $query = EventOverviewService::getEventOverviewQuery()->andWhere(["accident_id" => $accident_id]);
        //排序
        $this->initSequence($query, "start_time desc,id desc");
        //组合数据
        $data['data'] = EventOverviewService::dealwithList($query->all(), $accident_id);
        return $this->asJson($data);
    }

    /**
     * 时间概览添加
     * @return Response
     * @throws AjaxException
     * @throws Exception
     * @throws InvalidConfigException
     */
    public function actionAdd()
    {
        $post = Yii::$app->request->post();
        $case = CaseService::checkIsPublish($post["accident_id"], $this->userInfo);
        $param = [
            "accident_id" => $post["accident_id"],
            "assess" => $post["assess"],
            "assess_two" => $post["assess_two"],
            "content" => $post["content"],
            "important_content" => $post["important_content"],
            "is_important" => $post["is_important"] ? 1 : 0,
            "is_time_period" => isset($post["is_time_period"]) && $post["is_time_period"] ? 1 : 0,
            "end_time" => isset($post["end_time"]) && $post["end_time"] !== "" ? $post["end_time"] : 0,
            "source_data" => $post["source_data"],
            "start_time" => $post["start_time"],
        ];
        $model = new EventOverview($param);
        $origin = AdminLogService::getOrigin($model, $this->getAllParams(), true);
        if (!$model->save()) {
            throw new Exception($model->getErrorSummary(true)[0]);
        }
        AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $this->getAllParams());
        $data = [];
        return $this->asJson($data);
    }

    /**
     * 事件概览修改
     * @param $id
     * @return Response
     * @throws AjaxException
     * @throws InvalidConfigException
     */
    public function actionUpdate($id)
    {
        $model = EventOverviewService::getEventOverviewById($id);
        $origin = AdminLogService::getOrigin($model, $this->getAllParams());
        $case = CaseService::checkIsPublish($model->accident_id, $this->userInfo);
        $this->setAttributeFromGetAndPost($model);
        if ($model->save()) {
            AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $this->getAllParams());
            return $this->asJson();
        } else {
            return $this->returnError($model->getErrorSummary(true)[0]);
        }
    }

    /**
     * 事件概览删除
     * @param $id
     * @return Response
     * @throws AjaxException
     * @throws Exception
     */
    public function actionDelete($id)
    {
        $model = EventOverviewService::getEventOverviewById($id);
        $case = CaseService::checkIsPublish($model->accident_id, $this->userInfo);
        $model->delete_time = time();
        if (!$model->save()) {
            throw new Exception($model->getErrorSummary(true)[0]);
        }
        //返回数据
        $origin = AdminLogService::getOrigin($model, $this->getAllParams());
        AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $this->getAllParams());
        return $this->asJson();
    }
}