<?php

namespace App\Forms\Customer;

class ClientForm 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',
      'name',
      'email',
      'phone',
      'role',
      'status',
    ];
    $filters = [
      'site',
      'role',
      'status',
    ];
    $indexFilter = $filters;
    $batchjobFilters = ['site', 'role', 'status'];
    switch ($action) {
      case 'view':
        $fields = [...$fields, 'status', 'created_at'];
        break;
      case 'index':
      case 'download':
        $fields = [
          'site',
          'name',
          'email',
          'phone',
          'role',
          'account_balance',
          'coupon_balance',
          'point_balance',
          'status',
          'created_at',
          'updated_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 "name":
        $formRules[$field] = sprintf("required|trim|string%s", in_array($this->formAction, ["create", "create_form"]) ? "|is_unique[{$this->getAttribute('table')}.{$field}]" : "");
        break;
      case "site":
        $formRules[$field] = "required|trim|string";
        break;
      case "role":
        $formRules[$field] = 'required|is_array|at_least_one';
        $formRules['role.*'] = 'permit_empty|trim|in_list[user,vip,reseller]';
        break;
      case "email":
        $formRules[$field] = "permit_empty|trim|valid_email";
        break;
      case "phone":
      case "history":
        $formRules[$field] = "permit_empty|trim|string";
        break;
      case "account_balance":
      case "coupon_balance":
      case "point_balance":
        $formRules[$field] = "permit_empty|numeric";
        break;
      default:
        $formRules = parent::getFormRule($field, $formRules);
        break;
    }
    return $formRules;
  }
}
