<?php

namespace App\Helpers\Customer;

use RuntimeException;

class ServiceHelper extends CustomerHelper
{
    public function __construct()
    {
        parent::__construct();
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'site':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $extras['onChange'] = "$('select[name=\'clientinfo_uid\']').select2('open')";
                $form = form_dropdown($field, $viewDatas['formOptions'][$field]['options'], $value, $extras);
                break;
            case 'serverinfo_uid':
            case 'clientinfo_uid':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $form = form_dropdown($field,  $viewDatas['formOptions'][$field]['options'], $value, $extras);
                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 'serverinfo_uid':
                $value = view_cell("\App\Cells\Equipment\ServerCell::detail", [
                    'serviceEntity' => $viewDatas['entity'],
                    'template'       => 'servicelist',
                ]);
                break;
            case 'amount':
            case 'sale':
                $value = number_format($value) . "원";
                break;
            case 'billing_at':
                if (array_key_exists('unPaids', $viewDatas) && array_key_exists($viewDatas['entity']->getPK(), $viewDatas['unPaids'])) {
                    $value .= sprintf(
                        "<div><a href=\"/admin/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 RuntimeException(static::class . "->" . __FUNCTION__ . "에서 오류발생:{$field}에 해당하는 Return 값이 배열형식입니다.\n" . var_export($value, true));
        }
        return $value;
    }
    public function getListLabel(string $field, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'serverinfo_uid':
                $label = parent::getListLabel($field, $label, $viewDatas, $extras);
                $label .= "<span class=\"float-start rounded border border-primary\" style=\"cursor:pointer;\" onclick=\"copyServerPartsToClipboard()\">ALL 📋</span>";
                break;
            default:
                $label = parent::getListLabel($field, $label, $viewDatas, $extras);
                break;
        }
        return $label;
    }
    public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'detail':
                $action = form_label(
                    $label,
                    'modify_service',
                    [
                        "data-src"       => "/admin/customer/service/modify/{$viewDatas['entity']->getPK()}?clientinfo_uid={$viewDatas['entity']->getClientInfoUid()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class" => "text-primary form-label-sm",
                    ]
                );
                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 'unpaid':
                $action = "{$label} 0원";
                if (array_key_exists($viewDatas['entity']->getPK(), $viewDatas['unPaids'])) {
                    $action = form_label(
                        sprintf("%s건/%s원", $viewDatas['unPaids'][$viewDatas['entity']->getPK()]['cnt'], number_format($viewDatas['unPaids'][$viewDatas['entity']->getPK()]['amount'])),
                        'payment_unpaid',
                        [
                            "data-src"       => "/admin/payment?serviceinfo_uid={$viewDatas['entity']->getPK()}&status=unpaid&ActionTemplate=popup",
                            "data-bs-toggle" => "modal",
                            "data-bs-target" => "#modal_action_form",
                            "class" => "text-primary form-label-sm",
                        ]
                    );
                }
                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;
            case 'onetime':
            case 'prepayment':
                $action = form_label(
                    $label ? $label : ICONS['ONETIME'],
                    $action,
                    [
                        "data-src"       => "/admin/payment/{$action}?serviceinfo_uid={$viewDatas['entity']->getPK()}&ActionTemplate=popup/payment",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class" => "btn btn-sm form-label-sm",
                        ...$extras
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
