<?php

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

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

class UploadController extends Controller
{
    use AgentTrait;

    private Board $boardModel;
    private PostFile $postFileModel;

    public function __construct(Board $board, PostFile $postFile)
    {
        $this->boardModel = $board;
        $this->postFileModel = $postFile;
    }

    /**
     * 게시판 > 첨부파일(Upload)
     * @method GET
     * @see /admin/board/file/upload
     */
    public function index(Request $request)
    {
        $params = SearchData::fromRequest($request);
        $params->boardID = $request->get('board_id');

        $postFileData = $this->postFileModel->data($params);

        if ($postFileData->rows > 0) {
            $num = listNum($postFileData->total, $params->page, $params->perPage);
            foreach ($postFileData->list as $i => $row) {
                $row->num = $num--;
                $row->postURL = route('board.post.view', [$row->code, $row->post_id]);
                $row->fileLink = route('admin.board.file.download.show', $row->id);
                $row->editURL = route('admin.board.file.upload.edit', $row->id);
                $row->fileSize = byteFormat($row->file_size, 2);
                $row->fileExists = file_exists($row->file_path);
                $row->download = number_format($row->download);

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

        return view('admin.board.file.upload.index', [
            'postFileData' => $postFileData,
            'params' => $params,
            'boardData' => $this->boardModel->all()
        ]);
    }

    /**
     * 게시판 > 첨부파일(Upload) 삭제
     * @method DELETE
     * @see /admin/board/file/upload/destroy
     */
    public function destroy(Request $request)
    {
        try {

            $chk = $request->post('chk');

            if ($chk) {
                foreach ($chk as $postFileID) {
                    $postFile = $this->postFileModel->findOrNew($postFileID);

                    if(!$postFile->exists) {
                        throw new Exception($postFileID . '번 파일은 존재하지 않습니다.');
                    }

                    if(!$this->postFileModel->remove($postFileID)) {
                        throw new Exception($postFileID . "번 파일을 삭제할 수 없습니다.");
                    }
                }
            }

            $message = '첨부파일이 삭제되었습니다.';
            return redirect()->route('admin.board.file.upload.index')->with('message', $message);
        } catch (Exception $e) {
            return back()->withErrors($e->getMessage())->withInput();
        }
    }

    /**
     * 게시판 > 첨부파일(Upload) 상세 보기
     * @method GET
     * @see /admin/board/file/upload/{pk}/edit
     */
    public function edit(int $fileID)
    {
        $file = $this->postFileModel->findOrFail($fileID);
        $file->device = $this->device($file->user_agent);
        $file->platform = $this->platform($file->user_agent);
        $file->browser = $this->browser($file->user_agent);
        $file->fileLink = route('admin.board.file.download.show', $file->file_id);
        $file->fileSize = byteFormat($file->file_size, 2);
        $file->download = number_format($file->download);

        return view('admin.board.file.upload.edit', [
            'file' => $file
        ]);
    }
}
