<?php

namespace App\Controllers\Admin\Equipment;

use App\Controllers\Admin\AdminController;
use App\Services\Customer\ClientService;
use App\Services\Customer\ServiceService;
use App\Services\Equipment\LineService;

use App\Services\Equipment\ServerService;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

abstract class EquipmentController extends AdminController
{
    private ?ClientService $_clientService = null;
    private ?LineService $_lineService = null;
    private ?ServerService $_serverService = null;
    private ?ServiceService $_serviceService = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
    }
    final public function getClientService(): ClientService
    {
        if (!$this->_clientService) {
            $this->_clientService = new ClientService($this->request);
        }
        return $this->_clientService;
    }
    final public function getServiceService(): ServiceService
    {
        if (!$this->_serviceService) {
            $this->_serviceService = new ServiceService($this->request);
        }
        return $this->_serviceService;
    }

    final public function getLineService(): LineService
    {
        if (!$this->_lineService) {
            $this->_lineService = new LineService($this->request);
        }
        return $this->_lineService;
    }
    final public function getServerService(): ServerService
    {
        if (!$this->_serverService) {
            $this->_serverService = new ServerService($this->request);
        }
        return $this->_serverService;
    }

    protected function getFormFieldOption(string $field, array $options): array
    {
        switch ($field) {
            case 'clientinfo_uid':
                $temps = [];
                foreach ($this->getClientService()->getEntities() as $entity) {
                    $temps[$entity->getPK()] = $entity->getTitle();
                }
                $options[$field] = $temps;
                // dd($options);
                break;
            case 'serviceinfo_uid':
                $temps = [];
                foreach ($this->getServiceService()->getEntities() as $entity) {
                    $temps[$entity->getPK()] = $entity->getTitle();
                }
                $options[$field] = $temps;
                // dd($options);
                break;
            case 'lineinfo_uid':
                $temps = [];
                foreach ($this->getLineService()->getEntities() as $entity) {
                    $temps[$entity->getPK()] = $entity->getTitle();
                }
                $options[$field] = $temps;
                // dd($options);
                break;
            case 'serverinfo_uid':
                $temps = [];
                foreach ($this->getServerService()->getEntities() as $entity) {
                    $temps[$entity->getPK()] = $entity->getTitle();
                }
                $options[$field] = $temps;
                // dd($options);
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    //Index,FieldForm관련
}
