<?php

namespace App\Helpers\Equipment;

use RuntimeException;

class CHASSISHelper extends EquipmentHelper
{
    public function __construct()
    {
        parent::__construct();
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'cpuinfo_uid':
            case 'raminfo_uid':
            case 'diskinfo_uid':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $form = form_dropdown($field,  $viewDatas['formOptions'][$field]['options'], $value, $extras);
                break;
            default:
                $form = parent::getFieldForm($field, $value, $viewDatas, $extras);
                break;
        }
        return $form;
    }
    public function getFieldView(string $field, mixed $value, array $viewDatas, array $extras = []): string|null
    {
        switch ($field) {
            case 'price':
                $value = number_format($value) . "원";
                break;
            default:
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
        }
        if (is_array($value)) {
            throw new RuntimeException(static::class . "->" . __FUNCTION__ . "에서 오류발생:{$field}에 해당하는 Return 값이 배열형식입니다.\n" . var_export($value, true));
        }
        return $value;
    }
    public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'stock':
                $action = form_label(
                    $label ? $label : ICONS["SERVER_ITEM_{$action}"],
                    $action,
                    [
                        "data-src"       => "/admin/equipment/chassis/modify/{$viewDatas['entity']->getPK()}?ActionTemplate=popup",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class"          => "btn btn-sm form-label-sm text-primary",
                        ...$extras,
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
