var lmd = ['high', 'middle', 'low'];
var zdlx = ['custom', 'alarm', 'dog', 'warning area', 'monitoring area', 'welcome', 'Deep Water Warning', 'Private territory', 'danger zone', 'Valuable objects', 'High Warning', 'Private Parking'];
var bbcs = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
var times = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '23']
var xmlDocd;
$(document).ready(function() {
    $("#occsensitivity,#occDefensePlan").select2({
        minimumResultsForSearch: -1,
        width: 360,
    });
    $("#occSoundType").select2({
        minimumResultsForSearch: -1,
    });
    $("#occFrequencyBroadcast").select2({
        minimumResultsForSearch: -1,
    });
    $("#occdeploymentTime").select2({
        minimumResultsForSearch: -1,
    });
    // 初始化时间选择
    var btimecasetp = $("#occrecordDiv").TimePlan({
        sectionNum: 6
    });
    var btimecaset = $("#occBroadcast").TimePlan({
        sectionNum: 6
    });
    selectvalue(); //初始化下拉选项
    changeTab(); //进来默认点击
    getOcclusionAlarm(btimecaset, btimecasetp);
    // 切换栏目
    $("#occplace").click(function() {
        changeTab()
    });
    $("#occliand").click(function() {
        var qu = document.getElementById('occplace')
        var ld = document.getElementById('occliand')
        ld.classList.add('qutable');
        qu.classList.remove('qutable');
        $("#LinkageMode").show();
        $("#Regional").hide()
    });
    // 隐藏展示时间控件
    $('#occDefensePlan').change(function(v) {
        if (v.val == 'false') {
            $('#occrecordDiv').show()
        } else {
            $('#occrecordDiv').hide()
        }
    });
    $('#occdeploymentTime').change(function(v) {
        if (v.val == 'false') {
            $('#occBroadcast').show()
        } else {
            $('#occBroadcast').hide()
        }
    });

    // 点击试听按钮
    $('#occTyvolice').click(function() {
        setTryvoice('VideoShelter')
    });
    // 点击保存按钮
    $('#lsave,#rsave').click(function() {
        if ("admin" == config.user || "super_admin" == config.user) {
            var timeArrp = takeZero(btimecasetp.getvalue()); //普通布防
            var timeArr = takeZero(btimecaset.getvalue()); //声音布防
            setOcclusionAlarm(timeArr, timeArrp);
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }
    })
});
// 栏目切换
function changeTab() {
    var qu = document.getElementById('occplace')
    var ld = document.getElementById('occliand')
    qu.classList.add('qutable')
    ld.classList.remove('qutable')
    $("#Regional").show()
    $("#LinkageMode").hide()
}
// 下拉选项
function selectvalue() {
    // 灵敏度
    var st = $.i18n.prop("i18n-sensitivity-value").split(",")
    for (var i = 0; i < st.length; i++) {
        $("<option value='" + lmd[i] + "'>" + st[i] + "</option>").appendTo("#occsensitivity");
    }
    // 声音类型
    var shzdVoicetype = $(config.deviceAiCap[0].forfaceVoicetype);
    var sylx = $.i18n.prop("i18n-str_voice_type").split(",")
    for (var i = 0; i < 12; i++) {
        if (shzdVoicetype.find("Custom").text() == "true" && zdlx[i] == "custom") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        } else if (shzdVoicetype.find("Alarm").text() == "true" && zdlx[i] == "alarm") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        } else if (shzdVoicetype.find("dog").text() == "true" && zdlx[i] == "dog") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        } else if (shzdVoicetype.find("warning").text() == "true" && zdlx[i] == "warning area") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        } else if (shzdVoicetype.find("monitoring").text() == "true" && zdlx[i] == "monitoring area") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        } else if (shzdVoicetype.find("welcome").text() == "true" && zdlx[i] == "welcome") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        } else if (shzdVoicetype.find("DeepWater").text() == "true" && zdlx[i] == "Deep Water Warning") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        } else if (shzdVoicetype.find("PrivateTerritory").text() == "true" && zdlx[i] == "Private territory") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        } else if (shzdVoicetype.find("DangerZone").text() == "true" && zdlx[i] == "danger zone") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        } else if (shzdVoicetype.find("ValuableObjects").text() == "true" && zdlx[i] == "Valuable objects") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        } else if (shzdVoicetype.find("HighWarning").text() == "true" && zdlx[i] == "High Warning") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        } else if (shzdVoicetype.find("PrivateParking").text() == "true" && zdlx[i] == "Private Parking") {
            $("<option value='" + zdlx[i] + "'>" + sylx[i] + "</option>").appendTo("#occSoundType");
        };
    }
    // 播报次数
    var cs = $.i18n.prop("i18n-broadcast").split(",")
    for (var i = 0; i < 5; i++) {
        $("<option value='" + bbcs[i] + "'>" + cs[i] + "</option>").appendTo("#occFrequencyBroadcast");
    }
}
// 试听音频
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></AlarmInExAudioType>" +
        "<VideoShelterAudioType>" + $("#occSoundType").val() + "</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 getOcclusionAlarm(btimecaset, btimecasetp) {
    $.ajax({
        url: window.config.baseUrl + "/System/VideoShelterAlarmCfg",
        type: "GET",
        async: true,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            xmlDocd = xmlDoc;
            var EnableValuedd = $(xmlDoc).find('VideoShelterAlarmCfg').eq(0).find('Enable').eq(0).text();
            $("#isShowzdalarm").prop('checked', JSON.parse(EnableValuedd)); //是否启用遮挡报警
            $("#occsensitivity").val($(xmlDoc).find('VideoShelterAlarmCfg').eq(0).find('Sensitivity').eq(0).text()).trigger('change'); //灵敏度
            $("#occDefensePlan").val($(xmlDoc).find('VideoShelterAlarmCfg').eq(0).find('AllDay').eq(1).text()).trigger('change'); //布防计划
            // 判断普通报警布防显示隐藏
            if ($("#occDefensePlan").val() == 'false') {
                $('#occrecordDiv').show()
            } else {
                $('#occrecordDiv').hide()
            }
            // 联动项
            var BeepAlertEnable = $(xmlDoc).find('VideoShelterAlarmCfg').eq(0).find('BeepAlert').eq(0).find('Enable').text();
            $("#occAudiblealarm").prop('checked', JSON.parse(BeepAlertEnable)); //声音报警
            var baoaralm = $(xmlDoc).find('VideoShelterAlarmCfg').eq(0).find('AlarmOut').eq(0).find('Enable').text();
            $("#occalarmoutput").prop('checked', JSON.parse(baoaralm)); //触发报警输出
            $("#occTriggermail").prop('checked', JSON.parse($(xmlDoc).find("Mail").find("Enable").text())); //触发邮件
            $("#occTriggerFTP").prop('checked', JSON.parse($(xmlDoc).find("Ftp").find("Enable").text())); //触发FTP
            $("#occTriggerpush").prop('checked', JSON.parse($(xmlDoc).find("Push").find("Enable").text())); //触发推送
            $("#occTriggerSDcapture").prop('checked', JSON.parse($(xmlDoc).find("Snapshot").find("Enable").text())); //触发sd卡抓图
            $("#occTriggerSDrecording").prop('checked', JSON.parse($(xmlDoc).find("Record").find("Enable").text())); //触发sd卡录像
            $("#occLightalarm").prop('checked', JSON.parse($(xmlDoc).find("LightAlarm").find("Enable").text())); //灯光报警
            $("#occSoundType").val($(xmlDoc).find('VideoShelterAlarmCfg').eq(0).find('VoiceType').eq(0).text()).trigger('change'); //声音类型
            $("#occFrequencyBroadcast").val($(xmlDoc).find('VideoShelterAlarmCfg').eq(0).find('BroadcastCount').eq(0).text()).trigger('change'); //播报次数
            var status = $(xmlDoc).find('VideoShelterAlarmCfg').eq(0).find('DayMode').eq(0).text();
            if (status == "disable") {
                $("#occdeploymentTime").val($(xmlDoc).find('VideoShelterAlarmCfg').eq(0).find('AllDay').eq(0).text()).trigger('change')
            } else {
                $("#occdeploymentTime").val(status).trigger('change')
            }
            // 判断报警布防显示隐藏
            if ($("#occdeploymentTime").val() == 'false') {
                $('#occBroadcast').show()
            } else {
                $('#occBroadcast').hide()
            }
            setScheduleX(xmlDoc, btimecaset);
            setScheduleXp(xmlDoc, btimecasetp);
        },
        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 setScheduleXp(d, btimecasetp) {
    // 转换xml中的时间
    var timeArr = [];
    var time = [];
    for (var i = 0; i < 7; i++) {
        timeArr[i] = $(d).find('TimeBlock_' + i).eq(1).text()
        for (var o = 0; o < 24; o++) {
            if (timeArr[i].charAt(2 * o) == 1) {
                time.push(1)
            } else {
                time.push(0)
            }
        }
    }
    btimecasetp.setValue(changetimeFool(group(time, 24)));
}

// 设置遮挡报警配置
function setOcclusionAlarm(arr, parr) {
    var d, t
    d = $("#occdeploymentTime").val()
    if (d == "false") {
        d = "false"
        t = "disable"
    } else if (d == "true") {
        d = "true"
        t = "disable"
    } else if (d == "evening") {
        d = "true"
        t = "evening"
    } else {
        d = "true"
        t = "day"
    }
    var cyXml = '<?xml version="1.0" encoding="UTF-8" ?><VideoShelterAlarmCfg>' +
        "<Enable>" + $("#isShowzdalarm").is(':checked') + "</Enable>" +
        "<Sensitivity>" + $("#occsensitivity").val() + "</Sensitivity>" +
        "<VoiceType>" + $("#occSoundType").val() + "</VoiceType>" +
        "<BroadcastCount>" + $("#occFrequencyBroadcast").val() + "</BroadcastCount>" +
        '<Trigger Version="1.0"><NotifyAMS Version="1.0">' +
        "<Enable>false</Enable></NotifyAMS>" +
        '<BeepAlert Version="1.0">' +
        "<Enable>" + $("#occAudiblealarm").is(':checked') + "</Enable></BeepAlert>" +
        '<FullScreen Version="1.0"><Enable>false</Enable></FullScreen>' +
        '<Mail Version="1.0"><Enable>' + $("#occTriggermail").is(':checked') + '</Enable></Mail>' +
        '<Ftp Version="1.0"><Enable>' + $("#occTriggerFTP").is(':checked') + '</Enable></Ftp>' +
        '<Push Version="1.0"><Enable>' + $("#occTriggerpush").is(':checked') + '</Enable></Push>' +
        '<AlarmOut Version="1.0"><Enable>' + $("#occalarmoutput").is(':checked') + '</Enable>' +
        '<AlarmOutMask>1</AlarmOutMask></AlarmOut>' +
        '<Snapshot Version="1.0"><Enable>' + $("#occTriggerSDcapture").is(':checked') + '</Enable>' +
        '<SnapshotMask>1</SnapshotMask></Snapshot>' +
        '<Record Version="1.0"><Enable>' + $("#occTriggerSDrecording").is(':checked') + '</Enable>' +
        '<RecordMask>1</RecordMask></Record>' +
        '<PTZ Version="1.0"><Enable>false</Enable>' +
        '<PTZActionList Version="1.0"><PTZAction Version="1.0">' +
        '<ChannelID>1</ChannelID><ActionName>none</ActionName><ActionNum>0</ActionNum>' +
        '</PTZAction></PTZActionList></PTZ><LightAlarm Version="1.0">' +
        '<Enable>' + $("#occLightalarm").is(':checked') + '</Enable></LightAlarm></Trigger>' +
        '<Schedule Version="1.0"><AllDay>' + $("#occDefensePlan").val() + '</AllDay>' +
        '<TimeBlockList Version="1.0">' +
        '<TimeBlock_0>' + parr[6] + '</TimeBlock_0>' +
        '<TimeBlock_1>' + parr[0] + '</TimeBlock_1>' +
        '<TimeBlock_2>' + parr[1] + '</TimeBlock_2>' +
        '<TimeBlock_3>' + parr[2] + '</TimeBlock_3>' +
        '<TimeBlock_4>' + parr[3] + '</TimeBlock_4>' +
        '<TimeBlock_5>' + parr[4] + '</TimeBlock_5>' +
        '<TimeBlock_6>' + parr[5] + '</TimeBlock_6>' +
        '</TimeBlockList></Schedule>' +
        '<VoiceAlarmSchedule Version="1.0"><AllDay>' + d + '</AllDay>' +
        '<DayMode>' + t + '</DayMode><TimeBlockList Version="1.0">' +
        '<TimeBlock_0>' + arr[6] + '</TimeBlock_0>' +
        '<TimeBlock_1>' + arr[0] + '</TimeBlock_1>' +
        '<TimeBlock_2>' + arr[1] + '</TimeBlock_2>' +
        '<TimeBlock_3>' + arr[2] + '</TimeBlock_3>' +
        '<TimeBlock_4>' + arr[3] + '</TimeBlock_4>' +
        '<TimeBlock_5>' + arr[4] + '</TimeBlock_5>' +
        '<TimeBlock_6>' + arr[5] + '</TimeBlock_6>' +
        '</TimeBlockList></VoiceAlarmSchedule></VideoShelterAlarmCfg>'

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