<?php

namespace App\Forms\Equipment;

class CHASSISForm extends EquipmentForm
{
  public function __construct()
  {
    parent::__construct();
  }
  public function form_init_process(string $action, array &$formDatas = []): void
  {
    parent::form_init_process($action, $formDatas);
    $fields = [
      "title",
      "price",
      "used",
      "stock",
      "cpuinfo_uid",
      "cpu_cnt",
      "raminfo_uid",
      "ram_cnt",
      "diskinfo_uid",
      "disk_cnt",
      "status"
    ];
    $filters = [
      "cpuinfo_uid",
      "raminfo_uid",
      "diskinfo_uid",
      "status",
    ];
    $indexFilter = $filters;
    $batchjobFilters = ['status'];
    $actionButtons = ['view' => ICONS['SEARCH']];
    $batchjobButtons = ['batchjob' => '일괄처리'];
    switch ($action) {
      case 'view':
        $fields = [...$fields, 'status', 'created_at'];
        break;
      case 'index':
      case 'download':
        $fields = [...$fields, 'status', 'created_at'];
        break;
    }
    $this->setFormFields($fields);
    $this->setFormRules($fields);
    $this->setFormFilters($filters);
    $this->setFormOptions($filters, $formDatas);
    $this->setIndexFilters($indexFilter);
    $this->setBatchjobFilters($batchjobFilters);
    $this->setActionButtons($actionButtons);
    $this->setBatchjobButtons($batchjobButtons);
  }
  public function getFormRule(string $field, array $formRules): array
  {
    switch ($field) {
      case "used":
      case "price":
      case "stock":
        $formRules[$field] = "required|numeric";
        break;
      case "cpuinfo_uid":
      case "raminfo_uid":
      case "diskinfo_uid":
      case "cpu_cnt":
      case "ram_cnt":
      case "disk_cnt":
        $formRules[$field] = "permit_empty|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 "cpuinfo_uid":
        foreach ($this->getFormOption_process(service('part_cpuservice'), $field, $formDatas) as $tempEntity) {
          $tempOptions[$tempEntity->getPK()] = $tempEntity->getTitle();
          // $options['attributes'][$tempEntity->getPK()] = ['data-role' => $tempEntity->getRole())];
        }
        $options['options'] = $tempOptions;
        break;
      case "raminfo_uid":
        foreach ($this->getFormOption_process(service('part_ramservice'), $field, $formDatas) as $tempEntity) {
          $tempOptions[$tempEntity->getPK()] = $tempEntity->getTitle();
          // $options['attributes'][$tempEntity->getPK()] = ['data-role' => $tempEntity->getRole())];
        }
        $options['options'] = $tempOptions;
        break;
      case "diskinfo_uid":
        foreach ($this->getFormOption_process(service('part_diskservice'), $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;
  }
}
