<?php

namespace App\Controllers\Admin\Customer;

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

use App\Services\Customer\ServiceItemService;

use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class ServiceController extends CustomerController
{
    private ?ServiceItemService $_serviceItemService = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->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 getServiceItemService(): ServiceItemService
    {
        if (!$this->_serviceItemService) {
            $this->_serviceItemService = new ServiceItemService($this->request);
        }
        return $this->_serviceItemService;
    }
    protected function getResultSuccess(string $message = MESSAGES["SUCCESS"]): RedirectResponse|string
    {
        switch ($this->getAction()) {
            case 'index':
                $this->getHelper()->setViewDatas($this->getViewDatas());
                $result = view($this->view_path . 'service' . DIRECTORY_SEPARATOR . $this->getAction(), ['viewDatas' => $this->getViewDatas()]);
                break;
            default:
                $result = parent::getResultSuccess($message);
                break;
        }
        return $result;
    }
    //Index,FieldForm관련
    protected function index_process(): array
    {
        //추가 Field작업 처리
        $this->item_types = lang($this->getServiceItemService()->getClassName() . '.' . strtoupper('ITEM_TYPE'));
        foreach ($this->item_types as $field => $label) {
            $this->field_options = $this->getFormFieldOption($field, $this->field_options);
        }
        $entities = [];
        foreach (parent::index_process() as $entity) {
            foreach ($this->item_types as $field => $label) {
                $itemEntities = $this->getServiceItemService()->getEntities(['item_type' => $field]);
                $entity->setItemEntities($field, $itemEntities);
            }
            $entities[] = $entity;
        }
        // $this->modal_type = 'modal_fetch_v2'; //기본은 modal_iframe임
        return $entities;
    }
}
