<?php

namespace App\Models;

use App\Entities\CategoryEntity;

class CategoryModel extends BaseHierarchyModel
{
    //BaseHierarchyModel를 확장하면 grpno가 숫자이고, primarykey를 대분류 생성시 copy하여 grpno에 넣고 sorting하므로
    protected $table = "tw_category";
    protected $returnType = CategoryEntity::class;
    public function __construct()
    {
        parent::__construct('Category');
        $this->allowedFields = [...$this->allowedFields, 'name',  "head", "tail", "status"];
        $this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
    }
    public function getTitleField(): string
    {
        return 'name';
    }
    public function getContentField(): string
    {
        return 'head';
    }
    protected function getFieldRule(string $field, array $rules, string $action = ""): array
    {
        switch ($field) {
            case $this->getTitleField():
                $rules[$field] =  "required|trim|string";
                break;
            case "head":
            case "tail":
                $rules[$field] =  "if_exist|string";
                break;
            default:
                $rules = parent::getFieldRule($field, $rules, $action);
                break;
        }
        return $rules;
    }

    //Form 선택용 Options Data용
    public function getOptions(array $conditions = array(), $options = array()): array
    {
        //대분류 부분은 선택이 되지 않게 하기위해 따로 만듬 (form_dropdown의 optgroup 기능)
        $old_title = "";
        foreach ($this->where($conditions)->orderby("grpno DESC, grporder ASC")->findAll() as $entity) {
            if ($entity->getPrimaryKey() == $entity->getHierarchy_No()) {
                $options[$entity->getTitle()] = [];
                $old_title = $entity->getTitle();
            } else {
                // STATUS가 use가 아닐때 option을 disabled되게 하기위함
                if ($entity->getStatus() != DEFAULTS['STATUS']) {
                    $options[$old_title][$entity->getPrimaryKey() . "\" disabled=\"disabled"] = $entity->getTitle();
                } else {
                    $options[$old_title][$entity->getPrimaryKey()] = $entity->getTitle();
                }
            }
        }
        return $options;
    }

    public function getEntity($conditions): CategoryEntity
    {
        return parent::getEntity($conditions);
    }

    public function create(array $formDatas): CategoryEntity
    {
        return $this->create_process(new CategoryEntity(), $formDatas);
    }
    public function modify(CategoryEntity $entity, array $formDatas): CategoryEntity
    {
        return $this->modify_process($entity, $formDatas);
    }
    public function reply($parent_entity, array $formDatas): CategoryEntity
    {
        return $this->reply_process($parent_entity, new CategoryEntity(), $formDatas);
    }

    //Index관련
    public function setIndexWordFilter(string $word)
    {
        parent::setIndexWordFilter($word);
        $this->orLike($this->getTitleField(), $word, "both");
        $this->orLike("head", $word, "both"); //befor , after , both
        $this->orLike("tail", $word, "both"); //befor , after , both
    }
}
