<?php

namespace ModulesGarden\OpenXchangeAddon\App\UI\Admin\Home\Pages;

use \WHMCS\Database\Capsule as DB;
use ModulesGarden\OpenXchangeAddon\Core\UI\Widget\Others\Label;
use \ModulesGarden\OpenXchangeAddon\Core\UI\Interfaces\AdminArea;
use \ModulesGarden\OpenXchangeAddon\Core\UI\Widget\DataTable\Column;
use \ModulesGarden\OpenXchangeAddon\Core\UI\Widget\DataTable\DataTable;
use \ModulesGarden\OpenXchangeAddon\Core\UI\Widget\DataTable\DataProviders\DataProvider;
use \ModulesGarden\OpenXchangeAddon\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;

class HomePage extends DataTable implements AdminArea
{
    protected $id = 'HomePage';
    protected $name = 'HomePage';

    protected function loadHtml()
    {
        $this
            ->addColumn((new Column('id'))->setOrderable(DataProvider::SORT_DESC)->setSearchable(true, Column::TYPE_INT))
            ->addColumn((new Column('client'))->setOrderable()->setSearchable(true))
            ->addColumn((new Column('domain'))->setOrderable()->setSearchable(true))
            ->addColumn((new Column('contextid'))->setOrderable()->setSearchable(true))
            ->addColumn((new Column('contextname'))->setOrderable()->setSearchable(true))
            ->addColumn((new Column('domainstatus'))->setOrderable()->setSearchable(true))
            ;
    }

    public function initContent()
    {
        
    }

    public function replaceFieldClient($key, $row)
    {
        return '<a href="clientssummary.php?userid='.$row['userid'].'">'.$row['client'].'</a>';
    }


    protected function loadData()
    {
        $query = "select tblhosting.id, tblproducts.name, tblhosting.domain, tblhosting.userid, 
            tblhosting.domainstatus, CONCAT(tblclients.firstname,' ',tblclients.lastname) as client, valuess.value as contextid, tblhosting.username as contextname  from tblhosting 
        join tblproducts on tblproducts.id = tblhosting.packageid 
        join tblclients on tblclients.id = tblhosting.userid 
        left join (select * from
        (select id as fieldidd from tblcustomfields where fieldname like 'context_id%') fields 
            join tblcustomfieldsvalues on tblcustomfieldsvalues.fieldid = fields.fieldidd
        ) valuess on valuess.relid = tblhosting.id 
        where tblproducts.servertype = 'OpenXchange'";

        $data = DB::select($query);

        foreach($data as &$dat){
            $dat = json_decode(json_encode($dat), true);
        }

        $dataProvider = new ArrayDataProvider();
        $dataProvider->setData($data);
        $dataProvider->setDefaultSorting('id', 'DESC');
        $this->setDataProvider($dataProvider);

    }

    protected function replaceFieldDomainstatus($key, $row)
    {
        return (new Label())->initIds('label')
        ->setMessage($row['domainstatus'])
        ->setTitle($row['domainstatus'])
        ->setColor('ffffff')
        ->setBackgroundColor($this->getColor($row['domainstatus']))
        ->getHtml(); 
        return '<span style="color:'.$this->getColor($row['domainstatus']).'">'.$row['domainstatus'].'</span>';
    }

    private function getColor($status)
    {
        switch($status)
            {
                case 'Active':
                    return '5bc758';
                break;
                case 'Suspended':
                case 'Fraud':
                     return 'ed4040';
                break;
                case 'Terminated':
                    return '000000';
                case 'Pending':
                    return 'ff9900';
                default:
                    return '000000';
                break;
        }
    }
}
