<?php

namespace App\Forms;

use RuntimeException;
use App\Forms\CommonForm;

class InquiryForm extends CommonForm
{
  public function __construct()
  {
    parent::__construct();
  }
  public function action_init_process(string $action, array &$formDatas = []): void
  {
    parent::action_init_process($action, $formDatas);
    $fields = [
      'title',
      'email',
      'content',
      'status',
    ];
    $filters = [
      'status',
    ];
    $indexFilter = $filters;
    $batchjobFilters = ['status'];
    switch ($action) {
      case 'view':
        $fields = [
          'title',
          'email',
          'status',
          'created_at',
          'content'
        ];
        break;
      case 'index':
        $fields = [
          'title',
          'email',
          'status',
          'created_at'
        ];
        break;
      case 'download':
        $fields = [
          'title',
          'email',
          '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 "title":
      case "content":
        $formRules[$field] = "required|trim|string";
        break;
      case "email":
        $formRules[$field] = sprintf("required|trim|valid_email%s", in_array($this->_action, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "");
        break;
      case "status":
        $formRules[$field] = "permit_empty|trim|string";
        break;
      default:
        $formRules = parent::getFormRule($field, $formRules);
        break;
    }
    return $formRules;
  }
}
