<?php
function getFieldLabel_CardHelper($field, array $viewDatas): string
{
  $attributes = [];
  switch ($field) {
    default:
      if (strpos($viewDatas['fieldRules'][$field], 'required') !== false) {
        $attributes = ['style="color:red";'];
      }
      return sprintf("<span %s>%s</span>", implode(" ", $attributes), lang("{$viewDatas['className']}.label.{$field}"));
      break;
  }
}
//header.php에서 getFieldForm_Helper사용
function getFieldForm_CardHelper($field, $value, array $viewDatas, array $attributes = array())
{
  $value = $value ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'card_quota':
      $viewDatas['fieldFormOptions'][$field] = [DEFAULTS['EMPTY'] => lang("{$viewDatas['className']}.label.{$field}") . " 선택", ...$viewDatas['fieldFormOptions'][$field]];
      return form_dropdown($field, $viewDatas['fieldFormOptions'][$field], $value, $attributes);
      break;
    case 'card_number':
      return sprintf(
        "%s- %s- %s- %s",
        form_input("{$field}[]", DEFAULTS['EMPTY'], ['type' => "number", 'maxlength' => 4, 'size' => 4]),
        form_input("{$field}[]", DEFAULTS['EMPTY'], ['type' => "number", 'maxlength' => 4, 'size' => 4]),
        form_input("{$field}[]", DEFAULTS['EMPTY'], ['type' => "number", 'maxlength' => 4, 'size' => 4]),
        form_input("{$field}[]", DEFAULTS['EMPTY'], ['type' => "number", 'maxlength' => 4, 'size' => 4])
      );
      break;
    case 'card_expiration':
      $start = date('Y');
      $end = date('Y', strtotime(date("Y") . ' + 10 year'));
      $years = [];
      for ($i = $start; $i <= $end; $i++) {
        $years[$i] = "{$i}년";
      }
      $months = [];
      for ($i = 1; $i <= 12; $i++) {
        $months[sprintf("%02d", $i)] = "{$i}월";
      }
      return form_dropdown("{$field}[]", $years, DEFAULTS['EMPTY']) . '/ ' .
        form_dropdown("{$field}[]", $months, DEFAULTS['EMPTY']);
      break;
    case 'email':
      return form_input($field, $viewDatas['user']->email);
      break;
    case 'phone':
      return form_input($field, $viewDatas['user']->phone);
      break;
    default:
      return form_input($field, $value);
      break;
  }
} //

function getFieldView_CardHelper($field, $entity, array $viewDatas)
{
  $value = $entity->$field ?: DEFAULTS['EMPTY'];
  switch ($field) {
    default:
      return in_array($field, $viewDatas['fieldFilters'])  && $value ? $viewDatas['fieldFormOptions'][$field][$value] : $value;
      break;
  }
} //

function getFieldFilter_CardHelper($field, $value, array $viewDatas)
{
  $viewDatas['fieldFormOptions'][$field] = [DEFAULTS['EMPTY'] => lang("{$viewDatas['className']}.label.{$field}") . " 선택", ...$viewDatas['fieldFormOptions'][$field]];
  return form_dropdown($field, $viewDatas['fieldFormOptions'][$field], $value, ['class' => "select-field"]);
} //

function getFieldIndex_Column_CardHelper($field, array $viewDatas)
{
  $label = lang("{$viewDatas['className']}.label.{$field}");
  if ($field == $viewDatas['order_field']) {
    $label .=  $viewDatas['order_value'] == 'ASC' ? ICONS['UP'] : ICONS['DOWN'];
  }
  $value = $viewDatas['order_value'] == 'DESC' ? "ASC" : "DESC";
  $viewDatas['uri']->addQuery('order_field', $field);
  $viewDatas['uri']->addQuery('order_value', $value);
  $columnData = anchor($viewDatas['uri'], $label);
  switch ($field) {
    default:
      return sprintf("<th>%s</th>", $columnData);
      break;
  }
} //

//Front용
function getFieldIndex_Row_CardHelper($field, $entity, array $viewDatas): string
{
  $value = $entity->$field ?: DEFAULTS['EMPTY'];
  switch ($field) {
    default:
      return getFieldView_CardHelper($field, $entity, $viewDatas);
      break;
  }
} //