define(function(require){
	var $ = require('jquery');
	require('grid')($);
	require('uriTemplate')($);
	require('dialog')($);
	require('globalization'); 
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	require('../../../styles/cim/cim-grid.css');
	var qs = require('queryString');
	var IFA = require('util'); 
	
	$(function(){
		query();
		$("#query").click(function(){query();}); // 查询
		$("#reset").click(function(){reset();}); // 重置
	});
	
	function query(){
		var queryUrl = $("#queryUrl").val();
		var paymentStatus = $("#paymentStatus").val();
		$("#alipayList").grid({
			url:queryUrl,
			root:"alipayList",
			pageTheme:'Theme1',
			method:"POST",
			isREST:false,
			defaultParams:IFA.convertQueryStringToJson($("#alipayForm").serialize()),
			fields:[
			        {id:"resId",name:i18n["Entity.alipay.resId"],render:function(data){
		 			    return $('<a href="#" title='+data.resId+' class="bb"/>').html(data.resId).click(function(){
		 			    	showDetail(data.resId);
		     			});
		 			    
			        }},
			        {id:"enName",name:i18n["Entity.alipay.enName"],render:function(data){
			        	return data.en1stName+' '+data.enLstName;
			        }},
			        {id:"mobileTelephone",name:i18n["Entity.alipay.mobileTelephone"]},
			        {id:"paymentAmount",name:i18n["Entity.alipay.paymentAmount"],render:function(data){
			        	return Globalize.format(data.paymentAmount,'n');
			        }},
			        
			        {id:"subject",name:i18n["Entity.alipay.subject"]},
			        {id:"tradeDesc",name:i18n["Entity.alipay.tradeDesc"]},
			        //{id:"paymentType",name:i18n["Entity.alipay.paymentType"],
			        //	 options:{'0':i18n["Entity.alipay.paymentType.immediatePayment"],
			       // 	          '1':i18n["Entity.alipay.paymentType.securedTransaction"]}
			        //},
			       // {id:"buyerEmail",name:i18n["Entity.alipay.buyerEmail"]},
			       // {id:"modifierName",name:i18n["Entity.modifier"]},
			        {id:"modificationDate",name:i18n["Entity.modificationDate"],type:"time"}
			]
		});
	 };
	 
	 // 显示订单详细信息
	  function showDetail(resId){
		    var hiddenUrl = $('#showUrl').val();
			var urlStr = new $.Template(hiddenUrl);
			var urlParam = {"resId":resId};
			var url = urlStr.expand(urlParam);
		    location.replace(url);
	  }

	  // 完成支付,修改支付账单状态
	  function pay(resId){
		  $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Page.title.changeStatus"],{
				buttons:IFA.generateJson(i18n["Entity.page.YES"],function() {
			         $(this).dialog("close");
			         update(resId);
				  },i18n["Entity.page.No"],function() {$(this).dialog("close");}),
			width:350,height:150
	 		}
	 	);
	  }
	  
	  // 更新状态
	  function update(resId){
		  $.msg.alert(i18n["Page.title.requireInfo"],$("#forRemarks"),
				  function(){},{width:"450px",buttons:IFA.generateJson(i18n["Entity.Operation.ok"],function(){
		        	   var remarks = $("#remarks").val().trim();
			           if(remarks!=null && remarks!=""){
			        	   var updateUrl = new $.Template($("#updateUrl").val());
				           var params={"resId":resId,"paymentStatus":'TRADE_FINISHED',"remarks":remarks};
				    	   $.ajax({
				    		   url:updateUrl.expand(params)+".json",
				    		   dataType:'json',
				    		   success:function(response,ioArgs){
				    		     $(this).dialog("close");
								 $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.pay.succeeded"],{
										buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
											        $(this).dialog("close");
											        query();
											        }),
									   width:350,height:150
							 	 });// $.msg.alert()结束
				    	        },
				    	        error:function(response,ioArgs,errorThrown){
				    	        	 $(this).dialog("close");
									 $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Entity.pay.failed"],{
										 buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
										        $(this).dialog("close");
										        query();
										        }),
										   width:350,height:150
								 	 });// $.msg.alert()结束
				    	        }
				    	   });
			    	       $(this).dialog("close");
			           }else{
			        	   $(this).dialog("close");
			        	   $.msg.alert(i18n["Page.title.dialogshow.title"], i18n["Page.title.noremarks"],{
			        		   buttons: IFA.generateJson(i18n["Entity.Operation.ok"], function() {
							        $(this).dialog("close");
							        }),
				   			  width:350,height:150
				   	 		  }
				   	 	  );
			           }
              },i18n["Entity.Operation.cancel"],function(){$(this).dialog("close");})});
	  }
	  function reset(){
		  $("#resId").val("");
		  $("#cnName").val("");
		  $("#en1stName").val("");
		  $("#enLstName").val("");
		  $("#paymentAmount").val("");
	  };
});