<?php

$path = '../../../';
require_once $path . 'core/includes/header.inc.php';

use Mtc\Modules\B2B\Models\AccountOrder;
use Mtc\Modules\B2B\Models\TradeCustomer;

$action = filter_input(INPUT_GET, 'section');

switch ($action) {
    case 'trade_overview':
        $pendingApplications = TradeCustomer::pending()->count();
        $unpaidOrders = AccountOrder::unpaid()->count();
        $outstandingBalance = AccountOrder::unpaid()->sum('amount_due');
        $approvedCustomers = TradeCustomer::approved()->count();

        $html = $twig->render('b2b/admin/dashboard/trade_overview.twig', [
            'pending_applications' => $pendingApplications,
            'unpaid_orders' => $unpaidOrders,
            'outstanding_balance' => $outstandingBalance,
            'approved_customers' => $approvedCustomers,
        ]);

        $response = [
            'html' => $html,
        ];
        break;

    default:
        $response = ['html' => ''];
        break;
}

header('Content-Type: application/json');
echo json_encode($response);
