<?php


namespace frontend\modules\api\controllers;

use common\components\AjaxException;
use common\components\Routes;
use common\models\AccidentCasesUpdateHistory;
use common\models\AdminLog;
use common\services\AdminLogService;
use frontend\modules\api\components\BaseAdminController;
use yii\web\Response;

class AdminLogController extends BaseAdminController
{
    /**
     * 日志列表
     * @param int $current
     * @param int $page
     * @return Response
     */
    public function actionList(int $current = 1, int $page = 10)
    {
        $query = AdminLog::find()
            ->limit($page)
            ->offset(($current - 1) * $page);
        //排序
        $this->initSequence($query);
        //主要筛选条件
        $query = $this->addConditionToQuery("username", $query, true);
        $query = $this->addConditionToQuery("role", $query, true);
        $query = $this->addConditionToQuery("title", $query, true);
        $query = $this->addConditionToQuery("name", $query, true);
        $query = $this->addConditionToQuery("url", $query, true);
        $query = $this->addConditionToQuery("ip", $query, true);
        $query = $this->addConditionToQuery("content", $query, true);
        $query = $this->addStartEndConditionToQuery("create_time", $query);
        //组合数据
        $list = $query->select("id,role_name,uid,name,username,url,title,ip,create_time,content")->all();
        $data['data'] = $list;
        $data['total'] = $query->count();
        return $this->asJson($data);
    }

    /**
     * 用户操作日志列表
     * @param int $current
     * @param int $page
     * @return Response
     */
    public function actionStaffList(int $current = 1, int $page = 10)
    {
        $query = AccidentCasesUpdateHistory::find()
            ->limit($page)
            ->offset(($current - 1) * $page);
        //排序
        $this->initSequence($query);
        //主要筛选条件
        $query = $query->andWhere(["url" => AdminLogService::$staffTitle]);
        //
        $query = $this->addConditionToQuery("username", $query, true);
        $query = $this->addConditionToQuery("role", $query, true);
        $query = $this->addConditionToQuery("title", $query, true);
        $query = $this->addConditionToQuery("name", $query, true);
        $query = $this->addConditionToQuery("url", $query);
        $query = $this->addConditionToQuery("ip", $query, true);
        $query = $this->addConditionToQuery("content", $query, true);
        $query = $this->addStartEndConditionToQuery("create_time", $query);
        //组合数据
        $list = $query->select("id,role_name,uid,name,username,url,title,ip,create_time,content,origin,current,case_id,case_title")->all();
        $data['data'] = $list;
        $data['total'] = $query->count();
        return $this->asJson($data);
    }

    /**
     * 日志详情
     * @param $id
     * @return Response
     * @throws AjaxException
     */
    public function actionDetail($id)
    {
        $AdminLogInfo = AdminLog::findOne($id);
        if (!$AdminLogInfo) {
            throw new AjaxException("日志不存在！");
        }
        if ($AdminLogInfo->history_id != 0) {
            $history = AccidentCasesUpdateHistory::findOne($AdminLogInfo->history_id);
            if ($history) {
                $AdminLogInfo->origin = $history->origin;
                $AdminLogInfo->current = $history->current;
            }
        }
        $newInfo = $AdminLogInfo->getAttributes();
        $newInfo["origin"] = $AdminLogInfo->origin;
        $newInfo["current"] = $AdminLogInfo->current;
        $data['data'] = $newInfo;
        //组合数据
        return $this->asJson($data);
    }

    /**
     * 服务端urlTitle生成
     */
    public function actionUrlTitle()
    {
        $myfile = fopen("testfile.txt", "w");
        //获取服务端权限
        $array = [];
        $array2 = [];
        $routes = Routes::getAppRoutes('api');
        foreach ($routes as $key => $value) {
//            $name = substr(substr($key, 4), 0, strlen(substr($key, 4)) - 1);
//            $array[$name] = $name;
            if (isset($value['route'])) {
                foreach ($value['route'] as $k => $v) {
                    $name = substr($v[0], 4);
                    $title = $v[1];
                    $array[$name] = $title;
                    $txt = '"' . $name . '"' . "=>" . '"' . $title . '"' . ',' . "\n";
                    fwrite($myfile, $txt);
                }
            }
        }
        fclose($myfile);
        var_dump($array2);
        exit;
    }
}