<?php

namespace App\Forms;

use App\Forms\CommonForm;

class BoardForm extends CommonForm
{
  public function __construct()
  {
    parent::__construct();
  }
  public function action_init_process(string $action, array &$formDatas = []): void
  {
    parent::action_init_process($action, $formDatas);
    $fields = [
      'category',
      'worker_uid',
      'title',
      'status',
      'content',
    ];
    $filters = [
      'user_uid',
      'worker_uid',
      'category',
      'status',
    ];
    $indexFilter = $filters;
    $batchjobFilters = ['user_uid', 'category', 'status'];
    switch ($action) {
      case 'view':
        $fields = [
          'category',
          'worker_uid',
          'title',
          'status',
          'created_at',
          'content'
        ];
        break;
      case 'index':
        $fields = [
          'category',
          'worker_uid',
          'title',
          'status',
          'created_at'
        ];
        break;
      case 'download':
        $fields = [
          'category',
          'worker_uid',
          'title',
          'status',
          'created_at',
          'content'
        ];
        break;
    }
    $this->setFormFields($fields);
    $this->setFormRules($fields);
    $this->setFormFilters($filters);
    $this->setFormOptions($filters, $formDatas);
    $this->setIndexFilters($indexFilter);
    $this->setBatchjobFilters($batchjobFilters);
  }
  public function getFormRule(string $field, array $formRules): array
  {
    switch ($field) {
      case "category":
      case "title":
        $formRules[$field] = "required|trim|string";
        break;
      case "user_uid":
        $formRules[$field] = "required|numeric";
        break;
      case "worker_uid":
        $formRules[$field] = "permit_empty|numeric";
        break;
      case "content":
        $formRules[$field] = "permit_empty|string";
        break;
      default:
        $formRules = parent::getFormRule($field, $formRules);
        break;
    }
    return $formRules;
  }
  public function getFormOption(string $field, array $formDatas = [], array $options = ['options' => [], 'atttributes' => []]): array
  {
    $tempOptions = ['' => lang("{$this->getAttribute('class_path')}.label.{$field}") . " 선택"];
    switch ($field) {
      case 'worker_uid':
        foreach ($this->getFormOption_process(service('userservice'), $field, $formDatas) as $tempEntity) {
          $tempOptions[$tempEntity->getPK()] = $tempEntity->getTitle();
          // $options['attributes'][$tempEntity->getPK()] = ['data-role' => implode(DEFAULTS['DELIMITER_COMMA'], $tempEntity->getRole())];
        }
        $options['options'] = $tempOptions;
        break;
      default:
        $options = parent::getFormOption($field, $formDatas, $options);
        break;
    }
    return $options;
  }
}
