// DLG_Corp_TS.cpp : implementation file
//

#include "stdafx.h"
#include "smsmanager.h"
#include "DLG_Corp_TS.h"

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

/////////////////////////////////////////////////////////////////////////////
// DLG_Corp_TS dialog

#include "DLG_Corp_TS_Modify.h"

DLG_Corp_TS::DLG_Corp_TS(CWnd* pParent /*=NULL*/)
	: CDialog(DLG_Corp_TS::IDD, pParent)
{
	//{{AFX_DATA_INIT(DLG_Corp_TS)
	m_strCorpName = _T("");
	m_strCorpID = _T("");
	//}}AFX_DATA_INIT

	m_pDlg = (CUserDlg  *)pParent;
	m_lModifyCount = 0;
	memset( &m_TSReq , 0 , sizeof(m_TSReq) );
	m_pTSData     = NULL;
}


void DLG_Corp_TS::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DLG_Corp_TS)
	DDX_Control(pDX, IDC_CORP_LXR_LIST, m_wndList);
	DDX_Text(pDX, IDC_CORP_LXR_CORPNAME, m_strCorpName);
	DDX_Text(pDX, IDC_CORP_LXR_CORPID, m_strCorpID);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(DLG_Corp_TS, CDialog)
	//{{AFX_MSG_MAP(DLG_Corp_TS)
	ON_BN_CLICKED(IDC_CORP_LXR_ADD, OnCorpLxrAdd)
	ON_BN_CLICKED(IDC_CORP_LXR_MODIFY, OnCorpLxrModify)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DLG_Corp_TS message handlers

BOOL DLG_Corp_TS::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	
	m_strCorpName = m_TSReq.szCorpName;
	m_strCorpID.Format( _T("%d") , m_TSReq.lCorpID);

	this->UpdateData(false);
	
	//��ʼ��List
	m_Image.Create(16,16,ILC_COLOR16|ILC_MASK,5,5);
	m_Image.Add(AfxGetApp()->LoadIcon(IDI_L_CORP) );
	m_wndList.SubClassWindow2();
	m_wndList.SetHeadings(_T("���,50; Ͷ������,120;Ͷ��ʱ��,90; ������,80; ������,80;  Ͷ������,200;  �������,200;"));
	m_wndList.SetGridLines(true);
	m_wndList.SetImageList(&m_Image,LVSIL_SMALL);

	POSITION pos;
	TCHAR Buf[32];
	CString strTemp;
	for ( int i = 0 ; i< m_TSReq.lCount ; i++ )
	{
		ANS_Corp_TSData * pData = new ANS_Corp_TSData;
		*pData = m_pTSData[i];
		pos = m_TS.AddTail( pData );
		_stprintf( Buf , _T("%d") , i+1 );
		m_wndList.InsertItem( i , Buf , 0 );
		m_wndList.SetItemData( i , (DWORD_PTR)pos);
		m_wndList.SetItemText( i , 1 , pData->szTSType );
		strTemp.Format( _T("%04d.%02d.%02d") , pData->tTSTime.wYear,pData->tTSTime.wMonth,pData->tTSTime.wDay );
		m_wndList.SetItemText( i , 2 , strTemp );
		m_wndList.SetItemText( i , 3 , pData->szOperName );
		m_wndList.SetItemText( i , 4 , pData->szCreateUser );
		m_wndList.SetItemText( i , 5 , pData->szTSMsg );
		m_wndList.SetItemText( i , 6 , pData->szTSProcess );
	}
	

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void DLG_Corp_TS::SetParam(ANS_Corp_TS TS, ANS_Corp_TSData *pTSData)
{
	m_TSReq    = TS;
	m_pTSData  = pTSData;
}

void DLG_Corp_TS::OnCorpLxrAdd() 
{
	ANS_Corp_TSData  TS={0};

	DLG_Corp_TS_Modify dlg(this);
	dlg.SetParam(TS,0);
	if ( dlg.DoModal() == IDOK )
	{
		ANS_Corp_TSData * pTS = new ANS_Corp_TSData;
		*pTS = dlg.GetParam();
		::GetLocalTime( &pTS->tCreateTime );
		pTS->ucStatus = CORP_TS_ADD;
		m_lModifyCount ++;    //��Ҫ�޸ĵļ�¼����
		POSITION pos = m_TS.AddTail( pTS );
		long lCount = m_wndList.GetItemCount();
		TCHAR Buf[32];
		_stprintf( Buf, _T("%d") , lCount +1 );

		CString strTemp;
		//�����µ�����
		m_wndList.InsertItem( lCount , Buf , 0 );
		m_wndList.SetItemData( lCount , (DWORD_PTR)pos);
		m_wndList.SetItemText( lCount , 1 , pTS->szTSType );
		strTemp.Format( _T("%04d.%02d.%02d") , pTS->tTSTime.wYear,pTS->tTSTime.wMonth,pTS->tTSTime.wDay );
		m_wndList.SetItemText( lCount , 2 , strTemp );
		m_wndList.SetItemText( lCount , 3 , pTS->szOperName );
		m_wndList.SetItemText( lCount , 4 , pTS->szCreateUser );
		m_wndList.SetItemText( lCount , 5 , pTS->szTSMsg );
		m_wndList.SetItemText( lCount , 6 , pTS->szTSProcess );

	}	
}

void DLG_Corp_TS::OnCorpLxrModify() 
{
	long lItem = GetCurrentSelected();
	if ( lItem < 0 )
		return ;

	POSITION pos = (POSITION)m_wndList.GetItemData(lItem);
	POSITION pos2 = pos;
	ANS_Corp_TSData * pTS = m_TS.GetNext( pos );
	if (pTS)
	{
		DLG_Corp_TS_Modify dlg(this);
		dlg.SetParam( *pTS,pos2);
		if ( dlg.DoModal() == IDOK )
		{
			*pTS = dlg.GetParam();  //ȡ���޸Ĺ����ֵ

			CString strTemp;
			m_wndList.SetItemText( lItem , 1 , pTS->szTSType );
			strTemp.Format( _T("%04d.%02d.%02d") , pTS->tTSTime.wYear,pTS->tTSTime.wMonth,pTS->tTSTime.wDay );
			m_wndList.SetItemText( lItem , 2 , strTemp );
			m_wndList.SetItemText( lItem , 3 , pTS->szOperName );
			m_wndList.SetItemText( lItem , 4 , pTS->szCreateUser );
			m_wndList.SetItemText( lItem , 5 , pTS->szTSMsg );
			m_wndList.SetItemText( lItem , 6 , pTS->szTSProcess );


			if ( pTS->ucStatus == 0 )
			{
				pTS->ucStatus = CORP_TS_MODIFY;  //��Ҫ�޸�
				m_lModifyCount ++;    //��Ҫ�޸ĵļ�¼����
			}
		}
	}		
}

long DLG_Corp_TS::GetCurrentSelected()
{
	POSITION pos = m_wndList.GetFirstSelectedItemPosition();
	if ( pos )
	{
		return m_wndList.GetNextSelectedItem(pos);
	}
	return -1;
}