var multipleCdCount =0;
var multipleCdCur =0;
var multipleCdSelRows ={};
var pageList=[''];
var pageCur = 0; 
/*
	当日委托展示字段
*/
var drcjList =	[
				[
				{checkbox:true,width:12},
				{field:'entrust_no',title:'委托号',width:60,halign:'center',align:'center',sortable:true},
				{field:'curr_date',title:'委托日期',width:70,halign:'center',align:'center',sortable:true},
				{field:'curr_time',title:'委托时间',width:70,halign:'center',align:'center',sortable:true,formatter:formatTimeStr,sorter:sortNumber},
				{field:'op_entrust_way',title:'委托方式',width:80,halign:'center',align:'center',sortable:true,formatter:formatWtfs},
				{field:'exchange_type',title:'交易类别',width:70,halign:'center',align:'center',sortable:true,formatter:formatJylb},
				{field:'stock_code',title:'证券代码',width:70,halign:'center',align:'center',sortable:true},
				{field:'stock_name',title:'证券名称',width:70,halign:'center',align:'center',sortable:true},
				{field:'entrust_bs',title:'买卖方向',width:60,halign:'center',align:'center',sortable:true,formatter:formatMmfx},
				{field:'entrust_type',title:'委托类别',width:70,halign:'center',align:'center',sortable:true,formatter:formatWtlb},
				{field:'entrust_prop',title:'委托属性',width:180,halign:'center',align:'center',sortable:true,formatter:formatWtsx},
				{field:'entrust_price',title:'委托价格',width:70,halign:'center',align:'center',sortable:true,sorter:sortNumber},
				{field:'entrust_amount',title:'委托数量',width:100,halign:'center',align:'center',sortable:true,sorter:sortNumber},
				{field:'business_amount',title:'成交数量',width:100,halign:'center',align:'center',sortable:true,sorter:sortNumber},
				{field:'entrust_status',title:'委托状态',width:100,halign:'center',align:'center',sortable:true,formatter:formatWtzt}
				]
			    ];

$(function(){
	gf_dzjy_share.createGrid('dzjy_cd_grid','tdx_center',null,drcjList,false);
	$("#dzjy_cd_grid").datagrid("options").onDblClickRow = onDblClickRow;
	$("#dzjy_cd_grid").datagrid("options").alignColList =['entrust_prop'];
	$("#dzjy_cd_grid").datagrid("options").text_align ='left';
	$('#dzjy_cd_grid').datagrid("options").onLoadSuccess = gridColAlign;
	get_wtcx('');
})

/*
	委托查询
*/

function get_wtcx(sPos){
	var query_kind = (document.getElementById("mycheck").checked)?1:0;
	var wtcfg ={
		"funcid":"332704",
		'en_entrust_prop':'AFC,AFW',
		"query_kind":query_kind,
		"position_str":"",
		'@POS':sPos,
		"funcname":'ret_wtcx'
	};
	gf_dzjy_share.setIX(wtcfg);
}

function ret_wtcx(_fromid,_funid,_flagtype,data){
	if (_funid == '5010:SIMPLE.332704'){
		var data = FormatResult(data, 1);
		if (data.ErrorCode == 0){
			if($.inArray(data.POS, pageList)==-1){
				pageList.push(data.POS);
			}
			gridUpdateData('dzjy_cd_grid',1,data);
		} 
		else{
			$.messager.alert("提示",data.ErrorInfo,"error");
		}
	} 
}

/*
	撤单操作
*/
function cdOperation(selrows) {
	if (selrows.length == 0) {
		$.messager.alert('提示',"请至少选择一笔需要撤单的委托!",'info');
		return;
	} else if (selrows.length == 1) {
		singleCdOperation(selrows[0]);
	} else if (selrows.length > 1) {
		multiplCdOperation(selrows);
	}

}

/*
	单笔撤单请求
*/
function get_cd(wt_no){
	var wtcfg ={
		"funcid":"332703",
		'entrust_no':wt_no,
		"funcname":'ret_cd'
	};
	gf_dzjy_share.setIX(wtcfg);
}

function ret_cd(_fromid, _funid, _flagtype, data) {
	if (_funid == '5010:SIMPLE.332703') {
		try {
			var data = FormatResult(data, 1);
			if (data.ErrorCode == 0) {
				$.messager.alert("提示", "撤单成功！","info");
				init_wtcx();
			} else {
				alert(data.ErrorInfo.substr(5));
			}

		} catch (e) {
			var temp_list =data.split('"');
			var tslabel = temp_list[1].substr(5)
			alert(tslabel);
		}
	}
}

/*
	单笔撤单
*/
function singleCdOperation(selrow){
	var wtbh = selrow.entrust_no;
	if (wtbh==nullGridStr) return;
	var s = '';
	s += '<div class="confirm_txt">';
	s += '<p>操作类别:撤单</p>';
	s += '<p>委托编号:' + selrow.entrust_no + '</p>';
	s += '<p>证券代码:' + selrow.stock_code + '</p>';
	s += '<p>证券名称:' + selrow.stock_name + '</p>';
	s += '<p>买卖方向:' + formatMmfx(selrow.entrust_bs) + '</p>';
	s += '</div>';
	$.messager.defaults = {
		ok: "是",
		cancel: "否"
	};
	$.messager.confirm('委托确认', s, function(r) {
		if (r) {
			get_cd(selrow.entrust_no);
		}
	});
}

/*
	多笔撤单请求
*/
function get_multipleCd(){
	var wtcfg ={
		"funcid":"332703",
		'entrust_no':multipleCdSelRows[multipleCdCur].entrust_no,
		"funcname":'ret_multipleCd'
	};
	gf_dzjy_share.setIX(wtcfg);
}

function ret_multipleCd(_fromid, _funid, _flagtype, data) {
	if (_funid == '5010:SIMPLE.332703') {
		multipleCdCur+=1;
		try {
			var data = FormatResult(data, 1);
			if (data.ErrorCode == 0) {
				multipleCdCount +=1;
			} else {
			}

		} catch (e) {
		}
		if(multipleCdCur>(multipleCdSelRows.length-1)){
			multipleCdSelRows ={};
			multipleCdCur =0;
			$.messager.alert("提示", "共有"+multipleCdCount+"笔撤单成功！","info");
			multipleCdCount =0;
			init_wtcx();
		}
		else{
			get_multipleCd();
		}
	}
}

/*
	多笔撤单
*/
function multiplCdOperation(selrows) {
	var s = '';
	s += '<p>共有' + selrows.length + '笔委托需要撤单，请确认！</p>';
	$.messager.defaults = {
		ok: "是",
		cancel: "否"
	};
	$.messager.confirm('提示', s, function(r) {
			if (r) {
				multipleCdSelRows =selrows;
				get_multipleCd();
		}
	});
}

/*
	撤单按钮响应
*/
function cdClick(){
	var selrows=$("#dzjy_cd_grid").datagrid("getChecked");
	cdOperation(selrows);
}

/*
	双击表格撤单
*/
function onDblClickRow(rowIndex, rowData) {
	singleCdOperation(rowData);
}

/*
	委托查询初始化
*/
function init_wtcx(){
	pageList=[''];
	pageCur = 0; 
	get_wtcx('');
}

function setCheckSta(){
	init_wtcx();
}

function refreshClick(){
	init_wtcx();
}

/*
	下一页按钮响应
*/
function setNextPage() {
	if (pageCur < (pageList.length - 1)) {
		pageCur += 1;
		get_wtcx(pageList[pageCur]);
	}
}

/*
	上一页按钮响应
*/
function setPrePage() {
	if (pageCur > 0) {
		pageCur -= 1;
		get_wtcx(pageList[pageCur]);
	}
}