<?php

namespace App\Forms\Customer;

class ServiceForm extends CustomerForm
{
  public function __construct()
  {
    parent::__construct();
  }
  public function form_init_process(string $action, array &$formDatas = []): void
  {
    parent::form_init_process($action, $formDatas);
    $fields = [
      "site",
      "location",
      "clientinfo_uid",
      'serverinfo_uid',
      "rack",
      "line",
      "title",
      "start_at",
      "billing_at",
      "status",
      'sale',
      "history",
    ];
    $filters = [
      'site',
      'location',
      "rack",
      "line",
      'clientinfo_uid',
      'serverinfo_uid',
      'user_uid',
      'status',
    ];
    $indexFilter = $filters;
    $batchjobFilters = [
      'site',
      'location',
      'clientinfo_uid',
      'status'
    ];
    switch ($action) {
      case 'modify':
        $fields = [...$fields, 'amount'];
        break;
      case 'view':
        $fields = [...$fields, 'created_at'];
        break;
      case 'index':
      case 'download':
        $fields = [
          'site',
          'location',
          'clientinfo_uid',
          'serverinfo_uid',
          'sale',
          'amount',
          'billing_at',
          'status',
          'start_at',
          'created_at'
        ];
        break;
      case 'alternative_create_form':
        $fields = ['serverinfo_uid'];
        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 "serverinfo_uid":
      case "amount":
      case "rack":
      case "line":
        $formRules[$field] = "required|numeric";
        break;
      case "sale":
        $formRules[$field] = "permit_empty|numeric";
        break;
      case "site":
      case "location":
        $formRules[$field] = "required|trim|string";
        break;
      case "billing_at":
      case "start_at":
        $formRules[$field] = "required|valid_date";
        break;
      case "end_at":
        $formRules[$field] = "permit_empty|valid_date";
        break;
      case 'title':
      case "history":
        $formRules[$field] = "permit_empty|trim|string";
        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 'serverinfo_uid':
        foreach ($this->getFormOption_process(service('equipment_serverservice'), $field, $formDatas) as $tempEntity) {
          $tempOptions[$tempEntity->getPK()] = $tempEntity->getCustomTitle();
          // $options['attributes'][$tempEntity->getPK()] = ['data-role' => $tempEntity->getRole())];
        }
        $options['options'] = $tempOptions;
        break;
      default:
        $options = parent::getFormOption($field, $formDatas, $options);
        break;
    }
    return $options;
  }
}
