<?php

namespace lib\Controllers\DBMS;

use lib\Services\ClientService;
use lib\Helpers\ServiceHelper;

class NavigatorController extends DBMSController
{

    private ?ClientService $_clientService = null;
    public function __construct()
    {
        parent::__construct();
        $this->getView()->setPath('navigator');
        $this->helper = new ServiceHelper();
    } //
    public function getClientService(): ClientService
    {
        if ($this->_clientService === null) {
            $this->_clientService = new ClientService();
        }
        return $this->_clientService;
    }

    //부가서비스 : 닷디펜더,딥파인더 등, deepfinder_list.php,dotdefender_list.php 
    //CLI 접속방법 : php index.php site/navigator/ipsearch
    //WEB 접속방법 : http://localhost/site/navigator/ipsearch
    public function ipsearch(array $params): string
    {
        $ip = array_key_exists('ip', $params) ? $params['ip'] : null;
        //전체 고객정보
        $this->clients = $this->getClientService()->getEntities();
        //IP형식이 ipv4인지 확인 후 값가져오기
        $services = [];
        if ($ip && $this->helper->isIPAddress($ip)) {
            $this->getServiceService()->getModel()->where('service_ip', $ip);
            $services[] = $this->getServiceService()->getEntity();
        } elseif ($ip) {
            $this->getServiceService()->getModel()->like('service_ip', $ip);
            $services = $this->getServiceService()->getEntities();
        } else {
            $services = $this->getServiceService()->getEntities();
        }
        $this->ip  = $ip;
        $this->services = $services;
        return $this->render(__FUNCTION__);
    }
} //Class
