<?php
function getFieldLabel_BillingHelper($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_BillingHelper($field, $value, array $viewDatas, array $attributes = array())
{
  $value = $value ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'order_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"]);
      // // return form_multiselect($field, $fieldFormOptions[$field], is_array($value) ? [...$value] : [$value], [...$attributes]);
      // foreach ($viewDatas['fieldFormOptions'][$field] as $key => $label) {
      //   $checkboxs[] = form_checkbox("{$field}[]", $key, in_array($key, explode(DEFAULTS["DELIMITER_ROLE"], $value))) . $label;
      // }
      // return implode("&nbsp;", $checkboxs);
      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 "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_BillingHelper($field, $entity, array $viewDatas)
{
  $value = $entity->$field ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'cost':
    case 'sale':
      return number_format(!$value ? 0 : $value) . "원";
      break;
    case 'price':
      return number_format(!$value ? 0 : $value) . "원";
      break;
    case 'response':
      return nl2br($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_BillingHelper($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_BillingHelper($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-3'>%s</th>", $columnData);
      break;
    default:
      return sprintf("<th>%s</th>", $columnData);
      break;
  }
} //

//Front용
function getFieldIndex_Row_BillingHelper($field, $entity, array $viewDatas): string
{
  $value = $entity->$field ?: DEFAULTS['EMPTY'];
  switch ($field) {
    case 'title':
    case 'name':
      return anchor(
        current_url() . '/view/' . $entity->getPrimaryKey() . '?category=' . $viewDatas['currentCategory']->getPrimaryKey(),
        $value,
        ["target" => "_self"]
      );
      break;
    case 'order_uid':
    case 'user_uid':
      return getFieldView_BillingHelper($field, $entity, $viewDatas);
      break;
    case 'board_file':
    case 'upload_file':
      // echo var_export($entity, true);
      // exit;
      return $entity->getFileDownload(
        base_url() .
          $viewDatas['control'] .
          "/billing/download/{$field}/{$entity->getPrimaryKey()}",
        $field
      );
      break;
    case 'type':
      if ($entity->status != DEFAULTS['STATUS']) {
        // $card = anchor(
        //   URLS['card'] . '/' . $entity->getPrimaryKey(),
        //   ICONS['CARD'] . lang("{$viewDatas['className']}.TYPE.CARD"),
        //   ["class" => "btn btn-sm btn-primary btn-circle", "style" => "color:white", "target" => "_self"]
        // );
        $deposit = anchor(
          URLS['deposit'] . '/' . $entity->getPrimaryKey(),
          ICONS['DEPOSIT'] . lang("{$viewDatas['className']}.TYPE.DEPOSIT"),
          ["class" => "btn btn-sm btn-info btn-circle", "style" => "color:white", "target" => "_self"]
        );
        return $deposit;
      } else {
        //결제처리가된경우
        $value = strtoupper($value);
        return ICONS[$value] . lang("{$viewDatas['className']}.TYPE.{$value}");
      }
      break;
    default:
      return getFieldView_BillingHelper($field, $entity, $viewDatas);
      break;
  }
} //
//Admin용
function getFieldIndex_Row_BillingHelper_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;
    case 'board_file':
    case 'upload_file':
      return $entity->getFileDownload(
        base_url() .
          $viewDatas['control'] .
          "/billing/download/{$field}/{$entity->getPrimaryKey()}",
        $field
      );
      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_BillingHelper($field, $entity->$field, $viewDatas, $attributes);
      }
      return getFieldIndex_Row_BillingHelper($field, $entity, $viewDatas);
      break;
  }
} //