function cbxRoomTypeOnSelect(r) {
	$('#dgDevice').datagrid('loadData', { total: 0, rows: [] });
	$('#cbxDevice').combogrid({ queryParams: { RoomTypeID: r.ID} });
}
function sceneTypeOnchange(target) {
	if ($(target).val() == 0) { //自定义场景
		enableComstomScene();
	} else if ($(target).val() == 1) { //指令场景
		enableCommandScene();
	}
}
//启用自定义场景
function enableComstomScene() {
	$('#txtGroupAddress').prop('readonly', 'readonly');
	$('#btnAddDevice').linkbutton('enable');
	$('#btnRemoveDevice').linkbutton('enable')
}
function enableCommandScene() {
	$('#txtGroupAddress').removeProp('readonly');
	$('#btnAddDevice').linkbutton('disable');
	$('#btnRemoveDevice').linkbutton('disable');
}

function add() {
	$('#dialog')
		.find('.dlg-content').panel({ height: 400, href: '/RoomTypeScene/Edit/', queryParams: {} }).end()
		.find('.dlg-footer .dlg-btn-save').unbind('click').bind('click', save).end()
		.dialog({
			title:lang.NewLightMode,
			width: 680,
			height: 490
		})
		.dialog('open');

	//enableComstomScene();
}

function edit(row) {
    row = row || $('#dg').datagrid('getSelected');
	$('#dialog')
		.find('.dlg-content').panel({ height: 400, href: '/RoomTypeScene/Edit/', queryParams: { ID: row.ID } }).end()
		.find('.dlg-footer .dlg-btn-save').unbind('click').bind('click', save).end()
		.dialog({
		    title: lang.EditLightMode,
			width: 680,
			height: 490
		})
		.dialog('open');
}

function del() {
	var rows = $('#dg').datagrid('getSelections');
	if (!rows || rows.length == 0) {
		$.tools.alert(lang.PleaseSelectTheData);
		return;
	}

	var param;
	$.each(rows, function (i, n) {
		if (i == 0) {
			param = 'idList=' + n.ID;
		} else {
			param += '&idList=' + n.ID;
		}
	});

	$.tools.delPost(param, '/RoomTypeScene/Delete/', function () {
		$('#dg').datagrid('clearSelections');
		$('#dg').datagrid('reload');
	}, this);
}

//保存灯光模式
function save() {
	if ($('#roomTypeSceneForm').form('enableValidation').form('validate')) {
		if ($.trim($('#txtName').val()) === '') {
		    $.tools.alert(lang.PleaseEnterTheModeName);
			return;
		}
        var jsonRoomTypeScene = $('#roomTypeSceneForm').serializeJson();
        var rows = $('#dgDevice').datagrid('getRows');
        var modals = [];
		for (var i in rows) {
		    modals.push({
		        ID: rows[i].ID,
		        Status: rows[i].Status,
		        Brightness: rows[i].Brightness,
		        CurrentTemp: rows[i].CurrentTemp,
		        Mode: rows[i].Mode,
		        FanSpeed: rows[i].FanSpeed,
		        Valve: rows[i].Valve 
            });
		}
        $.extend(jsonRoomTypeScene, { Modals: modals });
		var params = { jsonData: JSON.stringify(jsonRoomTypeScene) };

		$.tools.post(params, '/RoomTypeScene/Save/', function () {
			$('#dg').datagrid('reload');
			$('#dialog').window('close');
		});
	}
}
function delDevice() {
	var rows = $('#dgDevice').datagrid('getChecked');
	for (var i in rows) {
		var index = $('#dgDevice').datagrid('getRowIndex', rows[i]);
		$('#dgDevice').datagrid('deleteRow', index);
	}
}
