<?php

namespace App\Http\Controllers\Admin\Pair;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Fpdx\GroupModel;

class GroupController extends Controller
{


    /**
     * 创建群组
     * @param  Request  $request
     * @return array
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'group_name' => 'required|max:32',
            'stage_id' => 'required|integer',
            'activity_type' => 'in:72h,qbj',
            'number' => 'required|integer',
            'qrcode' => 'required|url',
            'type' => 'required|in:pair,alone',
            'qq_group_number' => 'required|integer',
        ]);
        $group = new GroupModel();
        $group->fill($request->post());
        $group->save();
        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $group
        ]);
    }


    /**
     * 查看群组
     * @param  Request  $request
     * @return array
     */
    public function show($id)
    {
        $group = GroupModel::findOrFail($id);
        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $group
        ]);
    }


    /**
     * 群组列表
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function index(Request $request)
    {
        $this->validate($request, [
            'stage_id' => 'integer',
            'activity_type' => 'in:72h,qbj',
        ]);
        $groups = GroupModel::when($request->filled('stage_id'), function ($query) use ($request) {
            return $query->where('stage_id', $request->stage_id);
        })->when($request->filled('activity_type'), function ($query) use ($request) {
            return $query->where('activity_type', $request->activity_type);
        })
            ->orderBy('id', 'desc')
            ->get();
        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $groups
        ]);
    }


    /**
     * 修改群组
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'group_name' => 'filled|max:32',
            'stage_id' => 'filled|integer',
            'activity_type' => 'in:72h,qbj',
            'number' => 'filled|integer',
            'qrcode' => 'filled|url',
            'type' => 'filled|in:pair,alone',
            'qq_group_number' => 'filled|integer',
        ]);
        $group = GroupModel::findOrFail($id);
        $group->fill($request->all());
        $group->save();
        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $group
        ]);
    }
}
