<?php

namespace App\Controllers\Admin;

use App\Services\BoardService;
use App\Services\Customer\ServiceService;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class Home extends AdminController
{
    private $_service = null;
    private ?BoardService $_boardService = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->content_title = lang("{$this->getService()->getClassName()}.title");
        $this->class_path .= $this->getService()->getClassName();
        $this->uri_path .= strtolower($this->getService()->getClassName('/')) . '/';
        $this->view_path .= 'welcome' . DIRECTORY_SEPARATOR;
    }

    final public function getService(): ServiceService
    {
        if (!$this->_service) {
            $this->_service = new ServiceService();
        }
        return $this->_service;
    }
    final public function getBoardService(): BoardService
    {
        if (!$this->_boardService) {
            $this->_boardService = new BoardService();
        }
        return $this->_boardService;
    }
    protected function getResultSuccess(string $message = MESSAGES["SUCCESS"], ?string $actionTemplate = null): RedirectResponse|string
    {
        switch ($this->getService()->getAction()) {
            case 'index':
                $this->service = $this->getService();
                $this->control = $this->getService()->getControlDatas();
                $this->getService()->getHelper()->setViewDatas($this->getViewDatas());
                $actionTemplate = $this->request->getVar('ActionTemplate') ?? $actionTemplate;
                if ($actionTemplate) {
                    $view_file = $this->view_path . $actionTemplate . DIRECTORY_SEPARATOR . $this->getService()->getAction();
                } else {
                    $view_file = $this->view_path . $this->getService()->getAction();
                }
                $result = view($view_file, ['viewDatas' => $this->getViewDatas()]);
                break;
            default:
                $result = parent::getResultSuccess($message, $actionTemplate);
        }
        return $result;
    }


    //Index,FieldForm관련
    public function index(): RedirectResponse|string
    {
        $this->getService()->setAction(__FUNCTION__);
        //기본전달값정의
        $this->getService()->setFormDatas($this->request->getGet());
        $this->getService()->setFormFields();
        $this->getService()->setFormFilters();
        $this->getService()->setFormRules();
        $this->getService()->setFormOptions();
        //요청업무
        $this->boardRequestTaskCount = $this->getBoardService()->getRequestTaskCount($this->getMyAuth()->getUIDByAuthInfo());
        //Total 서버 현황
        //interval을 기준으로 최근 신규 서비스정보 가져오기
        $this->interval = intval($this->request->getVar('interval') ?? SERVICE['NEW_INTERVAL']);
        $this->newServiceEntities = $this->getService()->getNewServiceEntities($this->interval);
        $this->newServiceCount    = count($this->newServiceEntities);
        //서비스별 미납 Count
        $unPaidTotalCount = $unPaidTotalAmount = 0;
        foreach (array_values($this->getService()->getPaymentService()->getUnPaids('serviceinfo_uid')) as $unPaid) {
            $unPaidTotalCount += $unPaid['cnt'];
            $unPaidTotalAmount += $unPaid['amount'];
        }
        $this->unPaidTotalCount = $unPaidTotalCount;
        $this->unPaidTotalAmount = $unPaidTotalAmount;
        helper(['form']);
        return $this->getResultSuccess();
    }
}
