define(function(require){
	var $ = require('jquery');
	require('uriTemplate')($);
	require('dialog')($);
	require('datepicker')($);
	require('grid')($);
	require('../../../js/seaJsExtends/autoComplete.js')($);
	var IFA = require('util');
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/cim-grid.css');
	require('../../../styles/cim/autoComplete.css');
	var contextPath = $("#contextPath").val();
	$(function(){
		hotKeyCompany();
		hotKeyDistrict();
		initializehDate();
		hotKeyProduct();
		//销售人员热键
		hotKeySalerId();
	});
	
	$("#exportBtn").click(function(){
		if($("#cdpNo").val()==""){
			$("#companyId").attr("value","");
			$("#companyNameSearch").attr("value","");
		}
		if($("#districtHokKey").val()==""){
			$("#districtId").attr("value","");
			$("#districtNameSearch").attr("value","");
		}
		if($("#serviceHokKey").val()==""){
			$("#serviceId").attr("value","");
			$("#serviceDescription").attr("value","");
		}
		var urlExportCheck = $("#exportCheckUrl").val().trim();
		$.post(urlExportCheck,$('#searchForm').serialize(),function(response){
			if(response.message>5000){
				$.msg.alert("数据超过5000条，请缩少查询范围!");
			}else if(response.message>0&&response.message<=5000){
				var url = $('#exportUrl').val().trim();
				$('#searchForm').attr("action",url);
				$('#searchForm').submit();
			}else{
				$.msg.alert("数据为空");
			}
		});
	});
	
	$("#countBtn").click(function(){
		if($("#cdpNo").val()==""){
			$("#companyId").attr("value","");
			$("#companyNameSearch").attr("value","");
		}
		if($("#districtHokKey").val()==""){
			$("#districtId").attr("value","");
			$("#districtNameSearch").attr("value","");
		}
		if($("#serviceHokKey").val()==""){
			$("#serviceId").attr("value","");
			$("#serviceDescription").attr("value","");
		}
		var urlExportCheck = $("#exportCheckUrl").val().trim();
		$.post(urlExportCheck,$('#searchForm').serialize(),function(response){
			if(response.message>5000){
				$("#countResult").attr("style","color:red;text-align: center;");
				$("#countResult").text(response.message+" 个订单,数据量太多，请缩小查询范围！");
			}else if(response.message>0&&response.message<=5000){
				$("#countResult").attr("style","color:green;text-align: center;");
				$("#countResult").text(response.message+" 个订单,可以导出！");
			}else{
				$("#countResult").attr("style","color:red;text-align: center;");
				$("#countResult").text(response.message+" 个订单!");
			}
		});
	});
	$("#resetBtn").click(function(){
		$("#searchForm :input").val("");
		$("#startDate").datepicker("destroy");
		$("#endDate").datepicker("destroy");
		$("#startCreationDate").datepicker("destroy");
		$("#endCreationDate").datepicker("destroy");
		$("select[name='resStatus'] option:first-child").attr("selected","selected");
		initializehDate();
	});
	$("select[name='resStatus']").change(function(){
		if($(this).val()==100){
			$("#startDate").val("");
			$("#endDate").val("");
			$("#startDate").datepicker("destroy");
			$("#endDate").datepicker("destroy");
			$("#startDate").attr("disabled","disabled");
			$("#endDate").attr("disabled","disabled");
			 
		}else{
			$("#startDate").removeAttr("disabled");
			$("#endDate").removeAttr("disabled");
			initializehDate();
		}
	});
	
	//销售人员ID热键
	function hotKeySalerId(){
		$.ajax({
			type:"get",
			url:$("#salerIDUrl").val(),
			success:function(response){
			var acdata=response.salerID;
			$("#salerName").autocomplete(acdata,
				      {
						caption: "&nbsp;&nbsp;"+i18n["JS.salerIdHotKey.caption"],
						findCaption: "&nbsp;,"+i18n["JS.districtHotKey.findCaption"],
						noFindCaption: "&nbsp;&nbsp;"+i18n["JS.salerIdHotKey.notFindCaption"]+":&nbsp; ",
						width: 200,
						max: 5,
						formatItem: function(row, i, max) {
				             var title="";
							return '<span title="{2}"><span class="ac_code" >{0}</span><span class="ac_Name" >{1}</span></span>'.patchContent(row.peopleId, row.cnName,title);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1}".patchContent(row.peopleId, row.cnName);
						},
						formatResult: function(row) {
							return "{1}".patchContent(row.peopleId, row.cnName);
							
						}
					}).result(function(event, data, formatted) {
						$("#salerId").val(data.peopleId);
						var id = $("#salerId").val();
					});
		}
		});
	}
	
	
	function initializehDate(){		
		$("#startDate").datepicker({
			
		});
		$("#endDate").datepicker({
		});
		$("#startCreationDate").datepicker({
			
		});
		$("#endCreationDate").datepicker({
			
		});
        $("#pickupDateStart").datepicker({
			
		});
		$("#pickupDateEnd").datepicker({
		});
		$("#dropoffDateStart").datepicker({
			
		});
		$("#dropoffDateEnd").datepicker({
			
		});
	}
	$("select[name='cityId']").change(function(){
		if($(this).val()==""){
			$("#parentCode").attr("value",null);
		}else{			
			$("#parentCode").attr("value",$(this).val());
			//$("#city").attr("value",$(this).text());
		}
		hotKeyDistrictForCity();
	});
	
	//公司hotkey
    function hotKeyCompany(){
	  	$.ajax({
			type:"get",
			url:$("#companyUrl").val(),
			success:function(response){
			$("#cdpNo").autocomplete(response.company,
				      {
						caption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.caption"],
						findCaption: "&nbsp;,"+i18n["JS.districtHotKey.findCaption"],
						noFindCaption: "&nbsp;&nbsp;"+i18n["JS.companyHotKey.noFindCaption"]+":&nbsp; ",
						max: 5,
						width: 200,
						formatItem: function(row, i, max) {
				             var cpdNo="cdpNo:"+row.cdpNo+"&#13;";
				             var companyName=i18n["JS.companyHotKey.coName"]+":"+row.companyName+"&#13;";
				             var companyHotkey=i18n["JS.companyHotKey"]+":"+row.companyHotkey+"&#13;";
				             var companyAddress=i18n["JS.companyHotKey.coAddr"]+":"+row.companyAddress+"&#13;";
				             var companyContact1st=i18n["JS.companyHotKey.coContact"]+":"+row.companyContact1st+"&#13;";
				             var title=cpdNo+companyName+companyHotkey+companyAddress+companyContact1st;
				             var companyName=row.companyName+"("+row.companyHotkey+")";
							return '<span title="{2}"><span class="ac_code" >{0}</span><span class="ac_Name">{1}</span></span>'.patchContent(row.cdpNo,companyName,title);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1} {2}".patchContent(row.cdpNo,row.companyHotkey,row.companyName);
						},
						formatResult: function(row) {
							return "{0}".patchContent(row.cdpNo);
						}
					}).result(function(event, data, formatted) {
						$("#companyId").val(data.companyId);
						$("#companyNameSearch").val(data.companyName);
					});
		}
		});
	}
    
    //门店HotKey
	function hotKeyDistrict(){
	  	$.ajax({
			type:"get",
			url:$("#districtUrl").val(),
			success:function(response){
			var acdata=response.district;
			$("#districtHokKey").autocomplete(acdata,
				      {
						caption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.caption"],
						findCaption: "&nbsp;,"+i18n["JS.districtHotKey.findCaption"],
						noFindCaption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.noFindCaption"]+":&nbsp; ",
						width: 200,
						max: 5,
						formatItem: function(row, i, max) {
							return '<span class="ac_code">{0}</span><span class="ac_Name">{1}</span>'.patchContent(row.districtHotkey, row.districtName);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1}".patchContent(row.districtHotkey, row.districtName);
						},
						formatResult: function(row) {
							return "{0}".patchContent(row.districtName);
						}
					}).result(function(event, data, formatted) {
						//$(this).attr("code", data.districtId);
						$("#districtId").val(data.districtId);
						$("#districtNameSearch").val(data.districtName);
						//$("#reservationForm").validate().element("#districtId");
					});
		}
		});
	}
	
	//二级门店Hotkey
	function hotKeyDistrictForCity(){
		var parentCode = $("#parentCode").val();
    	var hiddenUrl = $("#districtForCityUrl").val();
    	var showUrl = new $.Template(hiddenUrl);
		var param = {"parentCode":parentCode};
		var url = showUrl.expand(param);
	  	$.ajax({
			type:"get",
			url:url,
			success:function(response){
			var acdata=response.district;
			$("#districtHokKey").autocomplete(acdata,
				      {
						caption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.caption"],
						findCaption: "&nbsp;,"+i18n["JS.districtHotKey.findCaption"],
						noFindCaption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.noFindCaption"]+":&nbsp; ",
						width: 200,
						max: 5,
						formatItem: function(row, i, max) {
							return '<span class="ac_code">{0}</span><span class="ac_Name">{1}</span>'.patchContent(row.districtHotkey, row.districtName);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1}".patchContent(row.districtHotkey, row.districtName);
						},
						formatResult: function(row) {
							return "{0}".patchContent(row.districtName);
						}
					}).result(function(event, data, formatted) {
						//$(this).attr("code", data.districtId);
						$("#districtId").val(data.districtId);
						$("#districtNameSearch").val(data.districtName);
						//$("#reservationForm").validate().element("#districtId");
					}).flushCache();
		}
		});
	}
	
	//产品代码热键
	function hotKeyProduct(){
	  	$.ajax({
			type:"get",
			url:$("#hotkeyProduct").val(),
			success:function(response){
			var acdata=response.servicingProduct;
			$("#serviceHokKey").autocomplete(acdata,
				      {
						caption: "&nbsp;&nbsp;"+i18n["JS.districtHotKey.caption"],
						findCaption: "&nbsp;,"+i18n["JS.districtHotKey.findCaption"],
						noFindCaption: "&nbsp;&nbsp;"+i18n["JS.productHotKey.noFindCaption"]+":&nbsp; ",
						width: 200,
						max: 5,
						formatItem: function(row, i, max) {
				             var title="";
				             if(row.isRestrictive == 'Y'){
				            	 var maxService=Math.floor(row.fullUsageHrs/row.avgTransactionActual);
				            	 title=i18n["JS.productHotKey.title0"]+".&#13;"+i18n["JS.productHotKey.title1"]+":"+maxService;
				             }else{
				            	 title=i18n["JS.productHotKey.title2"];
				             }
							return '<span title="{2}"><span class="ac_code" >{0}</span><span class="ac_Name" >{1}</span></span>'.patchContent(row.serviceId, row.serviceDescription,title);
						},
						formatMatch: function(row, i, max) {
							return "{0} {1}".patchContent(row.serviceId, row.serviceDescription);
						},
						formatResult: function(row) {
							return "{0}".patchContent(row.serviceDescription);
						}
					}).result(function(event, data, formatted) {
						$("#serviceId").val(data.serviceId);
						$("#serviceDescription").val(data.serviceDescription);
					});
		}
		});
	}
	
	function hve_display(tId,iId){//显示隐藏程序
		var t_id = tId;//表格ID
		var i_id = iId;//图片ID
		var on_img=contextPath+"/images/btn_arrow_down.gif";//打开时图片
		var off_img=contextPath+"/images/btn_arrow_right.gif";//隐藏时图片
		if (t_id.style.display == "none") {//如果为隐藏状态
			t_id.style.display="";//切换为显示状态
			i_id.src=on_img;//换图
		}
		else{
			t_id.style.display="none";//切换为隐藏状态
			i_id.src=off_img;//换图
		}
		}
	
	$(".tableRes h2").bind("click",function(){
		var tal = $(this).parents(".tableRes").next(".yTable");
		var img = $(this).children();
		console.log(tal);
		hve_display(tal.get(0),img.get(0));
	});
	
	$("#allOpen").bind("click",function(){
		var tal = $("table[name='table']");
		var img = $("img[name='img']");
		var i = 0;
		$.each(tal,function(){
			hve_open(tal.get(i),img.get(i));
			i++;
		});
	});
	
	$("#allClose").bind("click",function(){
		var tal = $("table[name='table']");
		var img = $("img[name='img']");
//		var off_img=contextPath+"/images/btn_arrow_right.gif";//隐藏时图片
		//console.log(tal);
		var i = 0;
		$.each(tal,function(){
			hve_close(tal.get(i),img.get(i));
//			tal.get(i).style.display="none";
//			img.get(i).src=off_img;
			i++;
		});
	});
	function hve_open(tId,iId){//显示隐藏程序
		var t_id = tId;//表格ID
		var i_id = iId;//图片ID
		var on_img=contextPath+"/images/btn_arrow_down.gif";//打开时图片
			t_id.style.display="";//切换为显示状态
			i_id.src=on_img;//换图
		}
	
	function hve_close(tId,iId){//显示隐藏程序
		var t_id = tId;//表格ID
		var i_id = iId;//图片ID
		var off_img=contextPath+"/images/btn_arrow_right.gif";//隐藏时图片
			t_id.style.display="none";//切换为隐藏状态
			i_id.src=off_img;//换图
		}
	$("#checkAll").click(function() {
		 $('input[type="checkbox"]').each(
			 function(){
				 this.checked=true;
			 });
	});
	$("#checkNothing").click(function() {
		 $('input[type="checkbox"]').each(
			 function(){
				 this.checked=false;
			 });
	});
});