<?php

namespace MGModule\DNSManager2\controllers\addon\admin;

use \MGModule\DNSManager2 as main;
use \MGModule\DNSManager2\mgLibs\custom\AjaxResponse;
use \MGModule\DNSManager2\models\custom\notification;
use \MGModule\DNSManager2\models\custom\zone;
use MGModule\DNSManager2\models\custom\notification\helpers\RowFormatter;

class dashboard extends main\mgLibs\process\abstractController{

    public function indexJSON($input, $vars = array()) {
        return AjaxResponse::I()->refreshPage($this->indexHTML($input, $vars))->toArray();
    }

    function indexHTML($input, $vars = array()){
        $vars['zones_used'] = zone\Repository::factory()->getServersZonesUsed();

        $vars['email_zone_created'] = notification\Repository::factory()->byName(notification\NotificationNameEnum::ZONE_CREATED_EMAIL)->count();
        $vars['email_zone_edited'] = notification\Repository::factory()->byName(notification\NotificationNameEnum::ZONE_EDITED_EMAIL)->count();
        $vars['email_zone_removed'] = notification\Repository::factory()->byName(notification\NotificationNameEnum::ZONED_REMOVED_EMAIL)->count();

        $vars['tutorial'] = main\models\custom\globalsetting\GlobalSetting::byKey(main\models\custom\globalsetting\GlobalSettingEnum::TUTORIAL_DISABLED)!='on'?true:false;

        return array(
            'tpl'   => 'main'
            ,'vars' => $vars
        );
    }

    function refreshNotificationTableJSON($input, $vars = array()) {


        $notifications = new notification\Repository();

        $helper = new main\mgLibs\custom\RepoTableHelper($notifications, $input);

        $vars = $helper->getDataTableArray();


        foreach($helper->get() as $notification)
        {
            //$notification = RowFormatter::format($notification);
            $vars['data'][] = $this->dataTablesParseRow('notification-row', ['notification' => $notification]);
        }
        return $vars;
    }

    function disableTutorialJSON($input, $vars = array()) {
        main\models\custom\globalsetting\GlobalSetting::set(main\models\custom\globalsetting\GlobalSettingEnum::TUTORIAL_DISABLED, 'on');
        return AjaxResponse::I()->refreshPage($this->indexHTML($input))->toArray();
    }

}
