var csylx = ['custom', 'alarm', 'dog', 'warning area', 'monitoring area', 'welcome', 'Deep Water Warning', 'Private territory', 'danger zone', 'Valuable objects', 'High Warning', 'Private Parking']
var bcs = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
var alarmin_channel = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
var gs_alarmout_cfg;
var gs_alarmin_cfg;
var alarmOutputflag = "1";
var alarmInputflag = "1";
var aloutArr = [];
var alarmState;
$(document).ready(function() {
    if (config.isPush == true) {
        $(".isPush").show()
    } else {
        $(".isPush").hide()
    }
    if (config.deviceCap.diskEnable == "true") {
        $(".isSD").show()
    } else {
        $(".isSD").hide()
    }

    // 报警输入下拉框
    $("#Alarminputs,#AlarminputType,#inSoundType,#inFrequencyBroadcast,#indeploymentTime").select2({
        minimumResultsForSearch: -1,
    });
    // 报警输出下拉框
    $("#Alarmoutputs,#outputdelay").select2({
        minimumResultsForSearch: -1,
        width: 340,
    });
    // 初始化时间选择
    var timeplan = $("#alarmDeployment").TimePlan({
        sectionNum: 6
    });
    if (config.deviceCap.AlarmInCount == '0') { //隐藏报警输入 且不下发接口
        $("#myTab").find("li").eq(0).hide();
        $(".tab-content").find('div[role="tabpanel"]').eq(0).hide();
        $("#myTab li").removeClass("active");
        $("#myTab").find("li").eq(1).addClass("active");
        $('.tab-content div[role="tabpanel"]').removeClass("active");
        $(".tab-content").find('div[role="tabpanel"]').eq(1).addClass("active");
        getAlarmout();
    } else {
        selectvalue();
        getAlarmin() //进来获取数据
        getAlarmInEx(timeplan)
    }
    // 切换展示时间控件
    $('#indeploymentTime').change(function(v) {
        if (v.val == 'false') {
            $('#alarmDeployment').show()
        } else {
            $('#alarmDeployment').hide()
        }
    });
    // 点击报警输出
    $('#outp').click(function() {
        getAlarmout();
    });
    // 点击报警输入
    $('#intp').click(function() {
        getAlarmin(); //进来获取数据
        getAlarmInEx(timeplan);
    });
    // 点击试听
    $('#tryVoice').click(function() {
        setTryvoice('AlarmIn')
    });
    // 报警输出延迟切换
    $("#outputdelay").change(function(v) {
        if (v.val == 'keep') {
            $('#timeinput').hide()
        } else {
            $('#timeinput').show()
        }
    });
    // 保存报警输出配置
    $("#outputsave").click(function(e) {
        if ("admin" == config.user || "super_admin" == config.user) {
            e.preventDefault()
            setAlarmout()
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }


    });
    // 保存报警输入
    $("#inptsave").click(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            setAlarmin()
            setAlarmInEx(timeplan)
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }

    });

});
// 初始化下拉框数据
function selectvalue() {
    // 声音类型
    var ni = $.i18n.prop("i18n-str_voice_type").split(",")
    var baojinVoicetype = $(config.deviceCap.bjsrAudioAlarmType);
    for (var i = 0; i < 12; i++) {
        // $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        if (baojinVoicetype.find("Custom").text() == "true" && csylx[i] == "custom") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        } else if (baojinVoicetype.find("Alarm").text() == "true" && csylx[i] == "alarm") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        } else if (baojinVoicetype.find("dog").text() == "true" && csylx[i] == "dog") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        } else if (baojinVoicetype.find("warning").text() == "true" && csylx[i] == "warning area") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        } else if (baojinVoicetype.find("monitoring").text() == "true" && csylx[i] == "monitoring area") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        } else if (baojinVoicetype.find("welcome").text() == "true" && csylx[i] == "welcome") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        } else if (baojinVoicetype.find("DeepWater").text() == "true" && csylx[i] == "Deep Water Warning") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        } else if (baojinVoicetype.find("PrivateTerritory").text() == "true" && csylx[i] == "Private territory") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        } else if (baojinVoicetype.find("DangerZone").text() == "true" && csylx[i] == "danger zone") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        } else if (baojinVoicetype.find("ValuableObjects").text() == "true" && csylx[i] == "Valuable objects") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        } else if (baojinVoicetype.find("HighWarning").text() == "true" && csylx[i] == "High Warning") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        } else if (baojinVoicetype.find("PrivateParking").text() == "true" && csylx[i] == "Private Parking") {
            $("<option value='" + csylx[i] + "'>" + ni[i] + "</option>").appendTo("#inSoundType");
        };

    }
    // 播报次数
    var bc = $.i18n.prop("i18n-broadcast").split(",")
    for (var i = 0; i < 10; i++) {
        $("<option value='" + bcs[i] + "'>" + bc[i] + "</option>").appendTo("#inFrequencyBroadcast");
    }
}
// 获取报警输入配置
function getAlarmin() {
    $.ajax({
        url: window.config.baseUrl + "/Alarm/Alarmin",
        type: "GET",
        async: true,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            gs_alarmin_cfg = xmlDoc;
            var xml_alarmin_size = $(xmlDoc).find('AlarmInList').eq(0).find('AlarmIn').length;
            $("#Alarminputs").empty();
            for (var i = 0; i < xml_alarmin_size; i++) {
                $("<option value='" + alarmin_channel[i] + "'>" + alarmin_channel[i] + "</option>").appendTo("#Alarminputs");
            }
            $("#Alarminputs").val(alarmInputflag).trigger('change'); //报警输入
            $("#Alarminputname").val($(xmlDoc).find('AlarmInList').eq(0).find('AlarmIn').eq(alarmInputflag - 1).find('Name').text());
            $("#AlarminputType").val($(xmlDoc).find('AlarmInList').eq(0).find('AlarmIn').eq(alarmInputflag - 1).find('AlarmInType').text()).trigger('change'); //输入类型

            //联动处理
            $("#alTriggermail").prop('checked', JSON.parse($(xmlDoc).find('AlarmInList').eq(0).find('AlarmIn').eq(alarmInputflag - 1).find('Trigger').eq(0).find('Mail').eq(0).find('Enable').eq(0).text()));
            $("#alTriggerSDcapture").prop('checked', JSON.parse($(xmlDoc).find("Snapshot").find("Enable").text()));
            $("#alTriggerSDrecording").prop('checked', JSON.parse($(xmlDoc).find("Record").find("Enable").text()));
            $("#alTriggeralarm").prop('checked', JSON.parse($(xmlDoc).find("AlarmOut").find("Enable").text()));
            $("#alTriggerpush").prop('checked', JSON.parse($(xmlDoc).find("Push").find("Enable").text()));
            var BeepAlertEnable = $(xmlDoc).find('BeepAlert').eq(0).find('Enable').text();
            $("#alAudiblealarm").prop('checked', JSON.parse(BeepAlertEnable));
            $("#alTriggerFTP").prop('checked', JSON.parse($(xmlDoc).find('AlarmInList').eq(0).find('AlarmIn').eq(alarmInputflag - 1).find('Trigger').eq(0).find('Ftp').eq(0).find('Enable').eq(0).text()));
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
// 获取声音报警信息
function getAlarmInEx(timeplan) {
    $.ajax({
        url: window.config.baseUrl + "/Alarm/AlarmInEx",
        type: "GET",
        async: true,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            xmlDocmotiontetect = xmlDoc;
            $("#inSoundType").val($(xmlDoc).find('AlarmInExList').eq(0).find('AlarmInEx').eq(0).find('VoiceType').text()).trigger('change') //声音类型
            $("#inFrequencyBroadcast").val($(xmlDoc).find('AlarmInExList').eq(0).find('AlarmInEx').eq(0).find('BroadCastTime').text()).trigger('change') //播报次数
            var q = $(xmlDoc).find('AlarmInExList').eq(0).find('AlarmInEx').eq(0).find('DayMode').text()
            if (q == "disable") {
                $("#indeploymentTime").val($(xmlDoc).find('AlarmInExList').eq(0).find('AlarmInEx').eq(0).find('AllDay').text()).trigger('change')
            } else {
                $("#indeploymentTime").val(q).trigger('change')
            }
            if ($("#indeploymentTime").val() == 'false') {
                $('#alarmDeployment').show()
            } else {
                $('#alarmDeployment').hide()
            }
            setScheduleX(xmlDoc, timeplan)

        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
// 设置声音报警布防区域
function setScheduleX(d, btimecaset) {
    // 转换xml中的时间
    var timeArr = [];
    var time = [];
    for (var i = 0; i < 7; i++) {
        timeArr[i] = $(d).find('TimeBlock_' + i).eq(0).text()
        for (var o = 0; o < 24; o++) {
            if (timeArr[i].charAt(2 * o) == 1) {
                time.push(1)
            } else {
                time.push(0)
            }
        }
    }
    btimecaset.setValue(changetimeFool(group(time, 24)));
}
// 试听音频
function setTryvoice(v) {
    var voiceXmldoc = '<?xml version="1.0" encoding="UTF-8" ?>' +
        "<AudioAlarmConfig><ChannelID>1</ChannelID>" +
        "<PeopleAudio></PeopleAudio><CarAudio></CarAudio>" +
        "<FireAudioType></FireAudioType><AudioOutputDelay></AudioOutputDelay>" +
        "<AudioVolume></AudioVolume>" +
        "<TryListenType>" + v + "</TryListenType>" +
        "<AlarmInExAudioType>" + $("#inSoundType").val() + "</AlarmInExAudioType>" +
        "<VideoShelterAudioType></VideoShelterAudioType>" +
        "<MotionAudioType></MotionAudioType>" +
        "<ElectronicDenceAudioType></ElectronicDenceAudioType>" +
        "<CrossBorderDetectAudioType></CrossBorderDetectAudioType>" +
        "<OffDutyDetectAudioType></OffDutyDetectAudioType>" +
        "</AudioAlarmConfig>"
    var cyURL = window.config.baseUrl + "/System/1/TryListenSelectedAlarmAudioV1";
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: voiceXmldoc,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr, 1);
        }
    });

}
// 保存报警输入配置
function setAlarmin() {
    aloutArr.length = 0;
    var xml_alarmin_size = $(gs_alarmin_cfg).find('AlarmInList').eq(0).find('AlarmIn').length;
    if ($("#alout1").is(':checked') || $("#alout2").is(':checked')) {
        alout = true;
    } else {
        alout = false;
    }
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><AlarmInList Version='1.0' />";
    if (xml_alarmin_size == '1') {
        cyXml = "<?xml version='1.0' encoding='UTF-8' ?><AlarmInList Version='1.0'><AlarmIn  Version='1.0'><ID>0</ID><Name>" + $("#Alarminputname").val() + "</Name><AlarmInType>" + $("#AlarminputType").val() + "</AlarmInType><Trigger Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><NotifyAMS Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>false</Enable></NotifyAMS><BeepAlert Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $('#alAudiblealarm').is(':checked') + "</Enable> </BeepAlert><FullScreen Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>false</Enable></FullScreen><Mail Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#alTriggermail").is(':checked') + "</Enable></Mail><Ftp Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#alTriggerFTP").is(':checked') + "</Enable></Ftp><Push Version='1.0'><Enable>" + $("#alTriggerpush").is(':checked') + " </Enable></Push><AlarmOut Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#alTriggeralarm").is(':checked') + "</Enable><AlarmOutMask>1</AlarmOutMask></AlarmOut><Snapshot Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#alTriggerSDcapture").is(':checked') + "</Enable><SnapshotMask>1</SnapshotMask></Snapshot><Record Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#alTriggerSDrecording").is(':checked') + "</Enable><RecordMask>1</RecordMask></Record><PTZ Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'> <Enable>false</Enable><PTZActionList Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><PTZAction Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><ChannelID>1</ChannelID><ActionName>none</ActionName><ActionNum>0</ActionNum></PTZAction></PTZActionList></PTZ></Trigger></AlarmIn></AlarmInList>";
    } else if (xml_alarmin_size == '2') {
        if ($("#alarmInput").val() == '1') {
            alarmInputflag = "1";
            if ($("#alout1").is(':checked') || $("#alout2").is(':checked')) {
                alout = true;
            } else {
                alout = false;
            }
            var alarmin_name = $(gs_alarmin_cfg).find('AlarmInList').eq(0).find('AlarmIn').eq(1).find('Name').text();
            var alarmin_type = $(gs_alarmin_cfg).find('AlarmInList').eq(0).find('AlarmIn').eq(1).find('AlarmInType').text();
            if ($("#alout1").is(':checked')) {
                aloutArr.push('1');
            } else {
                aloutArr.push('0');
            }
            if ($("#alout2").is(':checked')) {
                aloutArr.push('1');
            } else {
                aloutArr.push('0');
            }
            cyXml = "<?xml version='1.0' encoding='UTF-8'?><AlarmInList Version='1.0'><AlarmIn Version='1.0'><ID>0</ID><Name>" + $("#alarmInName").val() + "</Name><AlarmInType>" + $("#AlarminputType").val() + "</AlarmInType><Trigger Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><NotifyAMS Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable></NotifyAMS><BeepAlert Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $('#BeepAlert').is(':checked') + "</Enable> </BeepAlert><FullScreen Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable></FullScreen><Mail Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#alEmail").is(':checked') + "</Enable></Mail><Ftp Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#alftp").is(':checked') + "</Enable></Ftp><AlarmOut Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + alout + "</Enable><AlarmOutMask>" + aloutArr.join('') + "</AlarmOutMask></AlarmOut><Snapshot Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#sdpick").is(':checked') + "</Enable><SnapshotMask></SnapshotMask></Snapshot><Record Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#sdvdeo").is(':checked') + "</Enable><RecordMask></RecordMask></Record><PTZ Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'> <Enable></Enable><PTZActionList Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><PTZAction Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><ChannelID></ChannelID><ActionName></ActionName><ActionNum></ActionNum></PTZAction></PTZActionList></PTZ></Trigger></AlarmIn><AlarmIn Version='1.0'><ID>1</ID><Name>" + alarmin_name + "</Name><AlarmInType>" + alarmin_type + "</AlarmInType></AlarmIn></AlarmInList>";
        } else if ($("#alarmInput").val() == '2') {
            alarmInputflag = "2";
            if ($("#alout1").is(':checked') || $("#alout2").is(':checked')) {
                alout = true;
            } else {
                alout = false;
            }
            var alarmin_name = $(gs_alarmin_cfg).find('AlarmInList').eq(0).find('AlarmIn').eq(0).find('Name').text();
            var alarmin_type = $(gs_alarmin_cfg).find('AlarmInList').eq(0).find('AlarmIn').eq(0).find('AlarmInType').text();
            var alEmail = $(gs_alarmin_cfg).find('AlarmInList').eq(0).find('AlarmIn').eq(0).find('Trigger').eq(0).find('Mail').eq(0).find('Enable').text();
            var alFtp = $(gs_alarmin_cfg).find('AlarmInList').eq(0).find('AlarmIn').eq(0).find('Trigger').eq(0).find('Ftp').eq(0).find('Enable').text();
            var alarmoutable = $(gs_alarmin_cfg).find('AlarmInList').eq(0).find('AlarmIn').eq(0).find('Trigger').eq(0).find('AlarmOut').eq(0).find('Enable').text();
            var alarmoutmask = $(gs_alarmin_cfg).find('AlarmInList').eq(0).find('AlarmIn').eq(0).find('Trigger').eq(0).find('AlarmOut').eq(0).find('AlarmOutMask').text();

            //触发报警输出状态和哪一个报警输出
            if ($("#alout1").is(':checked')) {
                aloutArr.push('1');
            } else {
                aloutArr.push('0');
            }
            if ($("#alout2").is(':checked')) {
                aloutArr.push('1');
            } else {
                aloutArr.push('0');
            }
            cyXml = "<?xml version='1.0' encoding='UTF-8'?><AlarmInList Version='1.0'>" +
                "<AlarmIn Version='1.0'><ID>0</ID><Name>" + alarmin_name + "</Name><AlarmInType>" + alarmin_type + "</AlarmInType>" +
                "<Trigger Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><NotifyAMS Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable></NotifyAMS>" +
                "<BeepAlert Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable> </BeepAlert><FullScreen Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable></FullScreen>" +
                "<Mail Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + alEmail + "</Enable></Mail><Ftp Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + alFtp + "</Enable></Ftp>" +
                "<AlarmOut Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + alarmoutable + "</Enable><AlarmOutMask>" + alarmoutmask + "</AlarmOutMask></AlarmOut><Snapshot Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable><SnapshotMask></SnapshotMask></Snapshot>" +
                "<Record Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + alRecord + "</Enable><RecordMask></RecordMask></Record><PTZ Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable><PTZActionList Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><PTZAction Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><ChannelID></ChannelID><ActionName></ActionName><ActionNum></ActionNum></PTZAction></PTZActionList>" +
                "</PTZ></Trigger></AlarmIn><AlarmIn Version='1.0'><ID>1</ID><Name>" + $("#alarmInName").val() + "</Name><AlarmInType>" + $("#alarmInType").val() + "</AlarmInType><Trigger Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><NotifyAMS Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable></NotifyAMS><BeepAlert Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable> </BeepAlert><FullScreen Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable></FullScreen>" +
                "<Mail Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#alEmail").is(':checked') + "</Enable></Mail><Ftp Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + $("#alftp").is(':checked') + "</Enable></Ftp><AlarmOut Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable>" + alout + "</Enable><AlarmOutMask>" + aloutArr.join('') + "</AlarmOutMask></AlarmOut><Snapshot Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable><SnapshotMask></SnapshotMask></Snapshot>" +
                "<Record Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><Enable></Enable><RecordMask></RecordMask></Record><PTZ Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'> <Enable>" + $("#moPtz").is(':checked') + "</Enable><PTZActionList Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><PTZAction Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><ChannelID></ChannelID><ActionName></ActionName><ActionNum></ActionNum></PTZAction></PTZActionList></PTZ></Trigger></AlarmIn></AlarmInList>";
        }
    }

    var cyURL = window.config.baseUrl + "/Alarm/Alarmin";
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveStateTwo(alarmState, xhr)
        }
    });
}
// 保存声音报警信息
function setAlarmInEx(timeplan) {
    var q = ""
    var r = ""
    if ($("#indeploymentTime").val() == "true") {
        q = true
        r = "disable"
    } else if ($("#indeploymentTime").val() == "false") {
        q = false
        r = "disable"
    } else {
        q = true
        r = $("#indeploymentTime").val()
    }
    // 布防区域时间选择
    var timeArr = takeZero(timeplan.getvalue());

    var xmlDocelecs = '<?xml version="1.0" encoding="UTF-8" ?>' +
        "<AlarmInExList><AlarmInEx>" +
        "<VoiceType>" + $("#inSoundType").val() + "</VoiceType>" +
        "<BroadCastTime>" + $("#inFrequencyBroadcast").val() + "</BroadCastTime>" +
        "<Schedule>" +
        "<AllDay>" + q + "</AllDay>" +
        "<DayMode>" + r + "</DayMode>" +
        "<TimeBlockList>" +
        "<TimeBlock_0>" + timeArr[6] + "</TimeBlock_0>" +
        "<TimeBlock_1>" + timeArr[0] + "</TimeBlock_1>" +
        "<TimeBlock_2>" + timeArr[1] + "</TimeBlock_2>" +
        "<TimeBlock_3>" + timeArr[2] + "</TimeBlock_3>" +
        "<TimeBlock_4>" + timeArr[3] + "</TimeBlock_4>" +
        "<TimeBlock_5>" + timeArr[4] + "</TimeBlock_5>" +
        "<TimeBlock_6>" + timeArr[5] + "</TimeBlock_6>" +
        "</TimeBlockList></Schedule></AlarmInEx></AlarmInExList>"
    var cyURL = window.config.baseUrl + "/Alarm/AlarmInEx";
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: false,
        processData: false,
        data: xmlDocelecs,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            getAlarmInEx(timeplan);
            alarmState = xhr;
        }
    });

}
// 获取报警输出配置
function getAlarmout() {
    $.ajax({
        url: window.config.baseUrl + "/Alarm/Alarmout",
        type: "GET",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            gs_alarmout_cfg = xmlDoc;
            var xml_alarmout_size = $(xmlDoc).find('AlarmOutList').eq(0).find('AlarmOut').length;
            $("#Alarmoutputs").empty();
            for (var i = 0; i < xml_alarmout_size; i++) {
                $("<option value='" + bcs[i] + "'>" + bcs[i] + "</option>").appendTo("#Alarmoutputs");
            }
            $("#Alarmoutputs").val(alarmOutputflag).trigger('change');
            $("#outputName").val($(xmlDoc).find('AlarmOutList').eq(0).find('AlarmOut').eq(alarmOutputflag - 1).find('Name').text());
            var alarmout_delay = $(xmlDoc).find('AlarmOutList').eq(0).find('AlarmOut').eq(alarmOutputflag - 1).find('Delay').text();
            if (alarmout_delay == "keep") {
                $("#outputdelay").val("keep").trigger('change');
                $('#timeinput').hide()
            } else {
                $('#timeinput').show()
                $("#outputdelay").val("manual").trigger('change');
                $("#yantimed").val(alarmout_delay);
            }
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
// 保存报警输出配置
function setAlarmout() {

    if ($("#yantimed").val() < 1 || $("#yantimed").val() > 600) {
        $.optionBootoast($.i18n.prop('i18n-exceedingdelaytime-tip'), "danger")
        return
    }




    var xml_alarmout_size = $(gs_alarmout_cfg).find('AlarmOutList').eq(0).find('AlarmOut').length; //1
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><AlarmOutList Version='1.0' />";
    if (xml_alarmout_size == '1') {
        if ($("#outputdelay").val() == "keep") {
            cyXml = "<?xml version='1.0' encoding='UTF-8' ?><AlarmOutList Version='1.0'><AlarmOut Version='1.0'><ID>0</ID><Name>" + $("#outputName").val() + "</Name><Delay>" + $("#outputdelay").val() + "</Delay></AlarmOut></AlarmOutList>";
        } else {
            cyXml = "<?xml version='1.0' encoding='UTF-8' ?><AlarmOutList Version='1.0'><AlarmOut Version='1.0'><ID>0</ID><Name>" + $("#outputName").val() + "</Name><Delay>" + $("#yantimed").val() + "</Delay></AlarmOut></AlarmOutList>";
        }
        //cyXml = "<?xml version='1.0' encoding='UTF-8' ?><AlarmOutList Version='1.0'><AlarmOut Version='1.0'><ID>0</ID><Name>" + $("#alarmOutname").val() + "</Name><Delay>" + $("#alarmOutdelay").val() + "</Delay></AlarmOut></AlarmOutList>";
    } else if (xml_alarmout_size == '2') {
        if ($("#Alarmoutputs").val() == '1') {
            alarmOutputflag = "1";
            var alarmout_name = $(gs_alarmout_cfg).find('AlarmOutList').eq(0).find('AlarmOut').eq(1).find('Name').text();
            var alarmout_delay = $(gs_alarmout_cfg).find('AlarmOutList').eq(0).find('AlarmOut').eq(1).find('Delay').text();
            if ($("#outputdelay").val() == "keep") {
                cyXml = "<?xml version='1.0' encoding='UTF-8' ?><AlarmOutList Version='1.0'><AlarmOut Version='1.0'><ID>0</ID><Name>" + $("#outputName").val() + "</Name><Delay>" + $("#outputdelay").val() + "</Delay></AlarmOut><AlarmOut Version='1.0'><ID>0</ID><Name>" + alarmout_name + "</Name><Delay>" + alarmout_delay + "</Delay></AlarmOut></AlarmOutList>";
            } else {
                cyXml = "<?xml version='1.0' encoding='UTF-8' ?><AlarmOutList Version='1.0'><AlarmOut Version='1.0'><ID>0</ID><Name>" + $("#outputName").val() + "</Name><Delay>" + $("#yantimed").val() + "</Delay></AlarmOut><AlarmOut Version='1.0'><ID>0</ID><Name>" + alarmout_name + "</Name><Delay>" + alarmout_delay + "</Delay></AlarmOut></AlarmOutList>";
            }
        } else if ($("#Alarmoutputs").val() == '2') {
            alarmOutputflag = "2";
            var alarmout_name = $(gs_alarmout_cfg).find('AlarmOutList').eq(0).find('AlarmOut').eq(0).find('Name').text();
            var alarmout_delay = $(gs_alarmout_cfg).find('AlarmOutList').eq(0).find('AlarmOut').eq(0).find('Delay').text();
            if ($("#outputdelay").val() == "keep") {
                cyXml = "<?xml version='1.0' encoding='UTF-8' ?><AlarmOutList Version='1.0'><AlarmOut Version='1.0'><ID>0</ID><Name>" + alarmout_name + "</Name><Delay>" + alarmout_delay + "</Delay></AlarmOut><AlarmOut Version='1.0'><ID>0</ID><Name>" + $("#outputName").val() + "</Name><Delay>" + $("#outputdelay").val() + "</Delay></AlarmOut></AlarmOutList>";
            } else {
                cyXml = "<?xml version='1.0' encoding='UTF-8' ?><AlarmOutList Version='1.0'><AlarmOut Version='1.0'><ID>0</ID><Name>" + alarmout_name + "</Name><Delay>" + alarmout_delay + "</Delay></AlarmOut><AlarmOut Version='1.0'><ID>0</ID><Name>" + $("#outputName").val() + "</Name><Delay>" + $("#yantimed").val() + "</Delay></AlarmOut></AlarmOutList>";
            }
        }
    }

    var cyURL = window.config.baseUrl + "/Alarm/Alarmout";
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr);
        }
    });
}
