<?php

use App\Models\UserModel;

function getFieldLabel_UserHelper(string $field, array $viewDatas, array $extras = []): string
{
    switch ($field) {
        default:
            if (strpos($viewDatas['field_rules'][$field], 'required') !== false) {
                $extras = [...$extras, "class" => 'text-danger'];
            }
            break;
    }
    return form_label(lang("{$viewDatas['class_path']}.label.{$field}"), $field, $extras);
}
//header.php에서 getFieldForm_Helper사용
function getFieldForm_UserHelper(string $field, mixed $value, array $viewDatas, array $extras = []): string
{
    $value = $value ?: DEFAULTS['EMPTY'];
    switch ($field) {
        case UserModel::TITLE:
            $form = form_input($field, $value, ["placeholder" => "예)", "style" => "width:100%; ::placeholder{color:silver; opacity: 1;}"]);
            break;
        case 'role':
            if (in_array($viewDatas['action'], ['create', 'modify'])) {
                $forms = [];
                foreach ($viewDatas['field_options'][$field] as $key => $label) {
                    $values = is_array($value) ? $value : explode(DEFAULTS["DELIMITER_ROLE"], $value);
                    $forms[]  = form_checkbox($field . "[]", $key, in_array($key, $values)) . $label;
                }
                $form = implode(" ", $forms);
            } else {
                $form = form_dropdown($field, [
                    "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
                    ...$viewDatas['field_options'][$field]
                ], $value, $extras);
            }
            break;
        case 'status':
            $form = form_dropdown($field, [
                "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
                ...$viewDatas['field_options'][$field]
            ], $value, $extras);
            break;
        case 'passwd':
            $form = form_password($field, "", ["style" => "width:100%;"]);
            break;
        case 'email':
            $form = form_input($field, $value, ["placeholder" => "예)test@example.com", "style" => "width:100%; ::placeholder{color:silver; opacity: 1;}"]);
            break;
        case 'mobile':
            $form = form_input($field, $value, ["placeholder" => "예)010-0010-0010", "style" => "width:100%; ::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:100%;"]);
            break;
    }
    return $form;
} //
function getFieldView_UserHelper(string $field, array $viewDatas, array $extras = []): string
{
    $value = $viewDatas['entity']->$field ?: DEFAULTS['EMPTY'];
    switch ($field) {
        case UserModel::TITLE:
            // $value = anchor(
            //     current_url() . '/view/' . $viewDatas['entity']->getPK(),
            //     $value,
            //     [
            //         "target" => "_self",
            //     ]
            // );
            break;
        case 'role':
            $roles = [];
            foreach (explode(DEFAULTS["DELIMITER_ROLE"], $value) as $key) {
                $roles[] = $viewDatas['field_options'][$field][$key];
            }
            $value = implode(" , ", $roles);
            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) {
                $extras["onChange"] = sprintf(
                    'location.href="%s/toggle/%s/%s?%s="+this.options[this.selectedIndex].value',
                    current_url(),
                    $viewDatas['entity']->getPK(),
                    $field,
                    $field
                );
                $value = getFieldForm_UserHelper($field, $viewDatas['entity']->$field, $viewDatas, $extras);
            }
            break;
    }
    return $value;
} //
function getListLabel_UserHelper(string $field, array $viewDatas, array $extras = []): string
{
    $label = getFieldLabel_UserHelper($field, $viewDatas, $extras);
    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);
    $label = anchor((string)$viewDatas['uri'], $label);
    switch ($field) {
        case UserModel::TITLE:
            break;
    }
    return sprintf("<th %s>%s</th>", implode(" ", $extras), $label);
}
function getListButtonLabel_UserHelper(string $action, string $label, array $viewDatas, array $extras = []): string
{
    switch ($viewDatas['action_form']) {
        case FORMS['MODAL']:
            $label = form_label(
                $label,
                "",
                [
                    "data-src" => $viewDatas['list_action_url'],
                    "data-bs-toggle" => "modal",
                    "data-bs-target" => "#index_action_form",
                    ...$extras
                ]
            );
            break;
        case FORMS['IFRAME']:
            if ($action == 'create') {
                $label = form_label($label, "", [
                    "onClick" => "changeIframe_src('{$viewDatas['list_action_url']}')",
                    ...$extras,
                ]);
            } else {
                $label = "";
            }
            break;
        default:
            $label = anchor($viewDatas['list_action_url'], $label, $extras);
            break;
    }
    return $label;
}
function getListButton_UserHelper(string $action, array $viewDatas, array $extras = []): string
{
    switch ($action) {
        case 'create':
            $viewDatas['list_action_url'] = current_url() . '/' . $action;
            $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras];
            $action = getListButtonLabel_UserHelper($action, '입력',  $viewDatas, $extras);
            break;
        case 'modify':
            $checkbox = form_checkbox([
                "id" => "checkbox_uid_{$viewDatas['entity']->getPK()}",
                "name" => "batchjob_uids[]",
                "value" => $viewDatas['entity']->getPK(),
                "class" => "batchjobuids_checkboxs"
            ]);
            $viewDatas['list_action_url'] = current_url() . '/' . $action . '/' . $viewDatas['entity']->getPK();
            $action = $checkbox . getListButtonLabel_UserHelper($action, $viewDatas['cnt'], $viewDatas, $extras);
            break;
        case 'delete':
            $viewDatas['list_action_url'] = current_url() . '/' . $action . '/' . $viewDatas['entity']->getPK();
            $extras = ["class" => "btn btn-sm btn-danger btn-circle", "target" => "_self", ...$extras];
            $action = anchor($viewDatas['list_action_url'], ICONS['DELETE'], $extras);
            break;
    }
    return $action;
}
