<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (27.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\ProxmoxVps\App\UI\BackupJob\Buttons;

use MGProvision\Proxmox\v2\models\BackupSchedule;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\DataJsonResponse;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Buttons\ButtonSwitchAjax;
use ModulesGarden\Servers\ProxmoxVps\App\Enum\ConfigurableOption;

class MailtoSwitchButton extends ButtonSwitchAjax implements ClientArea
{
    use ProductService;
    use ApiService;
    protected $switchColumn = 'mailto';
    protected $refreshActionIds = 'dackupJobDataTable';

    public function returnAjaxData()
    {
        try
        {
            $data           = json_decode(base64_decode($this->getRequestValue('actionElementId')), true);
            $storage        = $this->configuration()->getBackupStorage() ? $this->configuration()->getBackupStorage() : "local";
            $routing        = 1;
            $maxFiles       =  $this->configuration()->getBackupMaxFiles();
            if($this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) &&  $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)!="-1"){
                $maxFiles       = $this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES);
            }else if($this->getWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES)=="-1"){
                $maxFiles       = null;
            }
            $backupSchedule = new BackupSchedule();
            $backupSchedule->setApi($this->api());
            $backupSchedule->setAttributes([
                "id"        => $data['backupScheduleId'],
                "vmid"      => $this->vm()->getVmid(),
                "storage"   => $storage,
                "remove"    => $routing,
                "maxfiles"  => $maxFiles,
                "starttime" => $data['starttime'],
                "dow"       => $data['dow'],
                "mode"      => $data['mode'],
                "compress"  => $data['compress'],
                "mailto"    => $this->getRequestValue("value") == "on" ? $this->getWhmcsParamByKey('clientsdetails')['email'] : null,
            ]);
            $backupSchedule->update();
        }
        catch (\Exception $exc)
        {
            return (new DataJsonResponse())->setStatusError()->setMessage($exc->getMessage())->setCallBackFunction($this->callBackFunction)->addRefreshTargetId($this->refreshActionIds);
        }
        return (new DataJsonResponse())->setMessageAndTranslate('changesSaved')
                                       ->setCallBackFunction('pmOnBackupJobChanged');
    }

    /**
     *
     */
    protected function afterInitContent()
    {
        //Why?
    }
}