<?php

namespace App\Controllers\Admin;

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

class LoggerController 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 .= '/Logger';
        $this->_model = new LoggerModel();
        $this->_defines = [
            'view' => [
                'fields' => ['user_uid', 'title', 'content', 'status', 'created_at'],
                'fieldFilters'  => ['user_uid', 'status'],
                'fieldRules' => [],
            ],
            'index' => [
                'fields' => ['user_uid', 'title', 'status', 'created_at'],
                'fieldFilters'  => ['user_uid', 'status'],
                'batchjobFilters' => [],
            ],
            'excel' => [
                'fields' => ['user_uid', 'title', 'status', 'created_at'],
                'fieldFilters'  => ['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 = [DEFAULT_EMPTY => lang($this->_className . '.label.' . $field) . ' 선택'];
                    foreach ($this->getUserModel()->findAll() as $user) {
                        $this->_user_uids[$user['uid']] = $user['name'];
                    }
                }
                return $this->_user_uids;
                break;
            default:
                return parent::getFieldFormOption($field);
                break;
        }
    }
}
