<?php

namespace App\Controllers\Admin\Customer;

use App\Helpers\Customer\ServiceHelper;
use App\Services\Customer\ServicePaymentService;

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

class ServiceController extends CustomerController
{
    private ?CodeService $_codeService = null;
    private ?ServicePaymentService $_servicePaymentService = 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 .= strtolower($this->getService()->getClassName()) . DIRECTORY_SEPARATOR;

    }
    public function getService(): ServiceService
    {
        if (!$this->_service) {
            $this->_service = new ServiceService($this->request);
        }
        return $this->_service;
    }
    public function getHelper(): ServiceHelper
    {
        if (!$this->_helper) {
            $this->_helper = new ServiceHelper($this->request);
        }
        return $this->_helper;
    }
    public function getCodeService(): CodeService
    {
        if (!$this->_codeService) {
            $this->_codeService = new CodeService($this->request);
        }
        return $this->_codeService;
    }
    public function getServicePaymentService(): ServicePaymentService
    {
        if (!$this->_servicePaymentService) {
            $this->_servicePaymentService = new ServicePaymentService($this->request);
        }
        return $this->_servicePaymentService;
    }
    protected function getResultSuccess(string $message = MESSAGES["SUCCESS"], ?string $actionTemplate = null): RedirectResponse|string
    {
        switch ($this->getAction()) {
            case 'index':
            case 'view':
                $result = parent::getResultSuccess($message, $this->request->getVar('ActionTemplate') ?? $actionTemplate ?? 'service');
                break;
            default:
                $result = parent::getResultSuccess($message, $actionTemplate);
                break;
        }
        return $result;
    }
    //Index,FieldForm관련
    //수정관련
    protected function modify_process(mixed $entity, array $formDatas): mixed
    {
        //수정자 정보 자동추가용
        $formDatas['user_uid'] = $this->getMyAuth()->getUIDByAuthInfo();
        return parent::modify_process($entity, $formDatas);
    }
    //View 관련
    protected function view_process(mixed $entity): mixed
    {
        //LINE,IP,SERVER등 추가 FilterOption 셋팅용
        $this->setFilterOptionsByItemType();
        return $this->getService()->setItemEntitiesByService($entity);
    }
    //List 관련
    protected function setWordConditionForList(): void
    {
        $this->word = $this->request->getVar('word');
        if ($this->word !== null && $this->word !== '') {
            if ($this->getHelper()->isIPAddress($this->word, 'ipv4')) {
                $this->getService()->setSearchIp($this->word);
            } else {
                $this->getService()->getModel()->setList_WordFilter($this->word);
            }
        }
    }
    protected function index_process(): array
    {
        //서비스별 미납 Count
        $this->unPaids = $this->getServicePaymentService()->getUnPaidCountByService();
        //LINE,IP,SERVER등 추가 FilterOption 셋팅용
        $this->setFilterOptionsByItemType();
        $entities = [];
        foreach (parent::index_process() as $entity) {
            $entities[$entity->getPK()] = $this->getService()->setItemEntitiesByService($entity);
        }
        return $entities;
    }
}
