<?php

namespace App\Controllers\Admin;

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

class BoardController extends AdminController
{
    use UpDownloadTrait;
    private $_category_notice = 3;
    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 = ["category", 'title', "board_file", "passwd",  "status", "content"];
        switch ($action) {
            case "index":
            case "excel":
                return ["category", "user_uid",  'title', "board_file", "view_cnt", "status", "created_at"];
                break;
            case "view":
                return ["category", "user_uid",  'title', "board_file", "view_cnt", "status", "created_at", "content"];
                break;
            default:
                return $fields;
                break;
        }
    }
    public function getFieldFilters(): array
    {
        return ["category", "user_uid", "status"];
    }
    public function getFieldBatchFilters(): array
    {
        return parent::getFieldBatchFilters();
    }
    //Field별 Form Datas 처리용
    protected function getFieldFormData(string $field, $entity = null): array
    {
        switch ($field) {
            case 'board_file':
                $file = $this->upload_file_procedure($field);
                if (!is_null($file)) {
                    $this->_viewDatas['fieldDatas'][$field] = $file;
                }
                break;
            default:
                return parent::getFieldFormData($field, $entity);
                break;
        }
        return $this->_viewDatas['fieldDatas'];
    }

    private function build_notice()
    {
        $entitys = $this->_model->getEntitys(['category' => $this->_category_notice, 'status' => DEFAULTS['STATUS']]);
        $temps = array("<ul>");
        foreach ($entitys as $entity) {
            array_push($temps, sprintf(
                "<li><a href=\"/front/board/view/%s?category=3\">%s</a><span>%s</span></li>",
                $entity->getPrimaryKey(),
                $entity->getTitle(),
                $entity->created_at ? str_split($entity->created_at, 10)[0] : "",
            ));
        }
        array_push($temps, "</ul>");
        // echo var_export($temps, true);
        // exit;
        file_put_contents(APPPATH . 'Views' . "/layouts/main/board.php", implode("\n", $temps));
    }

    //Insert관련
    protected function insert_process()
    {
        $entity = parent::insert_process();
        if ($entity->category == $this->_category_notice) {
            $this->build_notice();
        }
        return $entity;
    }
    //Update관련
    protected function update_process($entity)
    {
        $entity = parent::update_process($entity);
        if ($entity->category == $this->_category_notice) {
            $this->build_notice();
        }
        return $entity;
    }
    //Toggle관련
    protected function toggle_process($entity)
    {
        $entity = parent::toggle_process($entity);
        if ($entity->category == $this->_category_notice) {
            $this->build_notice();
        }
        return $entity;
    }
    //Delete 관련
    protected function delete_process($entity)
    {
        $entity = parent::delete_process($entity);
        if ($entity->category->$this->_category_notice) {
            $this->build_notice();
        }
        return $entity;
    }
}
