<?php

namespace App\Http\Controllers\Welfare;

use App\Models\Welfare\PrizeModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Miniprogram\Auth;
use App\Models\User\UserModel;
use App\Models\Welfare\JigsawFlowModel;
use App\Models\Welfare\StoreModel;

class JigsawController extends Controller
{

    /**
     * 拼图明细
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function flows(Request $request)
    {
        $uid = Auth::auth();
        $per_page = $request->input('per_page', 20);
        $page = $request->input('page', 1);
        $offset = ($page - 1) * $per_page;
        $flows = JigsawFlowModel::where('uid', $uid);
        $count = $flows->count();
        $list = $flows->orderBy('id', 'asc')->offset($offset)->limit($per_page)->get();
        return response()->json([
            'code' => 200,
            'data' => [
                'list' => $list,
                'count' => $count,
                'per_page' => $per_page,
                'page' => $page
            ]
        ]);
    }


    /**
     * 商城列表
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function stores(Request $request)
    {
        $stores = StoreModel::where('state', 1)->orderBy('sort', 'desc')->get();
        return response()->json([
            'code' => 200,
            'data' => [
                'list' => $stores,
            ]
        ]);
    }


    /**
     * 兑换
     * @param  Request  $request
     * @return array|\Illuminate\Http\JsonResponse
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function shopping(Request $request)
    {
        $uid = Auth::auth();
        $validator = \Validator::make($request->all(), [
            'id' => 'required|integer',
        ], [
            'id.*' => '请选择一个商品',
        ]);
        if ($validator->fails()) {
            return response()->json([
                'code' => 422,
                'message' => $validator->errors()->first(),
                'errors' => $validator->errors()
            ]);
        }

        $user = UserModel::find($uid);
        \DB::beginTransaction();

        // 判断商品ID是否正确
        $goods = StoreModel::find($request->id);
        if (!$goods) {
            return response()->json([
                'code' => 404,
                'message' => '此商品不存在'
            ]);
        }
        // 判断是否上下架
        if ($goods->state == 0) {
            return ['code' => 10002, 'message' => '商品已下架'];
        }

        // 判断用户水晶是否足够
        if ($user->jigsaw_amount < $goods->price) {
            return ['code' => 10003, 'message' => '拼图不足'];
        }

        // 判断库存
        if ($goods->count <= 0) {
            return ['code' => 10004, 'message' => '商品已兑换完'];
        }
//        $amount = $goods->price;
//        while ($amount > 0) {
//            $jigasw = JigsawFlowModel::where('uid',$uid)->where('remains_amount','>',0)->orderBy('created_at','asc')->first();
//            if ($amount > $jigasw->remains_amount) {
//                $jigasw->decrement('remains_amount', $jigasw->remains_amount);
//                $amount -= $jigasw->remains_amount;
//            }else{
//                $jigasw->decrement('remains_amount', $amount);
//                $amount -= $amount;
//            }
//        }

        // 加入奖品
        $prize = PrizeModel::create([
            'uid' => $uid,
            'title' => $goods->title,
            'cover_url' => $goods->cover_url,
            'cost' => $goods->cost
        ]);

        // 扣拼图余额
        $user->decrement('jigsaw_amount', $goods->price);
        // 扣货存
        $goods->decrement('count');

        \DB::commit();
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => $prize
        ]);
    }
}
