<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use App\Models\PostBlame;
use App\Models\CommentBlame;

class AjaxController extends Controller
{
    /**
     * 게시판 신고처리 결과 등록
     */
    public function postBlameResultUpdate(Request $request)
    {
        $rules = [
            'post_blame_id' => 'required|exists:tb_post_blame,id',
            'status' => 'required|numeric|in:0,1',
            'memo' => 'required',
        ];

        $attributes = [
            'post_blame_id' => '신고 PK',
            'status' => '처리 상태',
            'memo' => '처리 내용',
        ];

        $validator = Validator::make($request->all(), $rules, [], $attributes);
        if ($validator->fails()) {
            return json_encode(['success' => 0, 'message' => $validator->errors()->first()]);
        }
        $posts = $validator->valid();

        if((new PostBlame)->postBlameResultUpdate($posts['post_blame_id'], $posts['status'], $posts['memo'])) {
            return json_encode(['success' => 1, 'message' => '신고 결과를 변경하였습니다.']);
        }
    }

    /**
     * 댓글 신고처리 결과 등록
     */
    public function commentBlameResultUpdate(Request $request)
    {
        $rules = [
            'comment_blame_id' => 'required|exists:tb_comment_blame,id',
            'status' => 'required|numeric|in:0,1',
            'memo' => 'required',
        ];

        $attributes = [
            'comment_blame_id' => '신고 PK',
            'status' => '처리 상태',
            'memo' => '처리 내용',
        ];

        $validator = Validator::make($request->all(), $rules, [], $attributes);
        if ($validator->fails()) {
            return json_encode(['success' => 0, 'message' => $validator->errors()->first()]);
        }
        $posts = $validator->valid();

        if((new CommentBlame)->commentBlameResultUpdate($posts['comment_blame_id'], $posts['status'], $posts['memo'])) {
            return json_encode(['success' => 1, 'message' => '신고 결과를 변경하였습니다.']);
        }
    }
}
