<?php

namespace App\Helpers\Equipment;

use App\Models\Equipment\ServerModel;
use CodeIgniter\HTTP\IncomingRequest;

class ServerHelper extends EquipmentHelper
{
    protected ?IncomingRequest $request = null;
    public function __construct(?IncomingRequest $request = null)
    {
        parent::__construct($request);
        $this->setTitleField(field: ServerModel::TITLE);
    }
    // public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    // {
    //     if (in_array($viewDatas['action'], ['create', 'modify'])) {
    //         $extras = (strpos($viewDatas['field_rules'][$field], 'required') !== false) ? ["class" => "form-control", "required" => "", ...$extras] : ["class" => "form-control", ...$extras];
    //     }
    //     switch ($field) {
    //         case 'model':
    //             if (!is_array($viewDatas['field_options'][$field])) {
    //                 throw new \Exception(__METHOD__ . "에서 {$field}의 field_options가 array형태가 아닙니다.");
    //             }
    //             $extra_class = isset($extras['class']) ? $extras['class'] . ' select-field' : 'select-field';
    //             $formOptions = ["" => lang($viewDatas['class_path'] . '.label.' . $field) . ' 선택'];
    //             foreach ($viewDatas['field_options'][$field] as $key => $label) {
    //                 $formOptions[$key] = $label;
    //             }
    //             $form = form_dropdown($field, $formOptions, $value, ['class' => $extra_class, ...array_diff_key($extras, ['class' => ''])]);
    //             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 ?? "";
        switch ($field) {
            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/equipment/link/" . strtolower($field) . "?&serverinfo_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;
    }
}
