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

//header.php에서 getFieldForm_Helper사용
function getFieldForm_BoardHelper($field, $value, array $fieldFormOptions, array $attributes = array())
{
  switch ($field) {
    case "board_config_uid":
    case "user_uid":
      $fieldFormOptions[$field] = [DEFAULTS['EMPTY'] => lang("Board.label.{$field}") . " 선택", ...$fieldFormOptions[$field]];
      return form_dropdown($field, $fieldFormOptions[$field], is_array($value) ? [...$value] : [$value], [...$attributes, 'class' => "select-field"]);
      break;
    case "status":
      $fieldFormOptions[$field] = [DEFAULTS['EMPTY'] => lang("Board.label.{$field}") . " 선택", ...$fieldFormOptions[$field]];
      return form_dropdown($field, $fieldFormOptions[$field], is_array($value) ? [...$value] : [$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("Board.label.confirmpassword"),
        form_password('confirmpassword', DEFAULTS['EMPTY'], [...$attributes]),
      );
      break;
    case 'content':
      return form_textarea($field, html_entity_decode($value), [...$attributes, 'class' => 'editor', 'rows' => '20', 'cols' => '100']);
      break;
    case 'upload_file':
    case 'board_file':
      return form_upload($field);
      break;
    case 'title':
      return form_input($field, $value, [...$attributes, "placeholder" => "예)", "style" => "width:60%; ::placeholder{ color:silver; opacity: 1; }"]);
      break;
    case 'view_cnt':
      return form_input($field, $value, [...$attributes, 'type' => 'number']);
      break;
    default:
      return form_input($field, $value, [...$attributes]);
      break;
  }
} //

function getFieldView_BoardHelper($field, $entity, array $fieldFilters, array $fieldFormOptions, array $attributes = array())
{
  $value = $entity->$field ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'title':
      return sprintf(
        "<div style=\"text-align:left;\">%s   %s</div>",
        anchor(
          current_url() . '/reply/' . $entity->getPrimaryKey(),
          ICONS['REPLY'],
          [
            ...$attributes,
            "style" => sprintf("padding-left:%spx", $entity->getHierarchy_Depth() * DEFAULTS['HIERARCHY_GRPDEPTH']),
            "target" => "_self"
          ]
        ),
        anchor(
          current_url() . '/view/' . $entity->getPrimaryKey(),
          $value,
          ["target" => "_self"]
        )
      );
      break;
    case 'board_file':
    case 'upload_file':
      return $value == DEFAULTS['EMPTY'] ? DEFAULTS['EMPTY'] : anchor(current_url() . "/download/{$field}/{$entity->getPrimaryKey()}", ICONS['IMAGE_FILE'] . explode(DEFAULTS['FILE_DLIMITER'], $value)[0], [...$attributes, "target" => "_self"]);
      break;
    case 'view_cnt':
      return number_format(!$value ? 0 : $value);
      break;
    case 'content':
      return html_entity_decode($value);
      break;
    case 'updated_at':
    case 'created_at':
      return $value ? str_split($value, 10)[0] : "";
      break;
    default:
      return in_array($field, $fieldFilters)  && $value ? $fieldFormOptions[$field][$value] : $value;
      break;
  }
} //

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

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

function getFieldIndex_Row_BoardHelper_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 getFieldFilter_BoardHelper($field, $entity->$field, $fieldFormOptions, $attributes);
      }
      return getFieldIndex_Row_BoardHelper($field, $entity, $fieldFormOptions, $attributes);
      break;
  }
} //