<?php

namespace App\Forms\Part;

use App\Forms\CommonForm;

abstract class PartForm extends CommonForm
{
  protected function __construct()
  {
    parent::__construct();
  }
  public function getFormRule(string $action, string $field, array $formRules): array
  {
    switch ($field) {
      case "title":
        $formRules[$field] = sprintf("required|trim|string%s", in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "");
        break;
      case "clientinfo_uid":
      case "serviceinfo_uid":
      case "serverinfo_uid":
        $formRules[$field] = "permit_empty|numeric";
        break;
      case "price":
      case "stock":
      case "used":
        $formRules[$field] = "required|numeric";
        break;
      case "status":
        $formRules[$field] = "permit_empty|trim|string";
        break;
      default:
        $formRules = parent::getFormRule($action, $field, $formRules);
        break;
    }
    return $formRules;
  }
  public function getFormOption(string $action, string $field, array $formDatas = [], array $options = ['options' => [], 'atttributes' => []]): array
  {
    $tempOptions = ['' => lang("{$this->getAttribute('class_path')}.label.{$field}") . " 선택"];
    switch ($field) {
      case 'serviceinfo_uid':
        foreach ($this->getFormOption_process(service('customer_serviceservice'), $action, $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;
      case 'serverinfo_uid':
        foreach ($this->getFormOption_process(service('equipment_serverservice'), $action, $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($action, $field, $formDatas, $options);
        break;
    }
    return $options;
  }
}
