<?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)
    {
        $this->_model = new BoardModel($this->getFields());
        parent::initController($request, $response, $logger);
        $this->_viewPath .= strtolower($this->_model->getClassName());
    }
    public function getFields(string $action = ""): array
    {
        $fields = ['title', "passwd", "content"];
        switch ($action) {
            case "index":
            case "excel":
                return ['title', "board_file", "view_cnt", "created_at"];
                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();
    }
    //Field별 Form Datas 처리용
    protected function getFieldFormData(string $field, $entity = null): array
    {
        switch ($field) {
            case 'passwd':
                $this->_viewDatas['fieldDatas'][$field] = $this->request->getVar($field);
                $this->_viewDatas['fieldDatas']['confirmpassword'] = $this->request->getVar('confirmpassword');
                break;
            default:
                return parent::getFieldFormData($field, $entity);
                break;
        }
        return $this->_viewDatas['fieldDatas'];
    }

    //Insert관련
    protected function insert_form_process()
    {
        //권한체크
        $this->isRole('insert');
        parent::insert_form_process();
        $this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => [
            'category_uid' => $this->_category,
            'category' => $this->_category
        ]];
    }
    protected function insert_process()
    {
        //권한체크
        $this->isRole('insert');
        return parent::insert_process();
    }
    //Update관련
    protected function update_form_process($entity)
    {
        //본인이 작성한글인지 최종확인용 정상접속이 아닌 위회해서 수정을 시도방지용
        if (!$this->_viewDatas[SESSION_NAMES['ISLOGIN']] || $entity->getUser_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->getUser_Uid() == $this->_viewDatas['auth'][AUTH_FIELDS['ID']]) {
            throw new \Exception("작성자 본인글인지 여부가 확인되지 않습니다.");
        }
        return parent::update_process($entity);
    }
    //Reply관련
    protected function reply_form_process($entity)
    {
        //권한체크
        $this->isRole('reply', $entity);
        return parent::reply_form_process($entity);
    }
    //Delete 관련
    protected function delete_process($entity)
    {
        //본인이 작성한글인지 최종확인용 정상접속이 아닌 위회해서 삭제 시도 방지용
        if (!$this->_viewDatas[SESSION_NAMES['ISLOGIN']] || $entity->getUser_Uid() == $this->_viewDatas['auth'][AUTH_FIELDS['ID']]) {
            throw new \Exception("작성자 본인글인지 여부가 확인되지 않습니다.");
        }
        return parent::delete_process($entity);
    }
    //View관련
    protected function view_process($entity)
    {
        //권한체크
        $this->isRole('view', $entity);
        //조회수 올리기
        $this->_model->addViewCount($entity);
        return parent::view_process($entity);
    }
    //Index관련
    protected function index_process()
    {
        //권한체크
        $this->isRole('index');
        return parent::index_process();
    }
    //Category 및 Status 조건추가
    protected function index_setCondition()
    {
        $this->_model->where("category_uid", $this->_viewDatas['category']->getPrimaryKey());
        $this->_model->where("status", DEFAULTS['STATUS']);
        parent::index_setCondition();
    }
    //Download관련
    public function download_process($entity)
    {
        //권한체크
        $this->isRole('download', $entity);
        return parent::download_process($entity);
    }
}
