<?php
Doo::loadModelAt('aconfig', 'admin');

/**
 * MainController
 * Feel free to delete the methods and replace them with your own code.
 *
 * @author NoNZero
 */
class UpgradeController extends DooController
{

    private $data, $modelconfig, $onoff = array('on', 'off');

    public function __construct()
    {
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
        $this->modelconfig = new AConfig();
    }

    public function getWebVersion()
    {
        exit(json_encode(array('version' => DOO::conf()->ver), JSON_UNESCAPED_UNICODE));
    }

    public function getVersionAndStatus()
    {
        header("Access-Control-Allow-Origin: *");
        $onoff = $this->modelconfig->getOne(array('select' => 'onoff', 'asArray' => TRUE))['onoff'];
        exit(json_encode(array('version' => DOO::conf()->ver, 'onoff' => $onoff), JSON_UNESCAPED_UNICODE));
    }

    public function getSoftwareVersion()
    {
        echo json_encode(array('status' => 'TRUE', 'msg' => ''), JSON_UNESCAPED_UNICODE);
    }

    public function setOnOff()
    {
        echo json_encode(array('status' => 'TRUE', 'msg' => ''), JSON_UNESCAPED_UNICODE);
    }

    public function getSwitchStatus()
    {
        $onoff = $this->modelconfig->getOne(array('select' => 'onoff', 'asArray' => TRUE))['onoff'];
        echo json_encode(array('onoff' => $onoff), JSON_UNESCAPED_UNICODE);
        die();
    }

    public function setSwitchStatus()
    {
        header("Access-Control-Allow-Origin: *");
        if (in_array($this->params['onoff'], $this->onoff)) {
            if ($this->params['onoff'] == 'on') {
                $this->modelconfig->onoff = 0;
                $stronoff = $this->modelconfig->update(array('where' => 'conid=1'));
            } else {
                $this->modelconfig->onoff = 1;
                $stronoff = $this->modelconfig->update(array('where' => 'conid=1',));
            }
        }
        echo json_encode(array('onoff' => $stronoff), JSON_UNESCAPED_UNICODE);
        die();
    }

    public function setUpgradeInfo()
    {

//                        error_log(var_dump($_POST, TRUE), 3, '/opt/html/dev/data/sss.txt');
//        if (in_array($this->params['onoff'], $this->onoff)) {
//            if ($this->params['onoff'] == 'on') {
        $this->modelconfig->upgradeinfo = $_POST['json'];
        $stronoff = $this->modelconfig->update(array('where' => 'conid=1'));
//            }
//        }
        echo json_encode(array('onoff' => $stronoff), JSON_UNESCAPED_UNICODE);
        die();
    }
}
