<?php


namespace App\Http\Controllers\Api;


use App\Services\PayMentService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PaymentController
{
    // 支付总入口
    public function pay(Request $request)
    {

        // 获取数据
        $userId = (int)$request->post('userID');
        $payAmt = (int)$request->input('payAmt');
        $userName = $request->input('userName');
        $userEmail = $request->input('userEmail');
        $userPhone = $request->input('userPhone');
        $pay_type = (int)$request->input('pay_type');
        $GiftsID = (int)$request->input('GiftsID');
        $buyIP = $request->ip();
        $AdId = $request->input('AdId');
        $eventType = $request->input('eventType');

        // 验证是不是10卢币首充礼包
        if ($GiftsID == 200 || ($GiftsID >= 104 && $GiftsID <= 107)) {
            // 查询状态
            $first = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
                ->where('StatusName', 'FirstChargeGiftBagStatus')
                ->first();

            if ($first->StatusValue != 1) return apiReturnFail(['web.payment.activity_closed',__('messages.api.payment.activity_closed')]);

            // 首充
            $firstCharge = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
                ->where('StatusName', 'FirstChargeGiftBag')
                ->first();
            $pay_type = $firstCharge->StatusValue;
        }

        $where[] = ['type', 'pay'];
        $where[] = ['status', 1];
        $where[] = ['id', $pay_type];

        if ($GiftsID == 'undefined') {
            $GiftsID = '';
        }

        $query = DB::table('agent.dbo.admin_configs')->where($where)->value('config_key');

        if (!$query) {
            return apiReturnFail(['web.payment.paytype_error',__('messages.api.payment.paytype_error')]);
        }
        $service = PayMentService::pay_order($query);

        $result = $service->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID,$buyIP,$AdId,$eventType);

        return $result;
    }
}
