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');
	$(function(){
		hotKeyDistrict();
		initializehDate();
	});
	
	$("#exportBtn").click(function(){
		if($("#districtHokKey").val()==""){
			$("#districtId").attr("value","");
			$("#districtNameSearch").attr("value","");
		}
		if($("#startDate").val()==""||$("#endDate").val()==""){
			$.msg.alert(i18ns("Entity.report.selectStatisticalTime"));
		}else{			
			var url = $('#exportUrl').val().trim();
			$('#searchForm').attr("action",url);
			$('#searchForm').submit();
		}
	});
	
	$("#resetBtn").click(function(){
		$("#searchForm :input").val("");
		$("#startDate").datepicker("destroy");
		$("#endDate").datepicker("destroy");
		initializehDate();
	});

	$("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 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 initializehDate(){	
		$("#startDate").datepicker({
			
		});
		$("#endDate").datepicker({
			beforeShow:function(date){
				var startDate = $("#startDate").val();
				if(startDate!=""){
					var date=$("#startDate").datepicker("getDate");
					 $("#endDate").datepicker("option","minDate",new Date(date.getTime()));
				}
				}
		});
	}
});