<?php

namespace WHMCS\Module\Widget;

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

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

/**
 * NetimOperations Widget
 *
 * Display all pending operations
 *  
 * @package	NETIM registrar module for WHMCS
 * @author	NETIM
 * @date	2022-04-14
 */


class NetimOperations extends AbstractWidget
{
    protected $title = '';
    protected $description = '';
    protected $weight = 250;
    protected $columns = 2;
    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.ope_title');
        $this->description = \Netim\Core\getLoadTranslate('netim.widget.ope_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"] = $opelist = $api->queryOpePending();;
            } catch (NetimAPIException $fault) {
                $data["error"] = $fault->getMessage();
            }
        }

        return $data;
    }

    public function generateOutput($data)
    {
        $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 {
            $opelist = $data["success"];
            $output .= "<div class='ope-cells'>";
            $output .= '<table width="100%" cellspacing="0"">
            <tr class="header">
                <td>' . \Netim\Core\getLoadTranslate('netim.widget.ope_col1') . '</td>
                <td>' . \Netim\Core\getLoadTranslate('netim.widget.ope_col2') . '</td>
                <td>' . \Netim\Core\getLoadTranslate('netim.widget.ope_col3') . '</td>
                <td>' . \Netim\Core\getLoadTranslate('netim.widget.ope_col4') . '</td>
                <td width="20"></td>
            </tr>';

            $id = 0;
            foreach ($opelist as $ope) {
                $trackingID = $ope->id_ope;
                $data = idn_to_utf8($ope->data_ope, IDNA_NONTRANSITIONAL_TO_ASCII);
                $date = Carbon::createFromFormat('Y-m-d H:i:s', $ope->date_ope);
                $date = $ope->date_ope;
                $operation = $ope->code_ope;
                $info = \Netim\Core\getDomainInfoByDomain($data);

                if (isset($info["id"]) && $info["id"] != "")
                    $link = "<a href='clientsdomains.php?&id=" . $info['id'] . "&userid=" . $info['userid'] . "'><img src='images/edit.gif' border='0'></a>";
                else
                    $link = "";

                $output .= '<tr class="line">
                    <td>' . $trackingID . '</td>
                    <td>' . $data . '</td>
                    <td>' . $operation . '</td>
                    <td>' . $date . '</td>
                    <td>' . $link . '</td>
                </tr>';

                $id++;
            }

            if ($id == 0)
                $output .= '<tr class="line"><td colspan="4" align="center">' . \Netim\Core\getLoadTranslate('global.norecordsfound') . '</td></tr>';

            $output .= '</table>';
            $output .= "</div>";
        }

        $libelle = \Netim\Core\getLoadTranslate('netim.widget.ope_manage');
        # return display
        return <<<EOF
<div class="widget-content-padded">
    {$output}
    <div align="right"><a href="https://netim.com/direct/" target="_blank">{$libelle} &raquo;</a></div>
</div>
<style>
.ope-cells {
    font-size: 0.9em;
    text-align: left;
    margin-bottom : 15px;
}
.ope-cells .header {
    background-color: #efefef;
    font-weight: bold;
}
.ope-cells .line {
    font-weight: normal;
}
</style>
EOF;
    }
}

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