<?php

namespace App\Http\Controllers\Miniprogram;

use App\Models\Deed\DeedModel;
use App\Models\Deed\InvitationCardModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User\UserModel;
use App\Models\ComplaintModel;

class ComplaintController extends Controller
{
    public function create(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|max:16',
            'content' => 'required|max:200',
            'images' => 'array',
            'phone' => 'required|integer',
            'type' => 'required|in:partner,user,pair,contract,game,goodnight,contact,question',
            'value' => 'required',
        ]);
        $uid = Auth::auth();
        $data = $request->all();
        $search = [];

        $user = UserModel::find($uid);
        $be_uid = 0;

        try {
            switch ($request->type) {
                case 'partner':
                    $partner = \App\Models\PartnerModel::find($request->value);
                    $be_uid = $partner->uid;
                    break;
                case "contract":
                    $deed = DeedModel::find($request->value);
                    $be_uid = $deed->uid;
                    switch ($deed->type) {
                        case "0":
                            $data['type'] = "contact";
                            break;
                        case "1":
                            $data['type'] = "pair";
                            break;
                        case "2":
                        case "3":
                            $data['type'] = "game";
                            break;
                        case "4":
                            $data['type'] = "goodnight";
                            break;
                    }
                    break;
                case "contact":
                    $contact = InvitationCardModel::findOrFail($request->value);
                    if ($contact->uid == $uid) {
                        $be_uid = $contact->invite_uid;
                    } else {
                        $be_uid = $contact->uid;
                    }
                    break;
                case "pair":
                    $pair = \App\Models\Fpdx\PairModel::findOrFail($request->value);
                    $be_uid = $pair->uid;
                    $data['value'] = $pair->id;
                    break;
                case "game":
                    $game = \App\Models\Gteam\EnterModel::findOrFail($request->value);
                    $be_uid = $game->uid;
                    $data['value'] = $game->id;
                    break;
                case "goodnight":
                    $goodnight = \App\Models\Goodnight\EnterModel::findOrFail($request->value);
                    ;
                    $be_uid = $goodnight->uid;
                    $data['value'] = $goodnight->id;
                    break;
                case "question":
                    $question = \App\Models\User\LikeInviteQuestionModel::find($request->value);
                    $be_uid = $question->uid;
                    $data['value'] = $question->id;
                    break;
            }
            $be_user = UserModel::findOrFail($be_uid);
            // 添加搜索关键字
            array_push($search, $request->phone);
            array_push($search, $user->nickname);
            array_push($search, $user->uid);
            array_push($search, $user->phone);
            if ($be_user) {
                array_push($search, $be_user->nickname);
                array_push($search, $be_user->uid);
                array_push($search, $be_user->phone);
            }
            $data['uid'] = $uid;
            $data['be_uid'] = $be_uid;
            $data['search'] = $search;
            ComplaintModel::create($data);
        } catch (\Exception $e) {
            app('sentry')->captureException($e);
        }
        return ['code' => 200, 'message' => 'OK'];
    }


    public function show(Request $request)
    {
        $this->validate($request, [
            'type' => 'in:partner,user,pair,contract,question',
            'value' => 'required',
        ]);
        $uid = Auth::auth();
        $type = $request->input('type', 'contract');
        $value = $request->value;
        $builder = ComplaintModel::select('id', 'content', 'images', 'reply_content', 'reply_at', 'title')
            ->where(['uid' => $uid, 'value' => $value]);
        switch ($type) {
            case "contract":
                $complaint = $builder->whereIn(
                    'type',
                    ['deed_contact', 'deed_goodnight', 'deed_fpdx', 'deed_gteam']
                )->first();
                break;
            case "partner":
                $complaint = $builder->where('type', $type)->first();
                break;
            default:
                $complaint = $builder->where('type', $type)->first();
        }
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => $complaint
        ]);
    }
}
