var xmlOverlayInfo = null,
    IsNeedReboot = false,
    rebootObj = {};
var powerNumber = 0;
$(document).ready(function() {
        // 智能跟踪
        if (config.deviceAiCap[powerNumber].IntelligentTrackdectecCar == "false" && config.deviceAiCap[powerNumber].IntelligentTrackdectecHuman == "false") {
            $(".isShow").hide();
        } else {
            $(".isShow").show();
        }
        //点击智能跟踪
        initSelecttrck()
        getinitSelecttrck();
        // 点击保存
        $("#intsave").click(function() {
            if ("admin" == config.user || "super_admin" == config.user) {
                IsNeedReboot = false;
                if ($("#intBox").is(':checked') && config.deviceCap[powerNumber].ObjectTrackFlag == "true") {
                    rebootObj = needReboot(5);
                    IsNeedReboot = rebootObj.isReBoot;
                    if (rebootObj.isReBoot == 'true') {
                        var isUpplementary = rebootObj.hasOwnProperty('isUpplementary') ? rebootObj.isUpplementary : 0;
                        $.RestartModel.mask(setinitSelecttrck, isUpplementary);
                        return;
                    }
                }
                setinitSelecttrck();
            } else {
                $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
            }
        });
        if (!$('.select2').hasClass('select2-offscreen')) {
            $(".select2").select2({
                minimumResultsForSearch: -1,
                width: 220
            });
        }

    })
    // 智能跟踪出石化下拉框数据
function initSelecttrck() {
    $("#IntelligentPell").empty()
        // 跟踪目标
    var trckDecteccar = config.deviceAiCap[powerNumber].IntelligentTrackdectecCar
    var trckDectechuman = config.deviceAiCap[powerNumber].IntelligentTrackdectecHuman
    var people = $.i18n.prop("i18n-human")
    var car = $.i18n.prop("i18n-car")
    if (trckDecteccar == "true") {
        $("<option value='car'>" + car + "</option>").appendTo("#IntelligentPell");
    }
    if (trckDectechuman == "true") {
        $("<option value='human'>" + people + "</option>").appendTo("#IntelligentPell");
    }
}
// 获取智能跟踪数据
function getinitSelecttrck() {
    $.ajax({
        url: window.config.baseUrl + "/AI/IntelligentTrack",
        type: "GET",
        async: false,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            xmltrack = xmlDoc;
            $("#intBox").prop('checked', JSON.parse($(xmlDoc).find('AIIntelligentTrack').eq(0).find('Enable').eq(0).text())); //是否启用电子围栏
            $("#IntelligentPell").val($(xmlDoc).find("AIIntelligentTrack").find("DetectTarget").text()).trigger('change');
        },
        error: function(xhr, textStatus, errorThrown) {}
    });

}
// 下发智能跟踪配置
function setinitSelecttrck() {
    $(xmltrack).find('AIIntelligentTrack').eq(0).find('Enable').eq(0).text($("#intBox").is(':checked'));
    $(xmltrack).find('AIIntelligentTrack').eq(0).find('DetectTarget').eq(0).text($("#IntelligentPell").val());
    var cyURL = window.config.baseUrl + "/AI/IntelligentTrack";
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: false,
        processData: false,
        data: xmltrack,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr);
            if (IsNeedReboot == 'true') {
                IsNeedReboot = false;
                if (rebootObj.isUpplementary != '3') {
                    $.optionBootoast($.i18n.prop('i18n-enevt-tip1'), "success");
                }
            }
        }
    });

}