var user = config.user;
$(document).ready(function() {
    initData(); //初始化数据
    $("#activeSave,#activeSaved").click(function() {
        if ("admin" == user || "super_admin" == user) {
            setRegysterintInfo();
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }
    });
    // 刷新
    $("#activeRefresh").click(function() {
        if ("admin" == user || "super_admin" == user) {
            getRegysterintInfo();
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }
    });

    $('#activeEnable').change(function() {
        if ($(this).is(":checked")) {
            $(".activeItme").show();
            $(".activeItmes").hide();

        } else {
            $(".activeItme").hide();
            $(".activeItmes").show();
        }
    });
});

function checkNum(val) {
    console.log(val.srcElement.value);
    document.getElementById('activeRegysterint').value = val.srcElement.value >= 0 ? val.srcElement.value : 0
}

function initData() {
    getRegysterintInfo();
}
// 获取注册配置
function getRegysterintInfo() {
    $.ajax({
        url: config.baseUrl + "/System/NVRRegisterCfg",
        type: "get",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            $("#activeEnable").prop('checked', JSON.parse($(xmlDoc).find('RegisterConfig').eq(0).find('CmsEnable').eq(0).text() || false));
            $("#activeEnable").change();
            $("#activeDress").val($(xmlDoc).find('RegisterConfig').eq(0).find('ManageHostAddress').eq(0).text());
            $("#activePort").val($(xmlDoc).find('RegisterConfig').eq(0).find('ManageHostPort').eq(0).text());
            $("#activeRegysterint").val($(xmlDoc).find('RegisterConfig').eq(0).find('AutoRegIntervals').eq(0).text());
            getActiveStatus();
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
// 获取当前注册状态
function getActiveStatus() {
    $.ajax({
        url: config.baseUrl + "/System/NVRRegisterState",
        type: "get",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            if ($(xmlDoc).find('RegisterState').eq(0).find('CmsState').eq(0).text() == '0') {
                $("#activeState").val($.i18n.prop('i18n-unregistered'));
            } else {
                $("#activeState").val($.i18n.prop('i18n-Active-success'));
            }
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}

function setRegysterintInfo() {
    var cyXml = '<?xml version="1.0" encoding="UTF-8" ?>' +
        '<RegisterConfig><CmsEnable>' + $("#activeEnable").is(":checked") + '</CmsEnable>' +
        '<ManageHostAddress>' + $("#activeDress").val() + '</ManageHostAddress>' +
        '<ManageHostPort>' + $("#activePort").val() + '</ManageHostPort>' +
        '<AutoRegIntervals>' + $("#activeRegysterint").val() + '</AutoRegIntervals></RegisterConfig>'
    var cyURL = config.baseUrl + "/System/NVRRegisterCfg";
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: true,
        processData: false,
        data: cyXml,
        contentType: "application/xml;charset=utf-8",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr)
        }
    });
}