<?php

namespace App\Http\Controllers\Admin\Board\Trash;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Traits\AgentTrait;
use App\Models\Board;
use App\Models\PostDeleted;
use App\Models\DTO\SearchData;

class PostController extends Controller
{
    use AgentTrait;

    private Board $boardModel;
    private PostDeleted $postDeletedModel;

    public function __construct(Board $board, PostDeleted $postDeleted)
    {
        $this->boardModel = $board;
        $this->postDeletedModel = $postDeleted;
    }

    /**
     * 게시판 > 휴지통 관리 (게시글)
     * @method GET
     * @see /admin/board/trash/post
     */
    public function index(Request $request)
    {
        $params = SearchData::fromRequest($request);
        $params->boardID = $request->get('board_id');

        $postDeletedData = $this->postDeletedModel->data($params);

        if ($postDeletedData->rows > 0) {
            $num = listNum($postDeletedData->total, $params->page, $params->perPage);
            foreach ($postDeletedData->list as $i => $row) {
                $row->num = $num--;
                $row->editURL = route('admin.board.trash.post.edit', $row->id);

                $postDeletedData->list[$i] = $row;
            }
        }

        return view('admin.board.trash.post.index', [
            'postDeletedData' => $postDeletedData,
            'params' => $params,
            'boardData' => $this->boardModel->all()
        ]);
    }

    /**
     * 게시판 > 휴지통 관리(게시글) > 전체삭제
     * @method GET
     * @see /admin/board/trash/post/truncate
     */
    public function truncate()
    {
        if(!$this->postDeletedModel->truncate()) {
            return back()->withErrors("휴지통 전체 삭제에 실패하였습니다.")->withInput();
        }

        $message = '휴지통에 게시글을 전체 삭제하였습니다.';
        return redirect()->route('admin.board.trash.post.index')->with('message', $message);
    }

    /**
     * 게시판 > 휴지통 관리(게시글) > 선택삭제
     * @method GET
     * @see /admin/board/trash/post/destroy
     */
    public function destroy(Request $request)
    {
        $chk = $request->post('chk');

        if ($chk) {
            foreach ($chk as $deletedID) {
                if(!$this->postDeletedModel->remove($deletedID)) {
                    return back()->withErrors($deletedID . "번 게시글을 삭제할 수 없습니다.")->withInput();
                }
            }
        }

        $message = '게시글이 삭제되었습니다.';
        return redirect()->route('admin.board.trash.post.index')->with('message', $message);
    }

    /**
     * 게시판 > 휴지통 관리(게시글) > 복원하기
     * @method POST
     * @see /admin/board/trash/post/recovery
     */
    public function recovery(Request $request)
    {
        $chk = $request->post('chk');

        if ($chk) {
            foreach ($chk as $deletedID) {
                if(!$this->postDeletedModel->recovery($deletedID)) {
                    return back()->withErrors($deletedID . "번 게시글을 복원할 수 없습니다.")->withInput();
                }
            }
        }

        $message = '게시글이 복원되었습니다.';
        return redirect()->route('admin.board.trash.post.index')->with('message', $message);
    }

    /**
     * 게시판 > 휴지통 관리(게시글) > 상세보기
     * @method GET
     * @see /admin/board/trash/post/{pk}/edit
     */
    public function edit(int $deletedID)
    {
        $deleted = $this->postDeletedModel->findOrFail($deletedID);
        $deleted->device = $this->device($deleted->user_agent);
        $deleted->platform = $this->platform($deleted->user_agent);
        $deleted->browser = $this->browser($deleted->user_agent);

        return view('admin.board.trash.post.edit', [
            'deleted' => $deleted
        ]);
    }
}
