<?php

namespace App\Forms;

use App\Forms\CommonForm;

class PaymentForm extends CommonForm
{
  public function __construct()
  {
    parent::__construct();
  }
  public function form_init_process(string $action, array &$formDatas = []): void
  {
    parent::form_init_process($action, $formDatas);
    $fields = [
      "serviceinfo_uid",
      "title",
      "amount",
      "billing",
      "billing_at",
      "pay",
      "status",
      "content",
    ];
    $filters = ['user_uid', 'clientinfo_uid', 'serviceinfo_uid', 'status', 'billing', 'pay'];
    $indexFilter = ['clientinfo_uid', 'serviceinfo_uid', 'status', 'billing', 'pay'];
    $batchjobFilters = ['status'];
    $actionButtons = ['paid' => '결제', 'view' => ICONS['SEARCH']];
    $batchjobButtons = ['batchjob' => '일괄결제', 'invoice' => '청구서발행'];
    switch ($action) {
      case 'view':
        $fields = [
          'clientinfo_uid',
          "serviceinfo_uid",
          'billing',
          'title',
          'amount',
          'billing_at',
          'pay',
          'status',
          'updated_at',
          'countdown',
          'user_uid',
          'created_at',
          'content'
        ];
        break;
      case 'index':
      case 'download':
        $fields = [
          'clientinfo_uid',
          "serviceinfo_uid",
          'billing',
          'title',
          'amount',
          'billing_at',
          'pay',
          'updated_at',
          'countdown',
          'user_uid',
          'created_at'
        ];
        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 "user_uid":
      case "clientinfo_uid":
      case "serviceinfo_uid":
      case "amount":
        $formRules[$field] = "required|numeric";
        break;
      case "serverpartinfo_uid":
      case "billing_month":
        $formRules[$field] = "permit_empty|numeric";
        break;
      case "title":
      case "billing":
      case "pay":
        $formRules[$field] = "required|trim|string";
        break;
      case "billing_at":
        $formRules[$field] = "required|valid_date";
        break;
      case "content":
        $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 '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;
      default:
        $options = parent::getFormOption($field, $formDatas, $options);
        break;
    }
    return $options;
  }
}
