<?php

namespace WHMCS\Module\Widget;

require_once ROOTDIR . '/includes/functions.php';
require_once ROOTDIR . '/includes/registrarfunctions.php';
require_once ROOTDIR . '/modules/registrars/netim/lib/Core.php';

use WHMCS\Carbon;
use WHMCS\Module\AbstractWidget;
use Netim\Core;

/**
 * NetimAccount Widget
 *
 * Display the balance of the reseller account
 *  
 * @package	NETIM registrar module for WHMCS
 * @author	NETIM
 * @date	2022-04-14
 */

class NetimAccount extends AbstractWidget
{
    protected $title = '';
    protected $description = '';
    protected $weight = 250;
    protected $columns = 1;
    protected $cache = false;
    protected $requiredPermission = 'Perform Registrar Operations';

    private $lang = '';

    function __construct()
    {

        # Language management
        $this->lang = \Netim\Core\getLoadTranslate('locale');
        $this->title = \Netim\Core\getLoadTranslate('netim.widget.account_title');
        $this->description = \Netim\Core\getLoadTranslate('netim.widget.account_desc');
    }

    public function getData()
    {
        $data = [];

        # Get API
        $params = getregistrarconfigoptions('netim');
        $params = array_merge($params, ["Lang" => $this->lang]);

        $apiArray = \Netim\Core\getApi($params);
        if (!isset($apiArray["api"]))
            $data["error"] = $apiArray["error"];
        else {
            $api = $apiArray["api"];

            try {
                # Get Reseller data
                $data["success"] = $api->queryResellerAccount();
            } catch (NetimAPIException $fault) {
                $data["error"] = $fault->getMessage();
            }
        }

        return $data;
    }

    public function generateOutput($data)
    {
        global $_ADMINLANG;

        $output = '';

        # Build output
        if (isset($data["error"]) && !empty($data["error"]))
            $output = '<p style="font-size: 1em;"><font color="red"><i class="fas fa-exclamation-triangle fa-fw"></i>&nbsp;<b>' . $data["error"] . '</b></font></p>';
        else {
            $reseller = $data["success"];

            if ($reseller->BALANCE_AMOUNT <= $reseller->BALANCE_HARD_LIMIT) {
                $output .= '<p style="font-size: 1.5em;">' . \Netim\Core\getLoadTranslate('netim.widget.account_amount') . ': <font color="red">' . $reseller->BALANCE_AMOUNT . ' EUR</font></p>';
                $output .= '<p style="font-size: 1em;"><font color="red"><i class="fas fa-exclamation-triangle fa-fw"></i>&nbsp;<b>' . \Netim\Core\getLoadTranslate('netim.widget.account_hard_limit') . '</b></font></p>';
            } else if ($reseller->BALANCE_AMOUNT < $reseller->BALANCE_LOW_LIMIT) {
                $output .= '<p style="font-size: 1.5em;">' . \Netim\Core\getLoadTranslate('netim.widget.account_amount') . ': <font color="orange">' . $reseller->BALANCE_AMOUNT . ' EUR</font></p>';
                $output .= '<p style="font-size: 1em;"><font color="orange"><i class="fas fa-exclamation-triangle fa-fw"></i>&nbsp;<b>' . \Netim\Core\getLoadTranslate('netim.widget.account_low_limit') . '</b></font></p>';
            } else {
                $output .= '<p style="font-size: 1.5em;">' . \Netim\Core\getLoadTranslate('netim.widget.account_amount') . ': <font color="green">' . $reseller->BALANCE_AMOUNT . ' EUR</font></p>';
            }
        }

        # return display
        $libelle = \Netim\Core\getLoadTranslate('netim.widget.account_manage');
        return <<<EOF
<div class="widget-content-padded">
    {$output}
    <div align="right"><a href="https://netim.com/direct/" target="_blank">{$libelle} &raquo;</a></div>
</div>
EOF;
    }
}

/*
add_hook('AdminHomeWidgets', 1, function() {
    return new NetimAccount();
});*/
