<?php
function getFieldLabel_OrderHelper($field, array $fieldRules, array $attributes = array()): string
{
  switch ($field) {
    default:
      if (strpos($fieldRules[$field], 'required') !== false) {
        array_push($attributes, 'style="color:red";');
      }
      return sprintf("<span %s>%s</span>", implode(" ", [...$attributes]), lang("Order.label.{$field}"));
      break;
  }
}

//header.php에서 getFieldForm_Helper사용
function getFieldForm_OrderHelper($field, $value, array $fieldFormOptions, array $attributes = array())
{
  switch ($field) {
    case "product_uid":
    case "user_uid":
      $fieldFormOptions[$field] = [DEFAULTS['EMPTY'] => lang("Order.label.{$field}") . " 선택", ...$fieldFormOptions[$field]];
      return form_dropdown($field, $fieldFormOptions[$field], $value, [...$attributes, 'class' => "select-field"]);
      break;
    case "status":
      $fieldFormOptions[$field] = [DEFAULTS['EMPTY'] => lang("Order.label.{$field}") . " 선택", ...$fieldFormOptions[$field]];
      return form_dropdown($field, $fieldFormOptions[$field], $value, $attributes);
      break;
    case 'updated_at':
    case 'created_at':
      return form_input($field, $value, [...$attributes, 'class' => 'calender']);
      break;
    case 'quantity':
    case 'price':
      return form_input($field, $value, [...$attributes, 'type' => 'number']);
      break;
    default:
      return form_input($field, $value, [...$attributes]);
      break;
  }
} //

function getFieldView_OrderHelper($field, $entity, array $fieldFilters, array $fieldFormOptions, array $attributes = array())
{
  $value = $entity->$field ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'name':
      return sprintf(
        "<div style=\"text-align:left;\">%s</div>",
        anchor(current_url() . '/view/' . $entity->getPrimaryKey(), $value, [...$attributes, "target" => "_self"])
      );
      break;
    case 'quantity':
    case 'price':
      return number_format(!$value ? 0 : $value);
      break;
    case 'updated_at':
    case 'created_at':
      return $value;
      break;
    default:
      return in_array($field, $fieldFilters) && $value ? $fieldFormOptions[$field][$value] : $value;
      break;
  }
} //

function getFieldFilter_OrderHelper($field, $value, array $fieldFormOptions, array $attributes = array())
{
  $value = $value ?: DEFAULTS['EMPTY'];
  switch ($field) {
    default:
      return getFieldForm_OrderHelper($field, $value, $fieldFormOptions, $attributes);
      break;
  }
} //

function getFieldIndex_Column_OrderHelper($field, $order_field, $order_value, array $attributes = array())
{
  $label = lang("Order.label.{$field}");
  $label = $field == $order_field ? sprintf('%s <i class="fa fa-arrow-%s"></i>', $label, $order_value == 'ASC' ? "up" : "down") : $label;
  $order_value = $order_value == 'DESC' ? "ASC" : "DESC";
  return anchor(current_url() . "?order_field={$field}&order_value={$order_value}", $label, [...$attributes]);
} //

function getFieldIndex_Row_OrderHelper($field, $entity, array $fieldFilters, $fieldFormOptions, $attributes = array()): string
{
  switch ($field) {
    default:
      return getFieldView_OrderHelper($field, $entity, $fieldFilters, $fieldFormOptions, $attributes);
      break;
  }
} //

function getFieldIndex_Row_OrderHelper_Admin($field, $entity, array $fieldFilters, $fieldFormOptions, $attributes = array()): string
{
  switch ($field) {
    default:
      if (in_array($field, $fieldFilters)) {
        $attributes["onChange"] = sprintf('location.href="%s/toggle/%s/%s?%s="+this.options[this.selectedIndex].value', current_url(), $entity->getPrimaryKey(), $field, $field);
        return getFieldForm_OrderHelper($field, $entity->$field, $fieldFormOptions, $attributes);
      }
      return getFieldIndex_Row_OrderHelper($field, $entity, $fieldFormOptions, $attributes);
      break;
  }
} //