<?php

namespace App\Helpers;

class PaymentHelper extends CommonHelper
{
    public function __construct()
    {
        parent::__construct();
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'billing':
            case 'pay':
                $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;
            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 "countdown": //결제일Countdown
                $value = $viewDatas['entity']->getCountDueAt();
                break;
            case 'amount':
                $value = number_format($value) . "원";
                break;
            default:
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
        }
        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;
            case 'delete':
                $action = $this->getAuthContext()->isAccessRole([ROLE['USER']['SECURITY']]) ? parent::getListButton($action, $label, $viewDatas, $extras) : "";
                break;
            case 'invoice':
                $action = form_submit($action . "_submit", $label ? $label : '청구서 발행', [
                    "formaction" => current_url() . '/' . $action,
                    "class" => "btn btn-sm btn-outline btn-primary",
                ]);
                break;
            case 'paid':
                $action = sprintf("<a href=\"/admin/payment/paid/%s\" class=\"btn btn-sm btn-warning\">%s</a>", $viewDatas['entity']->getPK(), $label ? $label : '결제');
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
