<?php

namespace App\Controllers\Admin\Customer\Wallet;

use App\Entities\Customer\ClientEntity;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;

class AccountController extends WalletController
{
  public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
  {
    parent::initController($request, $response, $logger);
    if ($this->service === null) {
      $this->service = service('customer_wallet_accountservice');
    }
    $this->addActionPaths('account');
  }
  //기본 함수 작업
  //Custom 추가 함수
  final public function create_form(): string|RedirectResponse
  {
    try {
      $uid = $this->request->getVar('clientinfo_uid');
      $clientEntity = null;
      if ($uid) {
        $clientEntity = service('customer_clientservice')->getEntity($uid);
        if (!$clientEntity) {
          throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$uid}에 해당하는 고객정보을 찾을수 없습니다.");
        }
      }
      $action = __FUNCTION__;
      $formDatas = [];
      $formDatas['clientinfo_uid'] = $uid;
      $formDatas['alias'] = $clientEntity instanceof ClientEntity ? $clientEntity->getTitle() : "";
      $formDatas['issue_at'] = date('Y-m-d');
      $this->action_init_process($action, $formDatas);
      $this->addViewDatas('formDatas', $formDatas);
      return $this->action_render_process($action, $this->getViewDatas(), $this->request->getVar('ActionTemplate'));
    } catch (\Throwable $e) {
      return $this->action_redirect_process('error', static::class . '->' . __FUNCTION__ . "에서 {$this->getTitle()} 생성폼 오류:" . $e->getMessage());
    }
  }
}
