<?php

namespace App\Forms\Equipment;

class ServerForm extends EquipmentForm
{
  public function __construct()
  {
    parent::__construct();
  }
  public function form_init_process(string $action, array &$formDatas = []): void
  {
    parent::form_init_process($action, $formDatas);
    $fields = [
      "code",
      "type",
      "chassisinfo_uid",
      "switchinfo_uid",
      "ip",
      "viewer",
      "title",
      "os",
      "price",
      "manufactur_at",
      "format_at",
      "status",
    ];
    //수정일때만 꼭 serviceinfo가 필요함
    if (in_array($this->formAction, ["modify", "modify_form"])) {
      $fields = ['serviceinfo_uid', ...$fields];
    }
    $filters = [
      "clientinfo_uid",
      "serviceinfo_uid",
      'type',
      "chassisinfo_uid",
      'switchinfo_uid',
      'ip',
      'os',
      "status",
    ];
    $indexFilter = $filters;
    $batchjobFilters = ['type', 'switchinfo_uid', 'ip', 'viewer', 'os', 'status'];
    switch ($action) {
      case 'view':
        $fields = ['clientinfo_uid', ...$fields, 'status', 'created_at'];
        break;
      case 'index':
      case 'download':
        $fields = [
          'clientinfo_uid',
          "code",
          "type",
          "switchinfo_uid",
          "ip",
          "viewer",
          "title",
          "os",
          "part",
          "price",
          "manufactur_at",
          "format_at",
          'status',
        ];
        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 'clientinfo_uid':
      case 'serviceinfo_uid':
        $formRules[$field] = "permit_empty|numeric";
        break;
      case "switchinfo_uid":
        $formRules[$field] = sprintf("permit_empty|numeric%s", in_array($this->formAction, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "");
        break;
      case "code":
      case "title":
        $formRules[$field] = "required|trim|string";
        break;
      case "chassisinfo_uid":
      case "price":
        $formRules[$field] = "required|numeric";
        break;
      case "type":
        $formRules[$field] = "required|trim|string";
        break;
      case "ip": //ipv4 , ipv6 , both(ipv4,ipv6)
        $formRules[$field] = sprintf("permit_empty|trim|valid_ip[both]%s", in_array($this->formAction, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "");
        break;
      case "os":
        $formRules[$field] = "permit_empty|trim|string";
        break;
      case "manufactur_at":
        $formRules[$field] = "required|valid_date";
        break;
      case "format_at":
        $formRules[$field] = "permit_empty|valid_date";
        break;
      default:
        $formRules = parent::getFormRule($field, $formRules);
        break;
    }
    return $formRules;
  }

  protected function getFormOption_process($service, string $field, array $formDatas = []): array
  {
    $entities = [];
    switch ($field) {
      case 'ip':
        if (in_array($this->formAction, ['create_form', 'modify_form'])) {
          if (array_key_exists($field, $formDatas)) {
            $where = sprintf("status = '%s' OR %s='%s'", STATUS['AVAILABLE'], $field, $formDatas[$field]);
            $entities = $service->getEntities([$where => null]);
          } else {
            $entities = parent::getFormOption_process($service, $field, $formDatas);
          }
        } else {
          $entities = parent::getFormOption_process($service, $field, $formDatas);
        }
        break;
      default:
        $entities = parent::getFormOption_process($service, $field, $formDatas);
        break;
    }
    return $entities;
  }

  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 'chassisinfo_uid':
        $tempOptions = [
          "" => [
            'value' => "",
            'text' => lang("{$this->getAttribute('class_path')}.label.{$field}") . " 선택"
          ]
        ];
        foreach ($this->getFormOption_process(service('equipment_chassisservice'), $field, $formDatas) as $tempEntity) {
          $tempOptions[$tempEntity->getPK()] = [
            'value' => $tempEntity->getPK(),
            'text' => $tempEntity->getTitle(),
            'data-title' => $tempEntity->getTitle(),
            'data-price' => $tempEntity->getPrice()
          ];
        }
        $options['options'] = $tempOptions;
        // dd($options);
        break;
      case 'switchinfo_uid':
        foreach ($this->getFormOption_process(service('part_switchservice'), $field, $formDatas) as $tempEntity) {
          $tempOptions[$tempEntity->getPK()] = $tempEntity->getTitle();
          // $options['attributes'][$tempEntity->getPK()] = ['data-role' => $tempEntity->getRole())];
        }
        $options['options'] = $tempOptions;
        // dd($options);
        break;
      case 'ip': //key=value이 같음주의
        foreach ($this->getFormOption_process(service('part_ipservice'), 'ip', $formDatas) as $tempEntity) {
          $tempOptions[$tempEntity->getTitle()] = $tempEntity->getTitle();
          // $options['attributes'][$tempEntity->getPK()] = ['data-role' => $tempEntity->getRole())];
        }
        //formDatas에 값이 있고, $tempOptions에 없다면 추가(VPN의 Customer IP 경우)
        if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
          if (!array_key_exists($formDatas[$field], $tempOptions)) {
            $tempOptions[$formDatas[$field]] = $formDatas[$field];
          }
        }
        $options['options'] = $tempOptions;
        break;
      default:
        $options = parent::getFormOption($field, $formDatas, $options);
        break;
    }
    return $options;
  }
}
