define(function(require){
	var $ = require('jquery');
	require('grid')($);
	require('uriTemplate')($);
	require('dialog')($);
	var IFA = require('util');
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/cim-grid.css');   
	$(function(){
		//alert(i18n["Validation.personprofile.phone"]);
		searchResult();
		$("#search").bind("click",searchResult);
		$("#reset").bind("click",reset);
		$("#searchForm :input").bind("keydown",function(e){
			var e = e  ? e : window.event;
			if(e.keyCode==13){
				searchResult();
			}
		});
	});
	//根据条件查询
	function searchResult(){
		var url=$("#queryUrl").val().trim();
		$("#searchResult").grid({
			url:url,
			root:"companyList",
			pageTheme:'Theme1',
			isREST :false,
			method:"POST",
			defaultParams:IFA.convertQueryStringToJson($("#searchForm").serialize()),
			fields:[
			        {name:"CDP#",id:"cdpNo",style:{width:"5%"}},
			        {name:i18n['Company.Profile.Name'],id:"companyName",style:{width:"13%"},render:function(data){
				    var name='';
	                var dataName=data.companyName;
	                if(dataName.length>10){
	             	    name=dataName.substring(0,10)+"...";
	                }else{
	                    name=dataName;
	                    }
	 			    return $('<a href="#" title='+dataName+' class="bb"/>').html(name).click(function(){
	  				        showDetail(data.companyId);
	     			    });
				       }},
				    {name:i18n['Company.Profile.HotKey'],style:{width:"8%"},id:"companyHotkey"},
			        {name:i18n['Company.Profile.Address'],id:"companyAddress",style:{width:"15%"}},
			        {name:i18n['Company.Profile.Industry'],id:"industry",style:{width:"10%"}},
			        {name:i18n['Company.Profile.Area'],id:"district",style:{width:"10%"}},
			        {name:i18n['Company.Profile.Contact1st'],id:"companyContact1st",style:{width:"7%"}},
			        {name:i18n['Company.Profile.Contact1stTel'],id:"contact1stPhone",style:{width:"10%"}},
			        {name:i18n['Company.Profile.Contact1stEmail'],id:"contact1stEmail",style:{width:"10%"}},
			        {name:i18n['Entity.Operation'],style:{width:"12%"},operation:[
			                                                  {text:i18n['Entity.Operation.Detail'],display:true,action:function(data){showDetail(data.companyId);}},
			                                                  {text:i18n['Entity.page.edit'],display:true,action:function(data){editCompany(data.companyId);}}]}
			        ] 
			});
	}
	//重置查询条件
	function reset(){
		$("#searchForm :input").val("");
	}
	//编辑公司档案
	function editCompany(companyId){
		var urlTemplate=$("#editUrl").val().trim();
		var urlString=new $.Template(urlTemplate);
		var urlParam = {"id":companyId};
		var url=urlString.expand(urlParam);
		location.href=url;
	}
	//显示详情
	function showDetail(companyId){
		var urlTemplate=$("#detailUrl").val().trim();
		var urlString=new $.Template(urlTemplate);
		var urlParam = {"id":companyId};
		var url=urlString.expand(urlParam);
		location.href=url;
	}
});