var user = config.user,
    dnPWD = "11111";
var oCheckPassword = new CheckPassword();
var ddns_status = [{
        state: "stopped",
        result: $.i18n.prop('i18n-ddns-status-stopped')
    },
    {
        state: "Registering",
        result: $.i18n.prop('i18n-ddns-status-stopping')
    },
    {
        state: "Registered",
        result: $.i18n.prop('i18n-ddns-status-registered')
    },
    {
        state: "stopping",
        result: $.i18n.prop('i18n-ddns-status-registering')
    }
];
$(document).ready(function() {
    initData(); //初始化数据
    $("#ddnsSave").click(function() {
        if ("admin" == user || "super_admin" == user) {
            setDDNSInfo();
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }
    });
    $('#ddnsEnable').change(function() {
        if ($(this).is(":checked")) {
            $(".ddnsItme").show();
        } else {
            $(".ddnsItme").hide();
        }
    });
    var myElement = document.getElementById('dnPWD');
    myElement.addEventListener('focus', function() {
        oCheckPassword.focusPassword($('#dnPWD'))
    });

    myElement.addEventListener('blur', function() {
        oCheckPassword.blurPassword($('#dnUser'), $('#dnPWD'))
    });
});

function initData() {
    if (!$('.select2').hasClass('select2-offscreen')) {
        $(".select2").select2({
            minimumResultsForSearch: -1,
            width: 360
        });
    }
    getDDNSserverList();
    getDDNSInfo();
}

function getDDNSserverList() {
    $.ajax({
        url: config.baseUrl + "/Network/DDNS/ServerList",
        type: "get",
        async: false,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            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("#dnServer");
            }

        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}

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) {
            $("#ddnsEnable").prop('checked', JSON.parse($(xmlDoc).find('Config').eq(0).find('Enable').eq(0).text() || false));
            $("#ddnsEnable").change();
            $("#dnServer").select2('val', $(xmlDoc).find('Config').eq(0).find('ProtocolName').eq(0).text());
            $("#dnHost").val($(xmlDoc).find('Config').eq(0).find('DomainName').eq(0).text());
            $("#dnUser").val($(xmlDoc).find('Config').eq(0).find('UserName').eq(0).text());
            oCheckPassword.checkUserName($(xmlDoc).find('Config').eq(0).find('UserName').eq(0).text(), $('#dnPWD'));
            $("#dnUPT").select2('val', $(xmlDoc).find('Config').eq(0).find('RefreshIntervals').eq(0).text());
            getDDNSStatus();
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}

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 ddnsstatus = $(xmlDoc).find('Status').eq(0).find('State').eq(0).text();
            for (var i = 0; i < ddns_status.length; i++) {
                if (ddnsstatus == ddns_status[i].state) {
                    $("#dnCsta").val(ddns_status[i].result)
                }
            }
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}

function setDDNSInfo() {
    if ($("#dnPWD").val() != 1111) {
        dnPWD = $("#dnPWD").val();
    }
    if ($("#dnPWD").val() == '******') {
        var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><Config Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#ddnsEnable").is(":checked") + "</Enable><ProtocolName>" + $("#dnServer").val() + "</ProtocolName><DomainName>" + $("#dnHost").val() + "</DomainName><ServerPort></ServerPort><UserName>" + $("#dnUser").val() + "</UserName><RefreshIntervals>" + $("#dnUPT").val() + "</RefreshIntervals></Config>";
    } else {
        var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><Config Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#ddnsEnable").is(":checked") + "</Enable><ProtocolName>" + $("#dnServer").val() + "</ProtocolName><DomainName>" + $("#dnHost").val() + "</DomainName><ServerPort></ServerPort><UserName>" + $("#dnUser").val() + "</UserName><Password>" + $("#dnPWD").val() + "</Password><RefreshIntervals>" + $("#dnUPT").val() + "</RefreshIntervals></Config>";
    }
    var cyURL = config.baseUrl + "/Network/DDNS/Config";
    $.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)
        }
    });
}

function CheckPassword() {
    this.m_szDefaultPassword = "******";
    this.m_szUserNmaeTag = "";
    this.checkUserName = function(szUserName, objPassword) {
        this.m_szUserNmaeTag = szUserName;
        if (szUserName == "") {
            objPassword.val("");
        } else {
            objPassword.val(this.m_szDefaultPassword);
        }
    }
    this.focusPassword = function(objPassword) {
        if (this.m_szUserNmaeTag != "") {
            if (objPassword.val() != "") {
                objPassword.val("");
            }
        }
    }
    this.blurPassword = function(objUserName, objPassword) {
        if (this.m_szUserNmaeTag != "") {
            if (objPassword.val() == "") {
                if (this.m_szUserNmaeTag == objUserName.val()) {
                    objPassword.val(this.m_szDefaultPassword);
                }
            }
        }
    }
}