<?php

    namespace app\controller;


    use app\business\LoginBusiness;
    use Illuminate\Support\Arr;
    use Respect\Validation\Validator;
    use support\Db;
    use support\Redis;
    use support\Request;
    use hg\apidoc\annotation as Apidoc;
    use Webman\Captcha\CaptchaBuilder;
    use Webman\Captcha\PhraseBuilder;

    #[Apidoc\Title("资金明细")]
    #[Apidoc\Group("Stream")]
    #[Apidoc\Sort(5)]
    class StreamController
    {
        #[Apidoc\Title("资金明细")]
        #[Apidoc\Url("api/stream.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
        #[Apidoc\Param("page", type: "int", desc: "", mock: 1)]
        #[Apidoc\Param("limit", type: "int", desc: "输出条数", mock: 10)]
        #[Apidoc\Param("mold", type: "int", require: true, desc: "钱包类型 不传查所有 1 债券分红钱包  2 华润财富余额 3 华润银联卡余额 ", mock: 1)]
        public function data(Request $request)
        {
            Db::beginTransaction();
            try {
                $param              = Arr::only($request->param_data, ['page', 'limit', 'mold']);
                $param['user_data'] = $request->user_data;
                Validator::input($param, [
                    'page'  => Validator::notEmpty()->intType()->setName('页面'),
                    'limit' => Validator::notEmpty()->intType()->setName('输出条数'),
                ]);
                $data = Db::table('wa_stream')->where(function ($query) use ($param) {
                    $query->where('user_id', $param['user_data']['id']);
                    if (Arr::get($param, 'mold')) {
                        $query->where('mold', $param['mold']);
                    }
                })->orderByDesc('id')
                    ->paginate(Arr::get($param,'limit',10),['*'],'page',Arr::get($param,'page'))
                    ->toArray();


                foreach ($data['data'] as $k => $v) {
                    $data['data'][$k]->type_name = streamType($v->type);
                    $data['data'][$k]->mold_name = moldType($v->mold);
                    if($v->mold == 10 && $v->type == 23 ){
                       $wa_withdraw =  Db::table('wa_withdraw')->where('id',$v->source_id)->first();
                       if($wa_withdraw->status==2){
                           $data['data'][$k]->remarks = '审核通过、打款中';
                       }else{
                           $data['data'][$k]->remarks = '解冻申请已提交，审核中';
                       }
                    }else{
                        $data['data'][$k]->remarks = '';
                    }
                }
            } catch (\Throwable $exception) {
                Db::rollBack();
                return error($exception->getMessage());
            }
            Db::commit();
            return success($data);

        }

        #[Apidoc\Title("钱包类型")]
        #[Apidoc\Url("api/stream/mold_type.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Returned(name: "value", type: "int", require: true, desc: '钱包ID', default: '1')]
        #[Apidoc\Returned(name: "name", type: "string", require: true, desc: '钱包名称', default: '收益余额')]
        public function moldType(Request $request)
        {
            $data = moldType();
            return success($data);
        }
    }
