// TestGTradeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestGTrade.h"
#include "TestGTradeDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestGTradeDlg dialog

CTestGTradeDlg::CTestGTradeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestGTradeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestGTradeDlg)
	m_strProfilePath = _T("");
	m_strFutAccount = _T("");
	m_strOptAccount = _T("");
	m_strStkAccount = _T("");
	m_strFutExchange = _T("TW");
	m_strFutMonth = _T("200804");
	m_strOptMonth = _T("200804");
	m_strOrderNo = _T("");
	m_strOptPrice = _T("");
	m_strFutPrice = _T("");
	m_strStkPrice = _T("");
	m_strFutQty = _T("1");
	m_strOptQty = _T("1");
	m_strStkQty = _T("1000");
	m_strReduceQty = _T("0");
	m_strFutStopPrice = _T("0");
	m_strOptStrikePrice = _T("");
	m_strFutSymbol = _T("FITX");
	m_strOptSymbol = _T("TXO");
	m_strStkSymbol = _T("2303");
	m_strFutType = _T("F");
	m_strOptType = _T("O");
	m_strStkType = _T("S");
	m_strFutUserKey = _T("FUT");
	m_strOptUserKey = _T("OPT");
	m_strStkUserKey = _T("STK");
	m_strReduceType = _T("R");
	m_strFutBuySell = _T("B");
	m_strFutBuySell2 = _T("B");
	m_strOptBuySell = _T("B");
	m_strOptBuySell2 = _T("B");
	m_strStkBuySell = _T("B");
	m_strOptCallPut = _T("C");
	m_strOptCallPut2 = _T("C");
	m_strFutDayTrade = _T("N");
	m_strOptDayTrade = _T("N");
	m_strStkMarketType = _T("T");
	m_strFutOpenClose = _T("A");
	m_strOptOpenClose = _T("A");
	m_strFutOrderType = _T("R");
	m_strOptOrderType = _T("R");
	m_strStkOrderType = _T("N");
	m_strFutPriceFlag = _T("L");
	m_strOptPriceFlag = _T("L");
	m_strStkPriceFlag = _T("L");
	m_strStkStockType = _T("N");
	m_strOptStopPrice = _T("0");
	m_strLicensePath = _T("");
	m_strFutMonth2 = _T("");
	m_strOptMonth2 = _T("");
	m_strOptStrikePrice2 = _T("");
	m_strFutSymbol2 = _T("");
	m_strOptSymbol2 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestGTradeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestGTradeDlg)
	DDX_Control(pDX, IDC_COMBO_STATEMENT_TYPE, m_comboStatementType);
	DDX_Control(pDX, IDC_COMBO_SESSION, m_comboSession);
	DDX_Control(pDX, IDC_LIST_ORDER_STATUS, m_listOrderStatus);
	DDX_Control(pDX, IDC_LIST_FILL_ORDER, m_listFillOrder);
	DDX_Text(pDX, IDC_EDIT_PROFILE_PATH, m_strProfilePath);
	DDX_Text(pDX, IDC_EDIT_ACCOUNT_FUT, m_strFutAccount);
	DDX_Text(pDX, IDC_EDIT_ACCOUNT_OPT, m_strOptAccount);
	DDX_Text(pDX, IDC_EDIT_ACCOUNT_STK, m_strStkAccount);
	DDX_Text(pDX, IDC_EDIT_EXCHANGE_FUT, m_strFutExchange);
	DDX_Text(pDX, IDC_EDIT_MONTH_FUT, m_strFutMonth);
	DDX_Text(pDX, IDC_EDIT_MONTH_OPT, m_strOptMonth);
	DDX_Text(pDX, IDC_EDIT_ORDER_NO, m_strOrderNo);
	DDX_Text(pDX, IDC_EDIT_PRICE_OPT, m_strOptPrice);
	DDX_Text(pDX, IDC_EDIT_PRICE_FUT, m_strFutPrice);
	DDX_Text(pDX, IDC_EDIT_PRICE_STK, m_strStkPrice);
	DDX_Text(pDX, IDC_EDIT_QTY_FUT, m_strFutQty);
	DDX_Text(pDX, IDC_EDIT_QTY_OPT, m_strOptQty);
	DDX_Text(pDX, IDC_EDIT_QTY_STK, m_strStkQty);
	DDX_Text(pDX, IDC_EDIT_REDUCE_QTY, m_strReduceQty);
	DDX_Text(pDX, IDC_EDIT_STOP_PRICE_FUT, m_strFutStopPrice);
	DDX_Text(pDX, IDC_EDIT_STRIKE_PRICE_OPT, m_strOptStrikePrice);
	DDX_Text(pDX, IDC_EDIT_SYMBOL_FUT, m_strFutSymbol);
	DDX_Text(pDX, IDC_EDIT_SYMBOL_OPT, m_strOptSymbol);
	DDX_Text(pDX, IDC_EDIT_SYMBOL_STK, m_strStkSymbol);
	DDX_Text(pDX, IDC_EDIT_TYPE_FUT, m_strFutType);
	DDX_Text(pDX, IDC_EDIT_TYPE_OPT, m_strOptType);
	DDX_Text(pDX, IDC_EDIT_TYPE_STK, m_strStkType);
	DDX_Text(pDX, IDC_EDIT_USER_KEY_FUT, m_strFutUserKey);
	DDX_Text(pDX, IDC_EDIT_USER_KEY_OPT, m_strOptUserKey);
	DDX_Text(pDX, IDC_EDIT_USER_KEY_STK, m_strStkUserKey);
	DDX_Text(pDX, IDC_EDIT_TYPE_REDUCE, m_strReduceType);
	DDX_CBString(pDX, IDC_COMBO_BUY_SELL_FUT, m_strFutBuySell);
	DDX_CBString(pDX, IDC_COMBO_BUY_SELL_FUT2, m_strFutBuySell2);
	DDX_CBString(pDX, IDC_COMBO_BUY_SELL_OPT, m_strOptBuySell);
	DDX_CBString(pDX, IDC_COMBO_BUY_SELL_OPT2, m_strOptBuySell2);
	DDX_CBString(pDX, IDC_COMBO_BUY_SELL_STK, m_strStkBuySell);
	DDX_CBString(pDX, IDC_COMBO_CALL_PUT_OPT, m_strOptCallPut);
	DDX_CBString(pDX, IDC_COMBO_CALL_PUT_OPT2, m_strOptCallPut2);
	DDX_CBString(pDX, IDC_COMBO_DAY_TRADE_FUT, m_strFutDayTrade);
	DDX_CBString(pDX, IDC_COMBO_DAY_TRADE_OPT, m_strOptDayTrade);
	DDX_CBString(pDX, IDC_COMBO_MARKET_TYPE_STK, m_strStkMarketType);
	DDX_CBString(pDX, IDC_COMBO_OPEN_CLOSE_FUT, m_strFutOpenClose);
	DDX_CBString(pDX, IDC_COMBO_OPEN_CLOSE_OPT, m_strOptOpenClose);
	DDX_CBString(pDX, IDC_COMBO_ORDER_TYPE_FUT, m_strFutOrderType);
	DDX_CBString(pDX, IDC_COMBO_ORDER_TYPE_OPT, m_strOptOrderType);
	DDX_CBString(pDX, IDC_COMBO_ORDER_TYPE_STK, m_strStkOrderType);
	DDX_CBString(pDX, IDC_COMBO_PRICE_FLAG_FUT, m_strFutPriceFlag);
	DDX_CBString(pDX, IDC_COMBO_PRICE_FLAG_OPT, m_strOptPriceFlag);
	DDX_CBString(pDX, IDC_COMBO_PRICE_FLAG_STK, m_strStkPriceFlag);
	DDX_CBString(pDX, IDC_COMBO_STOCK_TYPE_STK, m_strStkStockType);
	DDX_Text(pDX, IDC_EDIT_STOP_PRICE_OPT, m_strOptStopPrice);
	DDX_Control(pDX, IDC_GTRADECTRL, m_API);
	DDX_Text(pDX, IDC_EDIT_LICENSE_FILE, m_strLicensePath);
	DDX_Text(pDX, IDC_EDIT_MONTH_FUT2, m_strFutMonth2);
	DDX_Text(pDX, IDC_EDIT_MONTH_OPT2, m_strOptMonth2);
	DDX_Text(pDX, IDC_EDIT_STRIKE_PRICE_OPT2, m_strOptStrikePrice2);
	DDX_Text(pDX, IDC_EDIT_SYMBOL_FUT2, m_strFutSymbol2);
	DDX_Text(pDX, IDC_EDIT_SYMBOL_OPT2, m_strOptSymbol2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestGTradeDlg, CDialog)
	//{{AFX_MSG_MAP(CTestGTradeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_PLACE_STK_ORDER, OnButtonPlaceStkOrder)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
	ON_BN_CLICKED(IDC_BUTTON_PLACE_FUT_ORDER, OnButtonPlaceFutOrder)
	ON_BN_CLICKED(IDC_BUTTON_PLACE_OPT_ORDER, OnButtonPlaceOptOrder)
	ON_BN_CLICKED(IDC_BUTTON_REDUCE_ORDER, OnButtonReduceOrder)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_LOGIN, OnButtonLogin)
	ON_BN_CLICKED(IDC_BUTTON_LOGOUT, OnButtonLogout)
	ON_BN_CLICKED(IDC_BUTTON_QUERY_STATEMENT, OnButtonQueryStatement)
	ON_BN_CLICKED(IDC_BUTTON_RETRIVE, OnButtonRetrive)
	ON_NOTIFY(NM_CLICK, IDC_LIST_ORDER_STATUS, OnClickListOrderStatus)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE2, OnButtonBrowse2)
	ON_BN_CLICKED(IDC_BUTTON_LICENSE, OnButtonLicense)
	ON_BN_CLICKED(IDC_BUTTON_GET_LICENSE, OnButtonGetLicense)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestGTradeDlg message handlers

BOOL CTestGTradeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_listOrderStatus.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	m_listOrderStatus.InsertColumn(0, "帳號", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(1, "委託書號", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(2, "商品", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(3, "類別", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(4, "委託類別", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(5, "價格別", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(6, "買賣", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(7, "委託價", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(8, "有效委託", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(9, "已成交", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(10, "成交均價", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(11, "狀態", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(12, "時間", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(13, "來源別", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(14, "UserKey", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(15, "有效資料", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(16, "可增量", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(17, "可減量", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(18, "可改價", LVCFMT_LEFT, 80);
	m_listOrderStatus.InsertColumn(19, "交易所", LVCFMT_LEFT, 80);

	m_listFillOrder.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	m_listFillOrder.InsertColumn(0, "帳號", LVCFMT_LEFT, 80);
	m_listFillOrder.InsertColumn(1, "委託書號", LVCFMT_LEFT, 80);
	m_listFillOrder.InsertColumn(2, "商品", LVCFMT_LEFT, 80);
	m_listFillOrder.InsertColumn(3, "類別", LVCFMT_LEFT, 80);
	m_listFillOrder.InsertColumn(4, "買賣", LVCFMT_LEFT, 80);
	m_listFillOrder.InsertColumn(5, "成交量", LVCFMT_LEFT, 80);
	m_listFillOrder.InsertColumn(6, "成交價", LVCFMT_LEFT, 80);
	m_listFillOrder.InsertColumn(7, "時間", LVCFMT_LEFT, 80);
	m_listFillOrder.InsertColumn(8, "來源別", LVCFMT_LEFT, 80);
	m_listFillOrder.InsertColumn(9, "UserKey", LVCFMT_LEFT, 80);
	m_listFillOrder.InsertColumn(10, "倉別", LVCFMT_LEFT, 80);
	m_listFillOrder.InsertColumn(11, "交易所", LVCFMT_LEFT, 80);

	m_comboStatementType.SetCurSel(0);

	LoadProperties();
	
	char strBuf[256];
	::GetPrivateProfileString("PROPERTIES", "Profile", "", strBuf, 256, "Config.ini");
	m_strProfilePath=strBuf;
	::GetPrivateProfileString("PROPERTIES", "License", "", strBuf, 256, "Config.ini");
	m_strLicensePath=strBuf;
	UpdateData(FALSE);

	ReloadSessions();

	GetDlgItem(IDC_BUTTON_LOGIN)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_LOGOUT)->EnableWindow(FALSE);
	GetDlgItem(IDC_COMBO_SESSION)->EnableWindow(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestGTradeDlg::ReloadSessions()
{
	m_comboSession.ResetContent();
	if(!m_strProfilePath.IsEmpty())
	{
		char strBuf[1024];
		::GetPrivateProfileSectionNames(strBuf, 1024, m_strProfilePath);
		char* ptr=strBuf;
		while(*ptr)
		{
			CString strSession=ptr;
			if(strSession!="TOUCHANCE")
				m_comboSession.AddString(strSession);
			int nLen=strlen(ptr);
			ptr+=nLen+1;
		}
	}
	if(m_comboSession.GetCount())
		m_comboSession.SetCurSel(0);
}

void CTestGTradeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTestGTradeDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestGTradeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTestGTradeDlg::OnButtonBrowse() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFileDialog fDlg(TRUE, NULL, m_strProfilePath, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Profiles (*.ini)|*.dll|All Files (*.*)|*.*||");
	if(fDlg.DoModal()==IDOK)
	{
		m_strProfilePath=fDlg.GetPathName();
		UpdateData(FALSE);
		::WritePrivateProfileString("PROPERTIES", "Profile", m_strProfilePath, "Config.ini");
		ReloadSessions();
	}
}

void CTestGTradeDlg::SaveProperties() 
{
	UpdateData(TRUE);
	::WritePrivateProfileString("STK", "ACCOUNT",		m_strStkAccount,	"Config.ini");
	::WritePrivateProfileString("STK", "SYMBOL",		m_strStkSymbol,		"Config.ini");
	::WritePrivateProfileString("STK", "ORDER_TYPE",	m_strStkOrderType,	"Config.ini");
	::WritePrivateProfileString("STK", "BUY_SELL",		m_strStkBuySell,	"Config.ini");
	::WritePrivateProfileString("STK", "PRICE_FLAG",	m_strStkPriceFlag,	"Config.ini");
	::WritePrivateProfileString("STK", "PRICE",			m_strStkPrice,		"Config.ini");
	::WritePrivateProfileString("STK", "QTY",			m_strStkQty,		"Config.ini");
	::WritePrivateProfileString("STK", "MARKET_TYPE",	m_strStkMarketType, "Config.ini");
	::WritePrivateProfileString("STK", "STOCK_TYPE",	m_strStkStockType,	"Config.ini");
	::WritePrivateProfileString("STK", "USER_KEY",		m_strStkUserKey,	"Config.ini");

	::WritePrivateProfileString("FUT", "ACCOUNT",		m_strFutAccount,	"Config.ini");
	::WritePrivateProfileString("FUT", "SYMBOL",		m_strFutSymbol,		"Config.ini");
	::WritePrivateProfileString("FUT", "ORDER_TYPE",	m_strFutOrderType,	"Config.ini");
	::WritePrivateProfileString("FUT", "BUY_SELL",		m_strFutBuySell,	"Config.ini");
	::WritePrivateProfileString("FUT", "PRICE_FLAG",	m_strFutPriceFlag,	"Config.ini");
	::WritePrivateProfileString("FUT", "PRICE",			m_strFutPrice,		"Config.ini");
	::WritePrivateProfileString("FUT", "QTY",			m_strFutQty,		"Config.ini");
	::WritePrivateProfileString("FUT", "DAY_TRADE",		m_strFutDayTrade,	"Config.ini");
	::WritePrivateProfileString("FUT", "USER_KEY",		m_strFutUserKey,	"Config.ini");
	::WritePrivateProfileString("FUT", "EXCHANGE",		m_strFutExchange,	"Config.ini");
	::WritePrivateProfileString("FUT", "MONTH",			m_strFutMonth,		"Config.ini");
	::WritePrivateProfileString("FUT", "OPEN_CLOSE",	m_strFutOpenClose,	"Config.ini");
	::WritePrivateProfileString("FUT", "SYMBOL2",		m_strFutSymbol2,	"Config.ini");
	::WritePrivateProfileString("FUT", "BUY_SELL2",		m_strFutBuySell2,	"Config.ini");
	::WritePrivateProfileString("FUT", "MONTH2",		m_strFutMonth2,		"Config.ini");

	::WritePrivateProfileString("OPT", "ACCOUNT",		m_strOptAccount,	"Config.ini");
	::WritePrivateProfileString("OPT", "SYMBOL",		m_strOptSymbol,		"Config.ini");
	::WritePrivateProfileString("OPT", "ORDER_TYPE",	m_strOptOrderType,	"Config.ini");
	::WritePrivateProfileString("OPT", "BUY_SELL",		m_strOptBuySell,	"Config.ini");
	::WritePrivateProfileString("OPT", "PRICE_FLAG",	m_strOptPriceFlag,	"Config.ini");
	::WritePrivateProfileString("OPT", "PRICE",			m_strOptPrice,		"Config.ini");
	::WritePrivateProfileString("OPT", "QTY",			m_strOptQty,		"Config.ini");
	::WritePrivateProfileString("OPT", "DAY_TRADE",		m_strOptDayTrade,	"Config.ini");
	::WritePrivateProfileString("OPT", "USER_KEY",		m_strOptUserKey,	"Config.ini");
	::WritePrivateProfileString("OPT", "MONTH",			m_strOptMonth,		"Config.ini");
	::WritePrivateProfileString("OPT", "OPEN_CLOSE",	m_strOptOpenClose,	"Config.ini");
	::WritePrivateProfileString("OPT", "CALL_PUT",		m_strOptCallPut,	"Config.ini");
	::WritePrivateProfileString("OPT", "STRIKE_PRICE",	m_strOptStrikePrice,"Config.ini");
	::WritePrivateProfileString("FUT", "SYMBOL2",		m_strFutSymbol2,	"Config.ini");
	::WritePrivateProfileString("FUT", "BUY_SELL2",		m_strFutBuySell2,	"Config.ini");
	::WritePrivateProfileString("FUT", "MONTH2",		m_strFutMonth2,		"Config.ini");
	::WritePrivateProfileString("OPT", "CALL_PUT2",		m_strOptCallPut2,	"Config.ini");
	::WritePrivateProfileString("OPT", "STRIKE_PRICE2",	m_strOptStrikePrice2,"Config.ini");
}

void CTestGTradeDlg::LoadProperties() 
{
	char strBuf[256];
	::GetPrivateProfileString("STK", "ACCOUNT", "", strBuf, 256, "Config.ini");
	m_strStkAccount=strBuf;
	::GetPrivateProfileString("STK", "SYMBOL", "", strBuf, 256, "Config.ini");
	m_strStkSymbol=strBuf;
	::GetPrivateProfileString("STK", "ORDER_TYPE", "", strBuf, 256, "Config.ini");
	m_strStkOrderType=strBuf;
	::GetPrivateProfileString("STK", "BUY_SELL", "", strBuf, 256, "Config.ini");
	m_strStkBuySell=strBuf;
	::GetPrivateProfileString("STK", "PRICE_FLAG", "", strBuf, 256, "Config.ini");
	m_strStkPriceFlag=strBuf;
	::GetPrivateProfileString("STK", "PRICE", "", strBuf, 256, "Config.ini");
	m_strStkPrice=strBuf;
	::GetPrivateProfileString("STK", "QTY", "", strBuf, 256, "Config.ini");
	m_strStkQty=strBuf;
	::GetPrivateProfileString("STK", "MARKET_TYPE", "", strBuf, 256, "Config.ini");
	m_strStkMarketType=strBuf;
	::GetPrivateProfileString("STK", "STOCK_TYPE", "", strBuf, 256, "Config.ini");
	m_strStkStockType=strBuf;
	::GetPrivateProfileString("STK", "USER_KEY", "", strBuf, 256, "Config.ini");
	m_strStkUserKey=strBuf;

	::GetPrivateProfileString("FUT", "ACCOUNT", "", strBuf, 256, "Config.ini");
	m_strFutAccount=strBuf;
	::GetPrivateProfileString("FUT", "SYMBOL", "", strBuf, 256, "Config.ini");
	m_strFutSymbol=strBuf;
	::GetPrivateProfileString("FUT", "ORDER_TYPE", "", strBuf, 256, "Config.ini");
	m_strFutOrderType=strBuf;
	::GetPrivateProfileString("FUT", "BUY_SELL", "", strBuf, 256, "Config.ini");
	m_strFutBuySell=strBuf;
	::GetPrivateProfileString("FUT", "PRICE_FLAG", "", strBuf, 256, "Config.ini");
	m_strFutPriceFlag=strBuf;
	::GetPrivateProfileString("FUT", "PRICE", "", strBuf, 256, "Config.ini");
	m_strFutPrice=strBuf;
	::GetPrivateProfileString("FUT", "QTY", "", strBuf, 256, "Config.ini");
	m_strFutQty=strBuf;
	::GetPrivateProfileString("FUT", "DAY_TRADE", "", strBuf, 256, "Config.ini");
	m_strFutDayTrade=strBuf;
	::GetPrivateProfileString("FUT", "USER_KEY", "", strBuf, 256, "Config.ini");
	m_strFutUserKey=strBuf;
	::GetPrivateProfileString("FUT", "EXCHANGE", "", strBuf, 256, "Config.ini");
	m_strFutExchange=strBuf;
	::GetPrivateProfileString("FUT", "MONTH", "", strBuf, 256, "Config.ini");
	m_strFutMonth=strBuf;
	::GetPrivateProfileString("FUT", "OPEN_CLOSE", "", strBuf, 256, "Config.ini");
	m_strFutOpenClose=strBuf;
	::GetPrivateProfileString("FUT", "SYMBOL2", "", strBuf, 256, "Config.ini");
	m_strFutSymbol2=strBuf;
	::GetPrivateProfileString("FUT", "BUY_SELL2", "", strBuf, 256, "Config.ini");
	m_strFutBuySell2=strBuf;
	::GetPrivateProfileString("FUT", "MONTH2", "", strBuf, 256, "Config.ini");
	m_strFutMonth2=strBuf;

	::GetPrivateProfileString("OPT", "ACCOUNT", "", strBuf, 256, "Config.ini");
	m_strOptAccount=strBuf;
	::GetPrivateProfileString("OPT", "SYMBOL", "", strBuf, 256, "Config.ini");
	m_strOptSymbol=strBuf;
	::GetPrivateProfileString("OPT", "ORDER_TYPE", "", strBuf, 256, "Config.ini");
	m_strOptOrderType=strBuf;
	::GetPrivateProfileString("OPT", "BUY_SELL", "", strBuf, 256, "Config.ini");
	m_strOptBuySell=strBuf;
	::GetPrivateProfileString("OPT", "PRICE_FLAG", "", strBuf, 256, "Config.ini");
	m_strOptPriceFlag=strBuf;
	::GetPrivateProfileString("OPT", "PRICE", "", strBuf, 256, "Config.ini");
	m_strOptPrice=strBuf;
	::GetPrivateProfileString("OPT", "QTY", "", strBuf, 256, "Config.ini");
	m_strOptQty=strBuf;
	::GetPrivateProfileString("OPT", "DAY_TRADE", "", strBuf, 256, "Config.ini");
	m_strOptDayTrade=strBuf;
	::GetPrivateProfileString("OPT", "USER_KEY", "", strBuf, 256, "Config.ini");
	m_strOptUserKey=strBuf;
	::GetPrivateProfileString("OPT", "MONTH", "", strBuf, 256, "Config.ini");
	m_strOptMonth=strBuf;
	::GetPrivateProfileString("OPT", "OPEN_CLOSE", "", strBuf, 256, "Config.ini");
	m_strOptOpenClose=strBuf;
	::GetPrivateProfileString("OPT", "CALL_PUT", "", strBuf, 256, "Config.ini");
	m_strOptCallPut=strBuf;
	::GetPrivateProfileString("OPT", "STRIKE_PRICE", "", strBuf, 256, "Config.ini");
	m_strOptStrikePrice=strBuf;
	::GetPrivateProfileString("FUT", "SYMBOL2", "", strBuf, 256, "Config.ini");
	m_strFutSymbol2=strBuf;
	::GetPrivateProfileString("FUT", "BUY_SELL2", "", strBuf, 256, "Config.ini");
	m_strFutBuySell2=strBuf;
	::GetPrivateProfileString("FUT", "MONTH2", "", strBuf, 256, "Config.ini");
	m_strFutMonth2=strBuf;
	::GetPrivateProfileString("OPT", "CALL_PUT2", "", strBuf, 256, "Config.ini");
	m_strOptCallPut2=strBuf;
	::GetPrivateProfileString("OPT", "STRIKE_PRICE2", "", strBuf, 256, "Config.ini");
	m_strOptStrikePrice2=strBuf;

	UpdateData(FALSE);
}

void CTestGTradeDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	SaveProperties();
	CDialog::OnClose();
}

#define ORDER_INFO_TYPE			 0
#define ORDER_INFO_ACCOUNT		 1
#define ORDER_INFO_SYMBOL		 2
#define ORDER_INFO_ORDER_TYPE	 3
#define ORDER_INFO_BUY_SELL		 4
#define ORDER_INFO_PRICE_FLAG	 5
#define ORDER_INFO_PRICE	 	 6
#define ORDER_INFO_QTY			 7
#define ORDER_INFO_MARKET_TYPE 	 8
#define ORDER_INFO_STOCK_TYPE	 9
#define ORDER_INFO_DAY_TRADE	 10
#define ORDER_INFO_USER_KEY		 11
#define ORDER_INFO_EXCHANGE		 12
#define ORDER_INFO_MONTH		 13
#define ORDER_INFO_STOP_PRICE	 14
#define ORDER_INFO_OPEN_CLOSE 	 15
#define ORDER_INFO_CALL_PUT		 16
#define ORDER_INFO_STRIKE_PRICE	 17
#define ORDER_INFO_ORDER_NO		 18
#define ORDER_INFO_MULTIPLIER    19
#define ORDER_INFO_SYMBOL2		 20
#define ORDER_INFO_BUY_SELL2	 21
#define ORDER_INFO_MONTH2		 22
#define ORDER_INFO_CALL_PUT2	 23
#define ORDER_INFO_STRIKE_PRICE2 24
#define ORDER_INFO_DISPLAY		 25

void CTestGTradeDlg::OnButtonPlaceStkOrder() 
{
	UpdateData(TRUE);

	m_API.ClearOrder();
	m_API.SetOrderInfoString(ORDER_INFO_TYPE, "S");
	m_API.SetOrderInfoString(ORDER_INFO_ACCOUNT, m_strStkAccount);
	m_API.SetOrderInfoString(ORDER_INFO_SYMBOL, m_strStkSymbol);
	m_API.SetOrderInfoString(ORDER_INFO_ORDER_TYPE, m_strStkOrderType);
	m_API.SetOrderInfoString(ORDER_INFO_BUY_SELL, m_strStkBuySell);
	m_API.SetOrderInfoString(ORDER_INFO_PRICE_FLAG, m_strStkPriceFlag);
	m_API.SetOrderInfoValue(ORDER_INFO_PRICE, atof(m_strStkPrice));
	m_API.SetOrderInfoValue(ORDER_INFO_QTY, atoi(m_strStkQty));
	m_API.SetOrderInfoString(ORDER_INFO_MARKET_TYPE, m_strStkMarketType);
	m_API.SetOrderInfoString(ORDER_INFO_STOCK_TYPE, m_strStkStockType);
	m_API.SetOrderInfoString(ORDER_INFO_USER_KEY, m_strStkUserKey);
	int nRet=m_API.ProcessOrder();

	CString str;
	str.Format("Process Stk Order Ret=%d", nRet);
	WriteLog(str);
}

void CTestGTradeDlg::OnButtonPlaceFutOrder() 
{
	UpdateData(TRUE);

	m_API.ClearOrder();
	m_API.SetOrderInfoString(ORDER_INFO_ACCOUNT, m_strFutAccount);
	m_API.SetOrderInfoString(ORDER_INFO_SYMBOL, m_strFutSymbol);
	m_API.SetOrderInfoString(ORDER_INFO_ORDER_TYPE, m_strFutOrderType);
	m_API.SetOrderInfoString(ORDER_INFO_BUY_SELL, m_strFutBuySell);
	m_API.SetOrderInfoString(ORDER_INFO_PRICE_FLAG, m_strFutPriceFlag);
	m_API.SetOrderInfoValue(ORDER_INFO_PRICE, atof(m_strFutPrice));
	m_API.SetOrderInfoValue(ORDER_INFO_QTY, atoi(m_strFutQty));
	m_API.SetOrderInfoString(ORDER_INFO_DAY_TRADE, m_strFutDayTrade);
	m_API.SetOrderInfoString(ORDER_INFO_USER_KEY, m_strFutUserKey);
	m_API.SetOrderInfoString(ORDER_INFO_EXCHANGE, m_strFutExchange);
	m_API.SetOrderInfoString(ORDER_INFO_MONTH, m_strFutMonth);
	m_API.SetOrderInfoValue(ORDER_INFO_STOP_PRICE, atof(m_strFutStopPrice));
	m_API.SetOrderInfoString(ORDER_INFO_OPEN_CLOSE, m_strFutOpenClose);
	if( m_strFutSymbol2.IsEmpty() )
	{
		m_API.SetOrderInfoString(ORDER_INFO_TYPE, "F");
		m_API.SetOrderInfoValue(ORDER_INFO_MULTIPLIER, 1); //乘數, 實際下單價格會是m_strFutPrice*乘數,基本上只有國外會需要,
	}
	else
	{
		m_API.SetOrderInfoString(ORDER_INFO_TYPE, "F2");
		m_API.SetOrderInfoString(ORDER_INFO_SYMBOL2, m_strFutSymbol2);
		m_API.SetOrderInfoString(ORDER_INFO_BUY_SELL2, m_strFutBuySell2);
		m_API.SetOrderInfoString(ORDER_INFO_MONTH2, m_strFutMonth2);
	}
	int nRet=m_API.ProcessOrder();

	CString str;
	str.Format("Process Fut Order Ret=%d", nRet);
	WriteLog(str);
}

void CTestGTradeDlg::OnButtonPlaceOptOrder() 
{
	UpdateData(TRUE);

	m_API.ClearOrder();
	m_API.SetOrderInfoString(ORDER_INFO_ACCOUNT, m_strOptAccount);
	m_API.SetOrderInfoString(ORDER_INFO_SYMBOL, m_strOptSymbol);
	m_API.SetOrderInfoString(ORDER_INFO_ORDER_TYPE, m_strOptOrderType);
	m_API.SetOrderInfoString(ORDER_INFO_BUY_SELL, m_strOptBuySell);
	m_API.SetOrderInfoString(ORDER_INFO_PRICE_FLAG, m_strOptPriceFlag);
	m_API.SetOrderInfoValue(ORDER_INFO_PRICE, atof(m_strOptPrice));
	m_API.SetOrderInfoValue(ORDER_INFO_QTY, atoi(m_strOptQty));
	m_API.SetOrderInfoString(ORDER_INFO_DAY_TRADE, m_strOptDayTrade);
	m_API.SetOrderInfoString(ORDER_INFO_USER_KEY, m_strOptUserKey);
	m_API.SetOrderInfoString(ORDER_INFO_MONTH, m_strOptMonth);
	m_API.SetOrderInfoString(ORDER_INFO_OPEN_CLOSE, m_strOptOpenClose);
	m_API.SetOrderInfoString(ORDER_INFO_CALL_PUT, m_strOptCallPut);
	m_API.SetOrderInfoValue(ORDER_INFO_STRIKE_PRICE, atof(m_strOptStrikePrice));
	if(m_strOptSymbol2.IsEmpty())
		m_API.SetOrderInfoString(ORDER_INFO_TYPE, "O");
	else
	{
		m_API.SetOrderInfoString(ORDER_INFO_TYPE, "O2");
		m_API.SetOrderInfoString(ORDER_INFO_SYMBOL2, m_strOptSymbol2);
		m_API.SetOrderInfoString(ORDER_INFO_BUY_SELL2, m_strOptBuySell2);
		m_API.SetOrderInfoString(ORDER_INFO_MONTH2, m_strOptMonth2);
		m_API.SetOrderInfoString(ORDER_INFO_CALL_PUT2, m_strOptCallPut2);
		m_API.SetOrderInfoValue(ORDER_INFO_STRIKE_PRICE2, atof(m_strOptStrikePrice2));
	}

	int nRet=m_API.ProcessOrder();

	CString str;
	str.Format("Process Opt Order Ret=%d", nRet);
	WriteLog(str);
}

void CTestGTradeDlg::OnButtonReduceOrder() 
{
	UpdateData(TRUE);

	m_API.ClearOrder();
	m_API.SetOrderInfoString(ORDER_INFO_TYPE, "R");
	m_API.SetOrderInfoString(ORDER_INFO_ORDER_NO, m_strOrderNo);
	m_API.SetOrderInfoValue(ORDER_INFO_QTY, atoi(m_strReduceQty));
	int nRet=m_API.ProcessOrder();

	CString str;
	str.Format("Process ReduceOrder Ret=%d", nRet);
	WriteLog(str);
}

void CTestGTradeDlg::OnButtonLogin() 
{
	// TODO: Add your control notification handler code here
	int nIndex=m_comboSession.GetCurSel();
	CString strSession;
	m_comboSession.GetLBText(nIndex, strSession);
	if(!m_strProfilePath.IsEmpty() && !strSession.IsEmpty())
	{
		int nRet=m_API.Login(m_strProfilePath, strSession);
		CString strLog;
		strLog.Format("Login [%s] ret=%d", strSession, nRet);
		WriteLog(strLog);
	}
}

void CTestGTradeDlg::OnButtonLogout() 
{
	// TODO: Add your control notification handler code here
	int nIndex=m_comboSession.GetCurSel();
	CString strSession;
	m_comboSession.GetLBText(nIndex, strSession);
	if(!strSession.IsEmpty())
	{
		m_API.Logout(strSession);
		CString strLog;
		strLog.Format("Logout [%s]", strSession);
		WriteLog(strLog);
	}
}

BEGIN_EVENTSINK_MAP(CTestGTradeDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CTestGTradeDlg)
	ON_EVENT(CTestGTradeDlg, IDC_GTRADECTRL, 1 /* LoginReady */, OnLoginReady, VTS_BSTR)
	ON_EVENT(CTestGTradeDlg, IDC_GTRADECTRL, 2 /* LoginFail */, OnLoginFail, VTS_BSTR VTS_BSTR)
	ON_EVENT(CTestGTradeDlg, IDC_GTRADECTRL, 3 /* ConnectionLost */, OnConnectionLost, VTS_BSTR)
	ON_EVENT(CTestGTradeDlg, IDC_GTRADECTRL, 4 /* OrderStatus */, OnOrderStatus, VTS_I4)
	ON_EVENT(CTestGTradeDlg, IDC_GTRADECTRL, 5 /* FillOrder */, OnFillOrder, VTS_I4)
	ON_EVENT(CTestGTradeDlg, IDC_GTRADECTRL, 6 /* StatementUpdate */, OnStatementUpdate, VTS_I4)
	ON_EVENT(CTestGTradeDlg, IDC_GTRADECTRL, 7 /* Log */, OnLog, VTS_BSTR VTS_I4 VTS_BSTR)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CTestGTradeDlg::OnLoginReady(LPCTSTR strSession) 
{
	// TODO: Add your control notification handler code here
	CString strLog;
	strLog.Format("Login Ready on [%s]", strSession);
	WriteLog(strLog);

	int nCount=m_API.GetStatementCount(0);
	CString strAccount;
	for(int i=0; i<nCount; i++)
	{
		strAccount.Format("%s %s %s", m_API.GetStatementString(0, i, 0), m_API.GetStatementString(0, i, 1), m_API.GetStatementString(0, i, 2));
		WriteLog(strAccount);
	}
}

void CTestGTradeDlg::OnLoginFail(LPCTSTR strSession, LPCTSTR strErrorMsg) 
{
	// TODO: Add your control notification handler code here
	CString strLog;
	strLog.Format("Login Failed on [%s] Error=%s", strSession, strErrorMsg);
	WriteLog(strLog);
}

void CTestGTradeDlg::OnConnectionLost(LPCTSTR strSession) 
{
	// TODO: Add your control notification handler code here
	CString strLog;
	strLog.Format("Connection Lost on [%s]", strSession);
	WriteLog(strLog);
}

#define LOG_TYPE_ERROR	0
#define LOG_TYPE_INFO	1
#define LOG_TYPE_HINT	2
#define LOG_TYPE_MISC	3

void CTestGTradeDlg::OnLog(LPCTSTR strSession, long nLogType, LPCTSTR strLog) 
{
	// TODO: Add your control notification handler code here
	CString strLogType;
	switch(nLogType)
	{
	case LOG_TYPE_ERROR:
		strLogType="ERROR";
		break;
	case LOG_TYPE_INFO:
		strLogType="INFO";
		break;
	case LOG_TYPE_HINT:
		strLogType="HINT";
		break;
	case LOG_TYPE_MISC:
		strLogType="MISC";
		break;
	default:
		strLogType="UNDEFINED";
		break;
	}
	CString str;
	str.Format("[%s] %s > %s", strSession, strLogType, strLog);
	WriteLog(str);
}

void CTestGTradeDlg::OnOrderStatus(long nIndex) 
{
	// TODO: Add your control notification handler code here
	CString str;
	str.Format("委託回報 %d", nIndex);
	WriteLog(str);

	str.Format("委託回報 memo %s", m_API.GetOrderString(nIndex, 24) );
	WriteLog(str);

	int nListIndex;
	if(!m_mapOrderList.Lookup((LPVOID)nIndex, (void*&)nListIndex))//New
	{
		nListIndex=m_listOrderStatus.InsertItem(m_listOrderStatus.GetItemCount(), "");
		m_mapOrderList.SetAt((LPVOID)nIndex, (LPVOID)nListIndex);
	}
	//Update
	m_listOrderStatus.SetItemText(nListIndex, 0, m_API.GetOrderString(nIndex, 0));//帳號
	m_listOrderStatus.SetItemText(nListIndex, 1, m_API.GetOrderString(nIndex, 1));//委託書號
	m_listOrderStatus.SetItemText(nListIndex, 2, m_API.GetOrderString(nIndex, 2));//商品名稱
	m_listOrderStatus.SetItemText(nListIndex, 3, m_API.GetOrderString(nIndex, 3));//類別
	m_listOrderStatus.SetItemText(nListIndex, 4, m_API.GetOrderString(nIndex, 4));//委託類別
	m_listOrderStatus.SetItemText(nListIndex, 5, m_API.GetOrderString(nIndex, 5));//價格別
	m_listOrderStatus.SetItemText(nListIndex, 6, m_API.GetOrderString(nIndex, 6));//買賣別
	str.Format("%.6f", m_API.GetOrderValue(nIndex, 7));//委託價
	str.TrimRight('0');
	str.TrimRight('.');
	m_listOrderStatus.SetItemText(nListIndex, 7, str);
	str.Format("%d", (int)m_API.GetOrderValue(nIndex, 8));//有效委託
	m_listOrderStatus.SetItemText(nListIndex, 8, str);
	str.Format("%d", (int)m_API.GetOrderValue(nIndex, 9));//已成交
	m_listOrderStatus.SetItemText(nListIndex, 9, str);
	str.Format("%.6f", m_API.GetOrderValue(nIndex, 10));//成交均價
	str.TrimRight('0');
	str.TrimRight('.');
	m_listOrderStatus.SetItemText(nListIndex, 10, str);
	m_listOrderStatus.SetItemText(nListIndex, 11, m_API.GetOrderString(nIndex, 11));//狀態
	m_listOrderStatus.SetItemText(nListIndex, 12, m_API.GetOrderString(nIndex, 12));//時間
	m_listOrderStatus.SetItemText(nListIndex, 13, m_API.GetOrderString(nIndex, 13));//來源別
	m_listOrderStatus.SetItemText(nListIndex, 14, m_API.GetOrderString(nIndex, 14));//User Key
	m_listOrderStatus.SetItemText(nListIndex, 15, m_API.GetOrderString(nIndex, 20));//有效資料
	m_listOrderStatus.SetItemText(nListIndex, 16, m_API.GetOrderString(nIndex, 21));//可增量
	m_listOrderStatus.SetItemText(nListIndex, 17, m_API.GetOrderString(nIndex, 22));//可減量
	m_listOrderStatus.SetItemText(nListIndex, 18, m_API.GetOrderString(nIndex, 23));//可改價
	m_listOrderStatus.SetItemText(nListIndex, 19, m_API.GetOrderString(nIndex, 16));//交易所
}

void CTestGTradeDlg::OnFillOrder(long nIndex) 
{
	// TODO: Add your control notification handler code here
	CString str;
	str.Format("成交回報 %d", nIndex);
	WriteLog(str);

	int nListIndex;
	if(!m_mapDealList.Lookup((LPVOID)nIndex, (void*&)nListIndex))//New
	{
		nListIndex=m_listFillOrder.InsertItem(m_listFillOrder.GetItemCount(), "");
		m_mapDealList.SetAt((LPVOID)nIndex, (LPVOID)nListIndex);
	}
	//Update
	m_listFillOrder.SetItemText(nListIndex, 0, m_API.GetFillOrderString(nIndex, 0));//帳號
	m_listFillOrder.SetItemText(nListIndex, 1, m_API.GetFillOrderString(nIndex, 1));//委託書號
	m_listFillOrder.SetItemText(nListIndex, 2, m_API.GetFillOrderString(nIndex, 2));//商品名稱
	m_listFillOrder.SetItemText(nListIndex, 3, m_API.GetFillOrderString(nIndex, 3));//類別
	m_listFillOrder.SetItemText(nListIndex, 4, m_API.GetFillOrderString(nIndex, 6));//買賣別
	str.Format("%d", (int)m_API.GetFillOrderValue(nIndex, 9));//成交量
	m_listFillOrder.SetItemText(nListIndex, 5, str);
	str.Format("%.6f", m_API.GetFillOrderValue(nIndex, 10));//成交價
	str.TrimRight('0');
	str.TrimRight('.');
	m_listFillOrder.SetItemText(nListIndex, 6, str);
	m_listFillOrder.SetItemText(nListIndex, 7, m_API.GetFillOrderString(nIndex, 12));//時間
	m_listFillOrder.SetItemText(nListIndex, 8, m_API.GetFillOrderString(nIndex, 13));//來源別
	m_listFillOrder.SetItemText(nListIndex, 9, m_API.GetFillOrderString(nIndex, 14));//User Key
	m_listFillOrder.SetItemText(nListIndex, 10, m_API.GetFillOrderString(nIndex, 15));//倉別
	m_listFillOrder.SetItemText(nListIndex, 11, m_API.GetFillOrderString(nIndex, 16));//交易所
}

void CTestGTradeDlg::OnStatementUpdate(long nInfoType) 
{
	// TODO: Add your control notification handler code here
	CString strLog;
	strLog.Format("Statement Updated = %d", nInfoType);
	WriteLog(strLog);
}

void CTestGTradeDlg::WriteLog(CString strLog)
{
	SYSTEMTIME st;
	GetLocalTime(&st);
	CString str;
	str.Format("%02d:%02d:%02d:%03d %s\r\n", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, strLog);
	m_strLog=str+m_strLog;
	if(m_strLog.GetLength()>4096)
		m_strLog=m_strLog.Left(4096);
	GetDlgItem(IDC_EDIT_LOG)->SetWindowText(m_strLog);
}

void CTestGTradeDlg::OnButtonQueryStatement() 
{
	// TODO: Add your control notification handler code here
	int nType=m_comboStatementType.GetCurSel();
	m_API.QueryStatement(nType);
}

void CTestGTradeDlg::OnButtonRetrive() 
{
	// TODO: Add your control notification handler code here
	int nInfoType=m_comboStatementType.GetCurSel();
	int nCount=m_API.GetStatementCount(nInfoType);
	CString strLog;
	strLog.Format("Statement %d count = %d", nInfoType, nCount);
	WriteLog(strLog);
	for(int i=0; i<nCount; i++)
	{
		strLog="";
		if(nInfoType==0)
		{
			CString strAccount;
			strAccount.Format("%s %s %s", m_API.GetStatementString(nInfoType, i, 0), m_API.GetStatementString(nInfoType, i, 1), m_API.GetStatementString(nInfoType, i, 2));
			WriteLog(strAccount);
		}
		if(nInfoType==1)
		{
			CString str;
			for(int j=0; j<8; j++)
			{
				str.Format("%d:%s\t", j, m_API.GetStatementString(nInfoType, i, j));		
				strLog+=str;
			}
			WriteLog(strLog);
		}
		else if(nInfoType==2)
		{
			CString str;
			for(int j=0; j<19; j++)
			{
				str.Format("%d:%s\t", j, m_API.GetStatementString(nInfoType, i, j));		
				strLog+=str;
			}
			WriteLog(strLog);
		}
		else if(nInfoType==3)
		{
			CString str;
			for(int j=0; j<11; j++)
			{
				str.Format("%d:%s\t", j, m_API.GetStatementString(nInfoType, i, j));		
				strLog+=str;
			}
			WriteLog(strLog);
		}
		else if(nInfoType==4)
		{
			CString str;
			for(int j=0; j<8; j++)
			{
				str.Format("%d:%s\t", j, m_API.GetStatementString(nInfoType, i, j));		
				strLog+=str;
			}
			WriteLog(strLog);
		}
		else if(nInfoType==5)
		{
			CString str;
			for(int j=0; j<10; j++)
			{
				str.Format("%d:%s\t", j, m_API.GetStatementString(nInfoType, i, j));		
				strLog+=str;
			}
			WriteLog(strLog);
		}
		else if(nInfoType==6)
		{
			CString str;
			for(int j=0; j<4; j++)
			{
				str.Format("%d:%s\t", j, m_API.GetStatementString(nInfoType, i, j));		
				strLog+=str;
			}
			WriteLog(strLog);
		}
	}
}

void CTestGTradeDlg::OnClickListOrderStatus(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	POSITION pos=m_listOrderStatus.GetFirstSelectedItemPosition();
	if(pos)
	{
		int nIndex=m_listOrderStatus.GetNextSelectedItem(pos);
		CString strOrderNo=m_listOrderStatus.GetItemText(nIndex, 1);
		if(!strOrderNo.IsEmpty())
			SetDlgItemText(IDC_EDIT_ORDER_NO, strOrderNo);
	}
	*pResult = 0;
}

void CTestGTradeDlg::OnButtonBrowse2() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFileDialog fDlg(TRUE, NULL, m_strLicensePath, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "License File (*.lic)|*.lic|All Files (*.*)|*.*||");
	if(fDlg.DoModal()==IDOK)
	{
		m_strLicensePath=fDlg.GetPathName();
		UpdateData(FALSE);
		::WritePrivateProfileString("PROPERTIES", "License", m_strLicensePath, "Config.ini");
	}
}

void CTestGTradeDlg::OnButtonLicense() 
{
	// TODO: Add your control notification handler code here
	if(!m_strLicensePath.IsEmpty())
	{
		m_API.TouchanceLoginY(m_strLicensePath);
		GetDlgItem(IDC_BUTTON_LOGIN)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_LOGOUT)->EnableWindow(TRUE);
		GetDlgItem(IDC_COMBO_SESSION)->EnableWindow(TRUE);
	}
}

void CTestGTradeDlg::OnButtonGetLicense() 
{
	// TODO: Add your control notification handler code here
	if(!m_strProfilePath.IsEmpty() && m_API.TouchanceLogin(m_strProfilePath, "TOUCHANCE"))
	{
		GetDlgItem(IDC_BUTTON_LOGIN)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_LOGOUT)->EnableWindow(TRUE);
		GetDlgItem(IDC_COMBO_SESSION)->EnableWindow(TRUE);
	}
	else
		WriteLog("Fail to login TOUCHANCE service.");
}
