<?php

    namespace app\controller;


    use app\business\GoodsBusiness;
    use app\business\LoginBusiness;
    use app\business\PayorderBusiness;
    use app\business\WithdrawBusiness;
    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 WithdrawController
    {
        #[Apidoc\Title("提现申请")]
        #[Apidoc\Url("api/withdraw.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
        #[Apidoc\Param("money", type: "int", require: true, desc: "提现金额", mock: 10)]
        #[Apidoc\Param("mold", type: "int", require: true, desc: "钱包类型 ", mock: 1)]
        #[Apidoc\Param(name: "pay_characteristic", type: "int", require: true, desc: '支付通道标识', default: '1')]
        #[Apidoc\Param(name: "binding_type", type: "int", require: true, desc: '我的资产转入：1=支付宝，2=微信', default: '1')]
        public function applyfor(Request $request)
        {
            $param              = Arr::only($request->param_data,['money','mold','binding_type']);
            $param['user_data'] = $request->user_data;
            Db::beginTransaction();
            try {
                Validator::input($param, [
                    'money' => Validator::notEmpty()->Number()->setName('提现金额'),
                    'mold'  => Validator::notEmpty()->intType()->setName('钱包类型'),
                ]);
                $name = Db::table('wa_user_identity')->where('uid', $request->user_data['id'])->value('name');
                if (empty($name)) {
                    throw new \Exception('请实名后再进行提现!');
                }
                $account_holder = Db::table('wa_bank_card')->where('uid', $request->user_data['id'])->value('account_holder');
                if (empty($account_holder)) {
                    throw new \Exception('请绑定银行卡!');
                }
                if ($account_holder != $name) {
                    throw new \Exception('实名人和开户人不一致,请绑定实名人的银行卡!');
                }
                $arr = WithdrawBusiness::applyfor($param);
            } catch (\Throwable $exception) {
                Db::rollBack();
                return error($exception->getMessage());
            }
            Db::commit();
            return success($arr, '提现成功');

        }

        #[Apidoc\Title("提现记录")]
        #[Apidoc\Url("api/withdraw/stream.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
        #[Apidoc\Param("page", type: "int", require: true, desc: "页面", mock: 1)]
        #[Apidoc\Param("limit", type: "int", require: true, desc: "输入条数", mock: 10)]
        #[Apidoc\Param("mold", type: "int", require: true, desc: "钱包类型   ", mock: 1)]
        #[Apidoc\Param("type", type: "int", require: true, desc: "钱包类型 1=转入记录(全部)，2=支付宝转入，3=微信转入，4=提现记录  ", mock: 1)]
        #[Apidoc\Returned(name: "order_no", type: "string", require: true, desc: '订单号', default: '23213213213217182937291')]
        #[Apidoc\Returned(name: "money", type: "string", require: true, desc: '提现金额', default: '100.00')]
        #[Apidoc\Returned(name: "status", type: "string", require: true, desc: '提现状态 1 待审核 2打款中 3审核通过（成功，通过） 4驳回（失败）', default: '1')]
        #[Apidoc\Returned(name: "binding_type", type: "string", require: true, desc: '1=转入支付宝，2=转入微信', default: '1')]
        #[Apidoc\Returned(name: "affiliated_bank", type: "string", require: true, desc: '归属银行', default: '中国银行')]
        #[Apidoc\Returned(name: "account_holder", type: "string", require: true, desc: '开户人', default: '张三')]
        #[Apidoc\Returned(name: "card_number", type: "string", require: true, desc: '卡号', default: '12321321376763274682374681')]
        #[Apidoc\Returned(name: "created_at", type: "date", require: true, desc: '申请时间', default: '2023-05-20 23:49:49')]
        public function stream(Request $request)
        {
            $param              = $request->param_data;
            $param              = Arr::only($param, ['page', 'limit', 'mold']);
            $param['user_data'] = $request->user_data;
            try {
                Validator::input($param, [
                    'page'  => Validator::notEmpty()->intType()->setName('页码'),
                    'limit' => Validator::notEmpty()->intType()->setName('输出条数'),
                ]);
                $data = WithdrawBusiness::stream($param);
            } catch (\Throwable $exception) {
                return error($exception->getMessage());
            }
            return success($data['data'], '成功', 200, $data['total']);
        }


    }
