<?php

namespace App\Helpers\Customer;

use App\Entities\CommonEntity;
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 = []): string
    {
        $html = "";
        switch ($field) {
            case "serverinfo_uid":
                foreach ($viewDatas['control']['field_optons'][$field] as $option_key => $option_value) {
                    $isSelected = $option_key == $value ? ' selected' : '';
                    $isDisabled  = "";
                    $attribute = "";
                    $label      = "";
                    if ($option_value instanceof CommonEntity) {
                        if (in_array($viewDatas['control']['action'], ['create_form'])) {
                            if ($option_value->getStatus() != $option_value::DEFAULT_STATUS) {
                                $isDisabled = " disabled";
                            }
                        }
                        foreach ($attributes as $attribute_name => $attribute_value) {
                            $attribute .= sprintf(" %s=\"%s\"", $attribute_name, $option_value->$attribute_value());
                        }
                        $label = $option_value->getStatus() != $option_value::DEFAULT_STATUS ? STATUS_ICONS['NOT_AVAILABLE'] : STATUS_ICONS['AVAILABLE'];
                        $label .= $option_value->getCustomTitle();
                    } else {
                        $label = $option_value;
                    }
                    $html .= sprintf("<option value=\"%s\"%s%s%s>%s</option>", $option_key, $isSelected, $isDisabled, $attribute, $label);
                }
                break;
            default:
                $html = parent::form_dropdown_common_process($field, $value, $viewDatas, $extras, $attributes);
                break;
        }
        return $html;
    }

    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';
                $attributes = ['data-price' => 'getPrice'];
                $form = $this->form_dropdown_common($field, $value, $viewDatas, $extras, $attributes);
                break;
            case 'amount':
                $form = form_input($field, 0, ["readonly" => "readonly", ...$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':
                if (array_key_exists($value, $viewDatas['control']['field_optons'][$field])) {
                    $value = "<a href=\"/admin/customer/client/detail/{$value}\">" . $viewDatas['control']['field_optons'][$field][$value]->getTitle() . "</a>";
                }
                break;
            case 'billing_at':
                if (array_key_exists('unPaids', $viewDatas)) {
                    if (array_key_exists($viewDatas['entity']->getPK(), $viewDatas['unPaids'])) {
                        $value .= sprintf(
                            "<div><a href=\"/admin/customer/payment?serviceinfo_uid=%s\">미지급: <span style=\"color:red;\">%s개,총 %s원</span></a></div>",
                            $viewDatas['entity']->getPK(),
                            $viewDatas['unPaids'][$viewDatas['entity']->getPK()]['cnt'],
                            number_format($viewDatas['unPaids'][$viewDatas['entity']->getPK()]['amount'])
                        );
                    }
                }
                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, $label, $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/service/history?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;
    }
}
