<?php

namespace lib\Controllers;

use lib\Entities\ClientEntity;
use lib\Services\ClientService;
use lib\Services\OnetimeService;

class OnetimeController extends DBMSController
{
    private ?ClientService $_clientService = null;
    private ?OnetimeService $_onetimeService = null;
    public function __construct(array $params = [])
    {
        parent::__construct($params);
        $this->setPath('onetime');
    } //
    public function getClientService(): ClientService
    {
        if ($this->_clientService === null) {
            $this->_clientService = new ClientService();
        }
        return $this->_clientService;
    }
    public function getOnetimeService(): OnetimeService
    {
        if ($this->_onetimeService === null) {
            $this->_onetimeService = new OnetimeService();
        }
        return $this->_onetimeService;
    }
    //IdcCouponListMK.jsp -> domain_coupon.php
    //CLI 접속방법 : php index.php site/client/onetime/index
    //WEB 접속방법 : http://localhost/site/client/onetime/index
    public function index()
    {
        //기본적으로 필요한  client_code, member_code, service_code를 설정합니다.
        list($this->client, $this->member, $this->service) = $this->setDefaultRequestData(null, null, true);
        //사용자 코드가 있으면
        if ($this->client instanceof ClientEntity) {
            $this->getOnetimeService()->getModel()->where('client_code', $this->client->getClientCode());
        }
        //업체명_일회성만 나오게하기 위해서
        $this->getOnetimeService()->getModel()->where('service_code', $this->service->getServiceCode());
        $this->getOnetimeService()->getModel()->orderBy('onetime_num', 'DESC');
        [$this->total, $this->entities, $this->pagination, $this->curPage, $this->perPage] = $this->getOnetimeService()->getList(
            $this->request->get('curPage'),
            $this->request->get('perPage')
        );
        //전체 사용자정보
        $this->clients = $this->getClientService()->getEntities();
        //전체 관리자정보
        $this->members = $this->getMemberService()->getEntities();
        return $this->render(__FUNCTION__);
    }
} //Class
