<?php

namespace app\models\mikrotik\ppp;

use yii\base\Model;
use Yii;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Aaa extends Model {

    public $use_radius;
    public $accounting;
    public $use_circuit_id_in_nas_port_id;
    public $interim_update;

    public function rules() {
        return [
            ['use_radius', 'string'],
            ['accounting', 'string'],
            ['use_circuit_id_in_nas_port_id', 'string'],
            ['interim_update', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ppp/aaa/getall');
        $response = $connect->getConnection()->sendSync($getRequest);
        $this->use_radius = (($response->getProperty('use-radius') == 'true') ? true : false);
        $this->accounting = (($response->getProperty('accounting') == 'true') ? true : false);
        $this->use_circuit_id_in_nas_port_id = (($response->getProperty('use-circuit-id-in-nas-port-id') == 'true') ? true : false);
        $this->interim_update = $response->getProperty('interim-update');

        return;
    }

    public function update() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ppp/aaa/set');
        $setRequest->setargument('use-radius', (($this->use_radius == true) ? "true" : "false"));
        $setRequest->setargument('accounting', (($this->accounting == true) ? "true" : "false"));
        $setRequest->setargument('use-circuit-id-in-nas-port-id', (($this->use_circuit_id_in_nas_port_id == true) ? "true" : "false"));

        $setRequest->setargument('interim-update', $this->interim_update);

        if (isset($unset)) {
            $unsetRequest = new RouterOS\Request('/ppp/aaa/unset');
            foreach ($unset as $value) {

                $unsetRequest->setArgument('value-name', $value);
                $resp = $connect->getConnection()->sendSync($unsetRequest);
                if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
                    if (!isset($re)) {
                        $re = '';
                    } $re.= ' ' . $resp->getProperty('message');
                }
            }
        } $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            if (isset($re)) {
                $re.= ' ' . $resp->getProperty('message');
            } else {
                $re = $resp->getProperty('message');
            }
        } if (isset($re)) {
            return $re;
        } else {
            return '1';
        }
    }

}
