<?php

namespace App\Helpers\Customer;

use App\Models\Customer\ServicePaymentModel;
use CodeIgniter\HTTP\IncomingRequest;

class ServicePaymentHelper extends CustomerHelper
{
    protected ?IncomingRequest $request = null;
    public function __construct(?IncomingRequest $request = null)
    {
        parent::__construct($request);
        $this->setTitleField(field: ServicePaymentModel::TITLE);
    }

    public function getFieldView(string $field, mixed $value, array $viewDatas, array $extras = []): string|null
    {
        switch ($field) {
            case "countdown": //결제일Countdown
                $value = $viewDatas['entity']->getView_CounDueAt();
                break;
            case 'item_uid':
                // echo "Value:" . $viewDatas['entity']->getItemType() . ":" . $value;
                // dd($viewDatas['control']['filter_optons']);
                $value = $viewDatas['control']['filter_optons'][$viewDatas['entity']->getItemType()][$value]->getTitle();
                break;
            case 'amount':
                $value = number_format($value) . "원";
                break;
            case 'status':
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
            default:
                if (in_array($field, $viewDatas['control']['filter_fields'])) {
                    $value = $viewDatas['control']['filter_optons'][$field][$value]->getTitle();
                }
                break;
        }
        if (is_array($value)) {
            echo __METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다";
            exit;
        }
        return $value;
    }
    public function getListButton(string $action, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'modify':
                $action = $viewDatas['entity']->getPK();
                break;
            case 'invoice':
                $action = form_submit($action . "_submit", '청구서 발행', [
                    "formaction" => current_url() . '/' . $action,
                    "class" => "btn btn-outline btn-primary",
                    // "onclick" => "return submitBatchJob()"
                ]);
                break;
            case 'delete':
                if ($viewDatas['entity']->getStatus() !== DEFAULTS['STATUS']) {
                    $action = "";
                } else {
                    $action = parent::getListButton($action, $viewDatas, $extras);
                }
                break;
            default:
                $action = parent::getListButton($action, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
