// C_DBB.cpp : implementation file
//

#include "stdafx.h"
#include "smsmanager.h"
#include "C_DBB.h"

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

/////////////////////////////////////////////////////////////////////////////
// CC_DBB dialog
#include "MainFrm.h"
#include "ProcessSocket.h"


CC_DBB::CC_DBB(CWnd* pParent /*=NULL*/)
	: CDialog(CC_DBB::IDD, pParent)
{
	//{{AFX_DATA_INIT(CC_DBB)
	m_tBegin = COleDateTime::GetCurrentTime();
	m_tEnd = COleDateTime::GetCurrentTime();
	m_bMS = FALSE;
	m_lCorp = -1;
	//}}AFX_DATA_INIT

	m_bInit = false;
	m_bRefresh = false;
}


void CC_DBB::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CC_DBB)
	DDX_Control(pDX, IDC_C_DBB_CORPID, m_E_CorpID);
	DDX_Control(pDX, IDC_C_DBB_CORP, m_L_Corp);
	DDX_Control(pDX, IDC_C_TC_LIST, m_wndList);
	DDX_Control(pDX, IDC_C_TC_S1, m_S_S1);
	DDX_DateTimeCtrl(pDX, IDC_C_TC_TBEGIN, m_tBegin);
	DDX_DateTimeCtrl(pDX, IDC_C_TC_TEND, m_tEnd);
	DDX_Check(pDX, IDC_C_TC_MS, m_bMS);
	DDX_CBIndex(pDX, IDC_C_DBB_CORP, m_lCorp);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CC_DBB, CDialog)
	//{{AFX_MSG_MAP(CC_DBB)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_C_TC_QUERY, OnCTcQuery)
	ON_BN_CLICKED(IDC_C_DBB_QUERY2, OnCDbbQuery2)
	ON_NOTIFY(NM_RCLICK, IDC_C_TC_LIST, OnRclickCTcList)
	//}}AFX_MSG_MAP
	ON_COMMAND(MENU_DBB_COPY     , On_DBB_Copy )
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CC_DBB message handlers

void CC_DBB::Close()
{
	//������������
	while (!m_Corp.IsEmpty())
	{
		ANS_Corp_NameData * pData = m_Corp.GetHead();
		m_Corp.RemoveHead();
		delete pData;
	}
}

BOOL CC_DBB::RefreshInfo()
{
	if ( !m_pMainFrame->GetUserPurview(PURVIEW_TAB) )
	{
		return false;
	}
	if ( !m_pMainFrame->GetUserPurview(PURVIEW_MANAGER_SEE) )
	{
		return false;
	}

	m_pMainFrame->SendFrame(SMSFUNC_CORP_NAME,NULL,0);
	return true;
}

BOOL CC_DBB::ReHide()
{
	ShowWindow( SW_HIDE );
	return true;
}

BOOL CC_DBB::ReShow(BOOL bRefresh)
{
	ShowWindow( SW_SHOW );
	if ( bRefresh || !m_bRefresh )
	{
		RefreshInfo();
	}
	return true;
}

BOOL CC_DBB::ProcessSocket(Socket_Head *pHead)
{
	CString strTemp;
	if (pHead->lFuncType == SMSFUNC_CORP_NAME)
	{
		m_bRefresh = true;  //�Ѿ�ˢ�¹�
		ANS_Corp_Name     * pCorp = (ANS_Corp_Name *)pHead->pFrame;
		ANS_Corp_NameData * pData = (ANS_Corp_NameData *)(pHead->pFrame+sizeof(ANS_Corp_Name));
		m_L_Corp.ResetContent();  //ɾ��ԭ���е���ҵ
		m_L_Corp.AddString( _T("������ҵ") );
		this->Close();              //�ر�ԭ����
		for ( int i=0;i<pCorp->lCount;i++ )
		{
			ANS_Corp_NameData * pNewData = new ANS_Corp_NameData;
			*pNewData = *pData;
			POSITION pos = m_Corp.AddTail(pNewData);
			strTemp.Format( _T("%d-%s"),pData->lCorpID,pData->szCorpName);
			long lItem = m_L_Corp.AddString( strTemp );
			m_L_Corp.SetItemData( lItem , (DWORD)pos);
			pData ++;
		}
		m_L_Corp.SetCurSel(0);
		return true;
	}

	if ( pHead->lFuncType == SMSFUNC_DBB )
	{
		ANS_DBB     * pTab  = (ANS_DBB *)pHead->pFrame;
		ANS_DBBData * pData = (ANS_DBBData *)(pHead->pFrame+sizeof(ANS_DBB));
		if ( pTab->bMS )  //��ѯ��ϸ����
		{
			//m_wndList.ShowWindow(SW_HIDE);  //�ӿ���ʾ�ٶ�
			//m_wndList.DeleteAllColumns();
			//m_wndList.SetHeadings(_T("���,50; ��ҵID,70;��ҵ����,250;"));
			//m_wndList.DeleteAllItems();
			long lColMon[100][3]={0}; //��¼���������·�
			long lMaxMon=0;
			TCHAR Buf[32];
			CString strTemp;;
			long lCount=0;
			long lAllCount[36]={0};
			long lOldCorpID=0;
			for ( int i=0;i<pTab->lCount;i++)
			{
				if ( lOldCorpID != pData->lCorpID )
				{
					lOldCorpID = pData->lCorpID;
					lCount = m_wndList.GetItemCount();
					m_wndList.InsertItem( lCount , _itot(lCount+1,Buf,10),0);  //���
					strTemp.Format( _T("%d") , pData->lCorpID );
					m_wndList.SetItemText(lCount,1,strTemp);
					//m_wndList.SetItemText(lCount,2,GetName_Corp(pData->lCorpID));
					m_wndList.SetItemText(lCount,2,pData->szCorpName);
				}
				//�ҵ���Ӧ����
				long lMon = pData->tTJTime.wYear*100+pData->tTJTime.wMonth;
				long lColMon2=-1;
				for (int j=0 ; j<lMaxMon; j++ )
				{
					if (lColMon[j][0] == lMon )
					{
						lColMon2 = j;
						break;
					}
				}
				//û�е��У�Ҫ����
				if ( lColMon2 <0 )
				{
					strTemp.Format( _T("%04d.%02d����") , pData->tTJTime.wYear,pData->tTJTime.wMonth );
					m_wndList.InsertColumn(3+(lMaxMon*2),strTemp,LVCFMT_LEFT,100);
					strTemp.Format( _T("%04d.%02d����") , pData->tTJTime.wYear,pData->tTJTime.wMonth );
					m_wndList.InsertColumn(3+(lMaxMon*2)+1,strTemp,LVCFMT_LEFT,100);
					lColMon[lMaxMon][0] = lMon;
					lColMon2 = lMaxMon;
					lMaxMon ++;
				}
				lColMon[lColMon2][1] += pData->lSmsCount; //��¼����
				lColMon[lColMon2][2] += pData->lMmsCount; //��¼����

				lColMon2=lColMon2*2+3; //ǰ�滹��һЩ��
				strTemp.Format( _T("%d") , pData->lSmsCount );
				m_wndList.SetItemText(lCount,lColMon2,strTemp);
				strTemp.Format( _T("%d") , pData->lMmsCount );
				m_wndList.SetItemText(lCount,lColMon2+1,strTemp);

				pData++;
			}
			lCount = m_wndList.GetItemCount();
			m_wndList.InsertItem( lCount , _T(""),0);  //�ϼ�
			m_wndList.SetItemText(lCount,1, _T("�� ��"));  //�ϼ�

			for ( i=0 ; i<lMaxMon ; i++ )
			{
				strTemp.Format( _T("%d") , lColMon[i][1] );
				m_wndList.SetItemText( lCount,i*2+3, strTemp);  //�ϼ�
				strTemp.Format( _T("%d") , lColMon[i][2] );
				m_wndList.SetItemText( lCount,i*2+1+3, strTemp);  //�ϼ�
			}
			m_wndList.ShowWindow(SW_SHOW);  //�ӿ���ʾ�ٶ�
		}
		else
		{
			//��ѯ��������
			//m_wndList.ShowWindow(SW_HIDE);  //�ӿ���ʾ�ٶ�
			//m_wndList.DeleteAllColumns();
			//m_wndList.SetHeadings(_T("���,50; ��ҵID,70;��ҵ����,250;������,100;������,100;"));
			//m_wndList.DeleteAllItems();
			TCHAR Buf[32];
			CString strTemp;
			long lCount;
			long lSmsCount = 0;
			long lMmsCount = 0;
			for ( int i=0;i<pTab->lCount;i++)
			{
				lCount = m_wndList.GetItemCount();
				m_wndList.InsertItem( lCount , _itot(i+1,Buf,10),0);  //���
				strTemp.Format( _T("%d") , pData->lCorpID);
				m_wndList.SetItemText(lCount,1,strTemp);
				//m_wndList.SetItemText(lCount,2,GetName_Corp(pData->lCorpID));
				m_wndList.SetItemText(lCount,2,pData->szCorpName);
				strTemp.Format( _T("%d") , pData->lSmsCount );
				m_wndList.SetItemText(lCount,3,strTemp);
				strTemp.Format( _T("%d") , pData->lMmsCount );
				m_wndList.SetItemText(lCount,4,strTemp);
				lSmsCount += pData->lSmsCount;
				lMmsCount += pData->lMmsCount;
				pData++;
			}
			lCount = m_wndList.GetItemCount();
			m_wndList.InsertItem( lCount , _T(""),0);  //�ϼ�
			m_wndList.SetItemText( lCount,1, _T("�� ��"));  //�ϼ�
			m_wndList.SetItemText( lCount,3, _itot(lSmsCount,Buf,10));  //�ϼ�
			m_wndList.SetItemText( lCount,4, _itot(lMmsCount,Buf,10));  //�ϼ�
			m_wndList.ShowWindow(SW_SHOW);  //�ӿ���ʾ�ٶ�
		}

		return true;
	}

	return false;
}

BOOL CC_DBB::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//ȡ�ù��ò���
	//CMainFrame * pFrame = static_cast <CMainFrame *>((CMainFrame *)AfxGetMainWnd());
	this->GetParent()->GetParentFrame();
	m_pMainFrame = (CMainFrame *)this->GetParent()->GetParentFrame();
	m_pSocket    = &m_pMainFrame->m_Socket;

	//��ʼ��List
	m_Image.Create(16,16,ILC_COLOR16|ILC_MASK,5,5);
//	m_Image.Add(AfxGetApp()->LoadIcon(IDI_L_TAB) );
	m_wndList.SubClassWindow2();
	m_wndList.SetHeadings(_T("���,50; ��ҵID,��ҵ����,200; �·�,80; ��������,100;"));
	m_wndList.SetGridLines(true);
	m_wndList.SetImageList(&m_Image,LVSIL_SMALL);

	m_bInit = true;

	/*
	//Ĭ��ȡ���µ�����
	SYSTEMTIME t; ::GetLocalTime(&t);
	t.wDay = 1;//��1����
	t.wMonth = t.wMonth-1;
	if ( t.wMonth <=0 )
	{
		t.wMonth = 12;
		t.wYear  = t.wYear-1;
	}
	m_tBegin.SetDate( t.wYear,t.wMonth,t.wDay );  //��ʼ
	if ( t.wMonth == 1 ||
		 t.wMonth == 3 ||
		 t.wMonth == 5 ||
		 t.wMonth == 7 ||
		 t.wMonth == 8 ||
		 t.wMonth == 10 ||
		 t.wMonth == 12 )
	{
		 t.wDay = 31;
	}
	else
		if (t.wMonth == 2 )
			t.wDay = 28;
		else
			t.wDay = 30;
	m_tEnd.SetDate( t.wYear,t.wMonth,t.wDay );//����
	*/

	COleDateTime t = COleDateTime::GetCurrentTime();
	m_tBegin.SetDate( t.GetYear(),t.GetMonth(),1 );  //��ʼ
	//COleDateTimeSpan tSpan2(3,0,0,0);
	//t = t-tSpan2;
	//m_tEnd.SetDate( t.GetYear(),t.GetMonth(),t.GetDay() );//����

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

void CC_DBB::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	if ( m_bInit )
	{
		CRect rect;
		m_S_S1.GetWindowRect(&rect);
		m_wndList.MoveWindow( 0 , 0+rect.Height()+5 , cx,cy-rect.Height()-5);
	}	
}

void CC_DBB::OnCTcQuery() 
{
	if ( !m_pMainFrame->GetUserPurview(PURVIEW_LIST) )
	{
		return ;
	}
	if ( !m_pMainFrame->GetUserPurview(PURVIEW_MANAGER_SEE) )
	{
		return ;
	}

	UpdateData(true);
	COleDateTime tBegin2(m_tBegin.GetYear(),m_tBegin.GetMonth(),1,0,0,0);
	COleDateTime tEnd2(m_tEnd.GetYear(),m_tEnd.GetMonth(),1,0,0,0);
	if ( tEnd2 < tBegin2 )
	{
		MessageBox( _T("��ѯ�������ڲ���С����ʼ���ڣ������!") , _T("����") , MB_ICONWARNING );
		return ;
	}
	if ( m_bMS )  //��ѯ��ϸ����
	{
		m_wndList.ShowWindow(SW_HIDE);  //�ӿ���ʾ�ٶ�
		m_wndList.DeleteAllItems();
		m_wndList.DeleteAllColumns();
		m_wndList.SetHeadings(_T("���,50; ��ҵID,70;��ҵ����,250;"));
		m_wndList.DeleteAllItems();
		m_wndList.ShowWindow(SW_SHOW);
	}
	else
	{
		m_wndList.ShowWindow(SW_HIDE);  //�ӿ���ʾ�ٶ�
		m_wndList.DeleteAllItems();
		m_wndList.DeleteAllColumns();
		m_wndList.SetHeadings(_T("���,50; ��ҵID,70;��ҵ����,250;������,100;������,100;"));
		m_wndList.DeleteAllItems();
		m_wndList.ShowWindow(SW_SHOW);
	}
	REQ_DBB * pTab = new REQ_DBB;memset(pTab,0,sizeof(REQ_DBB));

	pTab->lCorpID = -1;
	if ( m_lCorp >= 0 )
	{
		if ( m_lCorp == 0 )  //ѡ��������ҵ
		{
			pTab->lCorpID = -1;
		}
		else
		{
			POSITION pos = (POSITION)m_L_Corp.GetItemData(m_lCorp);
			POSITION pos2 = pos;
			ANS_Corp_NameData * pData = m_Corp.GetNext( pos );
			if (pData)
			{
				pTab->lCorpID = pData->lCorpID;
			}
			else
			{
				return;
			}
		}
	}

	tBegin2.GetAsSystemTime( pTab->tBegin );
	tEnd2.GetAsSystemTime( pTab->tEnd );
	long lYear = pTab->tEnd.wYear;
	long lMon = pTab->tEnd.wMonth;
	long lDay = 30;
	if ( lMon == 1 ||
		 lMon == 3 ||
		 lMon == 5 ||
		 lMon == 7 ||
		 lMon == 8 ||
		 lMon == 10 ||
		 lMon == 12 )
	{
		 lDay = 31;
	}
	else
		if (lMon == 2 )
			if ( lYear%4 == 0 )
				lDay = 29;
			else
				lDay = 28;
		else
			lDay = 30;

	pTab->tEnd.wDay = lDay;
	pTab->bMS = m_bMS;
	m_pMainFrame->SendFrame(SMSFUNC_DBB,(BYTE*)pTab,sizeof(REQ_DBB));
	return ;	
}

void CC_DBB::OnCDbbQuery2() 
{
	CString strTemp;
	CString strFind;
	m_E_CorpID.GetWindowText(strFind);
	if ( strFind.GetLength()<=0 )
		return ;
	long lSel   = m_L_Corp.GetCurSel();
	long lCount = m_L_Corp.GetCount();
	for ( int i=lSel+1 ; i<lCount ; i++ )
	{
		m_L_Corp.GetLBText(i,strTemp);
		if ( strTemp.Find(strFind)>=0 )
		{
			m_L_Corp.SetCurSel(i);
			return ;
		}
	}
	for ( i=0 ; i<lSel ; i++ )
	{
		m_L_Corp.GetLBText(i,strTemp);
		if ( strTemp.Find(strFind)>=0 )
		{
			m_L_Corp.SetCurSel(i);
			return ;
		}
	}		
}

CString CC_DBB::GetName_Corp(long lCorpID)
{
	//������
	CString strTemp=_T("");
	POSITION pos = m_Corp.GetHeadPosition();
	POSITION pos2;
	while (pos)
	{
		pos2=pos;
		ANS_Corp_NameData * pCorp = m_Corp.GetNext( pos );
		if ( pCorp->lCorpID == lCorpID )
		{
			strTemp = pCorp->szCorpName;
			break;
		}
	}
	return strTemp;
}

void CC_DBB::OnRclickCTcList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	*pResult = 0;

	ShowRMenu();
}

void CC_DBB::ShowRMenu()
{
	if ( !m_pMainFrame->GetUserPurview(PURVIEW_MANAGER_SEE,false) )
	{
		return ;
	}

	CMenu menu;
	menu.CreatePopupMenu();
	menu.AppendMenu(MF_STRING     , MENU_DBB_COPY    , _T(" ���� ") );

	//��ʾ�˵�
	POINT pt ;
	::GetCursorPos( &pt );
	//this->ClientToScreen( &pt);
	menu.TrackPopupMenu(TPM_LEFTBUTTON | TPM_RIGHTBUTTON,pt.x,pt.y, this, NULL);
	menu.DestroyMenu();
}

void CC_DBB::On_DBB_Copy()
{
	m_wndList.CopyToClipboard();
}