<?php

namespace App\Helpers\Customer;

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

class ServiceHelper extends CustomerHelper
{
    public function __construct()
    {
        parent::__construct();
        $this->setTitleField(field: ServiceModel::TITLE);
    }
    protected function form_dropdown_common_process(string $field, mixed $value, array $viewDatas, array $extras = [], array $attributes = [], mixed $entity): string
    {
        $attribute = "";
        switch ($field) {
            case 'serverinfo_uid':
                $attribute = "";
                foreach ($attributes as $attribute_tag => $attribute_value) {
                    $attribute .= sprintf(" %s=\"%s\"", $attribute_tag, $entity instanceof ServerEntity ? $entity->$attribute_value() : $attribute_value);
                }
                break;
            default:
                $attribute = parent::form_dropdown_common_process($field, $value, $viewDatas, $extras, $attributes, $entity);
                break;
        }
        return $attribute;
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'site':
                $extras['onChange'] = "$('select[name=\'clientinfo_uid\']').select2('open')";
                $form = $this->form_dropdown_common($field, $value, $viewDatas, $extras);
                break;
            case 'serverinfo_uid':
                if ($value === null && array_key_exists('entity', $viewDatas)) {
                    $value = $viewDatas['entity']->getServerEntity()->getPK();
                }
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $extras['onChange'] = "document.querySelector('input[name=\'amount\']').value = this.options[this.selectedIndex].getAttribute('data-price')";
                $attributes = ['data-price' => 'getPrice'];
                $form = $this->form_dropdown_common($field, $value, $viewDatas, $extras, $attributes);
                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':
                $value = "<a href=\"/admin/customer/client/detail/{$value}\">" . $viewDatas['control']['field_optons'][$field][$value]->getTitle() . "</a>";
                break;
            case 'serverinfo_uid':
                $value = array_key_exists('entity', $viewDatas) ? $viewDatas['entity']->getServerEntity()->getCode() : "지정된서버없음";
                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;
            case 'history':
                $value = nl2br($value);
                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, $label ? $label : $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()}&serviceinfo_uid={$viewDatas['entity']->getPK()}&ActionTemplate=popup",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#index_action_form",
                        ...$extras
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
