<?php

namespace App\Helpers;

use App\Models\Customer\PaymentModel;

class HomeHelper extends CommonHelper
{
    public function __construct()
    {
        parent::__construct();
        $this->setTitleField(field: PaymentModel::TITLE);
    }

    public function getFieldView(string $field, mixed $value, array $viewDatas, array $extras = []): string|null
    {
        switch ($field) {
            case 'item_uid':
                $value = $viewDatas['control']['filter_optons'][$viewDatas['entity']->getItemType()][$value]->getTitle();
                break;
            case 'type':
                // echo $value;
                // dd($viewDatas['control']['filter_optons'][$field]);
                $value = $viewDatas['control']['filter_optons'][$field][$value]->getTitle();
                break;
            case 'amount':
                $value = number_format($value) . "원";
                break;
            case 'status':
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
            case "LINE":
            case "IP":
            case "SERVER":
            case "CPU":
            case "RAM":
            case "STORAGE":
            case "SOFTWARE":
            case "DEFENCE":
            case "DOMAIN":
                // if ($field == 'DEFENCE') {
                //     dd($viewDatas['entity']->getItemEntities($field));
                // }
                $temps = [];
                foreach ($viewDatas['entity']->getItemEntities($field) as $itemEntity) {
                    $temps[] = $viewDatas['control']['filter_optons'][$field][$itemEntity->getItemUid()]->getTitle();
                }
                $value = implode("<BR>", $temps);
                break;
            default:
                if (in_array($field, $viewDatas['control']['actionFilters'])) {
                    if (array_key_exists($value, $viewDatas['control']['filter_optons'][$field])) {
                        $value = $viewDatas['control']['filter_optons'][$field][$value]->getTitle();
                    } else {
                        $value = "";
                    }
                }
                break;
        }
        if (is_array($value)) {
            echo __METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다";
            exit;
        }
        return $value;
    }
    public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'new_service_view':
                $extras = ["class" => "btn btn-outline btn-light btn-circle", "target" => "_self", ...$extras];
                $action = form_label(
                    $label ? $label : ICONS['SEARCH'],
                    $action,
                    [
                        "data-src" => '/admin/customer/service/view/' . $viewDatas['entity']->getPK(),
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#index_action_form",
                        ...$extras
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
