<?php

namespace App\Controllers\Admin;

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

class SearchController extends AdminController
{
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        if ($this->service === null) {
            $this->service = service('customer_serviceservice');
        }
        $this->addActionPaths('service');
    }
    //Action작업관련
    //기본 함수 작업
    //Custom 추가 함수
    protected function index_entities_process(array $entities = []): array
    {
        $keyword = $this->request->getGet('keyword'); // 검색어
        if (!$keyword) {
            throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 [{$keyword}] 검색어가 지정되지 않았습니다. ");
        }
        //검색어에 따른 서버정보를 검색 후 해당하는 서비스리스트를 가져온다.
        $rows = service('equipment_serverservice')->getSearchServices($keyword);
        $uids = [];
        foreach ($rows as $row) {
            $uids[] = "'{$row->serviceinfo_uid}'";
        }
        if (count($uids) === 0) {
            return $entities;
        }
        //서비스별 서버리스트
        // $childServers = [];
        foreach ($this->service->getEntities([sprintf("uid IN (%s)", implode(",", $uids)) => null]) as $entity) {
            $entities[] = $entity;
            // $childServers[$entity->getPK()] = $this->getService()->getServerService()->getEntities(['serviceinfo_uid' => $entity->getPK()]);
        }
        // $this->childServers = $childServers;
        return $entities;
    }
}
