<?php

namespace app\models\mikrotik\ip\dhcpserver;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use yii\base\Model;
use Yii;
use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Dhcpserverconfig extends Model {

    public $store_leases_disk;

    public function rules() {
        return [
            ['store_leases_disk', 'required'],
            ['store_leases_disk', 'string'],
        ];
    }

    public function get() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/ip/dhcp-server/config/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        foreach ($responses as $response) {
            if ($response->getProperty('store-leases-disk') != null) {
                $this->store_leases_disk = $response->getProperty('store-leases-disk');
                return;
            }
        }
        return;
    }

    public function update() {
        $connect = MikrotikConnect::Instance();
        $setRequest = new RouterOS\Request('/ip/dhcp-server/config/set');
        $setRequest->setargument('store-leases-disk', $this->store_leases_disk);
        $resp = $connect->getConnection()->sendSync($setRequest);
        if ($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
            return $resp->getProperty('message');
        } else {
            return '1';
        }
    }

}
