<?php


namespace App\Http\Controllers\Game;


use App\Facade\TableName;
use App\Game\GlobalUserInfo;
use App\Http\helper\NumConfig;
use App\Models\AccountsInfo;
use App\Models\Treasure\GameScoreLocker;
use App\Notification\TelegramBot;
use App\Util;
use App\Utility\SetNXLock;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class WithDrawInfoController
{



    public function FreeWithDrawInfo(Request $request)
    {
        $user = $request->user();
        $userScoreData = GlobalUserInfo::getScoreDataByUserID($user->UserID);
        if(!$userScoreData['Recharge']){
            $totalWithdraw = max($userScoreData['InsureScore'],40);
            if($userScoreData['InsureScore']<40){
                //第一种状态，我免费领了10块钱，但是金额不足40，点进去页面填信息后点击提现，提示我去玩游戏
                return apiReturnSuc(['state' => 1, 'total' => $totalWithdraw,'InsureScore' => $userScoreData['InsureScore']]);
            }else{
                //第二种状态，我免费金额足够40，提现按钮会点亮，告知用户点击进去
                return apiReturnSuc(['state' => 2, 'total' => $totalWithdraw,'InsureScore' => $userScoreData['InsureScore']]);
            }
        }else{
            //第四种状态，VIP状态下，金额不足了，进度条颜色红色，按钮灰色（不可点击），次日整个消失
            if($userScoreData['InsureScore']<10){
                return apiReturnSuc(['state' => 4, 'total' => max($userScoreData['InsureScore'],40),'InsureScore' => max($userScoreData['InsureScore'],40)]);
            }else{
                //第三种状态，进去完成了充值未提现，提现按钮会高光，引导用户完成提现
                return apiReturnSuc(['state' => 3, 'total' => max($userScoreData['InsureScore'],40),'InsureScore' => max($userScoreData['InsureScore'],40)]);
            }
        }
    }

    public function WithDrawRecord(Request $request)
    {

        $user = $request->user();
        $UserID=$user->UserID;


//        $paypass = $request->input('paypass');
//        if (!empty($user->InsurePass)&&!Hash::check($paypass,$user->InsurePass)) {
//            return apiReturnFail(['web.user.paypass_fail', 'A senha original está errada, digite-a novamente.'], '', 2);
//        }

        $list=DB::table('QPAccountsDB.dbo.OrderWithDraw')
            ->where('UserID', $UserID)
            ->selectRaw("CreateDate,OrderId,[State],WithDraw,ServiceFee")
            ->paginate(10);
        return apiReturnSuc($list);
    }

    public function GetWithDrawBaseInfo(Request $request)
    {
        $user = $request->user();
        $UserID=$user->UserID;


//        $paypass = $request->input('paypass');
//
//        if (empty($user->InsurePass)||!Hash::check($paypass,$user->InsurePass)) {
//            return apiReturnFail(['web.user.paypass_fail', 'A senha original está errada, digite-a novamente.'], '', 2);
//        }

        $dbh = DB::connection()->getPdo();

        $sql="DECLARE  @return_value int
        set nocount on use QPAccountsDB
        EXEC  @return_value = GSP_GR_GetWithDrawBaseInfo '$UserID'
        SELECT 'ReturnValue' = @return_value";

//        echo $sql;die;
        $stmt = $dbh->prepare($sql);

        $stmt->execute();

        $retval = $stmt->fetch(\PDO::FETCH_ASSOC);

        $retval['srate']=env('CONFIG_24680_RATE',1);
        return apiReturnSuc($retval);
    }
    public function GoWithDraw(Request $request)
    {
        $user = $request->user();
        $UserID=$user->UserID;


//        $paypass = $request->input('paypass');
//
//        if (empty($user->InsurePass)||!Hash::check($paypass,$user->InsurePass)) {
//            return apiReturnFail(['web.user.paypass_fail', 'A senha original está errada, digite-a novamente.'], '', 2);
//        }

        $score=$request->input('score',0);
        if(!$score||is_float($score)||$score<=0){
            return apiReturnFail(['web.withdraw.score_fail', 'Amount must be an integer value.']);
        }
        $redisKey = 'withdraw_'.$UserID;
        if (!SetNXLock::getExclusiveLock($redisKey)) {
            return apiReturnFail(['web.withdraw.try_again_later','Tente novamente mais tarde']);
        }

        GameScoreLocker::where('UserID',$UserID)->delete();

        $dbh = DB::connection()->getPdo();

        $score=$score*NumConfig::NUM_VALUE;

        $sql="DECLARE  @return_value int
        set nocount on use QPAccountsDB
        EXEC  @return_value = GSP_GR_GetWithDraw '$UserID','$score'
        SELECT 'ReturnValue' = @return_value";

//        echo $sql;die;
        $stmt = $dbh->prepare($sql);

        $stmt->execute();

        SetNXLock::release($redisKey);

        $retval = $stmt->fetch(\PDO::FETCH_ASSOC);

        try {


//        set @ret = 1	--可提额度不足
//        set @ret = 2	--低于最低可提现金额
//        set @ret = 3	--高于最高可提现金额
//        set @ret = 4	--超过每天提现次数上限
//        set @ret = 5	--身上钱不够
//        set @ret = 6	--开在游戏里面
//        set @ret = 7	--未充值的用户TX额度
            if (isset($retval['ReturnValue'])) {
                $ReturnValue = $retval['ReturnValue'];
                // set @ret = 1 -- Insufficient withdrawal amount
// set @ret = 2 -- Lower than the minimum withdrawal amount
// set @ret = 3 -- Higher than the maximum withdrawal amount
// set @ret = 4 -- Exceeded the daily withdrawal limit
// set @ret = 5 -- Not enough money on hand
// set @ret = 6 -- Opened in the game
// set @ret = 7 -- Undeposited user TX amount
                $errors = [
                    [],
                    ['web.withdraw.no_withdraw_value', "The withdrawable amount is not enough"],
                    ['web.withdraw.too_low', 'Lower than the minimum withdrawal amount'],
                    ['web.withdraw.too_high', 'Higher than the maximum withdrawal amount'],
                    ['web.withdraw.day_max', 'Exceeded the daily withdrawal limit'],
                    ['web.withdraw.no_enouth_score', 'Not enough money on hand'],
                    ['web.withdraw.in_game', 'Sorry, You\'re in game for now'],
                    ['web.withdraw.no_deposit', ' You need deposit first!'],
                ];
                return apiReturnFail($errors[$ReturnValue], [], $ReturnValue == 7 ? 777 : 301);
            }else{
                return apiReturnSuc($retval);
            }
        }catch (\Exception $e){
            TelegramBot::getDefault()->sendProgramNotify("WithDraw Error:", var_export($retval,true).':'.$e->getTraceAsString());
            return apiReturnSuc($retval);
        }
    }
    public function saveCpf(Request $request)
    {
//        $user = $request->user();
//        $UserID=$user->UserID;
//
//        $account = $request->Account ?: '';
//        $phone = $request->Phone ?: '';
//        $email = $request->Email ?: '';
//        $PixNum = $request->PixNum ?: '';
//
//        if(!Util::validateCpf($PixNum)){
//            return apiReturnFail(['withdraw.account.tip.cpf_error','Cpf error format']);
//        }
//
//        $redisKey = 'Api_updateAccountsPayInfo_'.$UserID;
//        $lock = SetNXLock::getExclusiveLock($redisKey);
//        if (!$lock) {
//            return apiReturnFail(['web.withdraw.try_again_later','Tente novamente mais tarde']);
//        }
//        $exist = DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')->where('UserID', $UserID)->first();
//
//        if ($exist) {
//            DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')->where('UserID', $UserID)->update(['PixNum' => $PixNum]);
//        } else {
//
//            $data = ['BankUserName' => $account, 'PhoneNumber' => $phone, 'EmailAddress' => $email, 'UserID' => $UserID, 'PixNum' => $PixNum, 'Achieves' => '', 'HistoryWithDraw' => 0];
//            DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
//                ->insert($data);
//        }
//        SetNXLock::release($redisKey);
        return apiReturnSuc();
    }
    public function MexWithDrawInfo(Request $request)
    {
        $user = $request->user();
        $UserID=$user->UserID;
        $paypass = $request->input('paypass');


        $PixType = $request->input('cbPixType');;
        $BankNO = $request->input('account');
        $AccountsBank = $request->input('account');
        $BankUserName = $request->input('userName');
        $PhoneNumber = $request->input('phone');
        $EmailAddress = $request->input('email');
        $IFSCNumber = $request->input('IFSCNumber');
//        $PANNumber = $request->input('PAN');
//        $AdhaarNumber = $request->input('adhaar');
//        $BranchBank = $request->input('branceBank');
        $BranchBank = $request->input('bank');//银行编号
        $PixNum = $request->input('account');

        Util::WriteLog("mexwithdraw",$request->all());
        if(isset($EmailAddress)&&!empty($EmailAddress)){
            $validator = Validator::make(
                ['email' => $EmailAddress],
                ['email' => 'required|email']
            );

            if ($validator->fails()) {
                return apiReturnFail(['web.user.email_fail','Wrong email format']);
            }
        }
        if (!$AccountsBank || $AccountsBank == 'undefined') {
            return apiReturnFail(['web.bank.account_empty', 'The bank account cannot be empty']);
        }

        if (!(strlen($AccountsBank) == 16 || strlen($AccountsBank) == 18)) {
            return apiReturnFail(['web.bank.account_invalid_length', 'The bank number must have 16 or 18 digits']);
        }

        if (!(strlen($IFSCNumber) == 18 && $IFSCNumber)) {
            return apiReturnFail(['web.bank.clabe_invalid_length', 'The Clabe number must have 18 digits']);
        }

        if (!$BankUserName || $BankUserName == 'undefined') {
            return apiReturnFail(['web.bank.username_empty', 'The name cannot be empty']);
        }

        if (!$BranchBank || $BranchBank == 'undefined') {
            return apiReturnFail(['web.bank.branch_empty', 'The branch code cannot be empty']);
        }

        if (strlen($AccountsBank) == 16) {
            $PixType = 2;
        }

        if (strlen($AccountsBank) == 18) {
            $PixType = 1;
        }

        $redisKey = 'withDrawInfo_withDrawInfo_' . $UserID;
        if (!SetNXLock::getExclusiveLock($redisKey)) {
            return apiReturnFail(['web.bank.try_again_later', 'Please try again later']);
        }

        if (!$UserID) {
            Log::info('miss UserID', $request->all());
            SetNXLock::release($redisKey);
            return apiReturnFail(['web.bank.missing_userid', 'params error']);
        }

        if (empty($PixType)) {
            SetNXLock::release($redisKey);
            return apiReturnFail(['web.bank.pix_type_missing', 'The PIX type is missing']);
        }

// 验证PixNum绑定次数
        if (!empty($PixNum)) {
            $BindCount = DB::table(TableName::QPRecordDB() . 'RecordBankCardBind')
                ->where('BankCard', $PixNum)
                ->lock('WITH(NOLOCK)')
                ->count();
            if ($BindCount >= 3) {
                SetNXLock::release($redisKey);
                return apiReturnFail(['web.bank.cpf_used_multiple_times', 'The CPF number has been used multiple times and cannot be saved'], [], 302);
            }
        }


        // 验证玩家信息
        if (!empty($user->InsurePass)&&!Hash::check($paypass,$user->InsurePass)) {
            return apiReturnFail(['web.user.paypass_fail', 'A senha original está errada, digite-a novamente.'], '', 2);
        }


        if ($PixType < 10 && false) {
            $data = compact('PixType', 'BankUserName', 'PhoneNumber', 'EmailAddress', 'PixNum');
        } else {
            $data = compact('PixType', 'BankUserName','IFSCNumber', 'PhoneNumber', 'EmailAddress','BankNO', 'AccountsBank','BranchBank','PixNum');
            $PixNum = $BankNO;
        }

        foreach ($data as $key => &$val) {
            $data[$key] = trim($val);
        }
        unset($val);


        $AccountWithDrawInfo = DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
            ->lock('WITH(NOLOCK)')
            ->where('UserID', $UserID)
            ->first();
        if (!$AccountWithDrawInfo) {
            $data['UserID'] = $UserID;
            $data['Achieves'] = 0;
            $data['HistoryWithDraw'] = 0;
            try {
                DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                    ->where('UserID', $UserID)
                    ->insert($data);
            } catch (\Exception $e) {
                Log::error('insert AccountWithDrawInfo failed', ['data' => $data]);
            }

        } else {
            DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                ->where('UserID', $UserID)
                ->update($data);
        }

        // 添加绑定记录
        if (empty($AccountWithDrawInfo->PixNum) || $AccountWithDrawInfo->PixNum != $PixNum) {
            try {
                DB::table(TableName::QPRecordDB() . 'RecordBankCardBind')
                    ->insert([
                        'UserID' => $UserID,
                        'BankCard' => $PixNum ?: '',
                        'BindDate' => now()
                    ]);
            } catch (\Exception $e) {
                Log::error('insert RecordBankCardBind failed', ['data' => $data]);
            }

        }
        SetNXLock::release($redisKey);
        return apiReturnSuc();
    }
    public function RuWithDrawInfo(Request $request)
    {
        $user = $request->user();
        $UserID=$user->UserID;
        $paypass = $request->input('paypass');


        $PixType = $request->input('PixType');;
        $BankNO = $request->input('account');

        $BankUserName = $request->input('userName');
        $PhoneNumber = $request->input('phone');
//        $EmailAddress = $request->input('email');
//        $IFSCNumber = $request->input('IFSCNumber');
//        $PANNumber = $request->input('PAN');
//        $AdhaarNumber = $request->input('adhaar');
//        $BranchBank = $request->input('branceBank');
        $BranchBank = $request->input('bank');//银行编号
        $PixNum = $BankNO;
        $AccountsBank = $BankNO;

        Util::WriteLog("ruwithdraw",$request->all());


        if (!$BankUserName || $BankUserName == 'undefined') {
            return apiReturnFail(['web.bank.username_empty', 'The name cannot be empty']);
        }

        if($PixType==1) {
            if (!$AccountsBank || $AccountsBank == 'undefined') {
                return apiReturnFail(['web.bank.account_empty', 'The bank account cannot be empty']);
            }

            if (!(strlen($AccountsBank) == 16 )) {
                return apiReturnFail(['web.bank.account_invalid_length', 'The bank number must have 16  digits']);
            }

//        if (!(strlen($IFSCNumber) == 11 && $IFSCNumber)) {
//            return apiReturnFail(['web.bank.clabe_invalid_length', 'The Clabe number must have 18 digits']);
//        }


        }else{
            if(!$PhoneNumber||strlen($PhoneNumber)!=11){
                return apiReturnFail(['withdraw.account.tip.phone_error', 'The phone number is error']);
            }

            if (!$BranchBank || $BranchBank == 'undefined') {
                return apiReturnFail(['web.bank.branch_empty', 'The branch code cannot be empty']);
            }
        }

        $redisKey = 'withDrawInfo_withDrawInfo_' . $UserID;
        if (!SetNXLock::getExclusiveLock($redisKey)) {
            return apiReturnFail(['web.bank.try_again_later', 'Please try again later']);
        }

        if (!$UserID) {
            Log::info('miss UserID', $request->all());
            SetNXLock::release($redisKey);
            return apiReturnFail(['web.bank.missing_userid', 'params error']);
        }

        if (empty($PixType)) {
            SetNXLock::release($redisKey);
            return apiReturnFail(['web.bank.pix_type_missing', 'The PIX type is missing']);
        }

// 验证PixNum绑定次数
        if (!empty($PixNum)) {
            $BindCount = DB::table(TableName::QPRecordDB() . 'RecordBankCardBind')
                ->where('BankCard', $PixNum)
                ->lock('WITH(NOLOCK)')
                ->count();
            if ($BindCount >= 3) {
                SetNXLock::release($redisKey);
                return apiReturnFail(['web.bank.cpf_used_multiple_times', 'The CPF number has been used multiple times and cannot be saved'], [], 302);
            }
        }


        // 验证玩家信息
        if (!empty($user->InsurePass)&&!Hash::check($paypass,$user->InsurePass)) {
            return apiReturnFail(['web.user.paypass_fail', 'A senha original está errada, digite-a novamente.'], '', 2);
        }



        $data = compact('PixType', 'BankUserName', 'PhoneNumber','BankNO', 'AccountsBank','BranchBank','PixNum');
        $PixNum = $BankNO;

        foreach ($data as $key => &$val) {
            $data[$key] = trim($val);
        }
        unset($val);


        $AccountWithDrawInfo = DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
            ->lock('WITH(NOLOCK)')
            ->where('UserID', $UserID)
            ->first();
        if (!$AccountWithDrawInfo) {
            $data['UserID'] = $UserID;
            $data['Achieves'] = 0;
            $data['HistoryWithDraw'] = 0;
            try {
                DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                    ->where('UserID', $UserID)
                    ->insert($data);
            } catch (\Exception $e) {
                Log::error('insert AccountWithDrawInfo failed', ['data' => $data]);
            }

        } else {
            DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                ->where('UserID', $UserID)
                ->update($data);
        }

        // 添加绑定记录
        if (empty($AccountWithDrawInfo->PixNum) || $AccountWithDrawInfo->PixNum != $PixNum) {
            try {
                DB::table(TableName::QPRecordDB() . 'RecordBankCardBind')
                    ->insert([
                        'UserID' => $UserID,
                        'BankCard' => $PixNum ?: '',
                        'BindDate' => now()
                    ]);
            } catch (\Exception $e) {
                Log::error('insert RecordBankCardBind failed', ['data' => $data]);
            }

        }
        SetNXLock::release($redisKey);
        return apiReturnSuc();
    }
    public function withDrawInfo(Request $request)
    {

//        return apiReturnFail('params error');

        $user = $request->user();
        $UserID=$user->UserID;

//        $UserID = (int)$request->globalUser->UserID;//$request->input('UserID');
//        $RequestDynamicPass = $request->input('DynamicPass');
        $PixType = $request->input('cbPixType');
        $BankUserName = urldecode($request->input('userName'));
        $EmailAddress = $request->input('email');
        $PixNum = $request->input('PixNum');

        if(!empty($EmailAddress))$EmailAddress=Util::cleanEmptyString($EmailAddress);
        if(!empty($PixNum))$PixNum=Util::cleanEmptyString($PixNum);

        if (!empty($EmailAddress)&&!Util::validateEmail($EmailAddress)) {
            return apiReturnFail(['web.user.email_fail','Wrong email format']);
        }

        $redisKey = 'withDrawInfo_withDrawInfo_'.$UserID;
        if (!SetNXLock::getExclusiveLock($redisKey)) {
            Util::WriteLog("withdrawInfo",[1, $request->all()]);
            return apiReturnFail(['web.withdraw.try_again_later','Tente novamente mais tarde']);
        }

        if (!$UserID) {
            Util::WriteLog("withdrawInfo",[2, $request->all()]);
            Log::info('miss UserID', $request->all());
            SetNXLock::release($redisKey);
            return apiReturnFail(['web.withdraw.params_error','params error']);
        }
        if (empty($PixType)) {
            Util::WriteLog("withdrawInfo",[3, $request->all()]);
            SetNXLock::release($redisKey);
            return apiReturnFail(["web.withdraw.pix_type_missing",'O tipo PIX está ausente']);
        }

        // 验证PixNum绑定次数
//        if (!empty($PixNum)) {
//            $BindCount = DB::table(TableName::QPRecordDB() . 'RecordBankCardBind')
//                ->where('BankCard', $PixNum)
//                ->lock('WITH(NOLOCK)')
//                ->count();
//            if ($BindCount >= 6) {
//                Util::WriteLog("withdrawInfo",[4, $request->all()]);
//                SetNXLock::release($redisKey);
//                return apiReturnFail(['web.withdraw.cpf_number_used','O número do CPF foi usado várias vezes e não pode ser salvo'], [], 302);
//            }
//        }

        if ($PixType==1) {
            $data = compact('PixType', 'BankUserName',  'PixNum');
        } else {
            $data = compact('PixType', 'EmailAddress');
        }

        foreach ($data as $key => &$val) {
            $data[$key] = trim($val);
        }
        unset($val);


        $AccountWithDrawInfo = DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
            ->lock('WITH(NOLOCK)')
            ->where('UserID', $UserID)
            ->first();
        if (!$AccountWithDrawInfo) {
            $data['UserID'] = $UserID;
            $data['Achieves'] = 0;
            $data['HistoryWithDraw'] = 0;
            try {
                DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                    ->where('UserID', $UserID)
                    ->insert($data);
            } catch (\Exception $e) {
                    Log::error('insert AccountWithDrawInfo failed', ['data' => $data]);
                Util::WriteLog("withdrawInfo",[6, $request->all(),$data]);
            }

        } else {
            DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                ->where('UserID', $UserID)
                ->update($data);
        }

        SetNXLock::release($redisKey);
        return apiReturnSuc();
    }
    public function withDrawInfoSA(Request $request)
    {

//        return apiReturnFail('params error');

        $UserID = $request->input('UserID');
        $RequestDynamicPass = $request->input('DynamicPass');
        $PixType = $request->input('cbPixType');
        $BankNO = $request->input('bank');
        $AccountsBank = urldecode($request->input('account'));
        $BankUserName = urldecode($request->input('userName'));
        // 原始代码
        $BankUserName = Util::filterNickName($BankUserName);

        // 新增处理：在大写字母前加空格
        $BankUserName = preg_replace('/(?<!^)([A-Z])/', ' $1', $BankUserName);

        $PhoneNumber = $request->input('phone');
        $EmailAddress = $request->input('email');
        $IFSCNumber = $request->input('IFSC');
        $PANNumber = $request->input('PAN');
        $AdhaarNumber = $request->input('adhaar');
        $BranchBank = $request->input('branceBank');
        $PixNum = $request->input('PixNum');
        $redisKey = 'withDrawInfo_withDrawInfo_'.$UserID;
        if (!SetNXLock::getExclusiveLock($redisKey)) {
            Util::WriteLog("withdrawInfo",[1, $request->all()]);
            return apiReturnFail('Tente novamente mais tarde');
        }

        if (!$UserID) {
            Util::WriteLog("withdrawInfo",[2, $request->all()]);
            Log::info('miss UserID', $request->all());
            SetNXLock::release($redisKey);
            return apiReturnFail('params error');
        }
        if (empty($PixType)) {
            Util::WriteLog("withdrawInfo",[3, $request->all()]);
            SetNXLock::release($redisKey);
            return apiReturnFail('O tipo PIX está ausente');
        }

//        Util::validateSouthAmericanPhone('',)

        // 验证PixNum绑定次数
        if (!empty($PixNum)) {
            $BindCount = DB::table(TableName::QPRecordDB() . 'RecordBankCardBind')
                ->where('BankCard', $PixNum)
                ->lock('WITH(NOLOCK)')
                ->count();
            if ($BindCount >= 8) {
                Util::WriteLog("withdrawInfo",[4, $request->all()]);
                SetNXLock::release($redisKey);
                return apiReturnFail(['web.withdraw.cpf_number_used','The CPF number has been used several times and cannot be saved'], [], 302);
            }
        }

//        if(!is_numeric($PixNum)){
//            return apiReturnFail(['web.withdraw.pix_failed','The CPF number has been used several times and cannot be saved'], [], 302);
//        }
        // 验证玩家信息
        $DynamicPass = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
            ->where('UserID', $UserID)
            ->lock('WITH(NOLOCK)')
            ->value('DynamicPass');
        if ($DynamicPass != $RequestDynamicPass || empty($DynamicPass)) {
            SetNXLock::release($redisKey);
            Util::WriteLog("withdrawInfo",[5, $request->all()]);
            return apiReturnFail('As informações do jogador são inconsistentes');
        }
        $data = compact('PixType', 'BankUserName', 'PhoneNumber', 'EmailAddress', 'PixNum','BankNO', 'AccountsBank', 'IFSCNumber', 'PANNumber', 'AdhaarNumber', 'BranchBank');


        foreach ($data as $key => &$val) {
            $data[$key] = trim($val);
        }
        unset($val);


        $AccountWithDrawInfo = DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
            ->lock('WITH(NOLOCK)')
            ->where('UserID', $UserID)
            ->first();
        if (!$AccountWithDrawInfo) {
            $data['UserID'] = $UserID;
            $data['Achieves'] = 0;
            $data['HistoryWithDraw'] = 0;
            try {
                DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                    ->where('UserID', $UserID)
                    ->insert($data);
            } catch (\Exception $e) {
                Log::error('insert AccountWithDrawInfo failed', ['data' => $data]);
                Util::WriteLog("withdrawInfo",[6, $request->all(),$data]);
            }

        } else {

            $data = array_filter($data, function ($value) {
                return !is_null($value) && $value !== '';
            });
            DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                ->where('UserID', $UserID)
                ->update($data);
        }

        // 添加绑定记录
        if (empty($AccountWithDrawInfo->PixNum) || $AccountWithDrawInfo->PixNum != $PixNum) {
            try {
                if(!DB::table(TableName::QPRecordDB() . 'RecordBankCardBind')->where('UserID', $UserID)->exists()&&is_numeric($PixNum)) {
                    DB::table(TableName::QPRecordDB() . 'RecordBankCardBind')
                        ->insert([
                            'UserID'   => $UserID,
                            'BankCard' => $PixNum ?? '',
                            'BindDate' => now()
                        ]);
                    Util::WriteLog('update_pix', [
                        'UserID'   => $UserID,
                        'BankCard' => $PixNum ?? '',
                        'BindDate' => now()
                    ]);
                }
            } catch (\Exception $e) {
                Log::error('insert RecordBankCardBind failed', ['data' => $data]);
                Util::WriteLog("withdrawInfo",[7, $request->all(),$data]);
            }

        }
        SetNXLock::release($redisKey);
        return apiReturnSuc();
    }
    public function kycSimpleEU(Request $request)
    {

//        return apiReturnFail('params error');
        $user = $request->user();
        $UserID=$user->UserID;
        $firstName = Util::filterNickName(urldecode($request->input('firstName')));
        $lastName = Util::filterNickName(urldecode($request->input('lastName')));
        // 原始代码
        $BankUserName = $firstName.'|'.$lastName;


        $PhoneNumber = $request->input('phone');
        $EmailAddress = $request->input('email');


        Util::WriteLog("kyc_eu",$request->all());
        if(isset($EmailAddress)&&!empty($EmailAddress)){
            $validator = Validator::make(
                ['email' => $EmailAddress],
                ['email' => 'required|email']
            );

            if ($validator->fails()) {
                return apiReturnFail(['web.user.email_fail','Wrong email format']);
            }
        }


        if (!$BankUserName || $BankUserName == 'undefined') {
            return apiReturnFail(['web.bank.username_empty', 'The name cannot be empty']);
        }


        $redisKey = 'withDrawInfo_withDrawInfo_' . $UserID;
        if (!SetNXLock::getExclusiveLock($redisKey)) {
            return apiReturnFail(['web.bank.try_again_later', 'Please try again later']);
        }

        if (!$UserID) {
            Log::info('miss UserID', $request->all());
            SetNXLock::release($redisKey);
            return apiReturnFail(['web.bank.missing_userid', 'params error']);
        }


        $data = compact('BankUserName', 'PhoneNumber', 'EmailAddress');


        foreach ($data as $key => &$val) {
            $data[$key] = trim($val);
            if(empty($val))unset($data[$key]);
        }
        unset($val);


        $AccountWithDrawInfo = DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
            ->lock('WITH(NOLOCK)')
            ->where('UserID', $UserID)
            ->first();
        if (!$AccountWithDrawInfo) {
            $data['UserID'] = $UserID;
            $data['Achieves'] = 0;
            $data['HistoryWithDraw'] = 0;
            try {
                DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                    ->where('UserID', $UserID)
                    ->insert($data);
            } catch (\Exception $e) {
                Log::error('insert AccountWithDrawInfo failed', ['data' => $data]);
            }

        } else {
            DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                ->where('UserID', $UserID)
                ->update($data);
        }


        SetNXLock::release($redisKey);
        return apiReturnSuc();
    }
    public function withDrawInfoEU(Request $request)
    {

//        return apiReturnFail('params error');
        $user = $request->user();
        $UserID=$user->UserID;
//        $UserID = $request->input('UserID');
//        $RequestDynamicPass = $request->input('DynamicPass');
        $PixType = $request->input('cbPixType');
        $firstName = Util::filterNickName(urldecode($request->input('firstName')));
        $lastName = Util::filterNickName(urldecode($request->input('lastName')));
        // 原始代码
        $BankUserName = $firstName.'|'.$lastName;



        $BankNO = $request->input('account');
        $AccountsBank = $request->input('account');
        $PhoneNumber = $request->input('phone');
        $EmailAddress = $request->input('email');
        $IFSCNumber = $request->input('IFSCNumber');
        $BranchBank = $request->input('bank');//银行编号
        $PixNum = $request->input('account');

        Util::WriteLog("euwithdraw",$request->all());
        if(isset($EmailAddress)&&!empty($EmailAddress)){
            $validator = Validator::make(
                ['email' => $EmailAddress],
                ['email' => 'required|email']
            );

            if ($validator->fails()) {
                return apiReturnFail(['web.user.email_fail','Wrong email format']);
            }
        }
        if (!$AccountsBank || $AccountsBank == 'undefined') {
            return apiReturnFail(['web.bank.account_empty', 'The bank account cannot be empty']);
        }

        if (!(strlen($AccountsBank) == 16 || strlen($AccountsBank) == 18)) {
            return apiReturnFail(['web.bank.account_invalid_length', 'The bank number must have 16 or 18 digits']);
        }

        if (!(strlen($IFSCNumber) == 18 && $IFSCNumber)) {
            return apiReturnFail(['web.bank.clabe_invalid_length', 'The Clabe number must have 18 digits']);
        }

        if (!$BankUserName || $BankUserName == 'undefined') {
            return apiReturnFail(['web.bank.username_empty', 'The name cannot be empty']);
        }

        if (!$BranchBank || $BranchBank == 'undefined') {
            return apiReturnFail(['web.bank.branch_empty', 'The branch code cannot be empty']);
        }

        if (strlen($AccountsBank) == 16) {
            $PixType = 2;
        }

        if (strlen($AccountsBank) == 18) {
            $PixType = 1;
        }

        $redisKey = 'withDrawInfo_withDrawInfo_' . $UserID;
        if (!SetNXLock::getExclusiveLock($redisKey)) {
            return apiReturnFail(['web.bank.try_again_later', 'Please try again later']);
        }

        if (!$UserID) {
            Log::info('miss UserID', $request->all());
            SetNXLock::release($redisKey);
            return apiReturnFail(['web.bank.missing_userid', 'params error']);
        }

        if (empty($PixType)) {
            SetNXLock::release($redisKey);
            return apiReturnFail(['web.bank.pix_type_missing', 'The PIX type is missing']);
        }

// 验证PixNum绑定次数
        if (!empty($PixNum)) {
            $BindCount = DB::table(TableName::QPRecordDB() . 'RecordBankCardBind')
                ->where('BankCard', $PixNum)
                ->lock('WITH(NOLOCK)')
                ->count();
            if ($BindCount >= 3) {
                SetNXLock::release($redisKey);
                return apiReturnFail(['web.bank.cpf_used_multiple_times', 'The CPF number has been used multiple times and cannot be saved'], [], 302);
            }
        }


        // 验证玩家信息
        if (!empty($user->InsurePass)&&!Hash::check($paypass,$user->InsurePass)) {
            return apiReturnFail(['web.user.paypass_fail', 'A senha original está errada, digite-a novamente.'], '', 2);
        }


        if ($PixType < 10 && false) {
            $data = compact('PixType', 'BankUserName', 'PhoneNumber', 'EmailAddress', 'PixNum');
        } else {
            $data = compact('PixType', 'BankUserName','IFSCNumber', 'PhoneNumber', 'EmailAddress','BankNO', 'AccountsBank','BranchBank','PixNum');
            $PixNum = $BankNO;
        }

        foreach ($data as $key => &$val) {
            $data[$key] = trim($val);
        }
        unset($val);


        $AccountWithDrawInfo = DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
            ->lock('WITH(NOLOCK)')
            ->where('UserID', $UserID)
            ->first();
        if (!$AccountWithDrawInfo) {
            $data['UserID'] = $UserID;
            $data['Achieves'] = 0;
            $data['HistoryWithDraw'] = 0;
            try {
                DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                    ->where('UserID', $UserID)
                    ->insert($data);
            } catch (\Exception $e) {
                Log::error('insert AccountWithDrawInfo failed', ['data' => $data]);
            }

        } else {
            DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
                ->where('UserID', $UserID)
                ->update($data);
        }

        // 添加绑定记录
        if (empty($AccountWithDrawInfo->PixNum) || $AccountWithDrawInfo->PixNum != $PixNum) {
            try {
                DB::table(TableName::QPRecordDB() . 'RecordBankCardBind')
                    ->insert([
                        'UserID' => $UserID,
                        'BankCard' => $PixNum ?: '',
                        'BindDate' => now()
                    ]);
            } catch (\Exception $e) {
                Log::error('insert RecordBankCardBind failed', ['data' => $data]);
            }

        }
        SetNXLock::release($redisKey);
        return apiReturnSuc();
    }
    // 获取提现信息
    public function getWithDrawInfo(Request $request)
    {
//        $UserID = (int)$request->globalUser->UserID;//$request->input('UserID');
        $user = $request->user();
        $UserID=$user->UserID;
//        $paypass = $request->input('paypass');
//        if (!empty($user->InsurePass)&&!Hash::check($paypass,$user->InsurePass)) {
//            return apiReturnFail(['web.user.paypass_fail', 'A senha original está errada, digite-a novamente.'], '', 2);
//        }



        $info = DB::table(TableName::QPAccountsDB() . 'AccountWithDrawInfo')
            ->where('UserID', $UserID)
            ->lock('WITH(NOLOCK)')
            ->first();
        if (!$info) {   // 赋值 -- 避免客户端报错
            $info = [
//                'BankNO' => '',
//                'AccountsBank' => '',
                'BankUserName' => '',
//                'PhoneNumber' => '',
//                'IFSCNumber' => '',
                'EmailAddress' => '',
//                'Achieves' => '',
//                'PANNumber' => '',
//                'AdhaarNumber' => '',
//                'BranchBank' => '',
                'PixNum' => '',
                'PixType' => '',
            ];
        }

        $info = json_decode(json_encode($info),true);

        if(env('REGION_24680')=='na-mexico') {
            $info['bank_list'] = config('games.mex_bank_list');
        }else if(env('REGION_24680')=='eu-russian') {
            $info['bank_list'] = config('games.ru_bank_list');
        }


        $AccountsInfoModel = new AccountsInfo();
        $total = $AccountsInfoModel->accountTotalStatistics([$UserID]);
        $totalRecharge= @$total[0]->Recharge ??  0;

        $info['total_recharge'] = intval($totalRecharge);
        $info['withdraw_level'] = env('MIN_RECHARGE',20);

        return apiReturnSuc(compact('info'));
    }
}
