<?php

namespace App\Controllers;

use App\Entities\CategoryEntity;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class Home extends Controller
{
    private $_session = null;
    private $_viewDatas = array();
    private $_viewPath = "";
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->_viewDatas['control'] = 'main';
        $this->_viewDatas['title'] = '쇼핑몰페이지';
        $this->_viewDatas['layout'] = LAYOUTS['main'];
        $this->_session = \Config\Services::session();
        $this->_viewDatas['session'] = $this->_session;
        $this->_viewDatas['className'] = 'MAIN';
        // echo var_export($this->_viewDatas['layout'], true);
        // exit;
        //사용자 기본 Role 지정
        $this->_viewDatas[SESSION_NAMES['ISLOGIN']] = false;
        $this->_viewDatas['currentRoles'] = [DEFAULTS["ROLE"]];
        if ($this->_session->get(SESSION_NAMES['ISLOGIN'])) {
            $this->_viewDatas[SESSION_NAMES['ISLOGIN']] = true;
            $this->_viewDatas['auth'] = $this->_session->get(SESSION_NAMES['AUTH']);
            $currentRoles = explode(DEFAULTS['DELIMITER_ROLE'], $this->_viewDatas['auth'][AUTH_FIELDS['ROLE']]);
            $this->_viewDatas['currentRoles'] = is_array($currentRoles) ? $currentRoles : [DEFAULTS["ROLE"]];
        }
        //Default 회원정보 Category
        $this->_viewDatas['category'] = new CategoryEntity(['uid' => 1]);
    }


    public function index()
    {
        return view($this->_viewPath . 'welcome_message', ['viewDatas' => $this->_viewDatas]);
    }
}
