<?php

namespace App\Forms\Part;

class CSForm extends PartForm
{
  public function __construct()
  {
    parent::__construct();
  }
  public function form_init_process(string $action, array &$formDatas = []): void
  {
    parent::form_init_process($action, $formDatas);
    $fields = [
      "type",
      "ip",
      "accountid",
      "domain",
      "price",
      "status"
    ];
    $filters = [
      "clientinfo_uid",
      'serverinfo_uid',
      'type',
      'status'
    ];
    $indexFilter = $filters;
    $batchjobFilters = ['status'];
    switch ($action) {
      case 'view':
        $fields = [
          ...$fields,
          "clientinfo_uid",
          'serverinfo_uid',
          'type',
          'ip',
          'accountid',
          'domain',
          'price',
          'status',
          'created_at'
        ];
        break;
      case 'index':
      case 'download':
        $fields = [
          ...$fields,
          "clientinfo_uid",
          'serverinfo_uid',
          'type',
          'ip',
          'accountid',
          'domain',
          'price',
          'status',
          '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 "type":
        $formRules[$field] = "required|trim|string";
        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;
      case "accountid":
      case "domain":
        $formRules[$field] = "permit_empty|trim|string";
        break;
      default:
        $formRules = parent::getFormRule($field, $formRules);
        break;
    }
    return $formRules;
  }
}
