<?php

namespace App\Controllers\Admin;

use App\Entities\BillingEntity;
use App\Models\BillingModel;
use App\Models\OrderBillingModel;
use App\Models\OrderModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class BillingController extends AdminController
{
    private $_orderBillingModel = null;
    private $_orderModel = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->_model = new BillingModel();
        $this->_viewDatas['className'] = 'Billing';
        $this->_viewPath .= strtolower($this->_viewDatas['className']);
        $this->_viewDatas['title'] = lang($this->_viewDatas['className'] . '.title');
        $this->_viewDatas['class_icon'] = CLASS_ICONS[strtoupper($this->_viewDatas['className'])];
        helper($this->_viewDatas['className']);
    }

    final protected function getOrderModel(): OrderModel
    {
        return $this->_orderModel = $this->_orderModel ?: new OrderModel();
    }
    final protected function getOrderBillingModel(): OrderBillingModel
    {
        return $this->_orderBillingModel = $this->_orderBillingModel ?: new OrderBillingModel();
    }

    public function getFields(string $action = ""): array
    {
        switch ($action) {
            case "insert":
                return ["price"];
                break;
            case "index":
            case "excel":
                return ["user_uid", "title", "price", "email", "phone",  "status", "updated_at", "created_at"];
                break;
            case "view":
                return ["user_uid", "title", "price", "email", "phone", "status",  "updated_at", "created_at", 'response'];
                break;
            default:
                return [];
                break;
        }
    }
    public function getFieldFilters(): array
    {
        return ["user_uid", "status"];
    }
    public function getFieldBatchFilters(): array
    {
        return ["user_uid", "status"];
    }

    final protected function getOrdersByBillingEntity(BillingEntity $entity): array
    {
        //청구서 연결 주문정보 가져오기
        $orderBillings = $this->getOrderBillingModel()->getEntitys(['billing_uid' => $entity->getPrimaryKey()]);
        $orders = array();
        foreach ($orderBillings as $orderBilling) {
            array_push(
                $orders,
                $this->getOrderModel()->getEntity(
                    [$this->getOrderModel()->getPrimaryKey() => $orderBilling->order_uid]
                )
            );
        }
        return $orders;
    }

    //View 관련
    protected function view_process($entity)
    {
        $entity = parent::view_process($entity);
        //청구서 연결 주문정보 가져오기
        $this->_viewDatas['orders'] = $this->getOrdersByBillingEntity($entity);
        if (!count($this->_viewDatas['orders'])) {
            throw new \Exception("해당하는 주문정보가 없습니다.");
        }
        return $entity;
    }
}
