<?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\Jobs\Modals;

use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Hosting;
use ModulesGarden\ProxmoxAddon\App\UI\Jobs\Others\StatusLabel;
use ModulesGarden\ProxmoxAddon\Core\Queue\Models\Job;
use ModulesGarden\ProxmoxAddon\Core\Queue\Models\JobLog;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ModalActionButtons\BaseCancelButton;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Modals\BaseEditModal;

/**
 * Description of CloneModal
 *
 * @author Pawel Kopec <pawelk@modulesgarden.com>
 */
class InfoModal extends BaseEditModal implements AdminArea
{
    /**
     *
     * @var Job
     */
    protected $job;

    public function initContent()
    {
        $this->initIds('infoModal');
        if (!$this->getRequestValue('actionElementId'))
        {
            return;
        }
        /**
         * @var \ModulesGarden\ProxmoxAddon\App\Models\Job
         */
        $this->job                  = Job::findOrFail($this->getRequestValue('actionElementId'));
        $this->customTplVars['job'] = $this->job->toArray();
        //Job status
        $status                        = $this->job->status ? $this->job->status : 'pending';
        $this->customTplVars['status'] = (new StatusLabel())->setStatus($status)->getHtml();
        $this->customTplVars['data']   = unserialize($this->job->data);
        if ($this->job->rel_type == "hosting" && $this->job->rel_id)
        {
            /*@var $hosting Hosting*/
            $hosting                            = Hosting::findOrFail($this->job->rel_id);
            $this->customTplVars['productName'] = $hosting->product->name;

        }
        //JobLogs
        foreach (JobLog::where('job_id', $this->job->id)->orderBy('id', 'desc')->get() as $jobLog)
        {
            $log                              = $jobLog->toArray();
            $log['status']                    = (new StatusLabel())->setStatus($jobLog->type)->getHtml();
            $log['created_at']                = fromMySQLDate($jobLog->created_at, true);
            $this->customTplVars['jobLogs'][] = $log;
        }
    }

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