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

__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() {
		pageNav.Clear();
		get_orderlist();
	});
	
	//导出
	$('#query_out').bind('click', function() {
		ExprotToExcel("query_out","mygrid","hs_xqzp");	
	});
	
	//初始化查询
	get_orderlist();
}

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

	if(pos==undefined){
		pos="";
	}
	
	var _ix = new IXContent();
	var SelParm = "Select FID_CJRQ,FID_CJSJ,FID_QQLX,FID_QQHYDM,FID_QQHYMC,FID_BDZQLX,FID_MMFX,"
		+"FID_KPBZ,FID_QQBDBQ,FID_SBSJ,FID_CJBS,FID_CJSL,FID_CJJG,FID_JSJ,FID_CJJE From 667112 where FID_KPBZ='E'";
	_ix.Set('@COND',SelParm);
	_ix.Set('@COUNT', '499');
	_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_SORTTYPE', '1');

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

	$("#tdx_center p.empty").removeClass("loading");
	Win_CallTQL('ret_orderlist', '667112', _ix );
}

function ret_orderlist(_fromid,_funid,_flagtype,_json) {
	
	$("#mygrid").datagrid("loaded");
	$("#tdx_center").removeClass("loading");
	if(_funid == '667112'){
		loggerInfo(_json);
		var data = FormatResult(_json,1);
		exceldata = data;
		if (data.ErrorCode == 0) {
			if(data.Num==0 && pageNav.pageIndex==0){
				create_datagrid(data);
				$("#mygrid").datagrid("loadData",{rows:[]});
				$("<p class='empty' style='text-align:center;margin-top:15%;'>暂无记录</p>").appendTo($("#tdx_center .datagrid-body"));
			}
			else{
					create_datagrid(data);
					fill_datagrid(data);				
			}
		} 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_CJSJ',
		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_BDZQLX',
		align: 'center',
		sortable: true,
		width: 80,
		formatter: function(val) {
			return val;
		}
	}, {
		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
	}, {
		title: '申报时间',
		field: 'FID_SBSJ',
		align: 'center',
		sortable: true,
		width: 80
	}, {
		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: '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');
		_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"){
		//写文件内容
		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',10);
			for(var key in exceldata.rows[i]){
				if(tmp==3)				
					str = sjzd_hylx[exceldata.rows[i][key]];
				else if(tmp==5)
					str =sjzd_hs_ccfx[exceldata.rows[i][key]]; 				
				else if(tmp==6)
					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);
}