// C_JCB.cpp : implementation file
//

#include "stdafx.h"
#include "smsmanager.h"
#include "C_JCB.h"

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

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

CC_JCB::CC_JCB(CWnd* pParent /*=NULL*/)
	: CDialog(CC_JCB::IDD, pParent)
{
	//{{AFX_DATA_INIT(CC_JCB)
	m_lCorp = -1;
	m_tBegin = COleDateTime::GetCurrentTime();
	m_lQueryType = -1;
	m_tEnd = COleDateTime::GetCurrentTime();
	//}}AFX_DATA_INIT
	m_bInit = false;
	m_bRefresh = false;
	m_bProcess = false;
}


void CC_JCB::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CC_JCB)
	DDX_Control(pDX, IDC_C_JCB_QUERYTYPE, m_L_QueryType);
	DDX_Control(pDX, IDC_C_JCB_MSG, m_E_Msg);
	DDX_Control(pDX, IDC_C_JCB_S1, m_S_S1);
	DDX_Control(pDX, IDC_C_JCB_LIST, m_wndList);
	DDX_Control(pDX, IDC_C_JCB_CORPID, m_E_CorpID);
	DDX_Control(pDX, IDC_C_JCB_CORP, m_L_Corp);
	DDX_CBIndex(pDX, IDC_C_JCB_CORP, m_lCorp);
	DDX_DateTimeCtrl(pDX, IDC_C_JCB_TBEGIN, m_tBegin);
	DDX_CBIndex(pDX, IDC_C_JCB_QUERYTYPE, m_lQueryType);
	DDX_DateTimeCtrl(pDX, IDC_C_JCB_TEND, m_tEnd);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CC_JCB, CDialog)
	//{{AFX_MSG_MAP(CC_JCB)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_C_JCB_QUERY2, OnCJcbQuery2)
	ON_BN_CLICKED(IDC_C_JCB_QUERY, OnCJcbQuery)
	ON_CBN_SELCHANGE(IDC_C_JCB_QUERYTYPE, OnSelchangeCJcbQuerytype)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CC_JCB message handlers

BOOL CC_JCB::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_LIST) );
	m_wndList.SubClassWindow2();
	m_wndList.SetHeadings(_T("���,40;��ҵID,70;    ��ҵ����,150;���ڽ��,80;���ڴ���,80;��������,80;����ʹ��,80;���ڽ��,80;"));
	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 );//����
	*/

	SYSTEMTIME t; ::GetLocalTime(&t);
	m_tBegin.SetDate( t.wYear,t.wMonth,1 );  //��ʼ
	m_tEnd.SetDate( t.wYear,t.wMonth,t.wDay );//����
	m_tEnd = m_tEnd - COleDateTimeSpan(3,0,0,0); //��3��

	m_lQueryType = 0;  //Ĭ�ϲ鿴�����ֻ�����

	UpdateData(false);	

	OnSelchangeCJcbQuerytype();

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


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

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

BOOL CC_JCB::RefreshInfo()
{
	if ( !m_pMainFrame->GetUserPurview(PURVIEW_TAB) )
	{
		return false;
	}
	m_pMainFrame->SendFrame(SMSFUNC_CORP_NAME,NULL,0);
	return true;
}

BOOL CC_JCB::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_JCB_SEE )
	{
		ANS_JCB_See     * pJCB = (ANS_JCB_See *)pHead->pFrame;
		ANS_JCB_SeeData * pData= (ANS_JCB_SeeData *)(pHead->pFrame+sizeof(ANS_JCB_See));
		m_wndList.ShowWindow( SW_HIDE );  //�ӿ��ٶ�
		m_wndList.DeleteAllItems();
		TCHAR Buf[128]={0};
		long   lSmsAdd=0;
		long   lPresent=0;
		long   lSmsSend=0;
		long   lSmsAll=0;
		CString strMsg;
		for ( int i=0 ; i<pJCB->lCount ; i++ )
		{
			lSmsAdd += pData->lAddCount;
			lPresent += pData->lPresentCount;
			lSmsSend += pData->lSendCount;
			lSmsAll += pData->lUseCount;
			    strMsg+=_T("\r\n\r\n---------------------------------------------------------------");
				_stprintf( Buf ,_T("\r\n          ��ҵ�����ʵ� %04d.%02d.%02d - %04d.%02d.%02d"),pJCB->tBegin.wYear,pJCB->tBegin.wMonth,pJCB->tBegin.wDay,pJCB->tEnd.wYear,pJCB->tEnd.wMonth,pJCB->tEnd.wDay );
				strMsg+= Buf;
				         strMsg+= _T("\r\n                         (��λ:��)\r\n");
			//m_wndList.SetHeadings(_T("���,40;��ҵID,70;    ��ҵ����,150;���ڽ��,80;���ڴ���,80;��������,80;����ʹ��,80;���ڽ��,80;"));
			_stprintf( Buf , _T("%d") , i+1 );
			m_wndList.InsertItem( i , Buf , 0 );   //ItemΪ0��ԭ���ǵ�ת��ʾ
			_stprintf( Buf , _T("%d"),pData->lCorpID);
			m_wndList.SetItemText( i , 1 , Buf);
				strMsg+= _T("\r\n    ��ҵID:");
				strMsg+= Buf;
			m_wndList.SetItemText( i , 2 , pData->szCorpName );
				strMsg+= _T("\r\n  ��ҵ����:");
				strMsg+= pData->szCorpName;
			_stprintf( Buf , _T("%8d") , pData->lOldCount);
			m_wndList.SetItemText( i , 3 , Buf );
				strMsg+= _T("\r\n          ���ڽ��:");
				strMsg+= Buf;
			_stprintf( Buf , _T("%8d") , pData->lAddCount);
			m_wndList.SetItemText( i , 4 , Buf );
				strMsg+= _T("\r\n          ���ڴ���:");
				strMsg+= Buf;
			_stprintf( Buf , _T("%8d") , pData->lPresentCount);
			m_wndList.SetItemText( i , 5 , Buf );
				strMsg+= _T("\r\n          ��������:");
				strMsg+= Buf;
			_stprintf( Buf , _T("%8d") , pData->lSendCount);
			m_wndList.SetItemText( i , 6 , Buf );
				strMsg+= _T("\r\n          ����ʹ��:");
				strMsg+= Buf;

			_stprintf( Buf , _T("%8d") , pData->lUseCount);
			m_wndList.SetItemText( i , 7 , Buf );
				strMsg+= _T("\r\n          ���ڽ��:");
				strMsg+= Buf;

			pData ++;
		}
		
		m_wndList.InsertItem( i , _T("") , 0 );   //ItemΪ0��ԭ���ǵ�ת��ʾ
		i++;
		_stprintf( Buf , _T("%d") , i+1 );
		m_wndList.InsertItem( i , Buf , 0 );   //ItemΪ0��ԭ���ǵ�ת��ʾ
		_stprintf( Buf , _T("%8d") , lSmsAdd);
		m_wndList.SetItemText( i , 4 , Buf );
		_stprintf( Buf , _T("%8d") , lPresent);
		m_wndList.SetItemText( i , 5 , Buf );
		_stprintf( Buf , _T("%8d") , lSmsSend);
		m_wndList.SetItemText( i , 6 , Buf );
		_stprintf( Buf , _T("%8d") , lSmsAll);
		m_wndList.SetItemText( i , 7 , Buf );


		m_wndList.ShowWindow( SW_SHOW );  //�ӿ��ٶ�
		m_E_Msg.SetWindowText(strMsg);
	}

	return false;
}

void CC_JCB::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);
		m_E_Msg.MoveWindow( 0 , 0+rect.Height()+5 , cx,cy-rect.Height()-5);
	}		
}

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

void CC_JCB::OnCJcbQuery2() 
{
	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 ;
		}
	}	
}

void CC_JCB::OnCJcbQuery() 
{
	UpdateData(true);
	if ( m_tEnd > (COleDateTime::GetCurrentTime() - COleDateTimeSpan(3,0,0,0)) )
	{
		MessageBox( _T("�����Ľ������ڱ���������ǰ�������!") , _T("����") , MB_ICONWARNING );
		//m_tEnd = m_tEnd - COleDateTime(0,0,3,0,0,0); //��3��
		UpdateData(false);
		return ;
	}
	
	if ( m_tEnd < m_tBegin )
	{
		MessageBox( _T("��ѯ�������ڲ���С����ʼ���ڣ������!") , _T("����") , MB_ICONWARNING );
		return ;
	}
	REQ_JCB_See * pJCB = new REQ_JCB_See;memset(pJCB,0,sizeof(REQ_JCB_See));

	if ( m_lCorp == 0 )  //ѡ��������ҵ
	{
		pJCB->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)
		{
			pJCB->lCorpID = pData->lCorpID;
		}
		else
		{
			return;
		}
	}
	m_tBegin.GetAsSystemTime( pJCB->tBegin );
	m_tEnd.GetAsSystemTime( pJCB->tEnd );
	pJCB->lQueryType = m_lQueryType; 
	m_pMainFrame->SendFrame(SMSFUNC_JCB_SEE,(BYTE*)pJCB,sizeof(REQ_JCB_See));

	return ;
}

void CC_JCB::OnSelchangeCJcbQuerytype() 
{
	if ( m_L_QueryType.GetCurSel() == 0 )
	{
		m_wndList.ShowWindow( SW_SHOW );
		m_E_Msg.ShowWindow( SW_HIDE );
	}
	else
	{
		m_wndList.ShowWindow( SW_HIDE );
		m_E_Msg.ShowWindow( SW_SHOW );
	}
	
}