<?php
function getFieldLabel_ProductHelper($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("Product.label.{$field}"));
      break;
  }
}

//header.php에서 getFieldForm_Helper사용
function getFieldForm_ProductHelper($field, $value, array $fieldFormOptions, array $attributes = array())
{
  switch ($field) {
    case "category_uid":
    case "user_uid":
      $fieldFormOptions[$field] = [DEFAULTS['EMPTY'] => lang("Product.label.{$field}") . " 선택", ...$fieldFormOptions[$field]];
      return form_dropdown($field, $fieldFormOptions[$field], $value, [...$attributes, 'class' => "select-field"]);
      break;
    case "status":
      $fieldFormOptions[$field] = [DEFAULTS['EMPTY'] => lang("Product.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 'passwd':
      return sprintf(
        "%s %s %s",
        form_password($field, DEFAULTS['EMPTY'], [...$attributes]),
        lang("Product.label.confirmpassword"),
        form_password('confirmpassword', DEFAULTS['EMPTY'], [...$attributes]),
      );
      break;
    case 'head':
    case 'tail':
    case 'content':
      return form_textarea($field, html_entity_decode($value), [...$attributes, 'class' => 'editor', 'rows' => '20', 'cols' => '100']);
      break;
    case 'photo':
    case 'upload_file':
      return form_upload($field);
      break;
    case 'name':
      return form_input($field, $value, [...$attributes, "placeholder" => "예) HP-DL360G6...", "style" => "width:60%; ::placeholder{ color:silver; opacity: 1; }"]);
      break;
    case 'cost':
    case 'price':
    case 'sale':
    case 'stock':
      return form_input($field, $value, [...$attributes, 'type' => 'number']);
      break;
    default:
      return form_input($field, $value, [...$attributes]);
      break;
  }
} //

function getFieldView_ProductHelper($field, $entity, array $fieldFilters, array $fieldFormOptions, array $attributes = array())
{
  $value = $entity->$field ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'category_uid':
      $categorys = array();
      foreach (array_values($fieldFormOptions[$field]) as $category_2depth) {
        foreach ($category_2depth as $key => $label) {
          $categorys[$key] = $label;
        }
      }
      return $categorys[$value];
      break;
    case 'name':
      return sprintf(
        "<div style=\"text-align:left;\">%s</div>",
        anchor(current_url() . '/view/' . $entity->getPrimaryKey(), $value, [...$attributes, "target" => "_self"])
      );
      break;
    case 'photo':
    case 'upload_file':
      return $value == DEFAULTS['EMPTY'] ? DEFAULTS['EMPTY'] : anchor(current_url() . "/download/{$field}/{$entity->getPrimaryKey()}", ICONS['IMAGE_FILE'] . explode(DEFAULTS['DELIMITER_FILE'], $value)[0], [...$attributes, "target" => "_self"]);
      break;
    case 'cost':
    case 'price':
    case 'sale':
    case 'stock':
      return number_format(!$value ? 0 : $value);
      break;
    case 'content':
      return html_entity_decode($value);
      break;
    case 'updated_at':
    case 'created_at':
      return isset($value) ? str_split($value, 10)[0] : "";
      break;
    default:
      return in_array($field, $fieldFilters)  && $value ? $fieldFormOptions[$field][$value] : $value;
      break;
  }
} //

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

function getFieldIndex_Column_ProductHelper($field, $order_field, $order_value, array $attributes = array())
{
  $label = lang("Product.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_ProductHelper($field, $entity, array $fieldFilters, $fieldFormOptions, $attributes = array()): string
{
  switch ($field) {
    default:
      return getFieldView_ProductHelper($field, $entity, $fieldFormOptions, $attributes);
      break;
  }
} //

function getFieldIndex_Row_ProductHelper_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_ProductHelper($field, $entity->$field, $fieldFormOptions, $attributes);
      }
      return getFieldIndex_Row_ProductHelper($field, $entity, $fieldFormOptions, $attributes);
      break;
  }
} //