// JavaScript Document
document.charset = "utf-8";
var user = config.user;
var mainState = null
var subState = ""
var main = ""
var sub = ""
var mu = ""
var su = ""
var mc = ""
var se = ""
var g_bIsIE = !(/(msie\s|trident.*rv:)([\w.]+)/.exec(navigator.userAgent.toLowerCase()) == null);
$(document).ready(function () {
    if (!$('.select2').hasClass('select2-offscreen')) {
        $(".select2").select2({
            minimumResultsForSearch: -1,
            width: 220
        });
    }
    getRtmpInfo(); //初始化数据
    getsubRtmpInfo();
    if (mainState == "true" || subState == "true") {
        $("#rtmpEnble").prop('checked', true);
        $(".rtmpId").show();
    } else {
        $("#rtmpEnble").prop('checked', false);
        $(".rtmpId").hide();
    }
    $("#rtmpSave").click(function() {
        if ("admin" == user || "super_admin" == user) {
            multipleSaveState(setRtmpInfo(),setsubRtmpInfo())
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger");
        }
    });
    $("#rtmpEnble").change(function() {
        if ($(this).is(":checked")) {
            $(".rtmpId").show();
            $("#rtmpServer").val(mc);
            $("#Modetype").select2('val', 'true');
        } else {
            $(".rtmpId").hide();

        }
    })
    $("#Modetype").change(function() {
        if ($(this).select2('val') == "true") {
            $("#rtmpServer").val(mc)
        } else {
            $("#rtmpServer").val(se)
        }
    })


});

function getRtmpInfo() {
    $.ajax({
        url: config.baseUrl+ "/RTMP/1/1/Config",
        type: "get",
        async: false,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            mainState = $(xmlDoc).find('Config').eq(0).find('Enable').eq(0).text()
            mc = $(xmlDoc).find('Config').eq(0).find('PushURL').eq(0).text()
            if (mainState == "true") {
                $("#Modetype").select2('val',"true")
                $("#rtmpServer").val($(xmlDoc).find('Config').eq(0).find('PushURL').eq(0).text());
            }
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
function getsubRtmpInfo() {
    $.ajax({
        url: config.baseUrl + "/RTMP/1/2/Config",
        type: "get",
        async: false,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            subState = $(xmlDoc).find('Config').eq(0).find('Enable').eq(0).text();
            se = $(xmlDoc).find('Config').eq(0).find('PushURL').eq(0).text()
            if (subState == "true") {
                $("#Modetype").select2('val',"false")
                $("#rtmpServer").val($(xmlDoc).find('Config').eq(0).find('PushURL').eq(0).text());
            }
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
function setRtmpInfo() {
    var d = ""
    if ($("#rtmpEnble").is(":checked") && $("#Modetype").select2('val') == "true") {
        d = "true"
        main = $("#rtmpServer").val()
        if (main != "") {
            mu = Base64.encode(main)
        } else {
            mu = ""
        }
    } else {
        d = "false"
        main = mc
        if (main != "") {
            mu = Base64.encode(main)
        } else {
            mu = ""
        }

    }
    var cyXml = '<?xml version="1.0" encoding="utf-8"?>' +
        '<Config>' +
        '<Enable>' + d + '</Enable>' +
        '<PushURL>' + main + '</PushURL>' +
        '<PushURL_Base64>' + mu + '</PushURL_Base64>' +
        '<Status></Status>' +
        '<PlayURL></PlayURL>' +
        '<PlayURL_Base64></PlayURL_Base64>' +
        '<AudioEnable></AudioEnable>' +
        '</Config>'
    var protocolType = sessionStorage.getItem("xystyle");
    var cyURL = config.baseUrl + "/RTMP/1/1/Config";
    var xhrXML;
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: false,
        processData: false,
        data:  ["p2p"].indexOf(protocolType) != -1 && !g_bIsIE ? encodeURIComponent(cyXml) : cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            xhrXML = xhr
        }
    });
    return xhrXML;
}
function setsubRtmpInfo() {
    var c = ""
    if ($("#rtmpEnble").is(":checked") &&  $("#Modetype").select2('val') == "false") {
        c = "true"
        sub = $("#rtmpServer").val()
        if (sub != "") {
            su = Base64.encode(sub)
        } else {
            su = ""
        }

    } else {
        c = "false"
        sub = se
        if (sub != "") {
            su = Base64.encode(sub)
        } else {
            su = ""
        }
    }
    var xhrXML;
    var cyXml = '<?xml version="1.0" encoding="utf-8"?>' +
        '<Config>' +
        '<Enable>' + c + '</Enable>' +
        '<PushURL>' + sub + '</PushURL>' +
        '<PushURL_Base64>' + su + '</PushURL_Base64>' +
        '<Status></Status>' +
        '<PlayURL></PlayURL>' +
        '<PlayURL_Base64></PlayURL_Base64>' +
        '<AudioEnable></AudioEnable>' +
        '</Config>'

    var cyURL = config.baseUrl + "/RTMP/1/2/Config";
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: false,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function (xhr, textStatus) {
            console.log(xhr);
            xhrXML = xhr
        }
    });
    return xhrXML;
}