<?php

namespace App\Helpers\Customer;

use App\Models\Customer\ServiceModel;
use CodeIgniter\HTTP\IncomingRequest;

class ServiceHelper extends CustomerHelper
{
    protected ?IncomingRequest $request = null;
    public function __construct(?IncomingRequest $request = null)
    {
        parent::__construct($request);
        $this->setTitleField(field: ServiceModel::TITLE);
    }
    public function getFieldLabel(string $field, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case "LINE":
            case "IP":
            case "SERVER":
            case "CPU":
            case "RAM":
            case "STORAGE":
            case "SOFTWARE":
            case "DEFENCE":
            case "DOMAIN":
                $label = form_label(lang("{$viewDatas['class_path']}.label.{$field}"), $field, $extras);
                $extras = ["class" => "btn btn-light btn-circle", "target" => "_self", ...$extras];
                $label .= form_label(
                    ICONS['SETUP'],
                    $field,
                    [
                        "data-src"       => "/admin/customer/serviceitem?item_type={$field}&serviceinfo_uid={$viewDatas['entity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#index_action_form",
                        ...$extras
                    ]
                );
                break;
            default:
                $label = parent::getFieldLabel($field, $viewDatas, $extras);
                break;
        }
        return $label;
    }
    public function getFieldView(string $field, array $viewDatas, array $extras = []): string
    {
        $value = $viewDatas['entity']->$field ?? "";
        switch ($field) {
            case "LINE":
            case "IP":
            case "SERVER":
            case "CPU":
            case "RAM":
            case "STORAGE":
            case "SOFTWARE":
            case "DEFENCE":
            case "DOMAIN":
                $temps = ["<ol>"];
                foreach ($viewDatas['entity']->getItemEntities($field) as $itemEntity) {
                    $temps[] = "<li title=\"" . $itemEntity->getView_Price() . "\">" . $viewDatas['field_options'][$field][$itemEntity->getItemUid()] . '&nbsp;' . $itemEntity->getView_BillingCycle() . '&nbsp;' . $itemEntity->getView_Sale() . "</li>";
                }
                $temps[] = "</ol>";
                $value = implode("", $temps);
                break;
            default:
                $value = parent::getFieldView($field, $viewDatas, $extras);
                break;
        }
        if (is_array($value)) {
            echo __METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다";
            exit;
        }
        return $value;
    }
}
