<?php

use WHMCS\Authentication\CurrentUser;
use WHMCS\ClientArea;
use WHMCS\Database\Capsule as DB;

define('CLIENTAREA', true);

require __DIR__ . '/init.php';


$ca = new ClientArea();

$ca_pagetitle = DB::table('tbladdonmodules')->where('module', 'proxycheap')->where('setting', 'client_area_pagetitle')->value('value');

if (empty($ca_pagetitle)) {
    if (file_exists(ROOTDIR . '/modules/addons/proxycheap/proxycheap.php')) {
        require ROOTDIR . '/modules/addons/proxycheap/proxycheap.php';
        $AddonConfiguration = call_user_func('proxycheap_config');


        foreach ($AddonConfiguration['fields'] as $configField => $configFieldValue) {
            if ($configField == 'client_area_pagetitle') {
                $ca_pagetitle = $configFieldValue['Default'];
                break;
            }
        }

    }
}

$ca->setPageTitle($ca_pagetitle);

$ca->addToBreadCrumb('index.php', Lang::trans('globalsystemname'));
$ca->addToBreadCrumb('proxycheap.php', $ca_pagetitle);

$ca->initPage();


if ($_GET['pid']) {
    $configoption1 = DB::table('tblproducts')->where('id', (int) $_GET['pid'])->value('configoption1');

    $ca->assign('product_type', $configoption1);

    # Define the template filename to be used without the .tpl extension

    $ca->assign('pid', (int) $_GET['pid']);
    // $ca->assign('customfields', json_encode(getCustomFields("product", $_GET['pid'], "", "", "on")));
    // $ca->assign('configoptions', json_encode(getCartConfigOptions($_GET['pid'], [], "", "", "", true)));
    $ca->setTemplate('proxycheap-form');

    //$ca->assign('configoptions', getCustomFields("product", $pid, "", "", "on", $customfields));
    $ca->output();
    exit;
}
require_once ROOTDIR . "/includes/orderfunctions.php";

$productsCollection = \WHMCS\Product\Product::where('servertype', 'proxycheap')->get();
$unsortedProducts = [];
$pricing = new \WHMCS\Pricing();
$products = [];
foreach ($productsCollection as $product) {
    $pricingInfo = getPricingInfo($product->id, true);
    $pricing->loadPricing("product", $product->id);
    $description = WHMCS\Product\Product::getProductDescription($product->id, $product->description);

    if ($pricing->hasBillingCyclesAvailable() || $product->paymentType == "free") {
        $unsortedProducts[$product->displayOrder][] = array("pid" => $product->id, "bid" => 0, "type" => $product->type, "name" => WHMCS\Product\Product::getProductName($product->id, $product->name), "description" => $description, "paytype" => $product->paymentType, "pricing" => $pricingInfo, "freedomain" => $product->freeDomain, "freedomainpaymentterms" => $product->freeDomainPaymentTerms, "qty" => $product->stockControlEnabled ? $product->quantityInStock : "", "isFeatured" => $product->isFeatured);
    }
}
ksort($unsortedProducts);
foreach ($unsortedProducts as $items) {
    foreach ($items as $item) {
        $products[] = $item;
    }
}


$ca->assign('products', $products);
Menu::primarySidebar("orderFormView");
Menu::secondarySidebar("orderFormView");

# Define the template filename to be used without the .tpl extension

$ca->setTemplate('proxycheap');

$ca->output();