<?php
namespace App\Controllers;

use App\Models\AdminOrderModel;
use App\Models\OrderTrackingModel;

class AdminOrder extends BaseController
{
    protected $adminOrderModel;
    protected $trackingModel;

    public function __construct()
    {
        helper(['url', 'form']);
        $this->adminOrderModel = new AdminOrderModel();
        $this->trackingModel = new OrderTrackingModel();
    }

    // Show all pending orders with apartment and items
    public function index()
    {
        $orders = $this->adminOrderModel->getOrdersWithApartment('Pending');
        return view('admin_orders', ['orders' => $orders]);
    }

    // Confirm an order
    public function confirm($orderId)
    {
        $this->adminOrderModel->updateOrderStatus($orderId, 'Confirmed');

        // Add tracking entry
        $this->trackingModel->insert([
            'order_id' => $orderId,
            'status' => 'Confirmed',
            'remarks' => 'Order confirmed by admin',
            'updated_at' => date('Y-m-d H:i:s')
        ]);

        return redirect()->to(base_url('adminOrder'));
    }
}
