<?php

use App\Models\Cloudflare\AccountModel;

function getFieldLabel_AccountHelper(string $field, array $viewDatas, array $extras = []): string
{
    switch ($field) {
        case AccountModel::PARENT:
            $extras = [...$extras, "class" => 'text-danger'];
            break;
        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_AccountHelper(string $field, mixed $value, array $viewDatas, array $extras = []): string
{
    $value = $value ?: DEFAULTS['EMPTY'];
    switch ($field) {
        case AccountModel::PARENT:
            $form = form_dropdown($field, [
                "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
                ...$viewDatas['field_options'][$field]
            ], isset($viewDatas[AccountModel::PARENT]) ? $viewDatas[AccountModel::PARENT] : $value, [...$extras, 'class' => "select-field"]);
            // // return form_multiselect($field, $field_options[$field], is_array($value) ? [...$value] : [$value], [$extras]);
            // 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 AccountModel::TITLE:
            $form = form_input($field, $value, ["placeholder" => "예)test@exmaple.com", "style" => "width:100%; ::placeholder{color:silver; opacity: 1;}"]);
            break;
        case 'type':
        case 'status':
            $form = form_dropdown($field, [
                "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
                ...$viewDatas['field_options'][$field]
            ], $value, $extras);
            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_AccountHelper(string $field, array $viewDatas, array $extras = [])
{
    $value = $viewDatas['entity']->$field ?: DEFAULTS['EMPTY'];
    switch ($field) {
        case AccountModel::PARENT:
            if ($viewDatas['old_auth'] === $viewDatas['entity']->getParent()) {
                $value = "";
            } else {
                // dd($viewDatas['field_options']);
                $value = anchor(
                    base_url() . strtolower($viewDatas['root']) . "auth/reload/"  . $viewDatas['entity']->getParent(),
                    ICONS["RELOAD"],
                    [
                        "class" => "btn btn-sm btn-primary btn-circle",
                        "target" => "_self"
                    ]
                ) . " <span class=\"label_auth\">" .
                    preg_replace("/(\w+)@(.+)/", "$1", $viewDatas['field_options'][$field][$value])
                    . "</span>";
            }
            break;
        case AccountModel::TITLE:
            $value = anchor(
                current_url() . '/reload/' . $viewDatas['entity']->getPK(),
                ICONS["RELOAD"],
                [
                    "class" => "btn btn-sm btn-primary btn-circle",
                    "target" => "_self"
                ]
            ) . " " .
                anchor(
                    base_url() . strtolower($viewDatas['root']) . "zone?account_uid="   . $viewDatas['entity']->getPK(),
                    ICONS["FLAG"],
                    [
                        "class" => "btn btn-sm btn-primary btn-circle",
                        "target" => "_self"
                    ]
                ) . " <span class=\"label_Accounts\">{$value}</span>";
            break;
        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 '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_AccountHelper($field, $viewDatas['entity']->$field, $viewDatas, $extras);
            // }
            break;
    }
    return $value;
} //
function getListLabel_AccountHelper(string $field, array $viewDatas, array $extras = []): string
{
    $label = getFieldLabel_AccountHelper($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 AccountModel::TITLE:
            break;
    }
    return sprintf("<th %s>%s</th>", implode(" ", $extras), $label);
}
function getListButtonLabel_AccountHelper(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_AccountHelper(string $action, array $viewDatas, array $extras = []): string
{
    switch ($action) {
        case 'create':
            $viewDatas['list_action_url'] = current_url() . '/' . $action . '?' . $viewDatas['uri']->getQuery(['only' => AccountModel::PARENT]);
            $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras];
            $action = getListButtonLabel_AccountHelper($action, '입력',  $viewDatas, $extras);
            break;
        case 'sync':
            // $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_AccountHelper($action, $viewDatas['cnt'], $viewDatas, $extras);
            $action = $viewDatas['cnt'];
            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);
            $action = "";
            break;
    }
    return $action;
}
