<?php

namespace app\controller\api;

use app\extra\basic\Base;
use app\middleware\WxMiddleware;
use app\model\blue\BlueCategory;
use app\model\blue\BlueExpertPlatform;
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/platform"),Middleware(WxMiddleware::class)]
class Account extends Base
{

    protected array $noNeedLogin = [];

    #[Inject]
    protected BlueExpertPlatform $model;

    /**
     * 绑定账号列表
     * @param Request $request
     * @return Response
     */
    #[Route(path: "list",methods: "get")]
    public function getAccountList(Request $request): Response
    {
        try {
            $category = (new BlueCategory)->where("status",1)->where("type",1)->field("sign,name,icon")->select();
            if ($category->isEmpty()) return success("success.data");
            $platform = [];
            foreach ($category as $key=>$val)
            {
                $platform[$key] = $val;
                $bind = $this->model->where("source",$val['sign'])->where("openid",$request->user['openid'])->findOrEmpty();
                if ($bind->isEmpty()) {
                    $bindState = 0;
                } else {
                    $bindState = $bind['status'] + 1;
                }
                $platform[$key]['is_bind'] = $bindState;
            }
            return successTrans("success.data",$platform);
        } catch (\Throwable $th) {
            return error($th->getMessage());
        }
    }

    /**
     * 查看绑定详情
     * @param Request $request
     * @return Response
     */
    #[Route(path: "detail",methods: "get")]
    public function getAccountDetail(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "source.require"    => trans("empty.require")
            ],$request->method());
            if (!is_array($param)) return error($param);
            $platform = $this->model->where(['openid' => $request->user['openid'],'source' => $param['source']])->field("id,nickname,home_page,status,url,source,remark")->findOrEmpty();
            if ($platform->isEmpty()) return successTrans("success.data");
            return successTrans("success.data",$platform->toArray());
        } catch (\Throwable $th) {
            return error($th->getMessage());
        }
    }


    #[Route(path: "bind",methods: "post")]
    public function bindAccount(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "nickname.require"  => trans("empty.require"),
                "url.require"       => trans("empty.require"),
                "home.require"      => trans("empty.require"),
                "source.require"    => trans("empty.require")
            ],$request->method());
            if (!is_array($param)) return error($param);
            $platform = $this->model->where(['openid' => $request->user['openid'],'source' => $param['source']])->findOrEmpty();
            if (!$platform->isEmpty()) {
                if ($platform['status'] < 2) return error("请勿重复提交");
                $platform->nickname = $param['nickname'];
                $platform->url = $param['url'];
                $platform->home_page = $param['home'];
                $platform->status = 0; // 重置状态
                $state = $platform->save();
            } else {
                $state = $platform->insertGetId([
                    "nickname"  => $param['nickname'],
                    "url"       => $param['url'],
                    "home_page" => $param['home'],
                    "source"    => $param['source'],
                    "openid"    => $request->user['openid']
                ]);
            }
            if (!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $th) {
            return error($th->getMessage());
        }
    }

}