<?php

namespace App\Forms\Equipment;

use App\Services\CommonService;

class ServerPartForm extends EquipmentForm
{
  public function __construct()
  {
    parent::__construct();
  }
  public function form_init_process(string $action, array &$formDatas = []): void
  {
    parent::form_init_process($action, $formDatas);
    $fields = [
      "serverinfo_uid",
      "type",
      "billing",
      "part_uid",
      "cnt",
      "extra",
      "amount",
    ];
    $filters = [
      "serverinfo_uid",
      "type",
      "part_uid",
      "billing",
    ];
    $indexFilter = [
      "serverinfo_uid",
      "type",
      "billing",
    ];
    $batchjobFilters = ['billing', 'type'];
    switch ($action) {
      case 'view':
        $fields = [...$fields, 'created_at'];
        break;
      case 'index':
      case 'download':
        $fields = [...$fields, 'created_at'];
        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 "serverinfo_uid":
      case "part_uid":
      case "cnt":
      case "amount":
        $formRules[$field] = "required|numeric";
        break;
      case "clientinfo_uid":
      case "serviceinfo_uid":
        $formRules[$field] = "permit_empty|numeric";
        break;
      case "title":
      case "type":
      case "billing":
        $formRules[$field] = "required|trim|string";
        break;
      case "extra":
        $formRules[$field] = "permit_empty|trim|string";
        break;
      default:
        $formRules = parent::getFormRule($field, $formRules);
        break;
    }
    return $formRules;
  }
  private function getPartService(string $type): CommonService
  {
    return service('part_' . strtolower($type) . 'service');
  }
  public function getFormOption(string $field, array $formDatas = [], array $options = ['options' => [], 'atttributes' => []]): array
  {
    $tempOptions = ['' => lang("{$this->getAttribute('class_path')}.label.{$field}") . " 선택"];
    switch ($field) {
      case 'part_uid':
        $tempOptions = [];
        foreach (SERVERPART['ALL_PARTTYPES'] as $type) {
          $partService = $this->getPartService($type);
          $tempOptions[$type] = [
            "" => [
              'value' => "",
              'text' => lang("{$this->getAttribute('class_path')}.TYPE.{$type}") . " 선택",
            ]
          ];
          foreach ($this->getFormOption_process($partService, $field, $formDatas) as $tempEntity) {
            $tempOptions[$type][$tempEntity->getPK()] = [
              'value' => $tempEntity->getPK(),
              'text' => $tempEntity->getTitle(),
              'data-title' => $tempEntity->getTitle(),
              'data-price' => $tempEntity->getPrice()
            ];
          }
        }
        $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;
      case 'serviceinfo_uid':
        foreach ($this->getFormOption_process(service('customer_clientservice'), $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;
  }
}
