<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\Moderator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ModeratorController extends Controller
{
    use ResponseJson;

    public function index()
    {
        $moderators = Moderator::keyword()->status()->orderByDesc('id')->paginate();
        return $this->success('ok', $moderators);
    }

    public function audited(Request $request, Moderator $moderator)
    {
        DB::beginTransaction();
        $status = $request->status;
        $content = $request->input('content');
        if (empty($status))
            throw new \Exception("状态参数无效");
        $moderator->update(['status'=>$status, 'content'=>$content]);
        if ($status == 1) {
            //通知 todo
        }elseif ($status == -1) {
            if (empty($content)) {
                DB::rollBack();
                return $this->failure("请输入拒绝理由");
            }
            //通知 todo
        }

        DB::commit();
        return $this->success('ok');
    }

    public function show(Moderator $moderator)
    {
        return $this->success('ok', $moderator);
    }
}
