<?php
namespace App\Models;
use CodeIgniter\Model;

class OrderTrackingModel extends Model
{
    protected $table = 'order_tracking';
    protected $primaryKey = 'id';
    protected $allowedFields = ['order_id', 'status', 'remarks', 'updated_at'];

    // Fetch tracking for a specific order and phone
    public function getTrackingByPhone($orderId, $phone)
    {
        return $this->select('order_tracking.*, orders.total_amount, orders.status AS order_status')
                    ->join('orders', 'orders.id = order_tracking.order_id')
                    ->where('order_tracking.order_id', $orderId)
                    ->where('orders.phone', $phone)
                    ->orderBy('order_tracking.updated_at', 'DESC')
                    ->findAll();
    }

    // Fetch all orders for a specific phone (for dropdown)
    public function getOrdersByPhone($phone)
    {
        return $this->db->table('orders')
                        ->where('phone', $phone)
                        ->orderBy('created_at', 'DESC')
                        ->get()
                        ->getResultArray();
    }
}
