function SetAlarmLevel(){
	
	SetAlarmLevel.week=['星期一','星期二','星期三','星期四','星期五','星期六','星期日'];
	SetAlarmLevel.title = '0,2,4,6,8,10,12,14,16,18,20,22,24';
	SetAlarmLevel.data = [{time:''},{time:''},{time:''},{time:''},{time:''},{time:''},{time:''}];
	SetAlarmLevel.opt = {colunm:24};
	SetAlarmLevel.currentColor = "";
	SetAlarmLevel.backgroundColor = "#eeeeee";
	SetAlarmLevel.alarmLevel = {1:"#2196f3",2:"#ffeb3b",3:"#f9b55b",4:"#ff2222"};
	
}
SetAlarmLevel.init=function(group){
	/*var group=[{
		bjjb: 4,
		bz: null,
		cjrq: "2018-11-15 18:53:23",
		cjry: "impn",
		gjsbdy: 0,
		gzbh: "b9e9e1e8f4584f1b80dd91bb59cc10fd",
		jssj: "24:00",
		kssj: "00:00",
		xgrq: "2018-11-15 18:53:23",
		xgry: "impn",
		xh: "46f5c2a0690249f8923058b34aa5acb9",
		xqj: 1
	},
	{
		bjjb: 4,
		bz: null,
		cjrq: "2018-11-15 18:53:24",
		cjry: "impn",
		gjsbdy: 0,
		gzbh: "b9e9e1e8f4584f1b80dd91bb59cc10fd",
		jssj: "24:00",
		kssj: "00:00",
		xgrq: "2018-11-15 18:53:24",
		xgry: "impn",
		xh: "4924b578290640a79f56e6e2a650e28b",
		xqj: 7
	},
	{
		bjjb: 4,
		bz: null,
		cjrq: "2018-11-15 18:53:24",
		cjry: "impn",
		gjsbdy: 0,
		gzbh: "b9e9e1e8f4584f1b80dd91bb59cc10fd",
		jssj: "24:00",
		kssj: "00:00",
		xgrq: "2018-11-15 18:53:24",
		xgry: "impn",
		xh: "7ee3dc6c15d046d0be223ee239890776",
		xqj: 3
	},
	{
		bjjb: 4,
		bz: null,
		cjrq: "2018-11-15 18:53:24",
		cjry: "impn",
		gjsbdy: 0,
		gzbh: "b9e9e1e8f4584f1b80dd91bb59cc10fd",
		jssj: "24:00",
		kssj: "00:00",
		xgrq: "2018-11-15 18:53:24",
		xgry: "impn",
		xh: "9a6f98ab484e4a8d9bfcdb9262adc10a",
		xqj: 2
	},
	{
		bjjb: 4,
		bz: null,
		cjrq: "2018-11-15 18:53:24",
		cjry: "impn",
		gjsbdy: 0,
		gzbh: "b9e9e1e8f4584f1b80dd91bb59cc10fd",
		jssj: "24:00",
		kssj: "00:00",
		xgrq: "2018-11-15 18:53:24",
		xgry: "impn",
		xh: "d6e11ffc19f347ba88ea6bd24248c0f",
		xqj: 5
	},
	{
		bjjb: 4,
		bz: null,
		cjrq: "2018-11-15 18:53:24",
		cjry: "impn",
		gjsbdy: 0,
		gzbh: "b9e9e1e8f4584f1b80dd91bb59cc10fd",
		jssj: "24:00",
		kssj: "00:00",
		xgrq: "2018-11-15 18:53:24",
		xgry: "impn",
		xh: "dce34c70112847c8ad2ba0d4b2b37ef5",
		xqj: 4
	},
	{
		bjjb: 4,
		bz: null,
		cjrq: "2018-11-15 18:53:24",
		cjry: "impn",
		gjsbdy: 0,
		gzbh: "b9e9e1e8f4584f1b80dd91bb59cc10fd",
		jssj: "24:00",
		kssj: "00:00",
		xgrq: "2018-11-15 18:53:24",
		xgry: "impn",
		xh: "e0fc898e77f34551af3e5d4b1f90d869",
		xqj: 6
	}
	];*/
	SetAlarmLevel();
	SetAlarmLevel.initGrid();
	SetAlarmLevel.initMouseEvent();
	if(group){
		SetAlarmLevel.setAlarmGroup(group);
	}
};


//表格初始化
SetAlarmLevel.initGrid=function(){
	var head = SetAlarmLevel.title.split(","),headHtml = [];
	for(var i = 0;i<head.length;i++){
		headHtml.push("<span class='block-colunm'>"+ head[i] +"</span>")
	}
	headHtml = headHtml.join("");
	var toolbar = [{
        text:'<span class="round" style="background:#2196f3;"></span>提示',
        handler:function(){
        	SetAlarmLevel.currentColor = "#2196f3";
        }
    },{
        text:'<span class="round" style="background:#ffeb3b;"></span>一般',
        handler:function(){
        	SetAlarmLevel.currentColor = "#ffeb3b";
        }
    },{
        text:'<span class="round" style="background:#f9b55b;"></span>次要',
        handler:function(){
        	SetAlarmLevel.currentColor = "#f9b55b";
        }
    },{
        text:'<span class="round" style="background:#ff2222;"></span>重要',
        handler:function(){
        	SetAlarmLevel.currentColor = "#ff2222";
        }
    },{
        text:'24小时报警',
        iconCls:'icon-config',
        handler:function(){SetAlarmLevel.setAllColor();}
    },{
        text:'手动清除',
        iconCls:'icon-clear',
        handler:function(){
        	SetAlarmLevel.currentColor = "#eeeeee";
        }
    },{
        text:'全部清除',
        iconCls:'icon-clear',
        handler:function(){
        	SetAlarmLevel.clearAllColor("#eeeeee");
        }
    },{
            text:'确定',
            iconCls:'icon-clear',
            handler:function(){
            	console.log("SetAlarmLevel.getValueGroup()",SetAlarmLevel.getValueGroup());
            }
    }];

	$('#tableGrid').datagrid({
		toolbar : toolbar,
		data:SetAlarmLevel.data,
		pagination:false,
		columns : [[
		    {field: 'A',width:60,
		    	formatter:function(value,row,index){
		    		return SetAlarmLevel.week[index];
		    	}
		    },
			{field:'time',title:headHtml,hidden:false,width:500,
		    	formatter:function(value,row,index){
		    		var blockHtml = [];
		    		blockHtml.push("<div id='blockGroup"+ index +"' class='blockGroup'>");
		    		for(var i = 0;i<SetAlarmLevel.opt.colunm;i++){
		    			blockHtml.push("<div id="+ ((index + 1)*1000 + (i + 1))+ "  class='blockCell'></div>");
		    		}
		    		blockHtml.push("</div>");
		    		return blockHtml.join("");
		    	}
		    },
		]],
		onClickRow: function (rowIndex, rowData) {
            $(this).datagrid('unselectRow', rowIndex);
        }
	});
};



//鼠标事件
SetAlarmLevel.initMouseEvent=function(){
	var flag = 0,startId = null,nowId;
	$(".datagrid-cell-c1-time .blockCell").each(function(){
		$(this).mousedown(function(){
			flag = 1,startId = $(this).attr("id");
			$(this).css("background-color",SetAlarmLevel.currentColor);
		});
		$(this).mouseover(function(){
			if(flag == 1){
				nowId = $(this).attr("id");
				if (nowId > startId) {
					for (var i = 1; i <= nowId - startId; i++) {
						var index = startId*1 + i*1;
						$("#"+index).css("background-color",SetAlarmLevel.currentColor);
					}
				}else if (nowId < startId) {
					for (var i = 1; i <= startId - nowId; i++) {
						var index = startId*1 - i*1;
						$("#"+index).css("background-color",SetAlarmLevel.currentColor);
					}
				}
				startId = nowId;
			}
		});
		$(this).mouseup(function(){
			flag = 0;
			startId = null;
		});
	});
	$(".blockGroup").each(function(){
		$(this).mouseleave(function(){
			flag = 0;
			startId = null;
		});
	});
};


//24小时报警
SetAlarmLevel.setAllColor=function(){
	$(".datagrid-cell-c1-time .blockCell").each(function(){
		$(this).css("background-color",SetAlarmLevel.currentColor);
	});
};

//清除格子颜色
SetAlarmLevel.clearAllColor=function(color){
	$(".datagrid-cell-c1-time .blockCell").each(function(){
		$(this).css("background-color",color);
	});
};

//获得所有格子颜色
SetAlarmLevel.getColorGroup=function(){
	var colorGroup = [];
	for(var a = 0;a<7;a++){
		colorGroup[a] = [];
		$("#blockGroup"+ a +">.blockCell").each(function(index){
			colorGroup[a][index] = SetAlarmLevel.colorRGB2Hex($(this).css("background-color"));
		});
	}
	return colorGroup;
};

//rgb转十六进制
SetAlarmLevel.colorRGB2Hex=function(color) {
    var rgb = color.split(',');
    var r = parseInt(rgb[0].split('(')[1]);
    var g = parseInt(rgb[1]);
    var b = parseInt(rgb[2].split(')')[0]);
    var hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
    return hex;
};

//返回model
SetAlarmLevel.createModel=function(startNum,endNum,weekday,timeColor){
	var model ={};
	model.kssj = (startNum >= 10) ? (startNum+":00") : ("0"+startNum+":00");
	model.jssj = (endNum >= 10) ? (endNum+":00") : ("0"+endNum+":00");
	model.xqj = weekday + 1;
	$.each(SetAlarmLevel.alarmLevel,function(i,o){
		if(timeColor == o) model.bjjb = parseInt(i);
	});
	return model;
};

//获取数据显示
SetAlarmLevel.setAlarmGroup=function(group){
	for(var i = 0;i<group.length;i++){
		var cell = group[i],cellColor = cell.recordColor;
		$.each(SetAlarmLevel.alarmLevel,function(i,o){
			if(cell.bjjb == i) cellColor = o;
		});
		$("#blockGroup"+ (cell.xqj-1) +">.blockCell").each(function(index){
			if(index >= parseInt(cell.kssj) && index < parseInt(cell.jssj)){
				$("#"+(cell.xqj*1000 + (index + 1))).css("background-color",cellColor);
			}
		});
	}
};


//获取已设置告警数据
SetAlarmLevel.getValueGroup=function(){
	var colorInfo = SetAlarmLevel.getColorGroup(),alarmGroup = [],rs = [],weekday,startNum,endNum,timeColor = null;
	for(var row = 0;row<7;row++){
		weekday = row;
		$("#blockGroup"+ row +">.blockCell").each(function(index){
			if(timeColor == null && colorInfo[row][index] != SetAlarmLevel.backgroundColor){
				timeColor = colorInfo[row][index];
				startNum = index;
				if(index == (SetAlarmLevel.opt.colunm - 1)){
					endNum = index+1;
					rs.push(SetAlarmLevel.createModel(startNum,endNum,weekday,timeColor));
					timeColor = null,startNum = null,endNum = null;
				}
			}else if(timeColor != null && colorInfo[row][index] == SetAlarmLevel.backgroundColor){
				endNum = index;
				rs.push(SetAlarmLevel.createModel(startNum,endNum,weekday,timeColor));
				timeColor = null,startNum = null,endNum = null;
			}else if(timeColor != null && timeColor != colorInfo[row][index]){
				endNum = index;
				rs.push(SetAlarmLevel.createModel(startNum,endNum,weekday,timeColor));
				timeColor = colorInfo[row][index],startNum = index,endNum = null;
				if(index == (SetAlarmLevel.opt.colunm - 1)){
					endNum = index+1;
					rs.push(SetAlarmLevel.createModel(startNum,endNum,weekday,timeColor));
					timeColor = null,startNum = null,endNum = null;
				}
			}else if(timeColor != null && index == (SetAlarmLevel.opt.colunm - 1)){
				endNum = index+1;	
				rs.push(SetAlarmLevel.createModel(startNum,endNum,weekday,timeColor));
				timeColor = null,startNum = null,endNum = null;
			}
		});
	}
	return rs;
};
