<?php

namespace App\Helpers\Customer;

use App\Entities\Equipment\CodeEntity;
use App\Models\Customer\ServiceModel;

class ServiceHelper extends CustomerHelper
{
    public function __construct()
    {
        parent::__construct();
        $this->setTitleField(field: ServiceModel::TITLE);
    }
    public function getFieldLabel(string $field, string $label, 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?serviceinfo_uid={$viewDatas['entity']->getPK()}&item_type={$field}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#index_action_form",
                        ...$extras
                    ]
                );
                break;
            default:
                $label = parent::getFieldLabel($field, $label, $viewDatas, $extras);
                break;
        }
        return $label;
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'switchinfo_uid':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $form = $this->form_dropdown_custom($field, $value, $viewDatas, $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 'clientinfo_uid':
            case 'ownerinfo_uid':
                $temp = parent::getFieldForm($field, $value, $viewDatas, $extras);
                $value = $temp . form_label(
                    ICONS['HISTORY'],
                    'client_history',
                    [
                        "data-src" => "/admin/customer/clienthistory?clientinfo_uid={$value}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#index_action_form",
                        "class" => "btn btn-outline btn-primary btn-circle",
                        "target" => "_self"
                    ]
                );
                break;
            case "LINE":
            case "IP":
            case "SERVER":
            case "CPU":
            case "RAM":
            case "STORAGE":
            case "SOFTWARE":
            case "DEFENCE":
            case "DOMAIN":
                $temps = ["<ul>"];
                foreach ($viewDatas['entity']->getItemEntities($field) as $itemEntity) {
                    // dd($viewDatas['control']['filter_optons'][$field]);
                    $temps[] = sprintf(
                        "<li title=\"%s\">%s&nbsp;%s&nbsp;%s</li>",
                        $itemEntity->getView_Price(),
                        $viewDatas['control']['filter_optons'][$field][$itemEntity->getItemUid()]->getTitle(),
                        $itemEntity->getView_BillingCycle(),
                        $itemEntity->getView_Sale()
                    );
                }
                $temps[] = "</ul>";
                $value = implode("", $temps);
                break;
            case 'billing_at':
                if (array_key_exists('unPaids', $viewDatas)) {
                    if (array_key_exists($viewDatas['entity']->getPK(), $viewDatas['unPaids'])) {
                        $value .= "<div><a href=\"/admin/customer/payment?={$viewDatas['entity']->getPK()}\">미지급: <span style=\"color:red;\">" . $viewDatas['unPaids'][$viewDatas['entity']->getPK()] . "</span>개</a></div>";
                    }
                }
                break;
            default:
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
        }
        if (is_array($value)) {
            echo __METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다";
            exit;
        }
        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':
                $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras];
                $action = form_label(
                    $label ? $label : ICONS['HISTORY'],
                    $action,
                    [
                        "data-src" => "/admin/customer/servicehistory?serviceinfo_uid={$viewDatas['entity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#index_action_form",
                        ...$extras
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
