<?php

namespace App\Http\Controllers\V1;

use App\Http\Requests\StoreCourse;
use App\Models\CourseUser;
use App\Services\Api\CourseService;
use Illuminate\Http\Request;
use PHPUnit\Util\Exception;

class CourseController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->user = auth('api')->user();
        $this->CourseService = new CourseService();
    }

    //新增球场会员
    public function addCourseUser(StoreCourse $request)
    {
        try {
            $courseUser = CourseUser::query()
                ->where([
                    'course_id' => $request->course_id,
                    'user_id' => $this->user->id,
                ])
                ->whereNull('deleted_at')
                ->first();
            if ($courseUser) {
                throw new Exception(trans('api.CANNOT_BE_ADDED_REPEATED'));
            } else {
                $courseUser = new CourseUser();
                $courseUser->course_id = $request->course_id;
                $courseUser->user_id = $this->user->id;
                $courseUser->type = $request->type;
                $courseUser->member_name = $request->member_name;
                $courseUser->member_no = $request->member_no;
                $courseUser->whs = $request->whs;
                $courseUser->status = 0; //待审核
                if (!$courseUser->save()) {
                    throw new Exception(trans('api.DATA_SAVE_FALSE'));
                }
            }
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }

        return $this->success();
    }

    //球场会员
    public function userCourses(Request $request)
    {
        $status = $request->input('status', 1);

        $list = $this->CourseService->getUserCourseList($status, $this->user->id);

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

    //球场列表
    public function courseList()
    {
        $list = $this->CourseService->getCourseList();

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

    //附近的球场
    public function nearby(Request $request)
    {
        try {
            $lng = $request->input('lng');
            $lat = $request->input('lat');

            if (empty($lng) || empty($lat)) {
                throw new Exception(trans('api.PARAMS_ERROR'));
            }
            $list = $this->CourseService->getNearByCourses($lng, $lat);

        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }

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

    //修改会员主场
    public function setMainCourse(Request $request)
    {
        try {
            $user_id = $this->user->id;
            $course_id = $request->input('course_id');

            CourseUser::where('user_id', $user_id)->update(['type' => 2]);
            $courseUser = CourseUser::query()->where(['user_id' => $user_id, 'course_id' => $course_id])->first();
            $courseUser->type = 1;
            $courseUser->save();

        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
        return $this->success();
    }


}
