var highLightIndex = -1;
$(function() {
	var O_Panel = $(".todayinfo1");
	var mainlist = $("#mainlist");
	var O_AreaDrop = O_Panel.find("#areaDrop");
	var O_HotelDrop = O_Panel.find("#hotelDrop");
	var O_RoomDrop = O_Panel.find("#roomDrop");
	var O_YearDrop = O_Panel.find("#yearDrop");
	var O_MonthDrop = O_Panel.find("#monthDrop");
	var O_WeekDrop = O_Panel.find("#weekDrop");
	var O_SearchBtn = $("#O_SearchBtn");
	var O_Key = $("#searchKey");
	var O_KeyOffset = O_Key.offset();
	var O_Auto = $("#auto");
	var O_AutoF = $("#autoF");
	O_AutoF.hide();
	O_Auto.hide();//隐藏自动补全框
	var top = O_KeyOffset.top + O_Key.height() + 5;
	var left = O_KeyOffset.left;
	O_Auto.css("border", "1px solid #817F82")
	.css("background-color", "#fff")
	.css("position", "absolute")
	.css("z-index", "1000")
	.css("top", top)
	.css("left", left)
	.css("width", O_Key.width() + 3);
	
	O_AutoF.css("background-color", "#fff")
	.css("position", "absolute")
	.css("z-index", "20");
	
	//点击区域筛选下拉列
	O_AreaDrop.change(function() {
		var areaId = O_AreaDrop.val();
		$.post("?m=admin/hotel.getListByArea", { areaId:areaId, pageSize:0, json:true }, function(data) {
			O_HotelDrop.empty();
			O_HotelDrop.append("<option value=\"\">==酒店==</option>");
			$.each(data, function(id, item) {
				O_HotelDrop.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
			});
			O_Key.val("");
			var hotelId = O_HotelDrop.val();
			O_RoomDrop.empty();
			O_RoomDrop.append("<option value=\"\">==客房==</option>");
			if(hotelId.length > 0) {//选了具体酒店
				$.post("?m=admin/room.getListByHotel", { hotelId:hotelId, pageSize:0, json:true }, function(data) {
					$.each(data, function(id, item) {
						O_RoomDrop.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
					});
				}, "json");
			}
		}, "json");
	});
	//点击酒店筛选下拉列
	O_HotelDrop.change(function() {
		var hotelId = O_HotelDrop.val();
		if(hotelId.length > 0) {//选了具体酒店
			O_Key.val(O_HotelDrop.find("option:selected").text());
			$.post("?m=admin/room.getListByHotel", { hotelId:hotelId, pageSize:0, json:true }, function(data) {
				O_RoomDrop.empty();
				O_RoomDrop.append("<option value=\"\">==客房==</option>");
				$.each(data, function(id, item) {
					O_RoomDrop.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
				});
			}, "json");
		} else {//没选酒店
			O_Key.val("");
			var areaId = "";
			O_AreaDrop.attr("value", areaId);
			$.post("?m=admin/hotel.getListByArea", { areaId:areaId, pageSize:0, json:true }, function(data) {
				O_HotelDrop.empty();
				O_HotelDrop.append("<option value=\"\">==酒店==</option>");
				$.each(data, function(id, item) {
					O_HotelDrop.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
				});
				O_RoomDrop.empty();
				O_RoomDrop.append("<option value=\"\">==客房==</option>");
			}, "json");
		}
	});
	O_YearDrop.change(function() {
		var year = O_YearDrop.val();
		O_MonthDrop.empty();
		O_MonthDrop.append("<option value=\"\">==月==</option>");
		if(year.length > 0) {
			var date = new Date();
			var i = 1;
			if(year == date.getFullYear()) {
				i = date.getMonth() + 1;
			}
			for(; i <= 12; i++) {
				O_MonthDrop.append("<option value=\"" + i + "\">" + i + "月</option>");
			}
		}
	});
	O_SearchBtn.click(function(){
		hotelSearch();
	});
	O_Key.focus();
	O_Key.keyup(function(event) {
		var myEvent = event || window.event;
		var keyCode = myEvent.keyCode;//alert(keyCode);
		var key = O_Key.val().replace(/ /g, '').replace(/'/g, '');
		if(key.length > 0) {
			if(keyCode >= 48 && keyCode <= 57 || keyCode >= 65 && keyCode <= 90 || keyCode == 8 || keyCode == 46 || keyCode == 32) {//alert(keyCode);
				highLightIndex = -1;
				var field = "name";
				getKeyList(field, key);
			} else if (keyCode == 38 || keyCode == 40) {//alert(keyCode);
				//向上/向下箭头
				var nodes = O_Auto.children("div");
				//使未向上移时的当前项底色去掉
				if(highLightIndex != -1) { nodes.eq(highLightIndex).css("background-color", "#fff"); }
				if(keyCode == 38) { //向上
					nodes.css("background-color", "#fff").css("border", "1px solid #fff");
					highLightIndex--;
					if(highLightIndex == -1) { highLightIndex = nodes.length - 1; }
				}
				if(keyCode == 40) { //向下
					nodes.css("background-color", "#fff").css("border", "1px solid #fff");
					highLightIndex++;
					if(highLightIndex == nodes.length) { highLightIndex = 0; }
				}
				//使用当前项高亮
				nodes.eq(highLightIndex).css("background-color", "#eee").css("border", "1px solid #ccc");
			} else if (keyCode == 13) {
				//如果输入的是回车
				//如果下拉框有高亮内容
				if(highLightIndex != -1) {
					O_Key.val(O_Auto.children("div").eq(highLightIndex).html());
					highLightIndex = -1;
					O_Auto.html("");
					O_AutoF.hide();O_Auto.hide();
				} else {
					O_SearchBtn.trigger("click");
				}
			} else {
			}
		} else {
			highLightIndex = -1;
			O_Auto.html("");
			O_AutoF.hide();O_Auto.hide();
			//绑定回车键与点击快捷搜索一致
			if (keyCode == 13) { O_SearchBtn.trigger("click"); }
		}
		
	});
	//搜索失去焦点
	O_Key.blur(function() {
		if(highLightIndex != -1) {
			var nodes = O_Auto.children("div");
			O_Key.val(nodes.eq(highLightIndex).html());
			highLightIndex = -1;
		}
		O_AutoF.hide();
		O_Auto.hide();
		O_SearchBtn.focus();
	});
	//点击搜索关键字
	O_Key.click(function() {
		var field = "name";
		var key = O_Key.val().replace(/ /g, '').replace(/'/g, '');
		getKeyList(field, key);
	});
});
//获取关键字列表
function getKeyList(field, key){
	var O_Auto = $("#auto");
	var O_AutoF = $("#autoF");
	var O_Key = $("#searchKey");
	var O_KeyOffset = O_Key.offset();
	if(key.length > 0) {
		$.post("?m=admin/hotel.simpleSearch", { field:field, key:key, json:true }, function(data) {//alert(data);
			var str = "";
			$.each(data, function(id, item) {
				str += "<div>" + item.name + "</div>";
			});
			if(str.length > 0) {
				O_Auto.html(str);
				var top = O_KeyOffset.top + O_Key.height() + 5;
				var left = O_KeyOffset.left;
				O_Auto.css("top", top)
				.css("left", left);
				
				var width = O_Auto.width();
				var height = O_Auto.height() + 10;
				if($.browser.mozilla) {//修正火狐偏差
					width = width - 2;
					height = height - 4;
				}
				O_AutoF.css("top", top)
				.css("left", left)
				.css("width", width)
				.css("height", height);

				O_Auto.show();
				O_AutoF.show();
				
				var nodes = O_Auto.children("div");
				var width = O_Auto.width() - 2;
				nodes.each(function(i) {
					var node = $(this);
					node.attr("id", i).css("width", width).css("cursor", "pointer").css("overflow", "hidden").css("line-height", "25px").css("height", "25px").css("border", "1px solid #fff");
					//鼠标移上
					node.mouseover(function() {
						nodes.css("background-color", "#fff").css("border", "1px solid #fff");
						highLightIndex = this.id; 
						node.css("background-color", "#eee").css("border", "1px solid #ccc");
					});
					//鼠标离开
					node.mouseout(function() {
						node.css("background-color", "#fff").css("border", "1px solid #fff");
						highLightIndex = -1;
					});
					//鼠标点击
					node.click(function() {
						O_Key.focus();
						O_Key.val(node.html());
						highLightIndex = -1;
						O_Auto.html("");
						O_AutoF.hide();
						O_Auto.hide();
					});
				});
			} else {
				O_Auto.html("");
				O_AutoF.hide();
				O_Auto.hide();
			}
		}, "json");
	} else {
		O_Auto.html("");
		O_AutoF.hide();
		O_Auto.hide();
	}
}
//加载价格
function loadPrice() {
	var mainlist = $("#mainlist");
	var roomId = $("#roomDrop").val();
	var year = $("#yearDrop").val();
	var month = $("#monthDrop").val();
	var week = $("#weekDrop").val();
	if(roomId == "") {
		alert("请选择房型！");
	} else if(year == "") {
		alert("请选择年份！");
	} else {
		var O_GetPriceBtn = $("#O_GetPriceBtn");
		O_GetPriceBtn.html("加载中...");
		$.post("?m=admin/price.getListByRoom", { roomId:roomId, year:year, month:month, week:week }, function(data) {//alert(data);
			mainlist.html(data);
			O_GetPriceBtn.html("加载列表");
		});
	}
}
//酒店搜索
function hotelSearch() {
	var O_Panel = $(".todayinfo1");
	var mainlist = $("#mainlist");
	var O_AreaDrop = O_Panel.find("#areaDrop");
	var O_HotelDrop = O_Panel.find("#hotelDrop");
	var O_RoomDrop = O_Panel.find("#roomDrop");
	var v_field = "name";
	var O_Key = $("#searchKey");
	var v_key = O_Key.val().replace(/ /g, '').replace(/'/g, '');
	var O_SearchBtn = $("#O_SearchBtn");
	O_SearchBtn.html("搜索中...");
	$.post("?m=admin/hotel.getCount", { field:v_field, key:v_key }, function(data) {
		var count = data.all;
		if(count > 1) {
			//2个以上酒店
			$.post("?m=admin/hotel.simpleSearch", { field:v_field, key:v_key, pageSize:0, json:true }, function(data) {//alert(data);
				O_HotelDrop.empty();
				O_HotelDrop.append("<option value=\"\">==酒店==</option>");
				$.each(data, function(id, item) {
					O_HotelDrop.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
				});
				O_SearchBtn.html("搜索酒店");
			}, "json");
		} else if(count == 0) {
			var areaId = "";
			$.post("?m=admin/hotel.getListByArea", { areaId:areaId, pageSize:0, json:true }, function(data) {
				O_HotelDrop.empty();
				O_HotelDrop.append("<option value=\"\">==酒店==</option>");
				$.each(data, function(id, item) {
					O_HotelDrop.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
				});
				O_SearchBtn.html("搜索酒店");
			}, "json");
		} else if(count == 1) {
			$.post("?m=admin/hotel.getInfoByName", { name:v_key }, function(data) {
				if(data != null) {
					var areaId = data.areaId;
					var hotelId = data.id;
					O_AreaDrop.attr("value", areaId);
					$.post("?m=admin/hotel.getListByArea", { areaId:areaId, pageSize:0, json:true }, function(data) {
						O_HotelDrop.empty();
						O_HotelDrop.append("<option value=\"\">==酒店==</option>");
						$.each(data, function(id, item) {
							O_HotelDrop.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
						});
						O_HotelDrop.attr("value", hotelId);
						$.post("?m=admin/room.getListByHotel", { hotelId:hotelId, pageSize:0, json:true }, function(data) {
							O_RoomDrop.empty();
							O_RoomDrop.append("<option value=\"\">==客房==</option>");
							$.each(data, function(id, item) {
								O_RoomDrop.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
							});
						}, "json");
						O_SearchBtn.html("搜索酒店");
					}, "json");
				} else {
					O_Flush.show();
					var areaId = "";
					O_AreaDrop.attr("value", areaId);
					$.post("?m=admin/hotel.getListByArea", { areaId:areaId, pageSize:0, json:true }, function(data) {
						O_HotelDrop.empty();
						O_HotelDrop.append("<option value=\"\">==酒店==</option>");
						$.each(data, function(id, item) {
							O_HotelDrop.append("<option value=\"" + item.id + "\">" + item.name + "</option>");
						});
						O_SearchBtn.html("搜索酒店");
					}, "json");
				}
			}, "json");
		}
	}, "json");
}
//信息更新
function updatePrice(t) {
	var id = t.id.replace(/o_/g,'');
	var O_MarketPrice = $("#O_MarketPrice_" + id);
	var O_CostPrice = $("#O_CostPrice_" + id);
	var O_Price = $("#O_Price_" + id);
	var marketPrice = O_MarketPrice.val();
	var costPrice = O_CostPrice.val();
	var price = O_Price.val();
	$.post("?m=admin/price.updateByRoomHandle", { id:id, marketPrice:marketPrice, costPrice:costPrice, price:price }, function(data) {//alert(data);
		alert(data[1]);
	}, "json");
}
//更新全部
function updateAllPrice() {
	var values = "";
	var O_Rows = $(".pricerow");
	O_Rows.each(function() {
		var obj = $(this);
		var id = obj.attr("id").replace(/O_Row_/g, '');
		var marketPrice = obj.find("#O_MarketPrice_" + id).val();
		var costPrice = obj.find("#O_CostPrice_" + id).val();
		var price = obj.find("#O_Price_" + id).val();
		values += '{"id":"' + id + '","marketPrice":"' + marketPrice + '","costPrice":"' + costPrice + '","price":"' + price + '"},';
	});
	if(values.length > 0) {
		var O_Button = $(".searchbutton");
		O_Button.attr("disabled", true);
		values = "[" + values.substring(0, values.length - 1) + "]";
		$.post("?m=admin/price.updateAllByRoomHandle", { values:values }, function(data) {//alert(data);
			alert(data[1]);
			if(data[0] == "success") {
				loadPrice();
			}
			O_Button.attr("disabled", false);
		}, "json");
	}
}
//删除
function delClick(t) {
	if(confirm("删除所选客房价格，确认?")) { 
		var id = t.id.replace(/o_/g, '');
		$.post("?m=admin/price.delHandle", { id:id }, function(data) {//alert(data);
			if(data[0] == "success") {
				loadPrice();
			} else {
				alert(data[1]);
			}
		}, "json");
		return true;
	}
	return false;
}
//全选删除
function checkDelAll(){
	$(".delAll").attr("checked", $("#o_delall").attr("checked"));
}
//批量删除
function delSelectClick(){
	if(confirm("删除所选客房价格，确认?")) { 
		var O_Button = $(".searchbutton");
		O_Button.attr("disabled", true);
		//遍历得到每个checkbox的value值
        var arrChk = $(":input[class='delAll']:checked");  
        var v_ids = "";  
        for(var i = 0; i < arrChk.length; i++) {
            v_ids += arrChk[i].value;
            if(i < arrChk.length - 1) { v_ids += ","; }
        }
		$.post("?m=admin/price.delSelectHandle", { ids:v_ids }, function(data) {//alert(data);
			loadPrice();
			O_Button.attr("disabled", false);
		});
		return true;
	}
	return false;
}
//满房
function fullClick(t) {
	var id = t.id.replace(/o_full_/g, '');
	var isFull = $("#o_full" + id).attr("checked") ? "0" : "1";
	$.post("?m=admin/price.fullHandle", { id:id, isFull:isFull }, function(data) {
		if(data[0] == "success") {
			loadPrice();
		} else {
			alert(data[1]);
		}
	}, "json");
}
//全选满房
function checkFullAll() {
	$(".fullAll").attr("checked", $("#o_fullall").attr("checked"));
}
//批量满房
function fullSelectClick(){
	if(confirm("设置满房操作，确认?")) { 
		var O_Button = $(".searchbutton");
		O_Button.attr("disabled", true);
		//遍历得到每个checkbox的value值
        var arrChk = $(":input[class='fullAll']");  
        var v_ids = "";  
        for(var i = 0; i < arrChk.length; i++) {
            v_ids += arrChk[i].value + "-";
			if(arrChk[i].checked) { v_ids += "1"; }
			else { v_ids += "0"; }
            if(i < arrChk.length - 1) { v_ids += ","; }
        }
		$.post("?m=admin/price.fullSelectHandle", { ids:v_ids }, function(data) {//alert(data);
			loadPrice();
			O_Button.attr("disabled", false);
		});
		return true;
	}
	return false;
}
//批量录入
function addAllPrice() {
	var O_Button = $(".searchbutton");
	O_Button.attr("disabled", true);
	var O_Flush = $("#O_Flush");
	O_Flush.show();
	var mainlist = $("#mainlist");
	var roomId = $("#roomDrop").val();
	var year = $("#yearDrop").val();
	var month = $("#monthDrop").val();
	var week = $("#weekDrop").val();
	$.post("?m=admin/price.addByDictAndRoomHandle", { roomId:roomId, year:year, month:month, week:week }, function(data) {//alert(data);
		alert(data[1]);
		if(data[0] == "success") {
			loadPrice();
		}
		O_Button.attr("disabled", false);
		O_Flush.hide();
	}, "json");
}
//覆盖更新
function updateByDict() {
	var O_Button = $(".searchbutton");
	O_Button.attr("disabled", true);
	var O_Flush = $("#O_Flush");
	O_Flush.show();
	var mainlist = $("#mainlist");
	var roomId = $("#roomDrop").val();
	var year = $("#yearDrop").val();
	var month = $("#monthDrop").val();
	var week = $("#weekDrop").val();
	$.post("?m=admin/price.updateByDictAndRoomHandle", { roomId:roomId, year:year, month:month, week:week }, function(data) {//alert(data);
		alert(data[1]);
		if(data[0] == "success") {
			loadPrice();
		}
		O_Button.attr("disabled", false);
		O_Flush.hide();
	}, "json");
}
//加载数据字典
function loadPriceDict() {
	$("#divUpdate h1 span").html("客房价格字典更新");
	var O_Content = $("#contentUpdate");
	O_Content.html('<div style="width:580px;height:200px;_height:300px;font-size:14px;text-align:center;"><div style="margin-top:120px;margin-bottom:15px;">正在加载...</div><img src="theme/admin/images/loading.gif"></div>');
	setUpdate();
	var roomId = $("#roomDrop").val();
	$.post("?m=admin/room.dict", { id:roomId }, function(data) {//alert(data);
		O_Content.html(data);
		setUpdate();
		$("#O_SumbitUpdate").attr("disabled", false).unbind("click").click(function() { updateAllDict(); });
	});
	//启动浏览模式
	openUpdate();
}
//高亮行
function highLightRow(id) {
	$(".row").removeClass("on");
	$("#O_Row_" + id).addClass("on");
}
//获取窗体区域
function getRange() {//得到屏幕的大小
    var top = document.body.scrollTop;
    var left = document.body.scrollLeft;
    var height = document.body.clientHeight;
    var width = document.body.clientWidth;
    if (top == 0 && left == 0 && height == 0 && width == 0) {
        top = document.documentElement.scrollTop;
        left = document.documentElement.scrollLeft;
        height = document.documentElement.clientHeight;
        width = document.documentElement.clientWidth;
    }
    return { top: top, left: left, height: height, width: width };
}
//更新
function updateDict(obj) {
	var id = obj.id.replace(/o_/g, '');
	var row = $("#O_DictRow_" + id);
	var marketPrice = row.find("#O_MarketPrice_" + id).val();
	var costPrice = row.find("#O_CostPrice_" + id).val();
	var price = row.find("#O_Price_" + id).val();
	var week = row.find("#O_Week_" + id).text();
	var roomId = $("#O_RoomId").val();
	$.post("?m=admin/price.updateDictByRoomHandle", { roomId:roomId, week:week, marketPrice:marketPrice, costPrice:costPrice, price:price }, function(data) {//alert(data);
		alert(data[1]);
	}, "json");
}
//批量更新
function updateAllDict() {
	var roomId = $("#O_RoomId").val();
	var values = "";
	var O_Rows = $(".dictrow");
	O_Rows.each(function() {
		var obj = $(this);
		var id = obj.attr("id").replace(/O_DictRow_/g, '');
		var marketPrice = obj.find("#O_MarketPrice_" + id).val();
		var costPrice = obj.find("#O_CostPrice_" + id).val();
		var price = obj.find("#O_Price_" + id).val();
		var week = obj.find("#O_Week_" + id).text();
		values += '{"marketPrice":"' + marketPrice + '","costPrice":"' + costPrice + '","price":"' + price + '","week":"' + week + '"},';
	});
	if(values.length > 0) {
		var O_Submit = $("#O_SumbitUpdate");
		O_Submit.attr("disabled", true);
		values = '{"roomId":"' + roomId + '","price":[' + values.substring(0, values.length - 1) + ']}';
		$.post("?m=admin/price.updateAllDictByRoomHandle", { values:values }, function(data) {//alert(data);
			alert(data[1]);
			if(data[0] == "success") {
				closeUpdate();
			}
			O_Submit.attr("disabled", false);
		}, "json");
	}
}