<?php

namespace app\controller\api;

use app\extra\basic\Base;
use app\middleware\WxMiddleware;
use app\model\blue\BlueUserOpen;
use DI\Attribute\Inject;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Route;
use LinFly\Annotation\Route\Middleware;
use support\Request;
use support\Response;


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

    #[Inject]
    protected BlueUserOpen $model;

    #[Route(path: "data",methods: "get")]
    public function getUserData(Request $request): Response
    {
        try {
            $user = $request->user;
            if (empty($user)) return errorTrans("empty.data");
            $member = $this->model->where("openid",$user['openid'])->field("openid,headimg,nickname")
                ->append(["withdraw",'day','mon','bonus','task','sign','level','dy_level'])
                ->withAttr(['withdraw' => function(){
                        return 0; // 可提现金额
                    },'day' => function(){
                        return 0; // 今日佣金
                    },'mon' => function(){
                        return 0; // 本月佣金
                    },'bonus' => function(){
                        return 0; // 已获得奖金
                    },'task' => function(){
                        return 0; // 已接任务
                    },'sign' => function(){
                        return 0; // 0未签约，1已签约
                    },'level' => function(){
                        return 0; // 平台等级
                    },'dy_level' => function(){
                        return 0; // 抖音等级
                    }
                ])
                ->findOrEmpty();
            return success("ok",$member->toArray());
        } catch (\Throwable $th) {
            return error($th->getMessage());
        }
    }


    /**
     * 价目表
     */
    #[Route(path: "price",methods: "get")]
    public function getPriceData(): Response
    {
        try {
            $param = $this->_valid([
                "shop.require"  => "参数错误"
            ]);
            if (!is_array($param)) return error($param);
            $data = (new SaasPrice)->where("shop_id",$param['shop'])->field("paper_size,source,duplex,price,color,type")->select();
            if ($data->isEmpty()) return error("尚未配置价目表");
            $resp = [];
            foreach ($data as $key=>$val)
            {
                $color = $this->color[$val['color']];
                $type = $this->type[$val['type']];
                $duplex = $this->duplex[$val['duplex']];
                $resp[$val['paper_size']]['id'] = $val['paper_size'];
                $resp[$val['paper_size']]['child'][$key]['name'] = $type.$color.$duplex;
                $resp[$val['paper_size']]['child'][$key]['price'] = $val['price'];
            }
            return success("ok",$resp);
        } catch (\Throwable $exception) {
            return error($exception->getMessage());
        }
    }

}