<?php
namespace App\Http\Controllers\V1\User;

use App\Helper\SerialNumber;
use App\Http\Controllers\V1\Controller;
use App\Models\Pay;
use App\Models\RechargeCombo;
use App\Models\Setting;
use App\Models\UserConsumeRecord;
use App\Models\UserEpisodesRecord;
use App\Models\UserRechargeRecord;
use App\Models\UserWatchRecord;
use Carbon\Carbon;
use Dingo\Api\Http\Request;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\QueryException;

class RechargeController extends Controller
{
    public function record(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $offset = ($page - 1) * 10;
        $date = $request->input('date');
        $date= $date??Carbon::now()->format('Y-m');
        $lists  = UserRechargeRecord::filterUser()
            ->when($date,function ($query,$date){
                /* @var Builder  $query*/
                return $query->where('created_at', 'like', "$date%");
            })
            ->where('status', 1)
            ->orderByDesc('id')
            ->limit($limit)
            ->offset($offset)
            ->get();


        return $this->success($lists);
    }

    public function createOrder()
    {
        try {
            /* @var Setting $setting*/
            $setting = Setting::first();
            if($setting->is_review) {
                throw new \Exception('暂不支持，请联系管理员');
            }
            \DB::beginTransaction();
            $comboId = \request()->input('id');
            $app = $this->getUniFactory(\user()->info->platform);
            $combo = RechargeCombo::find($comboId);

            // 避免重复创建订单
            $recharge = UserRechargeRecord::where('user_id', \user()->id)
                ->where('combo_id', $combo->id)
                ->where('status', 0)
                ->where('created_at','like', Carbon::now()->toDateString()."%")
                ->first();

            if($recharge){
                $pay = Pay::find($recharge->pay_id);
                $res = [
                    'order_id' => $pay->prepay_id,
                    'order_token' => $pay->token,
                    'pay_id' => $pay->pay_id,
                ];
            }else{
                $res = app(Pay::class)->create($app, $combo->price, Pay::SOURCE_RECHARGE);


                $recharge = new UserRechargeRecord();
                $recharge->user_id = \user()->id;
                $recharge->combo_id = $combo->id;
                $recharge->price = $combo->price;
                $recharge->gold = $combo->gold;
                $recharge->gift = $combo->gift;
                $recharge->pay_id = $res['pay_id'];
                $recharge->status = 0;
                $recharge->save();
                if(\user()->info->platform == 3){
                    $pay = Pay::find($res['pay_id']);
                    $res = $app->payment()->jssdk->bridgeConfig($pay->prepay_id);
                }

            }

            \DB::commit();
            return $this->success($res);
        }catch (QueryException $e){
            \DB::rollBack();
            return $this->error('下单失败');
        }catch (\Exception $e){
            \DB::rollBack();
            return $this->error($e->getMessage());
        }
    }
}
