<?php

namespace App\Http\Controllers\Gteam;

use App\Http\Controllers\Miniprogram\Auth;
use App\Http\Resources\Gteam\EnterResource;
use App\Models\Gteam\EnterModel;
use App\Services\Gteam\EnterService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class EnterController extends Controller
{
    private $type = [
        "wz" => ["wz_wx", "wz_qq"],
        "cj" => ["cj_wx", "cj_qq"],
    ];

    public function store(Request $request)
    {
        $this->validate($request, [
            'activity_id' => 'required',
            'type' => 'required',
        ]);
        $uid = Auth::auth();
        $activity_id = $request->activity_id;
        $rank_type = $request->input('rank_type', 4);
        $game_type = $request->type;
        $sex = $request->input('sex', 0);

        \DB::beginTransaction();
        try {
            $enterService = new EnterService();
            $ack = $enterService->enter($uid, $activity_id, $game_type, $sex, $rank_type);

            $enterService->push($ack);

            \DB::commit();
            return response()->json([
                'code' => 200,
                'message' => 'OK',
                'data' => [
                    'id' => $ack
                ]
            ]);
        } catch (\Exception $e) {
            app('sentry')->captureException($e);
            \DB::rollBack();
            throw $e;
        }
    }

    /**
     * 预约的列表
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function index(Request $request)
    {
        $uid = Auth::auth();
        $this->validate($request, [
            'type' => 'in:wz,cj',
        ]);
        $builder = EnterModel::where('uid', $uid)->when($request->filled('type'), function ($query) use ($request) {
            return $query->whereIn('type', $this->type[$request->type]);
        })->where('activity_id', '>', 0);
        $enters = $builder->orderBy('created_at', 'desc')->paginate($request->get('per_page', 20));
        foreach ($enters as $enter) {
            $enter->activity;
        }
        return response()->json([
            'code' => 200,
            'data' => $enters
        ]);
    }

    public function show(Request $request, $id)
    {
        $uid = Auth::auth();
        $enter = EnterModel::findOrFail($id);
        $enter->room;
        $enter->activity;
        return new EnterResource($enter);
    }

    /**
     * 活动男女数量
     * @param  int  $activity_id
     * @return array
     */
    public function sexproportion(int $activity_id)
    {
        $es = new EnterService();
        $data = $es->sexproportion($activity_id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }


    /**
     * 取消报名
     * @param  Request  $request
     * @param $enter_id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request, $enter_id)
    {
        $service = new EnterService();
        $service->unenter($enter_id);
        return response()->json([
            "code" => 200,
            "message" => "OK"
        ]);
    }
}
