<?php

namespace App\Helpers\Customer;

use App\Models\Customer\PaymentModel;

class PaymentHelper extends CustomerHelper
{
    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;
            case 'item_uid':
                //ItemType에 따라 필터옵션에서 Title을 가져옴
                $value = $viewDatas['control']['filter_optons'][$viewDatas['entity']->getItemType()][$value]->getTitle();
                break;
            case 'serviceinfo_uid':
            case 'clientinfo_uid':
            case 'billing_method':
                $value = array_key_exists($value, $viewDatas['control']['filter_optons'][$field]) && $viewDatas['control']['filter_optons'][$field][$value] ? $viewDatas['control']['filter_optons'][$field][$value]->getTitle() : "";
                break;
            default:
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
        }
        if (is_array($value)) {
            echo __METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다";
            exit;
        }
        return $value;
    }
    public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'create':
            case 'batchjob':
            case 'batchjob_delete':
                $action = "";
                break;
            case 'delete':
                $action = $viewDatas['entity']->getStatus() !== $viewDatas['entity']::DEFAULT_STATUS ? "" : parent::getListButton($action, $label, $viewDatas, $extras);
                break;
            case 'invoice':
                $action = form_submit($action . "_submit", '청구서 발행', [
                    "formaction" => current_url() . '/' . $action,
                    "class" => "btn btn-outline btn-primary",
                ]);
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
