
function cbxRoomNumberOnSelect(r) {
    var opts = $(this).combobox("options");
    if (r[opts.textField] == lang.CheckAll) {
        $("#cbtGroup").combotree("enable");
        $("#cbxRoomStatus").combobox("enable");
        $("#cbxRoomType").combobox("enable");
        $("#dgLights").datagrid('loadData', { total: 0, rows: [] });
    } else {
        $("#cbtGroup").combotree("disable");
        $("#cbxRoomStatus").combobox("disable");
        $("#cbxRoomType").combobox("disable").combobox("select", r.RoomTypeID);
        $("#dgLights").datagrid({ url: '/HostModal/LoadHostModals/', queryParams: { HostID: r.ID} });
    }
    cbxRoomTypeOnSelect(null);
}

function cbxRoomTypeOnSelect(r) {
    $("#cbxScene1").combobox("clear").combobox("reload", "/RoomTypeScene/LoadAll/?roomTypeID=" +
    $("#cbxRoomType").combobox("getValue") + "&hostID=" + $("#cbxRoomNumber").combobox("getValue"));
}

function getFilterParams(takeInverse) {
    return {
        hostID: $("#cbxRoomNumber").combobox("getValue"),
        groupID: $("#cbtGroup").combotree("getValue"),
        roomStatusID: $("#cbxRoomStatus").combobox("getValue"),
        roomTypeID: $("#cbxRoomType").combobox("getValue"),
        takeInverse: takeInverse == 1 ? true : false
    };
}

//下发场景
function send1(takeInverse) {
	if ($("#form1").form("enableValidation").form("validate")) {
		var formData = $("#form1").serializeJson();
		var params = getFilterParams(takeInverse);
		$.extend(params, { jsonScene: JSON.stringify(formData) });
		$.tools.post(params, "/LightControl/SendScene/");
	}
}

//模式定时开关设置
function send2() {
	if ($("#form2").form("enableValidation").form("validate")) {
		var formData = $("#form2").serializeJson();
		var params = getFilterParams();
		$.extend(formData, { TimeFlag: (formData.TimeFlag === "on") });
		$.extend(params, { jsonScene: JSON.stringify(formData) });
		$.tools.post(params, "/LightControl/SendScene/");
	}
}

function timeFormatter(val) {
	var hour = Math.floor(val / 60);
	var minute = val % 60;
	return hour + lang.Hour + minute + lang.Minute;
}
//回路控制操作
function operationFormatter(val, row, index) {
    if (row.ModalAddress == '004000008' || row.Type == 'AirDetect') return '';
    var html = '';
    switch (row.Type) {
        case "Dimmer":
        case "Traic":
        case "PWMDimmer":
        case "PWMExpand":
        case "PBLED": //亮度
        case "LVout": //弱电输出
        case "PB20":
        case "PB20_LD":
        case "PB20_LS":
            html = "<select id='brightness' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            for (var i = 0; i <= 100; i += 1) {
                html += "<option value='" + i + "'" + (i == row.Brightness ? ' selected=selected' : '') + ">" + i + "%</option>";
            }
            html += "</select>";
            break;
        case "Strip": //调色
            if (row.ModalAddress.substr(6, 3) == '000') return '';
            //亮度
            html = "<select id='brightness' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            for (var i = 0; i <= 100; i += 1) {
                html += "<option value='" + i + "'" + (i == row.Brightness ? ' selected=selected' : '') + ">" + i + "%</option>";
            }
            html += "</select>";
            break;
        case "AirConditioner"://空调
            //开关
            html = "<select id='status' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='1'" + (1 == row.Status ? ' selected=selected' : '') + ">1-" + lang.Open + "</option>";
            html += "<option value='2'" + (1 != row.Status ? ' selected=selected' : '') + ">2-" + lang.Close + "</option>";
            html += "</select>&nbsp;";
            //设置温度
            html += "<select id='temperature' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.Temperature + "</option>";
            for (var i = 16; i <= 32; i += 1) {
                html += "<option value='" + i + "'" + (i == row.SettingTemp ? ' selected=selected' : '') + ">" + i + "℃</option>";
            }
            html += "</select>&nbsp;";
            //模式
            html += "<select id='mode' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.Mode + "</option>";
            html += "<option value='1'" + (1 == row.Mode ? ' selected=selected' : '') + ">1-" + lang.Refrigeration + "</option>";
            html += "<option value='2'" + (2 == row.Mode ? ' selected=selected' : '') + ">2-" + lang.Heating + "</option>";
            html += "<option value='3'" + (3 == row.Mode ? ' selected=selected' : '') + ">3-" + lang.BlowingIn + "</option>";
            html += "</select>&nbsp;";
            //风速
            html += "<select id='fanSpeed' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.FSpeed + "</option>";
            html += "<option value='0'" + (0 == row.FanSpeed ? ' selected=selected' : '') + ">0-" + lang.Automatic + "</option>";
            html += "<option value='1'" + (1 == row.FanSpeed ? ' selected=selected' : '') + ">1-" + lang.LowWindSpeed + "</option>";
            html += "<option value='2'" + (2 == row.FanSpeed ? ' selected=selected' : '') + ">2-" + lang.MediumWindSpeed + "</option>";
            html += "<option value='3'" + (3 == row.FanSpeed ? ' selected=selected' : '') + ">3-" + lang.HighWindSpeed + "</option>";
            html += "</select>&nbsp;";
            //阀门
            html += "<select id='valve' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.ValveName + "</option>";
            html += "<option value='1'" + (1 == row.Valve ? ' selected=selected' : '') + ">1-" + lang.ValveOpen + "</option>";
            html += "<option value='2'" + (2 == row.Valve ? ' selected=selected' : '') + ">2-" + lang.ValveClose + "</option>";
            html += "</select>&nbsp;";
            //当前温度
            html += lang.RoomTemp + ":" + row.CurrentTemp + "℃";
            break;
        case "Music": //背景音乐
            //开关
            html = "<select id='status' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='1'" + (1 == row.Status ? ' selected=selected' : '') + ">1-" + lang.Open + "</option>";
            html += "<option value='2'" + (1 != row.Status ? ' selected=selected' : '') + ">2-" + lang.Close + "</option>";
            html += "</select>&nbsp;";
            //设置音量
            html += "<select id='brightness' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.Music_Volume + "</option>";
            for (var i = 0; i <= 10; i += 1) {
                html += "<option value='" + i + "'" + (i == row.Brightness ? ' selected=selected' : '') + ">" + i + "</option>";
            }
            html += "</select>&nbsp;";
            //模式
            html += "<select id='mode' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.Option + "</option>";
            html += "<option value='1'" + (1 == row.Mode ? ' selected=selected' : '') + ">1-" + lang.Music_Play + "</option>";
            html += "<option value='2'" + (2 == row.Mode ? ' selected=selected' : '') + ">2-" + lang.Music_Pause + "</option>";
            html += "<option value='3'" + (3 == row.Mode ? ' selected=selected' : '') + ">3-" + lang.Music_PRE + "</option>";
            html += "<option value='4'" + (4 == row.Mode ? ' selected=selected' : '') + ">4-" + lang.Music_NEX + "</option>";
            html += "<option value='5'" + (5 == row.Mode ? ' selected=selected' : '') + ">5-" + lang.Volume_Up + "</option>";
            html += "<option value='6'" + (6 == row.Mode ? ' selected=selected' : '') + ">6-" + lang.Volume_Down + "</option>";
            html += "</select>&nbsp;";
            break;
        case "TV": //电视
            html = "<select id='tv_status' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.Status + "</option>";
            html += "<option value='1'>1-" + lang.Open + "</option>";
            html += "<option value='2'>2-" + lang.Close + "</option>";
            html += "</select>&nbsp;";
            //选项功能
            html += "<select id='mode' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.Option + "</option>";
            html += "<option value='1'" + (1 == row.Mode ? ' selected=selected' : '') + ">1-" + lang.Mute + "</option>";
            html += "<option value='2'" + (2 == row.Mode ? ' selected=selected' : '') + ">2-" + lang.Suspend_Mode + "</option>";
            html += "<option value='3'" + (3 == row.Mode ? ' selected=selected' : '') + ">3-" + lang.Volume_Up + "</option>";
            html += "<option value='4'" + (4 == row.Mode ? ' selected=selected' : '') + ">4-" + lang.Volume_Down + "</option>";
            html += "<option value='5'" + (5 == row.Mode ? ' selected=selected' : '') + ">5-" + lang.Channel_Add + "</option>";
            html += "<option value='6'" + (6 == row.Mode ? ' selected=selected' : '') + ">6-" + lang.Channel_Reduce + "</option>";
            html += "<option value='7'" + (7 == row.Mode ? ' selected=selected' : '') + ">7-" + lang.Up + "</option>";
            html += "<option value='8'" + (8 == row.Mode ? ' selected=selected' : '') + ">8-" + lang.Down + "</option>";
            html += "<option value='9'" + (9 == row.Mode ? ' selected=selected' : '') + ">9-" + lang.Left + "</option>";
            html += "<option value='10'" + (10 == row.Mode ? ' selected=selected' : '') + ">10-" + lang.Right + "</option>";
            html += "<option value='11'" + (11 == row.Mode ? ' selected=selected' : '') + ">11-" + lang.OK + "</option>";
            html += "<option value='12'" + (12 == row.Mode ? ' selected=selected' : '') + ">12-" + lang.Homepage + "</option>";
            html += "<option value='13'" + (13 == row.Mode ? ' selected=selected' : '') + ">13-" + lang.Menu + "</option>";
            html += "<option value='14'" + (14 == row.Mode ? ' selected=selected' : '') + ">14-" + lang.Message_Source + "</option>";
            html += "<option value='15'" + (15 == row.Mode ? ' selected=selected' : '') + ">15-" + lang.LookBack + "</option>";
            html += "</select>&nbsp;";
            //频道
            html += "<select id='tv_channel' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.Channel + "</option>";
            var channels = eval('(' + row.Channels + ')');
            for (var i = 0; i < channels.length; i++) {
                html += "<option value='" + channels[i].Code + "'" + (channels[i].Code == row.Valve ? ' selected=selected' : '') + ">" + channels[i].Code + "-" + channels[i].Name + "</option>";
            }
            html += "</select>&nbsp;";
            break;
        case "Curtain"://窗帘
            html = "<select id='status' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.Curtain + "</option>";
            html += "<option value='1'" + (1 == row.Status ? ' selected=selected' : '') + ">1-" + lang.Open + "</option>";
            html += "<option value='2'" + (2 == row.Status ? ' selected=selected' : '') + ">2-" + lang.Close + "</option>";
            html += "<option value='6'" + (6 == row.Status ? ' selected=selected' : '') + ">6-" + lang.Stop + "</option>";
            html += "</select>";
            break;
        case "FloorHot"://地暖
            //开关
            html = "<select id='status' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='1'" + (1 == row.Status ? ' selected=selected' : '') + ">1-" + lang.Open + "</option>";
            html += "<option value='2'" + (1 != row.Status ? ' selected=selected' : '') + ">2-" + lang.Close + "</option>";
            html += "</select>&nbsp;";
            //设置温度
            html += "<select id='temperature' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.Temperature + "</option>";
            for (var i = 5; i <= 45; i += 1) {
                html += "<option value='" + i + "'" + (i == row.SettingTemp ? ' selected=selected' : '') + ">" + i + "℃</option>";
            }
            html += "</select>&nbsp;";
            //模式
            html += "<select id='mode' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.Mode + "</option>";
            html += "<option value='1'" + (1 == row.Mode ? ' selected=selected' : '') + ">1-" + lang.Manually + "</option>";
            html += "<option value='2'" + (2 == row.Mode ? ' selected=selected' : '') + ">2-" + lang.Automatic + "</option>";
            html += "<option value='3'" + (3 == row.Mode ? ' selected=selected' : '') + ">3-" + lang.Timing + "</option>";
            html += "</select>&nbsp;";
            //阀门
            html += "<select id='valve' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.ValveName + "</option>";
            html += "<option value='1'" + (1 == row.Valve ? ' selected=selected' : '') + ">1-" + lang.ValveOpen + "</option>";
            html += "<option value='2'" + (2 == row.Valve ? ' selected=selected' : '') + ">2-" + lang.ValveClose + "</option>";
            html += "</select>&nbsp;";
            //当前温度
            html += lang.RoomTemp + ":" + row.CurrentTemp + "℃";
            break;
        case "ColorTemp": //色温
            html = "<select id='status' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='1'" + (1 == row.Status ? ' selected=selected' : '') + ">1-" + lang.Open + "</option>";
            html += "<option value='2'" + (1 != row.Status ? ' selected=selected' : '') + ">2-" + lang.Close + "</option>";
            html += "</select>&nbsp;";
            //亮度
            html += "<select id='brightness' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            for (var i = 0; i <= 100; i += 1) {
                html += "<option value='" + i + "'" + (i == row.Brightness ? ' selected=selected' : '') + ">" + i + "%</option>";
            }
            html += "</select>&nbsp;";
            //色温
            html += "<select id='color_temperature' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='-1'>" + lang.ColorTemp + "</option>";
            for (var i = 0; i <= 100; i += 1) {
                html += "<option value='" + i + "'" + (i == row.SettingTemp ? ' selected=selected' : '') + ">" + i + "</option>";
            }
            html += "</select>";
            break;
        default:
            html = "<select id='status' onchange='cbxOperation(this," + index + "," + row.HostID + "," + row.ModalID + ")'>";
            html += "<option value='1'" + (1 == row.Status ? ' selected=selected' : '') + ">1-" + lang.Open + "</option>";
            html += "<option value='2'" + (1 != row.Status ? ' selected=selected' : '') + ">2-" + lang.Close + "</option>";
            html += "</select>";
            break;
    }
    return html;
}
//操作控制
function cbxOperation(obj, rowIndex, hostId, modalId) {
    var selectValue = $(obj).val();
    if (selectValue == "-1") return;
    var row = $('#dgLights').datagrid('getSelected');
    console.log(row);
    var status = 1;
    var brightness = 100;
    var param = 'hostId=' + hostId + '&modalId=' + modalId;
    var whitchone = $(obj)[0].id;
    console.log(whitchone);
    switch (whitchone) {
        case "status":
            status = selectValue;
            brightness = selectValue == 1 ? 100 : 0;
            param += '&status=' + selectValue + '&brightness=' + brightness + '&temperature=' + row.SettingTemp + '&fanSpeed=' + row.FanSpeed + '&mode=' + row.Mode + '&valve=' + row.Valve;
            break;
        case "brightness":
            status = selectValue == 0 ? 2 : 1;
            brightness = selectValue;
            param += '&status=' + status + '&brightness=' + brightness + '&temperature=' + row.SettingTemp + '&fanSpeed=' + row.FanSpeed + '&mode=' + row.Mode + '&valve=' + row.Valve;
            break;
        case "temperature":
            param += '&status=1&brightness=100&temperature=' + selectValue + '&fanSpeed=' + row.FanSpeed + '&mode=' + row.Mode + '&valve=' + row.Valve;
            console.log(param);
            break;
        case "fanSpeed":
            param += '&status=1&brightness=100&temperature=' + row.SettingTemp + '&fanSpeed=' + selectValue + '&mode=' + row.Mode + '&valve=' + row.Valve;
            break;
        case "mode":
            param += '&status=1&brightness=100&temperature=' + row.SettingTemp + '&fanSpeed=' + row.FanSpeed + '&mode=' + selectValue + '&valve=' + row.Valve;
            break;
        case "valve":
            param += '&status=1&brightness=100&temperature=' + row.SettingTemp + '&fanSpeed=' + row.FanSpeed + '&mode=' + row.Mode + '&valve=' + selectValue;
            break;
        case "tv_status"://电视开关时，内容传0
            status = selectValue;
            brightness = selectValue == 1 ? 100 : 0;
            param += '&status=' + selectValue + '&brightness=' + brightness + '&temperature=' + row.SettingTemp + '&fanSpeed=' + row.FanSpeed + '&mode=0&valve=' + row.Valve;
            break;
        case "tv_channel": //频道
            param += '&status=4&brightness=100&temperature=' + row.SettingTemp + '&fanSpeed=' + row.FanSpeed + '&mode=' + row.Mode + '&valve=' + selectValue;
            break;
        case "color_temperature": //色溫
            param += '&status=3&brightness=' + row.Brightness + '&temperature=' + selectValue + '&fanSpeed=' + row.FanSpeed + '&mode=' + row.Mode + '&valve=' + row.Valve;
            break;
    }
    console.log(param);
    $.tools.post(param, '/LightControl/SetLight/', function (r) {
        if (r) {
            //$('#dgLights').datagrid('updateRow', { index: rowIndex, row: { Status: status, Brightness: brightness} });
            //$('#dgLights').datagrid('reload');
        }
    });
}
function dgLightsLoadFilter(r) {
	if (r.IsSuccess == undefined) {
		return r;
	} else {
		$.tools.alert(r.Message);
		return { total: 0, rows: [] };
	}
}
function cbtGroupOnLoadSuccess(node, data) {
	if (data != null && data.length > 0) {
		$('#cbtGroup').combotree('setValue', data[0].id);
	}
}