var gs_xml_doc, user = config.user,
    pwd = config.password;
$(document).ready(function() {
    if (!$('.select2').hasClass('select2-offscreen')) {
        $(".select2").select2({
            minimumResultsForSearch: -1,
            width: 220
        });
    }
    getNetworkDdnsServers();
    getDDNSInfo();
    // 点击保存
    $('#ddnsSave').click(function() {
        if ("admin" == user || "super_admin" == user) {
            setDDNSInfo();
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }
    });
    // 刷新
    $('#ddnsrefresh').click(function() {
        getDDNSInfo();
    });
    // 切换使能
    $("#ddnsenble").change(function() {
        if ($(this).val() == "true") {
            $(".showddns").show();
        } else {
            $(".showddns").hide();
        }
    })
});
// 获取当前ddns配置
function getDDNSInfo() {
    $.ajax({
        url: config.baseUrl + "/Network/DDNS/Config",
        type: "get",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            $("#ddnsenble").val($(xmlDoc).find('Config').eq(0).find('Enable').eq(0).text()).trigger("change"); //使能
            $("#ddnsenble").change();
            $("#ddnsServers").val($(xmlDoc).find('Config').eq(0).find('ProtocolName').eq(0).text()).trigger("change"); //服务器
            $("#ddnsyuname").val($(xmlDoc).find('Config').eq(0).find('DomainName').eq(0).text()); //域名
            $("#username").val($(xmlDoc).find('Config').eq(0).find('UserName').eq(0).text()); //用户名
            $("#DDNSUpdateTimeList").val($(xmlDoc).find('Config').eq(0).find('RefreshIntervals').eq(0).text()).trigger("change");
            getDDNSStatus();
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
// 保存当前ddns配置
function setDDNSInfo() {
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><Config Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>" +
        "<Enable>" + $("#ddnsenble").val() + "</Enable>" +
        "<ProtocolName>" + $("#ddnsServers").val() + "</ProtocolName>" +
        "<DomainName>" + $("#ddnsyuname").val() + "</DomainName>" +
        "<ServerPort></ServerPort>" +
        "<UserName>" + $("#username").val() + "</UserName>" +
        "<Password>" + $("#password").val() + "</Password>" +
        "<RefreshIntervals>" + $("#DDNSUpdateTimeList").val() + "</RefreshIntervals>" +
        "</Config>";
    $.ajax({
        type: "PUT",
        url: config.baseUrl + "/Network/DDNS/Config",
        async: true,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr);
        }
    });
}
// 获取当前连接状态
function getDDNSStatus() {
    $.ajax({
        url: config.baseUrl + "/Network/DDNS/Status",
        type: "get",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            var status = $(xmlDoc).find('Status').eq(0).find('State').eq(0).text();
            if (status == "stopped") {
                $("#ddnsStatus").val($.i18n.prop("i18n-ddns-status-stopped"));
            } else if (status == "Registering") {
                $("#ddnsStatus").val($.i18n.prop("i18n-ddns-status-registering"));
            } else if (status == "Registered") {
                $("#ddnsStatus").val($.i18n.prop("i18n-ddns-status-registered"));
            } else {
                $("#ddnsStatus").val($.i18n.prop("i18n-ddns-status-stopping"));
            }
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
// 获取当前服务器列表
function getNetworkDdnsServers() {
    $.ajax({
        url: config.baseUrl + "/Network/DDNS/ServerList",
        type: "GET",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            $("#ddnsServers").empty(); //先清空再赋值
            var sl = $(xmlDoc).find('ServerList').eq(0).find('ServerName').length;
            for (var i = 0; i < sl; i++) {
                $("<option value='" + $(xmlDoc).find('ServerList').eq(0).find('ServerName').eq(i).text() + "'>" + $(xmlDoc).find('ServerList').eq(0).find('ServerName').eq(i).text() + "</option>").appendTo("#ddnsServers");
            }
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}