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

//header.php에서 getFieldForm_Helper사용
function getFieldForm_ZoneHelper($field, $value, array $formOptions, array $attributes = array())
{
  $value = is_null($value) ? DEFAULT_EMPTY : $value;
  switch ($field) {
    case 'account_uid':
    case 'development_mode':
    case 'ipv6':
    case 'security_level':
    case 'status':
      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_ZoneHelper($field, array $fieldDatas, array $fieldFilters, array $fieldFormOptions, array $attributes = array())
{
  switch ($field) {
    default:
      if (in_array($field, $fieldFilters)) {
        return getFieldForm_ZoneHelper($field, $fieldDatas[$field], $fieldFormOptions, $attributes);
      }
      return $fieldDatas[$field];
      break;
  }
} //

function getFieldIndex_Column_ZoneHelper($field, $order_field, $order_value, array $attributes = array())
{
  $label = lang("Admin/Cloudflare/API/Zone.label.{$field}");
  switch ($field) {
    case 'domain':
      $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_domains'); return false;\" class=\"btn btn-sm btn-danger btn-circle\">Copy Domains</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_ZoneHelper($field, array $row, array $fieldFilters, $fieldFormOptions, $attributes = array(), string $old_account_uid = ''): string
{
  switch ($field) {
    case 'account_uid':
      $temp = '';
      if ($old_account_uid !== $row[$field]) {
        $temp = sprintf(
          '<div style="text-align:left;">%s %s</div>',
          anchor(base_url() . '/admin/cloudflare/account/reload/' . $row['account_uid'], ' ', ["class" => "btn btn-sm btn-primary btn-circle fa fa-refresh", "target" => "_self"]),
          preg_replace("/(\w+)@(.+)/", "$1", $fieldFormOptions[$field][$row[$field]])
        );
      }
      return $temp;
      break;
    case 'domain':
      return sprintf(
        '<div style="text-align:left;">%s %s<B><label class="label_domains">%s</label></B></div>',
        anchor(current_url() . '/reload/' . $row['uid'], ' ', ["class" => "btn btn-sm btn-primary btn-circle fa fa-refresh", "target" => "_self"]),
        anchor(base_url() . '/admin/cloudflare/record?zone_uid=' . $row['uid'], ' ', ["class" => "btn btn-sm btn-primary btn-circle fa fa-flag", "target" => "_self"]),
        $row[$field]
      );
      break;
    case 'name_servers':
    case 'original_name_servers':
      return str_replace(",", "<BR>", $row[$field]);
      break;
    case 'status':
      return $row[$field];
      break;
    case 'updated_at':
    case 'created_at':
      return 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_ZoneHelper($field, $row[$field], $fieldFormOptions, $attributes);
      }
      return $row[$field];
      break;
  }
} //