<?php


namespace App\Http\Controllers\Admin;


use App\Facade\TableName;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CodeController
{
    // 验证码查询
    public function query(Request $request)
    {
        $PhoneNum = $request->PhoneNum ?: '';

        $query = DB::table(TableName::QPTreasureDB() . 'GamePhoneVerityCode')
            ->where('PhoneNum', $PhoneNum)
            ->select('PhoneNum', 'Code')
            ->first();

        $BindDate = DB::table(TableName::QPAccountsDB() . 'AccountPhone')
            ->where('PhoneNum', $PhoneNum)
            ->value('BindDate');


        return view('admin.code.query', compact('query', 'PhoneNum', 'BindDate'));
    }

    // 绑定手机号 -- 手动
    public function bind_phone(Request $request, $UserID)
    {
        if ($request->isMethod('post')) {

            $post = $request->post();

            $Channel = DB::connection('write')->table(TableName::QPAccountsDB() . 'AccountsInfo')
                ->where('UserID', $UserID)->select('Channel')->first()->Channel;

            $IsUserBindPhone = DB::table(TableName::QPAccountsDB() . 'AccountPhone')
                ->where('UserID', $UserID)
                ->first();
            if ($IsUserBindPhone) {
                return apiReturnFail('用户已绑定');
            }
            DB::table(TableName::QPAccountsDB() . 'AccountPhone')
                ->insert([
                    'PhoneNum' => (int)$post['PhoneNum'],
                    'BindDate' => now(),
                    'LogonPass' => $post['LogonPass'],
                    'UserID' => $UserID,
                    'Channel' => $Channel,
                ]);

            return apiReturnSuc();
        } else {
            return view('admin.code.bind_phone', compact('UserID'));
        }
    }
}
