<?php

namespace app\controller\api;

use app\extra\basic\Base;
use app\middleware\WxMiddleware;
use app\model\blue\BlueExpert;
use app\model\blue\BlueExpertGroup;
use app\validate\blue\ExpertGroupValidate;
use app\validate\blue\ExpertValidate;
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/certification"),Middleware(WxMiddleware::class)]
class Certification extends Base
{

    #[Inject]
    protected ExpertValidate $validate;

    #[Inject]
    protected ExpertGroupValidate $groupValidate;

    #[Inject]
    protected BlueExpert $model;

    #[Inject]
    protected BlueExpertGroup $modelGroup;

    /**
     * @param Request $request
     * @return Response
     */
    #[Route(path: "user",methods: "post")]
    public function setUserCert(Request $request): Response
    {
        try {
            $param = $request->all();
            $param['openid'] = $request->user['openid'];
            $data = $this->model->where("openid",$param['openid'])->findOrEmpty();
            if (!$data->isEmpty()) {
                if ($data['status'] <> 1) return error("请勿重复提交");
            };
            if (!$this->validate->scene('add')->check($param)) return error($this->validate->getError());
            $state = $data->setAutoData($param);
            if (!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }


    /**
     * 入驻检测
     * @param Request $request
     * @return Response
     */
    #[Route(path: "user/check",methods: "get")]
    public function checkUserCert(Request $request): Response
    {
        try {
            $data = $this->model->where("openid",$request->user['openid'])->findOrEmpty();
            if ($data->isEmpty()) return success("ok",['status' => 0]);
            if ($data['status'] == 2) return success("ok",['status' => 2,"remark" => $data['remark']]);
            return success("ok",['status' => 1]);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }


    /**
     * @param Request $request
     * @return Response
     */
    #[Route(path: "group",methods: "post")]
    public function setGroupCert(Request $request): Response
    {
        try {
            $param = $request->all();
            $param['openid'] = $request->user['openid'];
            $data = $this->modelGroup->where("openid",$param['openid'])->findOrEmpty();
            if (!$data->isEmpty()) {
                if ($data['status'] <> 1) return error("请勿重复提交");
            };
            if (!$this->groupValidate->scene('add')->check($param)) return error($this->groupValidate->getError());
            $state = $data->setAutoData($param);
            if (!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }


    /**
     * 入驻检测
     * @param Request $request
     * @return Response
     */
    #[Route(path: "group/check",methods: "get")]
    public function checkGroupCert(Request $request): Response
    {
        try {
            $data = $this->modelGroup->where("openid",$request->user['openid'])->findOrEmpty();
            if ($data->isEmpty()) return success("ok",['status' => 0]);
            if ($data['status'] == 2) return success("ok",['status' => 2,"remark" => $data['remark']]);
            return success("ok",['status' => 1]);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

}