// C_FaxNum.cpp : implementation file
//

#include "stdafx.h"
#include "smsmanager.h"
#include "C_FaxNum.h"

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

/////////////////////////////////////////////////////////////////////////////
// CC_FaxNum dialog
#include "MainFrm.h"
#include "ProcessSocket.h"
#include "DLG_C_FaxNum_Set.h"


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


void CC_FaxNum::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CC_FaxNum)
	DDX_Control(pDX, IDC_C_PAY_CORPID, m_E_CorpID);
	DDX_Control(pDX, IDC_C_PAY_CORP, m_L_Corp);
	DDX_Control(pDX, IDC_C_AGENTPAY_S1, m_S_S1);
	DDX_Control(pDX, IDC_C_AGENTPAY_LIST, m_wndList);
	DDX_DateTimeCtrl(pDX, IDC_C_AGENTPAY_TBEGIN, m_tBegin);
	DDX_DateTimeCtrl(pDX, IDC_C_AGENTPAY_TEND, m_tEnd);
	DDX_CBIndex(pDX, IDC_C_PAY_CORP, m_lCorp);
	DDX_CBIndex(pDX, IDC_C_AGENTPAYPAY_FPTYPE, m_lFPType);
	DDX_CBIndex(pDX, IDC_C_AGENTPAYPAY_PAYTYPE, m_lPayType);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CC_FaxNum, CDialog)
	//{{AFX_MSG_MAP(CC_FaxNum)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_C_AGENTPAY_QUERY, OnCAgentpayQuery)
	ON_NOTIFY(NM_DBLCLK, IDC_C_AGENTPAY_LIST, OnDblclkCAgentpayList)
	ON_NOTIFY(NM_RCLICK, IDC_C_AGENTPAY_LIST, OnRclickCAgentpayList)
	ON_BN_CLICKED(IDC_C_PAY_QUERY2, OnCPayQuery2)
	ON_CBN_SELCHANGE(IDC_C_PAY_CORP, OnSelchangeCPayCorp)
	//}}AFX_MSG_MAP
	ON_COMMAND(MENU_FAXNUM_FP     , On_User_FP )
	ON_COMMAND(MENU_FAXNUM_EXPORT  , On_User_Export )

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CC_FaxNum message handlers

void CC_FaxNum::Close()
{
	Close_Corp();
	Close_FaxNumData();
}

BOOL CC_FaxNum::RefreshInfo()
{
	if ( !m_pMainFrame->GetUserPurview(PURVIEW_TAB) )
	{
		return false;
	}
#ifndef SMSMANAGER_KF
	m_pMainFrame->SendFrame(SMSFUNC_CORP_NAME,NULL,0);
#endif
	return true;
}

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

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

BOOL CC_FaxNum::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_Corp();              //�ر�ԭ����
		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_PTR)pos);
			pData ++;
		}
		m_L_Corp.SetCurSel(0);
		return true;
	}
	return false;
}

BOOL CC_FaxNum::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("���,50; �������,100; �û��ʺ�,100; BNet�ʺ�,100;   ����,100;   ������ҵ,150;�û�ID,80;   �� ע,130;"));
	m_wndList.SetGridLines(true);
//	m_wndList.SetImageList(&m_Image,LVSIL_SMALL);

	m_bInit = true;


	SYSTEMTIME t; ::GetLocalTime(&t);
	long lMon = t.wMonth;
	lMon--;
	if ( lMon<=0 )
	{
		lMon = 12;
		t.wYear --;
	}
	t.wMonth = lMon;

	m_tBegin.SetDate( t.wYear,t.wMonth,1 );  //��ʼ
	m_tEnd.SetDate( t.wYear,t.wMonth,1 );//����

	m_lPayType = 0;  //Ĭ�ϲ鿴�����ֻ�����
	m_lFPType = 0;  //Ĭ�ϲ鿴�����ֻ�����


	UpdateData(false);

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

void CC_FaxNum::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);
	}	
}


BOOL CC_FaxNum::PreTranslateMessage(MSG* pMsg) 
{
	if ( pMsg->hwnd == m_E_CorpID && pMsg->message == WM_KEYDOWN )
	{
		if ( pMsg->wParam == 13 ) //�س���
		{
			this->OnCPayQuery2();
			return true;
		}
	}
	
	return CDialog::PreTranslateMessage(pMsg);
}

void CC_FaxNum::OnCAgentpayQuery() 
{
	UpdateData(true);
	if ( m_tEnd < m_tBegin )
	{
		MessageBox( _T("��ѯ�������ڲ���С����ʼ���ڣ������!") , _T("����") , MB_ICONWARNING );
		return ;
	}
	REQ_FaxNum_Get * pPay = new REQ_FaxNum_Get;memset(pPay,0,sizeof(REQ_FaxNum_Get));

	//��ҵ
	if ( m_lCorp <= 0 )  //ѡ��������ҵ
	{
		pPay->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)
		{
			pPay->lCorpID = pData->lCorpID;
		}
		else
		{
			return;
		}
	}

	m_tBegin.GetAsSystemTime( pPay->tBegin );
	m_tEnd.GetAsSystemTime( pPay->tEnd );

	m_pMainFrame->SendFrame(SMSFUNC_FAXNUM_GET,(BYTE*)pPay,sizeof(REQ_FaxNum_Get));
}

void CC_FaxNum::ReShowFaxNum()
{
	long lCorpID=-1;
	m_wndList.ShowWindow(SW_HIDE);
	m_wndList.DeleteAllItems(); //ɾ��ԭ�е�����
	
	POSITION pos = m_FaxNumData.GetHeadPosition();
	POSITION pos2=0;
	int i=0;
	TCHAR Buf[128]={0};
	CString str;
	while ( pos )
	{
		pos2 = pos;
		ANS_FaxNum_Data * pData = m_FaxNumData.GetNext(pos);
		if ( pData )
		{
			if ( true )
			{
				//m_wndList.SetHeadings(_T("���,50; �������,80; �û��ʺ�,80; BNet�ʺ�,80;   ����,80;  ������ҵ,150;�û�ID,80;  �� ע,130;"));
				long iTemp=1;
				_stprintf( Buf , _T("%d") , i+1 );
				m_wndList.InsertItem( i , Buf , 0 );

				m_wndList.SetItemText( i , iTemp++ , pData->szFaxNum);
				m_wndList.SetItemText( i , iTemp++ , pData->szUserAccount);
				m_wndList.SetItemText( i , iTemp++ , pData->szBNetAccount);
				m_wndList.SetItemText( i , iTemp++ , pData->szPasswd);
				str.Format( _T("%d-%s"),pData->lCorpID,pData->szCorpName);
				m_wndList.SetItemText( i , iTemp++ , str);

				str.Format( _T("   %d") , pData->lUserID);  //�û�ID
				m_wndList.SetItemText( i , iTemp++ , str);

				m_wndList.SetItemText( i , iTemp++ , pData->szBZ);

				m_wndList.SetItemData( i , (DWORD_PTR)pos2 );    //��Pos

				
				i++;
			}
		}
	}


	m_wndList.ShowWindow(SW_SHOW);
}

void CC_FaxNum::OnDblclkCAgentpayList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	*pResult = 0;

	On_User_FP();
}

void CC_FaxNum::OnRclickCAgentpayList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	*pResult = 0;

	ShowRMenu();
}

void CC_FaxNum::ShowRMenu()
{
	if ( !m_pMainFrame->GetUserPurview(PURVIEW_TAB) )
	{
		return ;
	}

	CMenu menu;
	menu.CreatePopupMenu();
	BOOL bLock = false;
	long lItem = this->GetCurrentSelected();
	ANS_FaxNum_Data * pData = NULL;
	if ( lItem>=0 )
	{
		POSITION pos = (POSITION)m_wndList.GetItemData(lItem);
		if ( (LONG_PTR)pos>=0 )
		{
			pData = m_FaxNumData.GetNext( pos );
		}
	}
	if ( lItem>=0 && pData  )
	{
		menu.AppendMenu(MF_STRING     , MENU_FAXNUM_FP    , _T(" ����������ҵ ") );
	}
	else
	{
		menu.AppendMenu(MF_STRING|MF_DISABLED|MF_GRAYED   , MENU_FAXNUM_FP    , _T(" ����������ҵ ") );
	}
#ifndef SMSMANAGER_KF
	menu.AppendMenu(MF_SEPARATOR );
	menu.AppendMenu(MF_STRING     , MENU_FAXNUM_EXPORT    , _T(" �������� ") );
#endif
	//��ʾ�˵�
	POINT pt ;
	::GetCursorPos( &pt );
	//this->ClientToScreen( &pt);
	menu.TrackPopupMenu(TPM_LEFTBUTTON | TPM_RIGHTBUTTON,pt.x,pt.y, this, NULL);
	menu.DestroyMenu();
}

long CC_FaxNum::GetCurrentSelected()
{
	POSITION pos = m_wndList.GetFirstSelectedItemPosition();
	if ( pos )
	{
		return m_wndList.GetNextSelectedItem(pos);
	}
	return -1;
}
void CC_FaxNum::On_User_FP()
{
	if ( !m_pMainFrame->GetUserPurview(PURVIEW_TAB) )
	{
		return ;
	}
	long lItem = this->GetCurrentSelected();
	if ( lItem <0 )
		return ;

	ANS_FaxNum_Data * pData = NULL;
	POSITION pos = (POSITION)m_wndList.GetItemData(lItem);
	if ( (LONG_PTR)pos>=0 )
	{
		pData = m_FaxNumData.GetNext( pos );
	}

	DLG_C_FaxNum_Set dlg(this);
	dlg.SetParam(*pData);
	if ( dlg.DoModal()==IDOK )
	{
		REQ_FaxNum_Set * pSet = new REQ_FaxNum_Set;
		memset( pSet , 0 , sizeof(REQ_FaxNum_Set));

		pSet->lItem     = lItem;
		pSet->lFaxNumID = pData->lFaxNumID;
		pSet->fax       = dlg.GetFaxNumData();
	
		m_pMainFrame->SendFrame( SMSFUNC_FAXNUM_SET , (BYTE*)pSet , sizeof(REQ_FaxNum_Set));
		
	}


}
void CC_FaxNum::On_User_Export()
{
	m_wndList.CopyToClipboard();
}

void CC_FaxNum::Close_FaxNumData()
{
	//������������
	while (!m_FaxNumData.IsEmpty())
	{
		ANS_FaxNum_Data * pData = m_FaxNumData.GetHead();
		m_FaxNumData.RemoveHead();
		delete pData;
	}
}

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

void CC_FaxNum::OnCPayQuery2() 
{
	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);
			OnSelchangeCPayCorp();
			return ;
		}
	}
	for ( i=0 ; i<lSel ; i++ )
	{
		m_L_Corp.GetLBText(i,strTemp);
		if ( strTemp.Find(strFind)>=0 )
		{
			m_L_Corp.SetCurSel(i);
			OnSelchangeCPayCorp();
			return ;
		}
	}
}

void CC_FaxNum::OnSelchangeCPayCorp() 
{
	
}

BOOL CC_FaxNum::ProcessSocket(Socket_Head_Add *pHead, BYTE *pFrame)
{
	//ȡ����Ա��Ϣ
	if (pHead->lFuncType == SMSFUNC_FAXNUM_GET)
	{
		m_bRefresh = true;  //�Ѿ�ˢ�¹�
		ANS_FaxNum_Get * pFax = (ANS_FaxNum_Get *)pFrame;
		ANS_FaxNum_Data * pData = (ANS_FaxNum_Data *)(pFrame+sizeof(ANS_FaxNum_Get));
		this->Close_FaxNumData();              //�ر�ԭ����
		for ( int i=0;i<pFax->lCount;i++ )
		{
			ANS_FaxNum_Data * pNewData = new ANS_FaxNum_Data;
			*pNewData = *pData;
			POSITION pos = m_FaxNumData.AddTail(pNewData);
			pData ++;
		}
		ReShowFaxNum();
		return true;
	}
	if ( pHead->lFuncType == SMSFUNC_FAXNUM_SET )
	{
		ANS_FaxNum_Set * pFax = (ANS_FaxNum_Set *)pFrame;
		if ( pFax->bOK )
		{
			ANS_FaxNum_Data * pData = NULL;
			POSITION pos = (POSITION)m_wndList.GetItemData(pFax->lItem);
			if ( (LONG_PTR)pos>=0 )
			{
				pData = m_FaxNumData.GetNext( pos );
			}
			if ( pData )
			{
				pData->lCorpID = pFax->fax.lCorpID;
				_tcscpy(pData->szBZ , pFax->fax.szBZ);
			}

			//��������
			CString str;
			str.Format( _T("%d-%s"),pFax->fax.lCorpID,pFax->fax.szCorpName);
			m_wndList.SetItemText( pFax->lItem , 5 , str);

			m_wndList.SetItemText( pFax->lItem , 7 , pFax->fax.szBZ);


		}
		else
		{
			MessageBox(_T("���䴫���������������Ա��ϵ!") , _T("����") , MB_ICONWARNING|MB_OK);
		}
		return true;
	}
	

	return false;
}