<?php

namespace App\Helpers\Customer;

use App\Models\Customer\ServiceModel;

class ServiceHelper extends CustomerHelper
{
    public function __construct()
    {
        parent::__construct();
        $this->setTitleField(field: ServiceModel::TITLE);
    }
    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':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $attributes = ['data-price' => 'price'];
                $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 'amount':
            case 'sale':
                $value = number_format($value) . "원";
                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 'addServer':
                $action = form_label(
                    $label ? $label : ICONS['REBOOT'],
                    $action,
                    [
                        "data-src"       => "/admin/customer/service/addServer/{$viewDatas['entity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class" => "btn btn-sm form-label-sm",
                        ...$extras
                    ]
                );
                break;
            case 'onetime':
                $action = form_label(
                    $label ? $label : ICONS['ONETIME'],
                    $action,
                    [
                        "data-src"       => "/admin/customer/payment/create?serviceinfo_uid={$viewDatas['entity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class" => "btn btn-sm form-label-sm",
                        ...$extras
                    ]
                );
                break;
            case 'history':
                $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" => "#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;
    }
}
