var user = config.user
$(document).ready(function() {
    if (!$('.select2').hasClass('select2-offscreen')) {
        $(".select2").select2({
            minimumResultsForSearch: -1,
            width: 220
        });
    }
    if (config.deviceCap[0].HKProtocol == "false") {
        $("#HKProtoco").hide();
    }
    if (config.deviceCap[0].OnvifProtocol == "false") {
        $("#onvifeProtoco").hide();
    }
    if (config.deviceCap[0].AuthConfig == "false") {
        $("#chetook").hide();
    }
    if (config.deviceCap[0].HKProtocol != "false") {
        getconfig();
    }
    if(config.deviceCap[0].OnvifProtocol != "false" || config.deviceCap[0].AuthConfig != "false"){
        getOnvifeconfig();
    }
    $("#ipSave").click(function () {
        if ("admin" == user || "super_admin" == user) {
            if (config.deviceCap[0].HKProtocol != "false") {
                setconfig();
            }
            if(config.deviceCap[0].OnvifProtocol != "false" || config.deviceCap[0].AuthConfig != "false"){
                setOnvifeconfig();
            }
            
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }
    });
})

function getconfig() {
    $.ajax({
        url: config.baseUrl + "/System/1/ThirdProtocolManage",
        type: "GET",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            if ($(xmlDoc).find('ThirdPartyProtocolConfig ').eq(0).find('JSTP').eq(0).text() == "true") {
                $('#third_config').select2('val', 'true');
            } else {
                $('#third_config').select2('val', 'false');
            }
        },
        error: function(xhr, textStatus, errorThrown) {
            $.optionBootoast($.i18n.prop('i18n-request-was-aborted'), "danger")
        }
    });
}

function setconfig() {
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><ThirdPartyProtocolConfig  Version='1.0'><JSTP>"+ $("#third_config").val() +"</JSTP><ThirdPartyProtocolConfig>";
    var cyURL = config.baseUrl + "/System/1/ThirdProtocolManage";
    $.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,"",'Protocol');
        },
        error: function(xhr, textStatus, errorThrown) {
            // $.optionBootoast($.i18n.prop('i18n-savefail'), "danger")
        }
    });
}
// 获取onvife配置
function getOnvifeconfig() {
    $.ajax({
        url: config.baseUrl + "/System/1/OnvifConfig",
        type: "GET",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
        $('#onvife_xie').select2('val', $(xmlDoc).find('OnvifCfg ').eq(0).find('Enable').eq(0).text());
        $('#onvife_chetook').select2('val', $(xmlDoc).find('OnvifCfg ').eq(0).find('OnvifAuthEnable').eq(0).text());
        },
        error: function(xhr, textStatus, errorThrown) {
            $.optionBootoast($.i18n.prop('i18n-request-was-aborted'), "danger")
        }
    });
}
// 下发onvife配置
function setOnvifeconfig() {
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><OnvifCfg><Enable>"+$('#onvife_xie').val()+"</Enable><OnvifAuthEnable>"+$('#onvife_chetook').val()+"</OnvifAuthEnable></OnvifCfg>";
    var cyURL = config.baseUrl + "/System/1/OnvifConfig";
    $.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,"",'Onvif');
        },
        error: function(xhr, textStatus, errorThrown) {
            // $.optionBootoast($.i18n.prop('i18n-savefail'), "danger")
        }
    });
}


