<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Tracking extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->model('StatusBoard_model', 'board');
    }

    public function pesanan($token = '')
    {
        $idInvoice = (int) decrypt_url((string) $token);
        if ($idInvoice <= 0) {
            show_404();
            return;
        }

        $tracking = $this->board->getPublicTracking($idInvoice);
        if (!$tracking || empty($tracking['order'])) {
            show_404();
            return;
        }

        if (
            isset($tracking['order']['status_invoice']) &&
            strtolower((string) $tracking['order']['status_invoice']) === 'batal'
        ) {
            show_404();
            return;
        }

        $appInfo = function_exists('info') ? info() : [];
        $data = [
            'title' => 'Tracking Pesanan ' . ($tracking['order']['id_transaksi'] ?? '') . ' | ' . (!empty($appInfo['title']) ? $appInfo['title'] : 'Flexaprint'),
            'tracking' => $tracking,
            'company' => $appInfo,
        ];

        $this->load->view('tracking/pesanan', $data);
    }
}
