<?php
function getFieldLabel_AccountHelper(string $field, array $viewDatas, array $attributes = []): string
{
    switch ($field) {
        default:
            if (strpos($viewDatas['field_rules'][$field], 'required') !== false) {
                $attributes = ['style="color:red";'];
            }
            $label = sprintf(
                "<span %s>%s</span>",
                implode(" ", $attributes),
                lang("{$viewDatas['class_path']}.label.{$field}")
            );
            break;
    }
    return $label;
}
//header.php에서 getFieldForm_Helper사용
function getFieldForm_AccountHelper(string $field, mixed $value, array $viewDatas, array $attributes = []): string
{
    $value = $value ?: DEFAULTS['EMPTY'];
    switch ($field) {
        case 'user_uid':
            $form = form_dropdown($field, $viewDatas['field_options'][$field], $value, [...$attributes, 'class' => "select-field"]);
            // // return form_multiselect($field, $field_options[$field], is_array($value) ? [...$value] : [$value], [...$attributes]);
            // foreach ($viewDatas['field_options'][$field] as $key => $label) {
            //   $checkboxs[] = form_checkbox("{$field}[]", $key, in_array($key, explode(DEFAULTS["DELIMITER_ROLE"], $value))) . $label;
            // }
            // return implode("&nbsp;", $checkboxs);
            break;
        case 'content':
        case 'head':
        case 'tail':
            $form = form_textarea($field, html_entity_decode($value), ['class' => 'editor', 'rows' => '20', 'cols' => '100']);
            break;
        case "type":
        case "status":
            $form = form_dropdown($field, $viewDatas['field_options'][$field], $value, $attributes);
            break;
        case 'id':
            $form = form_input($field, $value, ["placeholder" => "예)sample@test.com", "style" => "width:60%; ::placeholder{color:silver; opacity: 1;}"]);
            break;
        case 'name':
            $form = form_input($field, $value, ["placeholder" => "예)", "style" => "width:60%; ::placeholder{color:silver; opacity: 1;}"]);
            break;
        case 'updated_at':
        case 'created_at':
            $form = form_input($field, $value, ['class' => 'calender']);
            break;
        default:
            $form = form_input($field, $value, ["style" => "width:60%;"]);
            break;
    }
    return $form;
} //

function getFieldView_AccountHelper(string $field, mixed $entity, array $viewDatas, array $attributes = [])
{
    $value = $entity->$field ?: DEFAULTS['EMPTY'];
    switch ($field) {
        case 'category_uid':
            foreach (array_values($viewDatas['field_options'][$field]) as $category_2depths) {
                foreach ($category_2depths as $key => $depth) {
                    if ($key == $depth) {
                        $value = $depth;
                    }
                }
            }
            break;
        case 'cost':
        case 'price':
        case 'sale':
            $value = number_format(!$value ? 0 : $value) . "원";
            break;
        case 'stock':
        case 'view_cnt':
            $value = number_format(!$value ? 0 : $value);
            break;
        case 'content':
            $value = html_entity_decode($value);
            break;
        case 'updated_at':
        case 'created_at':
            $value = $value ? date("Y-m-d", strtotime($value)) : "";
            break;
        default:
            if (in_array($field, $viewDatas['filter_fields']) && $value) {
                $value = $viewDatas['field_options'][$field][$value];
            }
            break;
    }
    return $value;
} //

function getListHeaders_AccountHelper(string $field, array $viewDatas, array $attributes = []): string
{
    $label = getFieldLabel_AccountHelper($field, $viewDatas, $attributes);
    if ($field == $viewDatas['order_field']) {
        $label .= $viewDatas['order_value'] == 'ASC' ? ICONS["UP"] : ICONS["DOWN"];
    }
    $order_value = $viewDatas['order_value'] == 'DESC' ? "ASC" : "DESC";
    $viewDatas['uri']->addQuery('order_field', $field);
    $viewDatas['uri']->addQuery('order_value', $order_value);
    $header = anchor((string)$viewDatas['uri'], $label);
    switch ($field) {
        case 'title':
            $attributes = [...$attributes, "class=\"col-2\""];
            break;
    }
    return sprintf("<th %s>%s</th>", implode(" ", $attributes), $header);
}
function getListColumns_AccountHelper(string $field, mixed $entity, array $viewDatas, array $attributes = []): string
{
    switch ($field) {
        case 'title':
        case 'name':
            $value = anchor(
                current_url() . '/view/' . $entity->getPK(),
                getFieldView_AccountHelper($field, $entity, $viewDatas),
                ["target" => "_self"]
            );
            break;
        default:
            if (in_array($field, $viewDatas['filter_fields'])) {
                $attributes["onChange"] = sprintf(
                    'location.href="%s/toggle/%s/%s?%s="+this.options[this.selectedIndex].value',
                    current_url(),
                    $entity->getPK(),
                    $field,
                    $field
                );
                $value                  = getFieldForm_AccountHelper($field, $entity, $viewDatas, $attributes);
            }
            $value = getFieldView_AccountHelper($field, $entity, $viewDatas);
            break;
    }
    return $value;
}
