var mygrid=false;
var pageNav=null;
var SelParm = null;
var ExprotToExcelBtn=null;
var grid="";
var exceldata="";
var _temp_list=null;

__DEBUG = false;

function loggerInfo(msg){
	if(__DEBUG) {
		alert(msg);
	}
}

function PageInit(){

	pageNav=new PageNav("pagebox");
	pageNav.Request=function(pos){
		get_orderlist(pos);
	}
	pageNav.LoadData=function(data){
		$("#mygrid").datagrid("loadData",data);
	}

	//查询
	$('#query_btn').bind('click', function() {
		_temp_list=null;
		pageNav.Clear();
		get_orderlist();
	});
	
	//导出
	$('#query_out').bind('click', function() {
		ExprotToExcel("query_out","mygrid","hs_xqzp_his");	
	});
	
	//初始化时间
	var now=new Date();
	var eday=formatDate(now,'-');
	var sday=formatDate(addDate('d',-2,now),'-');
	$('#sdate').val(sday);
	$('#edate').val(eday);//默认查询最近3天的数据
	
	//初始化查询
	get_orderlist();
}

//查询请求
function get_orderlist(pos) {

	if(pos==undefined){
		pos="";
	}

	var _time1 = $('#sdate').val();
	var _time2 = $('#edate').val();

	_time1 = _time1.replace(/-/g, "");
	_time2 = _time2.replace(/-/g, "");

	if(_time2*1-_time1*1<0){
		alert("结束日期不能小于开始日期");
		return;
	}

	var _ix = new IXContent();
	
	var SelParm = "Select FID_CJRQ,FID_CJSJ,FID_QQLX,FID_QQHYDM,FID_QQHYMC,FID_MMFX,"
		+"FID_KPBZ,FID_QQBDBQ,FID_SBSJ,FID_CJBS,FID_CJSL,FID_CJJG,FID_JSJ,FID_CJJE From 667110 where FID_KPBZ='E' and FID_QQHYDM!='#'";
	_ix.Set('@COND',SelParm);
	_ix.Set('@COUNT', '50');
	_ix.Set('@POS', pos);
	_ix.Set('@MAC', '##MAC##');
	_ix.Set('NODE', '');
	_ix.Set('WTFS', '32');
	_ix.Set('FBDM', User.yyb);
	_ix.Set('@MAC', '##MAC##');
	_ix.Set('DestFBDM', User.yyb);
	_ix.Set('FID_KHH', User.khh);
	_ix.Set('FID_KSRQ', _time1);
	_ix.Set('FID_JSRQ', _time2);
	_ix.Set('FID_SORTTYPE', '1');

	if(mygrid)
		$("#mygrid").datagrid("loading");
	else
		$("#tdx_center").addClass("loading");
	$("#tdx_center p.empty").remove();

	Win_CallTQL('ret_orderlist', '667110', _ix );
}

function ret_orderlist(_fromid,_funid,_flagtype,_json) {

	$("#mygrid").datagrid("loaded");
	$("#tdx_center").removeClass("loading");
	
	if(_funid == '667110'){
		loggerInfo(_json);
		var data = FormatResult(_json,1);
		exceldata = data;
		if (data.ErrorCode == 0) {
				for(var i=0;i<data.rows.length;i++){
					
					if(data.rows[i]['FID_QQLX']=='C'){
						data.rows[i]['newcol']=data.rows[i]['FID_CJSL']*(-10000);
					}
					else{
						data.rows[i]['newcol']=data.rows[i]['FID_CJSL']*(10000);
						data.rows[i]['FID_CJJE']=data.rows[i]['FID_CJJE']*(-10000);
					}
					
				}			
			if(_temp_list==null){
				_temp_list=data;

			}
			else
			{
				
				for(var i=0;i<data.rows.length;i++)
					_temp_list.rows.push(data.rows[i]);
					
			}
			if(data["POS"]!="")
				get_orderlist(data["POS"]);
			if(data["POS"]=="" && _temp_list!=null)
			{
				if(data.Num==0 && pageNav.pageIndex==0){
					create_datagrid(_temp_list);
					$("#mygrid").datagrid("loadData",{rows:[]});
					$("<p class='empty' style='text-align:center;margin-top:15%;'>暂无记录</p>").appendTo($("#tdx_center .datagrid-body"));
				}
					else{
						exceldata=_temp_list;
						_temp_list.total=_temp_list.rows.length-1;
						create_datagrid(_temp_list);
						fill_datagrid(_temp_list);
					}
			}	
		} else {
			if(data.ErrorInfo.indexOf("没有查询数据")==-1){
				alert(data.ErrorInfo);
			}
			else{
				if(mygrid)
					$("#mygrid").datagrid("loadData",{rows:[]});
				else
					{
						create_datagrid(data);
						$("<p class='empty' style='text-align:center;margin-top:15%;'>暂无记录</p>").appendTo($("#tdx_center .datagrid-body"));
				}
			}
		}
	}
}

/*
* 创建数据表格函数
*/
function create_datagrid(data)
{	

	var config=data.config;

	var cols=[];
	var keys=config.FieldKey;
	for(var key in keys){
		var _title=config.FieldName[key];
		var _align=config.Align[key];
		var _width=config.Width[key];
		var _hide=config.HideFlag[key];
		if(_hide==1){continue;}
		cols[cols.length]={field:key,title:(_title==undefined?key:_title),halign:'center',align:(_align==undefined?'right':_align),sortable:true,width:(_width==undefined?80:_width)};
	}
	//请在这里配置返回的字段
	cols= [{
			title: '成交日期',
			field: 'FID_CJRQ',
			align: 'center',
			sortable: true,
			width: 80
		},{
			title: '期权类型',
			field: 'FID_QQLX',
			align: 'center',
			sortable: true,
			width: 60,
			formatter: function(val) {
				return sjzd_dd_qqlx[val];
			}
		}, {
			title: '合约代码',
			field: 'FID_QQHYDM',
			align: 'center',
			sortable: true,
			width: 80
		}, {
			title: '合约名称',
			field: 'FID_QQHYMC',
			align: 'center',
			sortable: true,
			width: 150
		},  {
			title: '买卖方向',
			field: 'FID_MMFX',
			align: 'center',
			sortable: true,
			width: 80,
			formatter: function(val) {
				return sjzd_dd_mmbz[val];
			}
		}, {
			title: '开平标志',
			field: 'FID_KPBZ',
			align: 'center',
			sortable: true,
			width: 80,
			formatter: function(val) {
				return sjzd_dd_kpbz[val];
			}
		}, {
			title: '备兑标签',
			field: 'FID_QQBDBQ',
			align: 'center',
			sortable: true,
			width: 80,
			formatter: function(val) {
				return sjzd_hs_bdbz[val];
			}
		}, {
			title: '成交笔数',
			field: 'FID_CJBS',
			align: 'center',
			sortable: true,
			width: 80
		}, {
			title: '成交数量',
			field: 'FID_CJSL',
			align: 'center',
			sortable: true,
			width: 80			
		}, {
			title: '成交价格',
			field: 'FID_CJJG',
			align: 'center',
			sortable: true,
			width: 80
		}, {
			title: '结算价',
			field: 'FID_JSJ',
			align: 'center',
			sortable: true,
			width: 80
		},{
			title: '标的券收付数量',
			field: 'newcol',
			align: 'center',
			sortable: true,
			width: 80
		}, 
		{
			title: '成交金额',
			field: 'FID_CJJE',
			align: 'center',
			sortable: true,
			width: 80
		}];

	$("<div id='mygrid'></div>").appendTo($("#tdx_center"));

	$("#mygrid").datagrid({
		fit:true,
		scrollbarSize:0,
		border:false,
		rownumbers:false,
		singleSelect:true,
		remoteSort: false,
		loadMsg:'正在请求数据，请稍后...',
		columns:[cols],
  	    onLoadSuccess: function(data){
  	    	$("#f1").html(data.rows.length);	
    	},
		onBeforeLoad:function ()
		{
			return false;
		}
	});
	mygrid=true;
}

//填充数据表格
function fill_datagrid(data){
	try{
		$("#mygrid").datagrid("loadData",data);
		pageNav.SetData(data);
	}
	catch(e)
	{
		alert("name: " + e.name + "message: " + e.message + "lineNumber: " + e.lineNumber + "fileName: " + e.fileName + "stack: " + e.stack); 
	}
}

/*
* 本函数适用于使用jquery和easyui的datagrid数据表格的数据导出
* 框架需支持dxzq_savetoexcel_open系列接口
* btn_id:  导出按钮的id
* grid_id: 数据表格datagrid的id
* type:    导出类型，接口需要，用于扩展
* 
* 导出范例：ExprotToExcel("export_btn","mygrid","wtcx,cjcx,zjls");
*/
function ExprotToExcel(btn_id,grid_id,type){

	// if(ExprotToExcelBtn!=null && ExprotToExcelBtn!=undefined){
	// 	alert("另一个导出正在进行，请稍后操作！");
	// 	return;
	// }

	if(exceldata.Num == 0)
	{
		alert("没有导出数据!");
		return;
	}

	try{
		//写文件头
		ExprotToExcelBtn=$("#"+btn_id);
		//ExprotToExcelBtn.attr("disabled",true);//禁用导出按钮

		grid=$("#"+grid_id);
		var fields='';
		var cols=grid.datagrid("getColumnFields");
		var _ix = new IXContent();
		var j = 0;
		_ix.Set('type','hs_xqzp_his');
		_ix.Set('jsfunc','RET_ExprotToExcel');
		_ix.Set('fieldnum',cols.length);
		for(var i=0;i<cols.length;i++){
			j = i+1;
			var strtmp='';
			if(grid.datagrid("options").columns[0][i].title=="合约代码" || grid.datagrid("options").columns[0][i].title=="证券代码")
				strtmp = '1'+'|'+grid.datagrid("options").columns[0][i].title;
			else
				strtmp = '0'+'|'+grid.datagrid("options").columns[0][i].title;
			_ix.Set('field'+j,strtmp);
		}
		Win_CallTQL('ZHCX_CTPMorePlugin','zhcx_savetoexcel_open',_ix);
		return;
	}
	catch(e){
		eval("Ret_ExprotToExcel"+"('UNKONW ERROR')");
		return;
	}
}

//示范回调函数
function RET_ExprotToExcel(msg){

	ExprotToExcelBtn.removeAttr("disabled");//这两句话不要删了吧，用于恢复导出按钮
	ExprotToExcelBtn=null;

	//下面是你可以修改的代码
	if(msg=="hs_xqzp_his"){
		//写文件内容
		var rows=grid.datagrid("getData").rows;
		for(var i=0;i<rows.length;i++){
			var tmp= 1;
			var str="";
			var _ix = new IXContent();
			_ix.Set('datanum',11);
			for(var key in exceldata.rows[i]){
				if(tmp==2)				
					str = sjzd_hs_ccfx[exceldata.rows[i][key]];
				else if(tmp==5)
					str =sjzd_hylx[exceldata.rows[i][key]]; 				
				else if(tmp==7)
					str=parseFloat(exceldata.rows[i][key]).toFixed(4);
				else
					str=exceldata.rows[i][key];
					
				if(typeof(str)=="undefined")
					str="";
				_ix.Set('data'+tmp,str);			
				tmp++;
			}
			Win_CallTQL("ZHCX_CTPMorePlugin","zhcx_savetoexcel_write",_ix);	
			}
			//关闭文件
			var _ixNone = new IXContent();
			_ixNone.Set('opennow',0);
			Win_CallTQL("ZHCX_CTPMorePlugin","zhcx_savetoexcel_close",_ixNone);
			alert("Excel文件导出成功！");
	}
	else
		alert("Excel文件导出失败！错误:"+msg);
}