<?php

namespace App\Helpers\Customer\Wallet;

use RuntimeException;

class AccountHelper extends WalletHelper
{
    public function __construct()
    {
        parent::__construct();
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'bank':
                $forms = [];
                array_shift($viewDatas['formOptions'][$field]['options']);
                foreach ($viewDatas['formOptions'][$field]['options'] as $key => $label)
                    $forms[] = form_radio($field, $key, $key == $value, $extras) . $label;
                $form = implode(" ", $forms);
                break;
            case 'role':
                $currentRoles = is_array($value)
                    ? array_map('strtolower', array_map('trim', $value))
                    : [];
                $form = '';
                //Form페이지에서는 맨앞에것 제외하기 위함
                $firstOption = array_shift($viewDatas['formOptions'][$field]['options']);
                foreach ($viewDatas['formOptions'][$field]['options'] as $key => $label) {
                    $checked = in_array(strtolower(trim($key)), $currentRoles);
                    $form .= '<label class="me-3">';
                    $form .= form_checkbox('role[]', $key, $checked, ['id' => "role_{$key}", ...$extras]);
                    $form .= " {$label}";
                    $form .= '</label>';
                }
                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':
                $value = number_format($value) . "원";
                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 getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'modify':
                //역활이 보안관리자가 아니면 수정불가
                if ($this->getAuthContext()->isAccessRole([ROLE['USER']['SECURITY']])) {
                    $action = parent::getListButton($action, $label, $viewDatas, $extras);
                } else {
                    $oldBatchJobUids = old("batchjob_uids", null);
                    $oldBatchJobUids = is_array($oldBatchJobUids) ? $oldBatchJobUids : [$oldBatchJobUids];
                    $action = form_checkbox([
                        "id" => "checkbox_uid_{$viewDatas['entity']->getPK()}",
                        "name" => "batchjob_uids[]",
                        "value" => $viewDatas['entity']->getPK(),
                        "class" => "batchjobuids_checkboxs",
                        "checked" => in_array($viewDatas['entity']->getPK(), $oldBatchJobUids)
                    ]);
                    $action .= $label;
                }
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
