var react; //保存时间对象
var realtTimedata; //保存时间段
$(document).ready(function() {
    $("#NVR_ChnOSDChannelMask").select2({
        width: 220,
        minimumResultsForSearch: -1,
    })
    for (var i = 1; i <= config.deviceInfo.VideoInputChannels; i++) {
        $("#NVR_ChnOSDChannelMask").append('<option class="option" value="' + i + '">' + i + '</option>'); //Channel列表
    }
    // 默认第一个通道
    $("#NVR_ChnOSDChannelMask").val("1").trigger('change.select2')
    react = $("#rejBroadcast").TimePlan({
        sectionNum: 6
    })
    getRecordSchedule(1);
    // 点击保存
    $("#reacdSave").click(function() {
        realtTimedata = takeZero(react.getvalue())
        setRecordSchedule($("#NVR_ChnOSDChannelMask").val() - 0)
    });
    // 通道号改变切换
    $("#NVR_ChnOSDChannelMask").change(function() {
        getRecordSchedule($("#NVR_ChnOSDChannelMask").val());
    });
    // 默认录像类型
    initCheck();
    // 选择录像类型
    $("#dtime,#ymove,#zmove,#bmove").click(function() {
        initstyle()
        $("#" + $(this)[0].id + "").find(".bigy").css("background-color", "#3A77FE")
        $("#" + $(this)[0].id + "").find(".bigy").css("border", "1px solid #3A77FE")
        if ($(this)[0].id == "zmove") {
            $("#rejBroadcast_drawTypeSel").val("3")
            react.SetEventtype("3")
        } else if ($(this)[0].id == "ymove") {
            $("#rejBroadcast_drawTypeSel").val("2")
            react.SetEventtype("2")
        } else if($(this)[0].id == "bmove"){
            $("#rejBroadcast_drawTypeSel").val("4")
            react.SetEventtype("4")
        }else{
            $("#rejBroadcast_drawTypeSel").val("1")
            react.SetEventtype("1")
        }
    })
});
// 初始化所有单选样式
function initstyle() {
    $("#dtime").find(".bigy").css("background-color", "#ccc")
    $("#dtime").find(".bigy").css("border", "1px solid #ccc")
    $("#ymove").find(".bigy").css("background-color", "#ccc")
    $("#ymove").find(".bigy").css("border", "1px solid #ccc")
    $("#zmove").find(".bigy").css("background-color", "#ccc")
    $("#zmove").find(".bigy").css("border", "1px solid #ccc")
}
// 默认选中
function initCheck() {
    $("#dtime").find(".bigy").css("background-color", "#3A77FE");
    $("#dtime").find(".bigy").css("border", "1px solid #3A77FE");
    $("#rejBroadcast_drawTypeSel").val("1")
}
// 获取当前通道的录像计划
function getRecordSchedule(ch) {
    $.ajax({
        url: window.config.baseUrl + "/Record/" + ch + "/RecordSchedule",
        type: "GET",
        async: true,
        timeout: 0,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc) {
            var week = []
            for (var i = 0; i < 7; i++) {
                week[i] = $(xmlDoc).find('Schedule').eq(0).find('TimeBlockList').eq(0).find('TimeBlock_' + i).eq(0).text();
            }
            var timeArr = [];
            var time = [];
            for (var i = 0; i < 7; i++) {
                timeArr[i] = $(xmlDoc).find('TimeBlock_' + i).eq(0).text()
                for (var o = 0; o < 24; o++) {
                    if (timeArr[i].charAt(2 * o) == 1) {
                        time.push(1)
                    } else if (timeArr[i].charAt(2 * o) == 2) {
                        time.push(2)
                    } else if (timeArr[i].charAt(2 * o) == 4) {
                        time.push(3)
                    } else if(timeArr[i].charAt(2 * o) == 3){
                        time.push(4)
                    }else{
                        time.push(0)
                    }
                }
            }
            react.setValue(changetimeFool(group(time, 24)))
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}
// 保存当前通道的录像计划
function setRecordSchedule(ch) {
    var cyXml = "<?xml version='1.0' encoding='UTF-8' ?><RecordSchedule Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'><StreamType></StreamType>" +
        "<Schedule Version='1.0'><AllDay></AllDay><TimeBlockList Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>" +
        "<TimeBlock_0>" + realtTimedata[6] + "</TimeBlock_0><TimeBlock_1>" + realtTimedata[0] + "</TimeBlock_1><TimeBlock_2>" + realtTimedata[1] + "</TimeBlock_2><TimeBlock_3>" + realtTimedata[2] + "</TimeBlock_3><TimeBlock_4>" + realtTimedata[3] + "</TimeBlock_4><TimeBlock_5>" + realtTimedata[4] + "</TimeBlock_5><TimeBlock_6>" + realtTimedata[5] + "</TimeBlock_6></TimeBlockList></Schedule></RecordSchedule>";
    var cyURL = window.config.baseUrl + "/Record/" + ch + "/RecordSchedule";
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr);
            getRecordSchedule(ch)
        }
    });

}