<?php

namespace App\Helpers\Equipment;

use App\Models\Equipment\ServerModel;

class ServerHelper extends EquipmentHelper
{
    public function __construct()
    {
        parent::__construct();
        $this->setTitleField(field: ServerModel::TITLE);
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'clientinfo_uid':
            case 'serviceinfo_uid':
                $form = parent::getFieldForm($field, $value, $viewDatas, $extras);
                if (in_array($viewDatas['control']['action'], ['modify_form']) && $value) {
                    $form = $this->getFieldView($field, $value, $viewDatas, $extras);
                    $form .= form_hidden($field,  (string)$value);
                }
                break;
            case 'ip': //값 그대료 표시
                $form = "<div class=\"ac-wrap\" style=\"position:relative;\">";
                $form .= form_input($field, $value ?? "", [
                    'id' => 'ipInput',
                    "list" => "ipList",
                    "placeholder" => "예: 27.125.207.100",
                    "autocomplete" => "off",
                    ...$extras
                ]);
                $form .= "<div id=\"ipPanel\" class=\"ac-panel\" hidden></div>";
                $form .= "</div>";
                break;
            case 'switch': //값 그대료 표시
                $form = "<div class=\"ac-wrap\" style=\"position:relative;\">";
                $form .= form_input($field, $value ?? "", [
                    'id' => 'switchInput',
                    "placeholder" => "Switch 코드 입력",
                    "autocomplete" => "off",
                    ...$extras
                ]);
                $form .= "<div id=\"switchPanel\" class=\"ac-panel\" hidden></div>";
                $form .= "</div>";
                break;
            case 'manufactur_at':
            case 'format_at':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' calender' : 'calender';
                $form = form_input($field, $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 'ip': //값 그대료 표시
                break;
            case 'title':
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
            case 'manufactur_at':
            case 'format_at':
                $value = $value ? date("Y-m-d", strtotime($value)) : "";
                break;
            default:
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
        }
        if (is_array($value)) {
            throw new \Exception(__METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다");
        }
        return $value;
    }
    public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'modify':
                $action = parent::getListButton($action,   $viewDatas['entity']->getCode(), $viewDatas, $extras);
                break;
            case 'history':
                $action = $label ? $label : form_label(
                    $label ? $label : ICONS['HISTORY'],
                    $action,
                    [
                        "data-src" => "/admin/customer/clienthistory?clientinfo_uid={$viewDatas['entity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class" => "btn btn-sm btn-primary form-label-sm",
                        ...$extras
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
