<?php

namespace app\controller\api;

use app\extra\basic\Base;
use app\extra\service\blue\TaskService;
use app\middleware\WxMiddleware;
use app\model\blue\BlueExpertPlatform;
use app\model\blue\BlueTask;
use app\model\blue\BlueTaskUser;
use DI\Attribute\Inject;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Middleware;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;


#[Controller(prefix: "/wx_api/task"),Middleware(WxMiddleware::class)]
class Task extends Base
{


    protected array $noNeedLogin = ["getTaskDetail"];


    #[Inject]
    protected TaskService $service;

    #[Inject]
    protected BlueTask $model;

    /**
     * 任务列表
     * @param Request $request
     * @return Response
     */
    #[Route(path: "portal",methods: "get")]
    public function getTaskList(Request $request): Response
    {
        try {

        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }


    /**
     * 任务详情
     * @param Request $request
     * @return Response
     */
    #[Route(path: "detail",methods: "get")]
    public function getTaskDetail(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "task_sn.require"   => trans("empty.require"),
                "id.require"        => trans("empty.require"),
            ],$request->method());
            if (!is_array($param)) return error($param);
            $data = $this->service->getTaskDetail($param);
            if (empty($data)) return errorTrans("empty.data");
            $data['content'] = strip_img_attributes($data['content']);
            return success("ok",$data->toArray());
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    /**
     * 任务报名
     * @param Request $request
     * @return Response
     */
    #[Route(path: "sign",methods: "post")]
    public function taskUserReg(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "task_sn.require"   => trans("empty.require"),
                "task_id.require"   => trans("empty.require"),
            ],$request->method());
            if (!is_array($param)) return error($param);
            $task = $this->model->where(['task_sn' => $param['task_sn'],'id' => $param['task_id']])->findOrEmpty();
            if ($task->isEmpty()) return errorTrans("empty.data");
            $taskUser = (new BlueTaskUser)->where(['openid' => $request->user['openid'],'task_sn' => $param['task_sn'],'task_id' => $param['task_id']])->findOrEmpty();
            if (!$taskUser->isEmpty()) return error("请勿重复报名");
            $state = $taskUser->insertGetId([
                "openid"    => $request->user['openid'],
                "task_id"   => $param['task_id'],
                "task_sn"   => $param['task_sn'],
                "source"    => $task['category'],
                "status"    => 0 // 0待提交 1审核中 2审核通过 3已拒绝
            ]);
            if (!$state) return errorTrans("error.data");
            return successTrans("success.data",['task_user_id' => $state]);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

}