<?php

namespace App\Forms\Part;

class IPForm extends PartForm
{
  public function __construct()
  {
    parent::__construct();
  }
  public function form_init_process(string $action, array &$formDatas = []): void
  {
    parent::form_init_process($action, $formDatas);
    $fields = [
      "lineinfo_uid",
      "ip",
      "price",
      'status',
      "content",
    ];
    $filters = [
      'clientinfo_uid',
      'serverinfo_uid',
      'old_clientinfo_uid',
      "lineinfo_uid",
      'status',
    ];
    $indexFilter = $filters;
    $batchjobFilters = ['status'];
    $actionButtons = ['view' => ICONS['SEARCH']];
    $batchjobButtons = ['batchjob' => '일괄처리'];
    switch ($action) {
      case 'view':
        $fields = [
          'clientinfo_uid',
          'serverinfo_uid',
          'lineinfo_uid',
          'ip',
          'price',
          'status',
          'created_at',
          'old_clientinfo_uid',
          'content',
        ];
        break;
      case 'index':
        $fields = [
          'clientinfo_uid',
          'serverinfo_uid',
          'lineinfo_uid',
          'ip',
          'price',
          'status',
          'old_clientinfo_uid',
          'created_at',
        ];
        break;
      case 'download':
        $fields = [
          'clientinfo_uid',
          'serverinfo_uid',
          'lineinfo_uid',
          'ip',
          'price',
          'status',
          'created_at',
          'old_clientinfo_uid',
          'content',
        ];
        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 "lineinfo_uid":
        $formRules[$field] = "required|numeric";
        break;
      case "old_clientinfo_uid":
      case "clientinfo_uid":
      case "serviceinfo_uid":
      case "serverinfo_uid":
        $formRules[$field] = "permit_empty|numeric";
        break;
      case "ip": //ipv4 , ipv6 , both(ipv4,ipv6)
        $formRules[$field] = sprintf("required|trim|valid_ip[both]%s", in_array($this->formAction, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "");
        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 'lineinfo_uid':
        foreach ($this->getFormOption_process(service('equipment_lineservice'), $field, $formDatas) as $tempEntity) {
          $tempOptions[$tempEntity->getPK()] = $tempEntity->getTitle();
          // $options['attributes'][$tempEntity->getPK()] = ['data-role' => $tempEntity->getRole())];
        }
        $options['options'] = $tempOptions;
        break;
      case 'old_clientinfo_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())];
        }
        // dd($tempOptions);
        $options['options'] = $tempOptions;
        break;
      default:
        $options = parent::getFormOption($field, $formDatas, $options);
        break;
    }
    return $options;
  }
}
