<?php

namespace App\Helpers\Cloudflare;

use App\Helpers\CommonHelper;
use App\Models\Cloudflare\AccountModel;

class AccountHelper extends CommonHelper
{
    public $old_parent = "";
    public function __construct()
    {
        parent::__construct();
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        $value = $value ?: DEFAULTS['EMPTY'];
        switch ($field) {
            case AccountModel::PARENT:
                $extra_class = isset($extras['class']) ? $extras['class'] . ' select-field' : 'select-field';
                $form = form_dropdown($field, [
                    "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
                    ...$viewDatas['field_options'][$field]
                ], isset($viewDatas[$field]) ? $viewDatas[$field] : $value, ['class' => $extra_class, ...array_diff_key($extras, ['class' => ''])]);
                break;
            case AccountModel::TITLE:
                $form = form_input($field, $value, ["placeholder" => "예)test@exmaple.com", ...$extras]);
                break;
            case 'type':
                $form = form_dropdown($field, [
                    "" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택',
                    ...$viewDatas['field_options'][$field],
                ], $value, $extras);
                break;
            default:
                $form = parent::getFieldForm($field, $value, $viewDatas, $extras);
                break;
        }
        return $form;
    } //
    public function getFieldView(string $field, array $viewDatas, array $extras = []): string
    {
        $value = $viewDatas['entity']->$field ?: DEFAULTS['EMPTY'];
        switch ($field) {
            case AccountModel::PARENT:
                if ($this->old_parent === $viewDatas['entity']->getParent()) {
                    $value = "";
                } else {
                    // dd($viewDatas['field_options']);
                    $value = anchor(
                        current_url() . "/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>";
                }
                $this->old_parent = $viewDatas['entity']->getParent();
                break;
            case AccountModel::TITLE:
                $value = anchor(
                    base_url() . $viewDatas['uri_path'] . 'zone/reload/' . $viewDatas['entity']->getPK(),
                    ICONS["RELOAD"],
                    [
                        "class" => "btn btn-sm btn-primary btn-circle",
                        "target" => "_self",
                    ]
                ) . " " .
                    anchor(
                        base_url() . $viewDatas['uri_path'] . "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 'type':
            case 'status':
                $value = $viewDatas['field_options'][$field][$value];
                break;
            default:
                $value = parent::getFieldView($field, $viewDatas, $extras);
                break;
        }
        return $value;
    } //
    public function getListButton(string $action, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'create':
                $action = "";
                break;
            case 'modify':
                $action = $viewDatas['cnt'];
                break;
            case 'delete':
                $action = "";
                break;
            case 'batchjob':
                $action = "";
                break;
        }
        return $action;
    }
}
