<?php

/* * ********************************************************************
 * Wordpress_Manager Product developed. (Dec 19, 2017)
 * *
 *
 *  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\ProxmoxAddon\App\UI\Vms\Modals;

use ModulesGarden\ProxmoxAddon\Core\Helper;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ModalActionButtons\BaseCancelButton;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Modals\BaseEditModal;
use WHMCS\Database\Capsule as DB;
use WHMCS\Service\Service;

/**
 * Description of CloneModal
 *
 * @author Pawel Kopec <pawelk@modulesgarden.com>
 */
class DetailModal extends BaseEditModal implements AdminArea
{

    public function initContent()
    {
        $this->initIds('vmsDetailModal');
        /* @var $request \ModulesGarden\ProxmoxAddon\Core\Http\Request */
        $request = Helper\sl('request');
        if (!$request->get('actionElementId'))
        {
            return;
        }
        $hosting = Service::findOrFail($request->get('actionElementId'));
        if ($hosting->product->module == "ProxmoxCloudVps")
        {
            $this->customTplVars['vms'] = DB::table('ProxmoxAddon_Vm')
                ->where('hosting_id', $hosting->id)
                ->select('node', 'vmid', 'virtualization')->get();
        }
        else
        {
            if ($hosting->product->module == "proxmoxVPS")
            {
                $vm = new \stdClass;
                foreach ($hosting->customFieldValues as $cf)
                {
                    if ($cf->value && preg_match("/node/", $cf->customField->fieldName))
                    {
                        $vm->node = $cf->value;
                    }
                    if ($cf->value && preg_match("/vmid/", $cf->customField->fieldName))
                    {
                        $vm->vmid = $cf->value;
                    }
                }
                if ($vm->vmid)
                {
                    $this->customTplVars['vms'][] = $vm;
                }
            }
        }
    }

    protected function initActionButtons()
    {
        if (!empty($this->actionButtons))
        {
            return $this;
        }
        $this->addActionButton(new BaseCancelButton);
        return $this;
    }
}
