<?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 getFieldView(string $field, array $viewDatas, array $extras = []): string
    {
        $value = $viewDatas['entity']->$field ?? "";
        switch ($field) {
            case "IP":
            case "DEFENCE":
            case "SOFTWARE":
            case "CPU":
            case "RAM":
            case "DISK":
                $temps = [];
                foreach ($viewDatas['entity']->getPartEntities($field) as $partEntity) {
                    $temps[] = "<div>" . $partEntity->getTitle() . "</div>";
                }
                $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras];
                $temps[] = form_label(
                    '변경',
                    $field,
                    [
                        "data-src" => "/admin/customer/servicepart?type={$field}&serviceinfo_uid={$viewDatas['entity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#index_action_form",
                        ...$extras
                    ]
                );
                $value = implode("", $temps);
                break;
            default:
                $value = parent::getFieldView($field, $viewDatas, $extras);
                break;
        }
        return $value;
    }
}
