<?php

namespace lib\Controllers;

use lib\Services\AddDbService;
use lib\Services\ClientService;
use lib\Configs\Pagination;

class ServiceController extends DBMSController
{

    private ?ClientService $_clientService = null;
    private ?AddDbService $_addDbService = null;

    public function __construct(array $params = [])
    {
        parent::__construct($params);
        $this->setPath('service');
    } //
    public function getClientService(): ClientService
    {
        if ($this->_clientService === null) {
            $this->_clientService = new ClientService();
        }
        return $this->_clientService;
    }
    public function getAddDbService(): AddDbService
    {
        if ($this->_addDbService === null) {
            $this->_addDbService = new AddDbService();
        }
        return $this->_addDbService;
    }
    //IP검색 : 
    //CLI 접속방법 : php index.php site/service/ipsearch
    //WEB 접속방법 : http://localhost/site/service/ipsearch
    public function ipsearch()
    {
        $ip = $this->request->get('ip');
        //전체 고객정보
        $this->clients = $this->getClientService()->getEntities();
        //IP형식이 ipv4이면 해당 IP만 , 아니면 like , 없으면 all 값가져오기
        if ($ip && $this->helper->isIPAddress($ip)) {
            $this->getServiceService()->getModel()->where('service_ip', $ip);
        } elseif ($ip) {
            $this->getServiceService()->getModel()->like('service_ip', $ip);
        }
        $this->curPage = intval($this->request->get('curPage', 1));
        $this->perPage = intval($this->request->get('perPage', APP_VIEW_LIST_PERPAGE));
        [$this->total, $this->entities] = $this->getServiceService()->getList($this->curPage, $this->perPage);
        $this->pagination = new Pagination($this->total, (int)$this->curPage, (int)$this->perPage);
        return $this->render(__FUNCTION__);
    }

    //부가서비스 : 닷 디펜더,딥 파인더 ,M307-1,M394-2등, deepfinder_list.php,dotdefender_list.php 
    //CLI 접속방법 : php index.php site/service//extra/adddb_code/코드번호
    //WEB 접속방법 : http://localhost/site/service/extra/adddb_code/코드번호
    public function extra()
    {
        $adddb_code = $this->request->get('adddb_code');
        if (!$adddb_code) {
            throw new \Exception("adddb_code 값이 정의되지 않았습니다.");
        }
        $this->adddb_code = urldecode($adddb_code);
        //해당 부가서비스의 services_code 목록 가져오기
        //segment의 값이 한글인경우 urldecode가 필요 
        $this->getAddDbService()->getModel()->where('addDB_code', $this->adddb_code);
        $addDbEntities = $this->getAddDbService()->getEntities();
        $service_codes = [];
        foreach ($addDbEntities as $entity) {
            $service_codes[] = $entity->getServiceCode();
        }
        if (!count($service_codes)) {
            throw new \Exception("[{$this->adddb_code}] 값에 해당하는 부가서비스정보가 존재하지 않습니다.");
        }
        //전체 사용자정보
        $this->clients = $this->getClientService()->getEntities();
        //부가서비스용 서비스목록 가져오기
        $this->getServiceService()->getModel()->whereIn('service_code', $service_codes);
        [$this->total, $this->entities, $this->pagination, $this->curPage, $this->perPage] = $this->getServiceService()->getList(
            $this->request->get('curPage'),
            $this->request->get('perPage')
        );
        return $this->render(__FUNCTION__);
    }
} //Class
