<?php

namespace App\Forms\Part;

use App\Forms\CommonForm;

abstract class PartForm extends CommonForm
{
  protected function __construct()
  {
    parent::__construct();
  }
  public function getFormRule(string $field, array $formRules): array
  {
    switch ($field) {
      case "title":
        $formRules[$field] = sprintf("required|trim|string%s", in_array($this->formAction, ["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;
      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 'serviceinfo_uid':
        foreach ($this->getFormOption_process(service('customer_serviceservice'), $field, $formDatas) as $tempEntity) {
          $tempOptions[$tempEntity->getPK()] = $tempEntity->getTitle();
          // $options['attributes'][$tempEntity->getPK()] = ['data-role' => $tempEntity->getRole())];
        }
        $options['options'] = $tempOptions;
        break;
      case 'serverinfo_uid':
        foreach ($this->getFormOption_process(service('equipment_serverservice'), $field, $formDatas) as $tempEntity) {
          $tempOptions[$tempEntity->getPK()] = $tempEntity->getTitle();
          // $options['attributes'][$tempEntity->getPK()] = ['data-role' => $tempEntity->getRole())];
        }
        $options['options'] = $tempOptions;
        break;
      default:
        $options = parent::getFormOption($field, $formDatas, $options);
        break;
    }
    return $options;
  }
}
