<?php

namespace App\Controllers\Admin;

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

class BoardController extends \App\Controllers\Admin\AdminController
{
    private $_userModel = null;
    private $_user_uids = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->_className .= '/Board';
        $this->_model = new BoardModel();
        $this->_defines = [
            'insert' => [
                'fields' => ['board_category', 'title', 'content', 'passwd', 'confirmpassword', 'upload_file', 'status'],
                'fieldFilters'  => ['board_category', 'user_uid', 'status'],
                'fieldRules' => [
                    'board_category'        => 'required|string',
                    'title'                 => 'required|string',
                    'content'               => 'required|string',
                    'passwd'                => 'if_exist|trim|string',
                    'confirmpassword'       => 'if_exist|trim|matches[passwd]',
                    'view_cnt'              => 'if_exist|numeric',
                    'status'                => 'if_exist|string',
                    'upload_file'           => 'if_exist|uploaded[upload_file]|is_image[upload_file]|mime_in[upload_file,image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[upload_file,100]|max_dims[upload_file,1024,768]'
                ]
            ],
            'update' => [
                'fields' => ['board_category', 'title', 'content', 'passwd', 'confirmpassword', 'upload_file', 'status'],
                'fieldFilters'  => ['board_category', 'user_uid', 'status'],
                'fieldRules' => [
                    'board_category'        => 'required|string',
                    'title'                 => 'required|string',
                    'content'               => 'required|string',
                    'passwd'                => 'if_exist|trim|string',
                    'confirmpassword'       => 'if_exist|trim|matches[passwd]',
                    'view_cnt'              => 'if_exist|numeric',
                    'status'                => 'if_exist|string',
                    'upload_file'           => 'if_exist|uploaded[upload_file]|is_image[upload_file]|mime_in[upload_file,image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[upload_file,100]|max_dims[upload_file,1024,768]',
                ],
            ],
            'view' => [
                'fields' => ['board_category', 'user_uid', 'title',  'view_cnt', 'status', 'updated_at', 'created_at', 'content'],
                'fieldFilters'  => ['board_category', 'user_uid', 'status'],
                'fieldRules' => ['board_category', 'user_uid', 'status'],
            ],
            'index' => [
                'fields' => ['board_category', 'user_uid', 'title',  'view_cnt', 'status', 'updated_at', 'created_at'],
                'fieldFilters'  => ['board_category', 'user_uid', 'status'],
                'batchjobFilters' => ['board_category', 'user_uid', 'status'],
            ],
            'excel' => [
                'fields' => ['board_category', 'user_uid', 'title',  'view_cnt', 'status', 'updated_at', 'created_at'],
                'fieldFilters'  => ['board_category', 'user_uid', 'status'],
            ],
        ];
        helper($this->_className);
        $this->_viewPath = strtolower($this->_className);
        $this->_viewDatas['title'] = lang($this->_className . '.title');
        $this->_viewDatas['className'] = $this->_className;
    }

    private function getUserModel(): UserModel
    {
        return is_null($this->_userModel) ? new UserModel() : $this->_userModel;
    }

    //Field별 Form Option용
    protected function getFieldFormOption(string $field): array
    {
        switch ($field) {
            case 'user_uid':
                if (is_null($this->_user_uids)) {
                    //모든 필요한 FormOption등 조기화작업 필요
                    $this->_user_uids = $this->getUserModel()->getFieldFormOptions(['status' => 'use']);
                }
                return $this->_user_uids;
            default:
                return parent::getFieldFormOption($field);
                break;
        }
    }

    //Field별 Form Datas 처리용
    protected function getFieldFormData(string $field, $entity = null)
    {
        switch ($field) {
            case 'upload_file':
                $this->_viewDatas['fieldDatas'][$field] = $this->single_upload_procedure($field, $entity);
                break;
            default:
                return parent::getFieldFormData($field, $entity);
                break;
        }
    }

    ////Action 모음
    //Insert관련
    protected function insert_process()
    {
        // upload처리
        return parent::insert_process();
    }
    final public function insert()
    {
        return $this->insert_procedure();
    }
    //Update관련
    final public function update($uid)
    {
        return $this->update_procedure($uid);
    }
    //Reply 관련
    final public function reply(int $uid)
    {
        return $this->reply_procedure($uid);
    }
    //Toggle관련
    final public function toggle($uid, string $field)
    {
        return $this->toggle_procedure($uid, $field);
    }
    //Batchjob 관련
    final public function batchjob()
    {
        return $this->batchjob_procedure();
    }
    //Delete 관련
    final public function delete($uid)
    {
        return $this->delete_procedure($uid);
    }
    //View 관련
    protected function view_process($entity)
    {
        // view_cnt에 추가하기위함
        $this->_model->increaseViewCount($entity->getPrimaryKey());
        return parent::view_process($entity);
    }
    final public function view($uid)
    {
        return $this->view_procedure($uid);
    }
    //Index 관련
    final public function index()
    {
        return $this->index_procedure();
    }
    //Excel 관련
    final public function excel()
    {
        return $this->excel_procedure();
    }
}
