<?php

use WHMCS\Module\Addon\ProxyCheap\Dispatcher;
use WHMCS\Database\Capsule as DB;
use WHMCS\Module\Addon\ProxyCheap\Admin\AdminDispatcher;

if (!defined("WHMCS")) {
    die("This file cannot be accessed directly");
}

function proxycheap_output(array $vars)
{
    $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';

    $dispatcher = new AdminDispatcher();
    $response = $dispatcher->dispatch($action, $vars);
    echo $response;
}
function proxycheap_config()
{
    $update = '';
    if (strpos($_SERVER['PHP_SELF'], 'configaddonmods') !== 0) {
        $version = @file_get_contents('https://ip.cheap/whmcs/modules/servers/proxycheap/version.txt');

        $currentVersion = '1.1.2';

        if ($version && $version != $currentVersion) {
            $update = '<a href="https://marketplace.whmcs.com/product/6889-whitelabel-proxy-reseller-module" target="_blank"><span class="label active">! Update to ' . $version . ' available !</span></a>';
        }
    }

    $configarray = [
        "name" => "Whitelabel Proxy",
        "description" => $update . "The module allows to resell ProxyCheap products.",
        "version" => $currentVersion,
        "author" => "Proxy Cheap",
        "fields" => [
            "apikey" => [
                "FriendlyName" => "API Key",
                "Type" => "text",
                "Size" => "25",
                "Description" => "",
                "Default" => "",
            ],
            "apisecret" => [
                "FriendlyName" => "API Secret",
                "Type" => "password",
                "Size" => "25",
                "Description" => "",
            ],
            "customhostname" => [
                "FriendlyName" => "Custom hostname",
                "Type" => "text",
                "Size" => "25",
                "Default" => 'resi.global',
                "Description" => "It will replace default hostname in credentials generator [starting with proxy-us. and proxy-eu.]",
            ],
            'client_area_pagetitle' => [
                "FriendlyName" => "Order Form page title",
                "Type" => "text",
                "Size" => "100",
                "Default" => 'Ordering proxy',
                "Description" => "Page header in order form + breadcrumb",
            ]
        ]
    ];
    return $configarray;
}

function proxycheap_clientarea($params)
{
    if ($_REQUEST['json']) {
        $ctrl = isset($_REQUEST['c']) ? $_REQUEST['c'] : 'home';
        $dispatcher = new Dispatcher();
        $response = $dispatcher->dispatch($ctrl, $params);
        echo $response;
    }
}
