<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (26.03.19)
* *
*
*  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
*  CONTACT                        ->       contact@modulesgarden.com
*
*
* This software is furnished under a license and may be used and copied
* only  in  accordance  with  the  terms  of such  license and with the
* inclusion of the above copyright notice.  This software  or any other
* copies thereof may not be provided or otherwise made available to any
* other person.  No title to and  ownership of the  software is  hereby
* transferred.
*
*
* ******************************************************************** */

namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Admin\User\Pages;

use MGProvision\Proxmox\v2\models\User;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\UserService;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataTable;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class UserDataTable extends DataTable implements AdminArea
{

    use ProductService;
    use UserService;
    use ApiService;

    protected $id = 'userDataTable';
    protected $title = 'userDataTable';
    protected $searchable = false;
    protected $tableLength = "100";

    protected function loadHtml()
    {
        $this->addColumn((new Column('username')))
            ->addColumn((new Column('realm')))
            ->addColumn((new Column('password')))
            ->addColumn((new Column('enable')))
            ->addColumn((new Column('expire')))
            ->addColumn((new Column('name')))
            ->addColumn((new Column('comment')));
    }

    public function replaceFieldExpire($key, $row)
    {
        if ($row[$key] == 0)
        {
            return sl("lang")->tr("Never");
        }
        return $row[$key];
    }

    public function replaceFieldEnable($key, $row)
    {
        if ($row[$key])
        {
            return '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr("Yes") . '</span>';
        }
        return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr("No") . '</span>';
    }

    protected function loadData()
    {
        $data = [];
        if ($this->isUser())
        {
            $user        = $this->getUser();
            $userService = new User("{$user->username}@{$user->realm}");
            $userService->setApi($this->api());
            $configuration = $userService->configuration();
            $data[]        = [
                'username' => $user->username,
                'password' => $user->getPassword(),
                'realm'    => $user->realm,
                'enable'   => $configuration['enable'],
                'expire'   => $configuration['expire'],
                'name'     => $configuration['firstname'] . " " . $configuration['lastname'],
                'comment'  => $configuration['comment'],
            ];
        }
        $dataProv = new ArrayDataProvider();
        $dataProv->setData($data);
        $this->setDataProvider($dataProv);
    }


    public function initContent()
    {

    }

    public function isViewFooter()
    {
        return false;
    }

    public function isViewTopBody()
    {
        return false;
    }

}