<?php

namespace App\Controllers\Front;

use App\Models\OrderModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class OrderController extends FrontController
{
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        $this->_model = new OrderModel($this->getFields());
        parent::initController($request, $response, $logger);
        $this->_viewDatas['title'] = lang($this->_model->getClassName() . '.title');
        $this->_viewPath .= strtolower($this->_model->getClassName());
        helper($this->_model->getClassName());
    }

    final public function getFields(string $action = ""): array
    {
        $fields = ["product_uid", "quantity", "price", "status"];
        switch ($action) {
            case "index":
            case "excel":
            case "view":
                return ['name', "quantity", "price", "status", "updated_at", "created_at"];
                break;
            default:
                return $fields;
                break;
        }
    }
    final public function getFieldFilters(): array
    {
        return ['product_uid', "status"];
    }
    final public function getFieldBatchFilters(): array
    {
        return ["status"];
    }

    //Index관련
    protected function index_setCondition()
    {
        parent::index_setCondition();
        //세션에 Cart정보(order_uids)가 있으면
        $uids = $this->_session->get(SESSION_NAMES['CART']) ?: array('NONE');
        //또는 Login했으면 사용자정보(user_uid)에 맞는 Order정보 가져오기
        if ($this->_session->get(SESSION_NAMES['ISLOGIN'])) {
            $this->_model->where('user_uid', $this->_session->get(SESSION_NAMES['AUTH'])[AUTH_FIELDS['ID']]);
        } elseif (count($uids)) {
            $this->_model->whereIn('uid', $uids);
        }
    }
}
