<?php
function getFieldLabel_SitepageHelper($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_SitepageHelper($field, $value, array $viewDatas, array $attributes = array())
{
  $value = $value ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case "category_uid":
    case "user_uid":
      $viewDatas['fieldFormOptions'][$field] = [DEFAULTS['EMPTY'] => lang("{$viewDatas['className']}.label.{$field}") . " 선택", ...$viewDatas['fieldFormOptions'][$field]];
      return form_dropdown($field, $viewDatas['fieldFormOptions'][$field], $value, [...$attributes, 'class' => "select-field"]);
      break;
    case 'title':
    case 'name':
      return form_input($field, $value, ["placeholder" => "예)", "style" => "width:60%; ::placeholder{ color:silver; opacity: 1; }"]);
      break;
    case 'passwd':
      return sprintf(
        "%s %s %s",
        form_password($field, DEFAULTS['EMPTY']),
        lang("{$viewDatas['className']}.label.confirmpassword"),
        form_password('confirmpassword', DEFAULTS['EMPTY']),
      );
      break;
    case 'content':
    case 'head':
    case 'tail':
      return form_textarea($field, html_entity_decode($value), ['class' => 'editor', 'rows' => '20', 'cols' => '100']);
      break;
    case 'upload_file':
    case 'board_file':
      return form_upload($field);
      break;
    case 'view_cnt':
      return form_input($field, $value, ['type' => 'number']);
      break;
    case "category":
    case "status":
      $viewDatas['fieldFormOptions'][$field] = [DEFAULTS['EMPTY'] => lang("{$viewDatas['className']}.label.{$field}") . " 선택", ...$viewDatas['fieldFormOptions'][$field]];
      return form_dropdown($field, $viewDatas['fieldFormOptions'][$field], $value, $attributes);
      break;
    case 'updated_at':
    case 'created_at':
      return form_input($field, $value, ['class' => 'calender']);
      break;
    default:
      return form_input($field, $value);
      break;
  }
} //

function getFieldView_SitepageHelper($field, $entity, array $viewDatas)
{
  $value = $entity->$field ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'category_uid':
      foreach (array_values($viewDatas['fieldFormOptions'][$field]) as $category_2depths) {
        foreach ($category_2depths as $key => $label) {
          if ($key == $value) {
            return $label;
          }
        }
      }
      return $value;
      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, $viewDatas['fieldFilters'])  && $value ? $viewDatas['fieldFormOptions'][$field][$value] : $value;
      break;
  }
} //

function getFieldFilter_SitepageHelper($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_SitepageHelper($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((string)$viewDatas['uri'], $label);
  switch ($field) {
    case 'title':
    case 'name':
      return sprintf("<th class='col-4'>%s</th>", $columnData);
      break;
    default:
      return sprintf("<th>%s</th>", $columnData);
      break;
  }
} //

//Front용
function getFieldIndex_Row_SitepageHelper($field, $entity, array $viewDatas): string
{
  //front쪽은 table을 사용하지 않음 주의
  switch ($field) {
    case 'content':
      return getFieldView_SitepageHelper($field, $entity, $viewDatas);
      break;
    default:
      return getFieldView_SitepageHelper($field, $entity, $viewDatas);
      break;
  }
} //

//Admin용
function getFieldIndex_Row_SitepageHelper_Admin($field, $entity, array $viewDatas): string
{
  $value = $entity->$field ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'title':
    case 'name':
      return anchor(
        current_url() . '/view/' . $entity->getPrimaryKey(),
        $value,
        ["target" => "_self"]
      );
      break;
    default:
      if (in_array($field, $viewDatas['fieldFilters'])) {
        $attributes["onChange"] = sprintf(
          'location.href="%s/toggle/%s/%s?%s="+this.options[this.selectedIndex].value',
          current_url(),
          $entity->getPrimaryKey(),
          $field,
          $field
        );
        return getFieldForm_SitepageHelper($field, $entity->$field, $viewDatas, $attributes);
      }
      //front쪽은 table을 사용하지 않음 주의
      return getFieldIndex_Row_SitepageHelper($field, $entity, $viewDatas);
      break;
  }
} //