<?php
function getFieldLabel_HPILOHelper($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("Admin/HPILO.label.{$field}"));
      break;
  }
}

//header.php에서 getFieldForm_Helper사용
function getFieldForm_HPILOHelper($field, $value, array $formOptions, array $attributes = array())
{
  $value = is_null($value) ? DEFAULTS['EMPTY'] : $value;
  switch ($field) {
    case 'status':
    case 'power':
      return form_dropdown($field, $formOptions[$field], $value, $attributes);
      break;
    case 'passwd':
    case 'confirmpassword':
      return form_password($field, DEFAULTS['EMPTY'], $attributes);
      break;
    case 'updated_at':
    case 'created_at':
      $attributes["class"] = !array_key_exists("class", $attributes) ? "calender" : $attributes["class"] . " calender";
      return form_input($field, $value, $attributes);
      break;
    default:
      return form_input($field, $value, $attributes);
      break;
  }
} //

function getFieldView_HPILOHelper($field, $entity, array $fieldFilters, array $fieldFormOptions, array $attributes = array())
{
  switch ($field) {
    case 'status':
      return  lang("Admin/HPILO." . strtoupper($field) . "." . $entity->$field);
      break;
    case 'memory':
      return $entity->$field . "GB";
      break;
    case 'detail':
      return nl2br($entity->$field);
      break;
    default:
      if (in_array($field, $fieldFilters)) {
        return getFieldForm_HPILOHelper($field, $entity->$field, $fieldFormOptions, $attributes);
      }
      return !isset($entity->$field) ? "{$field}:{$entity->uid}" : $entity->$field;
      return $entity->$field;
      break;
  }
} //

function getFieldIndex_Column_HPILOHelper($field, $order_field, $order_value, array $attributes = array())
{
  $label = lang("Admin/HPILO.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_HPILOHelper($field, array $row, array $fieldFilters, $fieldFormOptions, $attributes = array()): string
{
  switch ($field) {
    case 'customer':
      return sprintf(
        "%s %s %s",
        anchor(base_url() . '/admin/hpilo/reload/' . $row['uid'], ' ', ["class" => "btn btn-sm btn-primary btn-circle fa fa-refresh", "target" => "_self"]),
        anchor(base_url() . '/admin/hpilo/console/' . $row['uid'], ' ', ["class" => "btn btn-sm btn-warning btn-circle fa fa-desktop", "target" => "_self"]),
        $row[$field]
      );
      break;
    case 'model':
      return anchor(current_url() . '/view/' . $row['uid'], $row[$field], ["target" => "_self"]);
      break;
    case 'memory':
      return $row[$field] . "GB";
      break;
    case 'power':
      $attributes["onChange"] = sprintf('location.href="%s/reset/%s/"+this.options[this.selectedIndex].value', current_url(), $row['uid']);
      return getFieldForm_HPILOHelper($field, $row[$field], $fieldFormOptions, $attributes);
      break;
    case 'updated_at':
    case 'created_at':
      return isset($row[$field]) ? str_split($row[$field], 10)[0] : "";
      break;
    default:
      if (in_array($field, $fieldFilters)) {
        $attributes["onChange"] = sprintf('location.href="%s/toggle/%s/%s?%s="+this.options[this.selectedIndex].value', current_url(), $row['uid'], $field, $field);
        return getFieldForm_HPILOHelper($field, $row[$field], $fieldFormOptions, $attributes);
      }
      return $row[$field];
      break;
  }
} //