<?php
function getFieldLabel_RecordHelper($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/Cloudflare/API/Record.label.{$field}"));
      break;
  }
}

//header.php에서 getFieldForm_Helper사용
function getFieldForm_RecordHelper($field, $value, array $formOptions, array $attributes = array())
{
  $value = is_null($value) ? DEFAULT_EMPTY : $value;
  switch ($field) {
    case 'zone_uid':
      return form_dropdown($field, $formOptions[$field], $value, ['class' => "select-field form-select-sm"]);
      break;
    case 'type':
    case 'proxiable':
    case 'proxied':
    case 'locked':
    case 'fixed':
      return form_dropdown($field, $formOptions[$field], $value, $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_RecordHelper($field, array $fieldDatas, array $fieldFilters, array $fieldFormOptions, array $attributes = array())
{
  switch ($field) {
    case 'locked':
      return lang("Admin/Cloudflare/API/Record.label.{$fieldDatas[$field]}");
      break;
    default:
      if (in_array($field, $fieldFilters)) {
        return getFieldForm_RecordHelper($field, $fieldDatas[$field], $fieldFormOptions, $attributes);
      }
      return $fieldDatas[$field];
      break;
  }
} //

function getFieldIndex_Column_RecordHelper($field, $order_field, $order_value, array $attributes = array())
{
  $label = lang("Admin/Cloudflare/API/Record.label.{$field}");
  switch ($field) {
    case 'zone_uid':
      $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) .
        "<button onClick=\"getElementsByClassNameCopyToClipboard('label_zones'); return false;\" class=\"btn btn-sm btn-danger btn-circle\">Copy Zones</buttion>";
      break;
    case 'host':
      $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) .
        "<button onClick=\"getElementsByClassNameCopyToClipboard('label_hosts'); return false;\" class=\"btn btn-sm btn-danger btn-circle\">Copy Hosts</buttion>";
      break;
    default:
      $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);
      break;
  }
} //

function getFieldIndex_Row_RecordHelper($field, array $row, array $fieldFilters, $fieldFormOptions, $attributes = array(), string $old_zone_uid = ''): string
{
  switch ($field) {
    case 'zone_uid':
      $temp = '';
      if ($old_zone_uid !== $row[$field]) {
        $temp = sprintf(
          '<div style="text-align:left;">%s <label class="label_zones">%s</label></div>',
          anchor(base_url() . '/admin/cloudflare/zone/reload/' . $row['zone_uid'], ' ', ["class" => "btn btn-sm btn-primary btn-circle fa fa-refresh", "target" => "_self"]),
          $fieldFormOptions[$field][$row[$field]]
        );
      }
      return $temp;
      break;
    case 'host':
      return sprintf(
        '<div style="text-align:left; padding-left:20px;"><B><label class="label_hosts">%s</label></B></div>',
        anchor(base_url() . '/admin/cloudflare/record/cdnToggle/' . $row['uid'], $row[$field], ["class" => $row['fixed'] == 'on' ? 'btn btn-outline-secondary fa fa-lock' : '', "target" => "_self"])
      );
      break;
    case 'type':
    case 'fixed':
      $attributes["onChange"] = sprintf('location.href="%s/toggle/%s/%s?%s="+this.options[this.selectedIndex].value', current_url(), $row['uid'], $field, $field);
      return getFieldForm_RecordHelper($field, $row[$field], $fieldFormOptions, $attributes);
      break;
    case 'proxied':
      if ($row['fixed'] == 'on') {
        return $fieldFormOptions[$field][$row[$field]];
      }
      $attributes["onChange"] = sprintf('location.href="%s/toggle/%s/%s?%s="+this.options[this.selectedIndex].value', current_url(), $row['uid'], $field, $field);
      return getFieldForm_RecordHelper($field, $row[$field], $fieldFormOptions, $attributes);
    case 'locked':
      return $fieldFormOptions[$field][$row[$field]];
      break;
    case 'updated_at':
    case 'created_at':
      return str_split($row[$field], 10)[0];
      break;
    default:
      return $row[$field];
      break;
  }
} //