<?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("Binding")]
#[Apidoc\Sort(5)]
class BindingController
{

    #[Apidoc\Title("绑定支付宝，绑定微信，绑定数字人民币 扫码支付")]
    #[Apidoc\Url("api/binding.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    #[Apidoc\Param("account_number", type: "int", require: true, desc: "账号", mock: 10)]
    #[Apidoc\Param("type", type: "int", require: true, desc: "类型：1=支付宝，2=微信 ", mock: 2)]
    public function addbinding(Request $request)
    {
        $param              = $request->param_data;
        $param['user_data'] = $request->user_data;
        Db::beginTransaction();
        try {
            Validator::input($param, [
                'account_number' => Validator::notEmpty()->setName('账号'),
                'type'           => Validator::notEmpty()->setName('类型'),
            ]);
            $data = [
                'user_id'        => $param['user_data']['id'],
                'account_number' => $param['account_number'],
                'mobile'         => $param['user_data']['mobile'],
                'type'           => $param['type'],
                'created_at'     => date('Y-m-d H:i:s'),
                'updated_at'     => date('Y-m-d H:i:s'),
            ];

            $binding = Db::table('wa_binding')
                ->where('user_id', $param['user_data']['id'])
                ->where('type', $param['type'])->first();
            if (empty($binding)) {
                Db::table('wa_binding')->insert($data);
            } else {
                Db::table('wa_binding')->where('id', $binding->id)->update($data);
            }

        } catch (\Throwable $exception) {
            Db::rollBack();
            return error($exception->getMessage());
        }
        Db::commit();
        return success();

    }

    #[Apidoc\Title("绑定记录详情")]
    #[Apidoc\Url("api/binding/binding_details.html")]
    #[Apidoc\Method("POST")]
    #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
    #[Apidoc\Param("type", type: "int", require: true, desc: "类型：1=支付宝，2=微信", mock: 2)]
    #[Apidoc\Returned(name: "account_number", type: "string", require: true, desc: '账号', default: '0.00')]
    #[Apidoc\Returned(name: "mobile", type: "string", require: true, desc: '手机号', default: '0.00')]
    public function binding_details(Request $request)
    {
        $param              = $request->param_data;
        $param['user_data'] = $request->user_data;
        Db::beginTransaction();
        try {
            Validator::input($param, [
                'type' => Validator::notEmpty()->setName('类型'),
            ]);

            $bindingdata = Db::table('wa_binding')
                ->where('user_id', $param['user_data']['id'])
                ->where('type', $param['type'])
                ->first();
            if (empty($bindingdata)) {
                $data = [
                    'id'             => '',
                    'account_number' => '',
                    'mobile'         => '',
                    'type'           => '',
                ];
            } else {
                $data = [
                    'id'             => $bindingdata->id,
                    'account_number' => $bindingdata->account_number,
                    'mobile'         => $bindingdata->mobile,
                    'type'           => $bindingdata->type,
                ];
            }
        } catch (\Throwable $exception) {
            Db::rollBack();
            return error($exception->getMessage());
        }
        Db::commit();
        return success($data);

    }


}
