<?php


namespace frontend\modules\api\controllers;


use common\components\AjaxException;
use common\models\AccidentCasesFocus;
use common\services\CaseService;
use frontend\modules\api\components\BaseAdminController;
use yii\db\Exception;
use yii\web\Response;

class AccidentCasesFocusController extends BaseAdminController
{
    /**
     * 关注列表
     * @return Response
     */
    public function actionList()
    {
        $list = [];
        $AccidentCasesFocus = AccidentCasesFocus::findOne(["uid" => $this->uid]);
        if ($AccidentCasesFocus) {
            $accident_cases_id_list = json_decode($AccidentCasesFocus->accident_cases_id_list);
            $AccidentCasesList = CaseService::getCaseShowQuery()->andWhere(["id" => $accident_cases_id_list])->all();
            if (count($accident_cases_id_list) != count($AccidentCasesList)) {
                $accident_cases_id_list = array_column($AccidentCasesList, "id");
                $AccidentCasesFocus->accident_cases_id_list = json_encode($accident_cases_id_list);
                $AccidentCasesFocus->save();
            }
            $list = CaseService::dealWithFocusList($AccidentCasesList);
        }
        $data["data"] = $list;
        return $this->asJson($data);
    }

    /**
     * 添加关注
     * @param $id
     * @return Response
     * @throws AjaxException
     * @throws Exception
     */
    public function actionAdd($id)
    {
        CaseService::getCaseById($id);
        $maxCount = 10;
        $AccidentCasesFocus = AccidentCasesFocus::findOne(["uid" => $this->uid]);
        if (!$AccidentCasesFocus) {
            $AccidentCasesFocus = new AccidentCasesFocus();
            $AccidentCasesFocus->uid = $this->userInfo->id;
            $AccidentCasesFocus->accident_cases_id_list = json_encode([$id]);
        } else {
            $accident_cases_id_list = json_decode($AccidentCasesFocus->accident_cases_id_list, true);
            array_unshift($accident_cases_id_list, intval($id));
            $accident_cases_id_list = array_values(array_unique($accident_cases_id_list));
            if (count($accident_cases_id_list) > $maxCount) {
                throw new AjaxException("关注列表最大数量为" . $maxCount . "条！");
            }
            $AccidentCasesFocus->accident_cases_id_list = json_encode($accident_cases_id_list);
        }
        if (!$AccidentCasesFocus->save()) {
            throw new Exception($AccidentCasesFocus->getErrorSummary(true)[0]);
        }
        return $this->asJson([]);
    }

    /**
     * 取消关注
     * @param $id
     * @return Response
     * @throws Exception
     */
    public function actionCancel($id)
    {
        $AccidentCasesFocus = AccidentCasesFocus::findOne(["uid" => $this->uid]);
        if ($AccidentCasesFocus) {
            $accident_cases_id_list = json_decode($AccidentCasesFocus->accident_cases_id_list);
            $newList = [];
            foreach ($accident_cases_id_list as $accident_cases_id) {
                if ($accident_cases_id != $id) {
                    $newList[] = $accident_cases_id;
                }
            }
            $AccidentCasesFocus->accident_cases_id_list = json_encode($newList);
            if (!$AccidentCasesFocus->save()) {
                throw new Exception($AccidentCasesFocus->getErrorSummary(true)[0]);
            }
        }
        return $this->asJson([]);
    }
}