<?php

namespace App\Controllers\Admin;

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

class OrderController extends AdminController
{
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->_model = new OrderModel();
        $this->_viewDatas['className'] = 'Order';
        $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 public function getFields(string $action = ""): array
    {
        switch ($action) {
            case 'update':
                return ["type", 'product_uid', "paymentday", "cost", "sale", "quantity", "status"];
                break;
            case "index":
            case "excel":
                return ["user_uid", "type", "name", "cost", "sale", "quantity", "price", "paymentday", "status", "updated_at", "created_at"];
                break;
            case "view":
                return ["user_uid", 'product_uid', "type",  "name", "cost", "sale", "quantity", "type", "paymentday", "status", "updated_at", "created_at"];
                break;
            default:
                return [];
                break;
        }
    }
    final public function getFieldFilters(): array
    {
        return ["user_uid", 'product_uid', "type", "status"];
    }
    final public function getFieldBatchFilters(): array
    {
        return [];
    }

    //가격이나,할인가,수량 변경했을경우 다시 계산해서 Price에 넣기위해
    protected function update_process($entity)
    {
        //가격이나,할인가,수량 변경했을경우 다시 계산해서 결제금액(price)에 넣기위해
        $this->_viewDatas['fieldDatas']['price'] =
            ($this->_viewDatas['fieldDatas']['cost'] -
                $this->_viewDatas['fieldDatas']['sale']) *
            $this->_viewDatas['fieldDatas']['quantity'];
        return parent::update_process($entity);
    }
}
