<?php

namespace App\Controllers\Admin;

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

class BoardController extends AdminController
{
    use UpDownloadTrait;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        $this->_model = new BoardModel();
        parent::initController($request, $response, $logger);
        $this->_viewPath .= strtolower($this->_model->getClassName());
    }

    public function getFields(string $action = ""): array
    {
        $fields = ["category_uid", 'title', "board_file", "passwd",  "status", "content"];
        switch ($action) {
            case "index":
            case "excel":
                return ["category_uid", "user_uid",  'title', "board_file", "view_cnt", "status", "created_at"];
                break;
            case "view":
                return ["category_uid", "user_uid",  'title', "board_file", "view_cnt", "status", "created_at", "content"];
                break;
            default:
                return $fields;
                break;
        }
    }
    public function getFieldFilters(): array
    {
        return ["category_uid", "user_uid", "status"];
    }
    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;
            case 'board_file':
                $this->_viewDatas['fieldDatas'][$field] = $this->upload_file_procedure($field);
                break;
            default:
                return parent::getFieldFormData($field, $entity);
                break;
        }
        return $this->_viewDatas['fieldDatas'];
    }
}
