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

//header.php에서 getFieldForm_Helper사용
function getFieldForm_UserHelper($field, $value, array $fieldFormOptions, array $attributes = array())
{
  $value = $value ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'role':
      // $fieldFormOptions[$field] = [DEFAULTS['EMPTY'] => lang("User.label.{$field}") . " 선택", ...$fieldFormOptions[$field]];
      // return form_dropdown($field, $fieldFormOptions[$field], $value, [...$attributes, 'class' => "select-field"]);
      // return form_multiselect($field, $fieldFormOptions[$field], $value, [...$attributes]);
      foreach ($fieldFormOptions[$field] as $key => $label) {
        $checkboxs[] = form_checkbox("{$field}[]", $key, in_array($key, is_array($value) ? [...$value] : explode(DEFAULTS['DELIMITER_ROLE'], $value)), $attributes) . $label;
      }
      return implode("&nbsp;", $checkboxs);
      break;
    case "status":
      $fieldFormOptions[$field] = [DEFAULTS['EMPTY'] => lang("User.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("User.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':
      return form_upload($field);
      break;
    case 'name':
      return form_input($field, $value, [...$attributes, "placeholder" => "예)홍길동", "style" => "width:60%; ::placeholder{ color:silver; opacity: 1; }"]);
      break;
    default:
      return form_input($field, $value, [...$attributes]);
      break;
  }
} //

function getFieldView_UserHelper($field, $entity, array $fieldFilters, array $fieldFormOptions, array $attributes = array())
{
  $value = $entity->$field ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'id':
      return anchor(current_url() . '/view/' . $entity->getPrimaryKey(), $value, [...$attributes, "target" => "_self"]);
      break;
    case 'upload_file':
      return $value == DEFAULTS['EMPTY'] ? DEFAULTS['EMPTY'] : anchor(current_url() . '/download/' . $entity->getPrimaryKey(), ICONS['IMAGE_FILE'] . explode(DEFAULTS['DELIMITER_FILE'], $value)[0], [...$attributes, "target" => "_self"]);
      break;
    case 'updated_at':
    case 'created_at':
      return $value ? str_split($value, 10)[0] : "";
      break;
    case 'upload_file':
      return $value == DEFAULTS['EMPTY'] ? DEFAULTS['EMPTY'] : anchor(current_url() . '/download/' . $entity->getPrimaryKey(), ICONS['IMAGE_FILE'] . explode(DEFAULTS['DELIMITER_FILE'], $value)[0], [...$attributes, "target" => "_self"]);
      break;
    case 'content':
      return html_entity_decode($value);
      break;
    default:
      return in_array($field, $fieldFilters)  && $value ? $fieldFormOptions[$field][$value] : $value;
      break;
  }
} //

function getFieldFilter_UserHelper($field, $value, array $fieldFormOptions, array $attributes = array())
{
  $value = $value ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'role':
      $fieldFormOptions[$field] = [DEFAULTS['EMPTY'] => lang("User.label.{$field}") . " 선택", ...$fieldFormOptions[$field]];
      return form_dropdown($field, $fieldFormOptions[$field], $value, [...$attributes, 'class' => "select-field"]);
      break;
    default:
      return getFieldForm_UserHelper($field, $value, $fieldFormOptions, $attributes);
      break;
  }
} //

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

function getFieldIndex_Row_UserHelper_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_UserHelper($field, $entity->$field, $fieldFormOptions, $attributes);
      }
      return getFieldIndex_Row_UserHelper($field, $entity, $fieldFormOptions, $attributes);
      break;
  }
} //