var networkXml, user = config.user,
    pwd = config.password;
$(document).ready(function() {
    if (!$('.select2').hasClass('select2-offscreen')) {
        $(".select2").select2({
            minimumResultsForSearch: -1,
            width: 360
        });
    }
    $('#netTpye').change(function() {
        if ($(this).select2('val') == 'true') {
            $(".ppoe").show();
        } else {
            $(".ppoe").hide();
        }
    });
    getNetworkPpoe();
    $("#ipSave").click(function() {
        if ("admin" == user || "super_admin" == user) {
            setNetworkPpoe();
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }
    });
})

function getNetworkPpoe() {
    $.ajax({
        url: config.baseUrl + "/Network/PPPoE",
        type: "GET",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            $("#username").val($(xmlDoc).find('PPPOE').find('UserName').eq(0).text());
            // $("#password").val($(xmlDoc).find('PPPOE').find('Interface').eq(0).find('SubnetMask').eq(0).text());
            //获取是否开启DHCP
            var status = $(xmlDoc).find('PPPOE').eq(0).find('Status').eq(0).find('State').text();
            if (status == "connecting") {
                $("#status").val($.i18n.prop("i18n-pppoe-status1"));
            } else if (status == "connected") {
                $("#status").val($.i18n.prop("i18n-ftp-status-connected"));
            } else if (status == "disconnecting") {
                $("#status").val($.i18n.prop("i18n-pppoe-status2"));
            } else {
                $("#status").val($.i18n.prop("i18n-wifi-disconnected"));
            }
            if ($(xmlDoc).find('PPPOE').eq(0).find('Config').eq(0).find('Enable').eq(0).text() == "true") {
                $('#netTpye').select2('val', 'true');
            } else {
                $('#netTpye').select2('val', 'false');
            }
            $('#netTpye').change();
        },
        error: function(xhr, textStatus, errorThrown) {
            $.optionBootoast($.i18n.prop('i18n-request-was-aborted'), "danger")
        }
    });

}

function setNetworkPpoe() {
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><PPPOE Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>" +
        "<Config Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>" +
        "<Enable>" + $("#netTpye").val() + "</Enable>" +
        "<UserName>" + $("#username").val() + "</UserName>" +
        "<Password>" + $("#password").val() + "</Password>" +
        "</Config>" +
        "</PPPOE>";

    var cyURL = config.baseUrl + "/Network/PPPoE";
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: true,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr) {
            SaveState(xhr);
        },
        error: function(xhr, textStatus, errorThrown) {
            $.optionBootoast($.i18n.prop('i18n-savefail'), "danger")
        }
    });
}