<?php

namespace App\Controllers\Front;

use App\Models\BoardModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class BoardController extends FrontController
{
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->_model = new BoardModel();
        $this->_viewDatas['className'] = 'Board';
        $this->_viewPath .= strtolower($this->_viewDatas['className']);
        $this->_viewDatas['title'] = lang($this->_viewDatas['className'] . '.title');
        $this->_viewDatas['class_icon'] = CLASS_ICONS[strtoupper($this->_viewDatas['className'])];
        helper($this->_viewDatas['className']);
    }
    public function getFields(string $action = ""): array
    {
        $fields = ['title', "passwd", "content"];
        switch ($action) {
            case "index":
            case "excel":
                return ['title', "board_file", "created_at", "view_cnt"];
                break;
            case "view":
                return ['title', "board_file", "view_cnt", "created_at", "content"];
                break;
            default:
                return $fields;
                break;
        }
    }
    public function getFieldFilters(): array
    {
        return [];
    }
    public function getFieldBatchFilters(): array
    {
        return parent::getFieldBatchFilters();
    }

    //Insert관련
    protected function insert_form_process()
    {
        //Category 확인
        $this->setCategory($this->request->getVar('category'));
        //권한체크
        $this->isRole('isaccess');
        parent::insert_form_process();
    }
    protected function insert_process()
    {
        //권한체크
        $this->isRole('iswrite');
        return parent::insert_process();
    }
    //Update관련
    protected function update_form_process($entity)
    {
        //Category 확인
        $this->setCategory($this->request->getVar('category'));
        //본인이 작성한글인지 최종확인용 정상접속이 아닌 위회해서 수정을 시도방지용
        if (!$this->_viewDatas[SESSION_NAMES['ISLOGIN']] || $entity->user_uid != $this->_viewDatas['auth'][AUTH_FIELDS['ID']]) {
            throw new \Exception("작성자 본인글인지 여부가 확인되지 않습니다.");
        }
        return parent::update_form_process($entity);
    }
    protected function update_process($entity)
    {
        //본인이 작성한글인지 최종확인용 정상접속이 아닌 위회해서 수정을 시도방지용
        if (!$this->_viewDatas[SESSION_NAMES['ISLOGIN']] || $entity->user_uid != $this->_viewDatas['auth'][AUTH_FIELDS['ID']]) {
            throw new \Exception("작성자 본인글인지 여부가 확인되지 않습니다.");
        }
        return parent::update_process($entity);
    }
    //Reply관련($entity는 부모의것임을 주의)
    protected function reply_form_process($entity)
    {
        //Category 확인
        $this->setCategory($this->request->getVar('category'));
        //권한체크
        $this->isRole('isreply');
        return parent::reply_form_process($entity);
    }
    //Delete 관련
    protected function delete_process($entity)
    {
        //본인이 작성한글인지 최종확인용 정상접속이 아닌 위회해서 삭제 시도 방지용
        if (!$this->_viewDatas[SESSION_NAMES['ISLOGIN']] || $entity->user_uid == $this->_viewDatas['auth'][AUTH_FIELDS['ID']]) {
            throw new \Exception("작성자 본인글인지 여부가 확인되지 않습니다.");
        }
        return parent::delete_process($entity);
    }
    //View관련
    protected function view_process($entity)
    {
        //Category 확인
        $this->setCategory($this->request->getVar('category'));
        //권한체크
        $this->isRole('view');
        //조회수 올리기
        $entity = $this->_model->addViewCount($entity);
        return parent::view_process($entity);
    }
    //Index관련
    protected function index_process()
    {
        //Category 확인
        $this->setCategory($this->request->getVar('category'));
        //권한체크
        $this->isRole('index');
        parent::index_process();
    }
    //Category 및 Status 조건추가
    protected function index_condition()
    {
        $this->_model->where("category", $this->_viewDatas['currentCategory']->getPrimaryKey());
        $this->_model->where("status", DEFAULTS['STATUS']);
        parent::index_condition();
    }
    //Download관련
    public function download_process($field, $entity)
    {
        //Category 확인
        $this->setCategory($this->request->getVar('category'));
        //권한체크
        $this->isRole('download');
        return parent::download_process($field, $entity);
    }
}
