var _tabled = null
var stateList = [];
var row = null;
$(document).ready(function() {
    initVideoManagement();
    $("#AudioInMethod").change(function() {
        saveSysinfs();
    })
});

function initVideoManagement() {
    if (!$('.select2').hasClass('select2-offscreen')) {
        $("#IPCProtocol,#AudioInMethod").select2({
            minimumResultsForSearch: -1,
            width: 360
        });
    }
    getSysinfs();
    dataTableDraw();
}

function dataTableDraw() {
    var aoColumns = [{
            "mDataProp": "DiskID",
            // "sWidth": "10%" //设置列宽
        },
        {
            "mDataProp": "DiskTypeName",
            "sWidth": "15%" //设置列宽
        },
        {
            "mDataProp": "hddState",
        },
        {
            "mDataProp": "TotalCapacity",
            "sWidth": "18%" //设置列宽
        },
        {
            "mDataProp": "AvailableCapacity",
        },
    ];
    var retrieveData = getConnectDeviceList();
    _tabled = new table('vmTable', {
        bProcessing: true,
        isCheckBox: false,
        retrieve: true, //可能是表格重复使用时受到影响 必须加上
        bPaginate: false, //翻页功能
        bLengthChange: false, //改变每页显示数据数量
        bFilter: false, //过滤功能
        bSort: false, //排序功能
        bInfo: false, //页脚信息
        bAutoWidth: false, //自动宽度
        aoColumns: aoColumns,
        aaData: retrieveData
    });
}

function getConnectDeviceList() {
    var dataArr = [];
    $.ajax({
        url: config.baseUrl + "/Disk",
        type: "GET",
        async: false,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            var listLength = $(xmlDoc).find('DiskList').find('Disk').length;
            if (listLength > 0) {
                for (var i = 0; i < listLength; i++) {
                    var itme = {};
                    var TotalCapacity = $(xmlDoc).find('DiskList').find('Disk').eq(i).find('TotalCapacity').text();
                    itme.TotalCapacity = (parseInt(TotalCapacity) / 1024).toFixed(1);
                    var AvailableCapacity = $(xmlDoc).find('DiskList').find('Disk').eq(i).find('AvailableCapacity').text();
                    itme.AvailableCapacity = (parseInt(AvailableCapacity) / 1024).toFixed(1);
                    var hddState = $(xmlDoc).find('DiskList').find('Disk').eq(i).find('DiskStorageAttribute').text();
                    switch (hddState) {
                        case "idle":
                            hddState = $.i18n.prop("i18n-IDS-STATE01");
                            break;
                        case "writting":
                            hddState = $.i18n.prop("i18n-IDS-STATE02");
                            break;
                        case "reading":
                            hddState = $.i18n.prop("i18n-IDS-STATE03");
                            break;
                        case "diskexception":
                            hddState = $.i18n.prop("i18n-IDS-STATE04");
                            break;
                        case "diskerror":
                            hddState = $.i18n.prop("i18n-IDS-STATE05");
                            break;
                        case "readonly":
                            hddState = $.i18n.prop("i18n-IDS-STATE06");
                            break;
                        case "readwrite":
                            hddState = $.i18n.prop("i18n-IDS-STATE07");
                            break;
                    }
                    itme.hddState = hddState
                    itme.DiskTypeName = $(xmlDoc).find('DiskList').find('Disk').eq(i).find('DiskTypeName').text()
                    itme.DiskID = ($(xmlDoc).find('DiskList').find('Disk').eq(i).find('DiskID').text()) - 0 + 1
                    dataArr.push(itme);

                }
            } else {
                dataArr = []
            }
        }
    });
    return dataArr;
}
// 保存配置
function saveSysinfs() {
    var cyXml = "<?xml version='1.0' encoding='UTF-8'?><Record Version='1.0'>" +
        "<RecordWriteCircleType>" + $("#AudioInMethod").val() + "</RecordWriteCircleType>" +
        "<RecordDeleteType></RecordDeleteType><RecordStreamType></RecordStreamType><PreRecord></PreRecord><DelayRecord></DelayRecord><RecordMode></RecordMode></Record>";
    $.ajax({
        url: config.baseUrl + "/Record",
        type: "PUT",
        data: cyXml,
        async: false,
        timeout: 15000,
        datatype: "xml",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            SaveState(xhr);
        },
        error: function(xmlDoc, textStatus, xhr) {}
    });
}
// 获取配置
function getSysinfs() {
    $.ajax({
        url: config.baseUrl + "/Record",
        type: "GET",
        async: false,
        timeout: 15000,
        datatype: "xml",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            $("#AudioInMethod").val($(xmlDoc).find("Record").eq(0).find("RecordWriteCircleType").eq(0).text()).trigger("change.select2");
        }
    });
}
// 格式话
function DoFormat() {
    var cyXml = "";
    var cyURL = config.baseUrl + "/Record/Format/Call";
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            $.optionBootoast($.i18n.prop('i18n-disk-format-status4'), "success");
        }
    })
}