<?php

namespace App\Helpers;

use App\Models\PaymentModel;

class PaymentHelper extends CommonHelper
{
    public function __construct()
    {
        parent::__construct();
        $this->setTitleField(field: PaymentModel::TITLE);
    }

    public function getFieldView(string $field, mixed $value, array $viewDatas, array $extras = []): string|null
    {
        switch ($field) {
            case "countdown": //결제일Countdown
                $value = $viewDatas['entity']->getCountDueAt();
                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 'create':
            case 'batchjob_delete':
                $action = "";
                break;
            case 'modify':
                if ($this->getMyAuth()->isAccessRole(['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 = "";
                if ($this->getMyAuth()->isAccessRole(['security'])) {
                    $action = $viewDatas['entity']->getStatus() !== $viewDatas['entity']::DEFAULT_STATUS ? "" : parent::getListButton($action, $label, $viewDatas, $extras);
                }
                break;
            case 'invoice':
                $action = form_submit($action . "_submit", $label ? $label : '청구서 발행', [
                    "formaction" => current_url() . '/' . $action,
                    "class" => "btn btn-outline btn-primary",
                ]);
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
