// outsourcingDlg2.cpp : implementation file
//
#include "stdafx.h"
#include "ylgl.h"
#include "outsourcingDlg2.h"
#include "SelProvider.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// outsourcingDlg2
outsourcingDlg2::outsourcingDlg2(CWnd* pParent /*=NULL*/)
: CDialog(outsourcingDlg2::IDD)
{
	//{{AFX_DATA_INIT(outsourcingDlg2)IDC_COMBOid
	m_name1 = _T("");
	m_name2 = _T("");
	m_id = _T("");
	m_money1 = _T("");
	m_money2 = _T("");
	m_money3 = _T("");
	//}}AFX_DATA_INIT
}

void outsourcingDlg2::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(outsourcingDlg2)
	DDX_Control(pDX, IDC_LIST1, m_List1);
	DDX_Text(pDX, IDC_EDITname1, m_name1);
	DDX_Text(pDX, IDC_EDITname2, m_name2);
	DDX_Text(pDX, IDC_EDITid2, m_id);
	DDX_Text(pDX, IDC_EDITmoney1, m_money1);
	DDX_Text(pDX, IDC_EDITmoney2, m_money2);
	DDX_Text(pDX, IDC_EDITmoney3, m_money3);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(outsourcingDlg2, CDialog)
	//{{AFX_MSG_MAP(outsourcingDlg2)
	ON_BN_CLICKED(IDC_BUTclose, OnBUTclose)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTget, OnBUTget)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BTNdel, OnBTNdel)
	ON_BN_CLICKED(IDC_BTNdel2, OnBTNdel2)
	ON_WM_TIMER()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// outsourcingDlg2 diagnostics

/////////////////////////////////////////////////////////////////////////////
// outsourcingDlg2 message handlers

BOOL outsourcingDlg2::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message==WM_LBUTTONDBLCLK)
	{//list1
		CPoint pt;
		CRect rc,rc2;
		::GetCursorPos (&pt);
		m_List1.GetWindowRect (rc2);
		if(rc2.PtInRect (pt)==0)	return CDialog::PreTranslateMessage(pMsg);
		pt.x-=rc2.left ;
		pt.y-=rc2.top ;
		int iItem=m_List1.GetItemCount ()-1;
		if(iItem<0)
		{
			m_List1.GetHeaderCtrl()->GetItemRect (0, rc);
			int hei=rc.Height ();
			rc.top +=hei;
			rc.bottom +=hei;
			if(rc.PtInRect (pt))
			{
				m_List1.InsertItem(m_List1.GetItemCount (), "");
			}
			m_List1.GetHeaderCtrl()->GetItemRect (1, rc);
			rc.top +=hei;
			rc.bottom +=hei;
			if(rc.PtInRect (pt))
			{
				m_List1.InsertItem(m_List1.GetItemCount (), "");
			}
			UpdateZ();

		}
		else
		{
			m_List1.GetSubItemRect( iItem, 0, LVIR_BOUNDS, rc);
			int hei=rc.Height ();
			rc.top +=hei;
			rc.bottom +=hei;
			if(rc.PtInRect (pt))
			{
				if(m_List1.GetItemText (iItem, 0).IsEmpty ())
					return CDialog::PreTranslateMessage(pMsg);
				m_List1.InsertItem(m_List1.GetItemCount (), "");
				UpdateZ();
			}
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void outsourcingDlg2::OnBUTclose()	// �رհ�ť;
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel ();
}

void outsourcingDlg2::GetData() 
{
	// TODO: Add your control notification handler code here
	if(g_bAllBranch==0)
	{
		CString filter="id='"+m_id+"';";
		g_sendhead.code[0]=36;
		g_sendhead.code[1]=9;
		g_sendhead.tabcount=2;
		g_sendhead.bsql=0;
		g_pMainWnd->ProcessChatMessageRequest2(filter);if(g_bSendOK==0)return;
		DataToArray(&m_sparray,&g_List1array);
	}
	else
	{
		g_pMainWnd->OnDisconnect();
		g_branchip=GetIP(m_branch);
		g_pMainWnd->OnDisconnect();
		g_branchname=m_branch;
		g_bBranchModify=1;

		CString filter="id='"+m_id+"';";
		g_sendhead.code[0]=36;
		g_sendhead.code[1]=9;
		g_sendhead.tabcount=2;
		g_sendhead.bsql=0;
		g_pMainWnd->ProcessChatMessageRequest2(filter);if(g_bSendOK==0)return;
		DataToArray(&m_sparray,&g_List1array);
	}



	m_List1.DeleteAllItems ();
	for(int i=0; i<m_sparray.GetSize (); i++)
	{
		m_List1.InsertItem(i, m_sparray.ElementAt (i).ElementAt (0));
		m_List1.SetItemText(i, 1, m_sparray.ElementAt (i).ElementAt (1));
		m_List1.SetItemText(i, 2, m_sparray.ElementAt (i).ElementAt (2));
		m_List1.SetItemText(i, 3, m_sparray.ElementAt (i).ElementAt (3));
		m_List1.SetItemText(i, 4, m_sparray.ElementAt (i).ElementAt (4));
#if 0	// Jeff.delete;
		m_List1.SetItemText(i, 5, m_sparray.ElementAt (i).ElementAt (5));
		m_List1.SetItemText(i, 6, m_sparray.ElementAt (i).ElementAt (6));
#else
		m_List1.SetItemText(i, 5, m_sparray.ElementAt (i).ElementAt (5));
		m_List1.SetItemText(i, 6, m_sparray.ElementAt (i).ElementAt (6));
		m_List1.SetItemText(i, 7, m_sparray.ElementAt (i).ElementAt (7));
		m_List1.SetItemText(i, 8, m_sparray.ElementAt (i).ElementAt (8));
#endif

	}
	UpdateZ();
}
extern BOOL CALLBACK EnumChildProc2(HWND hwnd,LPARAM lParam);

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

	CRect rc2;
	GetWindowRect(rc2);
	::MoveWindow(m_hWnd,g_rc.left,g_rc.top,g_rc.Width(),g_rc.Height(),TRUE);
	g_WidthScale2 = (float)g_rc.Width()/(float)rc2.Width(); // new/Old
	g_HeightScale2 = (float)g_rc.Height()/(float)rc2.Height();
	EnumChildWindows(m_hWnd,(WNDENUMPROC)EnumChildProc2,0);


#ifdef CHILD_VERSION
	GetDlgItem(IDC_STATIC2)->SetWindowText("�ҳ�����:");
	GetDlgItem(IDC_STATIC4)->SetWindowText("��������:");
#endif
	// TODO: Add extra initialization here
	// Here we create the outbar control using the splitter as its parent
	// and setting its id to the first pane.
	m_List1.InitStyle();
	m_List1.InsertColumn(0,_T("��Ƭ����"),LVCFMT_LEFT,300);
	m_List1.InsertColumn(1,_T("����"),LVCFMT_LEFT,60);
	m_List1.InsertColumn(2,_T("���"),LVCFMT_LEFT,80);
	m_List1.InsertColumn(3,_T("�Ƿ񷢳�"),LVCFMT_LEFT,115);
	m_List1.InsertColumn(4,_T("��������"),LVCFMT_LEFT,90);

#if 1// Jeff������ ����,��������;(�뷢��ȡƬһ��);
	m_List1.InsertColumn(5,_T("����"),LVCFMT_LEFT,90);
	m_List1.InsertColumn(6,_T("��������"),LVCFMT_LEFT,90);
	m_List1.InsertColumn(7,_T("�Ƿ����"),LVCFMT_LEFT,90);
	m_List1.InsertColumn(8,_T("��������"),LVCFMT_LEFT,90);
#else
	m_List1.InsertColumn(5,_T("�Ƿ����"),LVCFMT_LEFT,90);
	m_List1.InsertColumn(6,_T("��������"),LVCFMT_LEFT,90);
#endif

	GetData();

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

void outsourcingDlg2::OnOK() // ���水ť;
{
	// TODO: Add extra validation here
	m_sparray.RemoveAll ();
	m_sparray.SetSize(m_List1.GetItemCount ());

	int realcount=0;
	CString str;
	for(int i=0; i<m_List1.GetItemCount (); i++)
	{
		str=m_List1.GetItemText (i, 0);
		str.TrimLeft ();		str.TrimRight ();
		if(str.IsEmpty ())continue;
		m_sparray.ElementAt (realcount).RemoveAll ();	
		m_sparray.ElementAt (realcount).Add ( m_id );
		m_sparray.ElementAt (realcount).Add (m_List1.GetItemText (i, 0) );
		m_sparray.ElementAt (realcount).Add (m_List1.GetItemText (i, 1) );
		m_sparray.ElementAt (realcount).Add (m_List1.GetItemText (i, 2) );
		m_sparray.ElementAt (realcount).Add (m_List1.GetItemText (i, 3) );
		m_sparray.ElementAt (realcount).Add (m_List1.GetItemText (i, 4) );
#if 0
		m_sparray.ElementAt (realcount).Add (m_List1.GetItemText (i, 5) );
		m_sparray.ElementAt (realcount).Add (m_List1.GetItemText (i, 6) );
#else
		m_sparray.ElementAt (realcount).Add (m_List1.GetItemText (i, 5) );	// ����;
		m_sparray.ElementAt (realcount).Add (m_List1.GetItemText (i, 6) );	// ��������;
		m_sparray.ElementAt (realcount).Add (m_List1.GetItemText (i, 7) );	// ȡ��״̬;
		m_sparray.ElementAt (realcount).Add (m_List1.GetItemText (i, 8) );	// ȡ������;
#endif
		realcount++;

	}
	m_sparray.SetSize(realcount);
	if(m_sparray.GetSize()==0)
	{
		m_sparray.SetSize(1);
		m_sparray.ElementAt(0).Add( m_id );
	}
	CMemFile memfile;
	CArchive ar(&memfile, CArchive::store);
	for(int ii=0; ii<m_sparray.GetSize(); ii++)
	{
		m_sparray.ElementAt(ii).Serialize(ar);
	}
	ar.Close();

	int length = memfile.GetLength ();
	BYTE *pData = memfile.Detach();
	int size = m_sparray.GetSize ();
	BYTE *pData2 = new BYTE[length+sizeof(int)];
	memcpy(pData2, pData, length);
	memcpy(pData2+length, &size, sizeof(int));
	delete []pData;
	length+=sizeof(int);

	g_nSendCode=13;
	g_pMainWnd->ProcessChatMessageRequest2(pData2, length);
	g_nSendCode=0;
	delete []pData2;
	if(g_bSendOK==0)return;

	AfxMessageBox("����ɹ�!", MB_ICONINFORMATION);
	CDialog::OnOK();
}

void outsourcingDlg2::OnButton1()	// ȫ��������ť;
{
	// TODO: Add your control notification handler code here
	SelProvider dlg;
	if(dlg.DoModal ()!=IDOK)return;
	for(int i=0; i<m_List1.GetItemCount (); i++)
	{
		m_List1.SetItemText (i, 3, dlg.m_provider);
		m_List1.SetItemText (i, 4, g_date);
	}	
}

void outsourcingDlg2::OnButton3()	// ȫ������-���ð�ť;
{
	// TODO: Add your control notification handler code here
	for(int i=0; i<m_List1.GetItemCount (); i++)
	{
		m_List1.SetItemText (i, 3, "");
		m_List1.SetItemText (i, 4, "");
		m_List1.SetItemText (i, 5, "");
		m_List1.SetItemText (i, 6, "");
#if 1
		m_List1.SetItemText(i,7,"");
		m_List1.SetItemText(i,8,"");
#endif
	}	
}

void outsourcingDlg2::OnButton2()	// ȫ��������ť;
{
	// TODO: Add your control notification handler code here
	for(int i=0; i<m_List1.GetItemCount(); i++)
	{
#if 0
		m_List1.SetItemText (i, 5, "OK");
		m_List1.SetItemText (i, 6, g_date);
#else
		m_List1.SetItemText(i, 7, "OK");
		m_List1.SetItemText(i, 8, g_date);
#endif
	}	
}

void outsourcingDlg2::OnButton4()	// ȫ������-���ð�ť;
{
	// TODO: Add your control notification handler code here
	for(int i=0; i<m_List1.GetItemCount (); i++)
	{
#if 0
		m_List1.SetItemText (i, 5, "");
		m_List1.SetItemText (i, 6, "");
#else
		m_List1.SetItemText(i, 7, "");
		m_List1.SetItemText(i, 8, "");
#endif
	}		
}
extern CString g_domain;
void outsourcingDlg2::OnBUTget()	// �����;
{
	// TODO: Add your control notification handler code here
	if(m_List1.GetItemCount ())
	{
		if(AfxMessageBox("�˵�������Ƭ��¼, �Ƿ��ٴλ�ȡ?", MB_YESNO|MB_ICONSTOP)!=IDYES)
			return;
	}
	//	m_List1.DeleteAllItems ();
	CString	m_path=g_path3+"\\";

	if(g_bAllBranch&&m_branch!="")
	{
		CString domain=GetDomainFromBranch(m_branch);
		if(domain!=g_domain)
		{
			m_path+=domain+"\\"; 
		}
		//CString GetDomainFromBranch(CString branch)
	}


	m_path+=m_id;
	m_path +="\\";
	CStringArray m_patharray1;
	if(1)
	{
		m_path.MakeLower ();
		CStringArray dirarray; 
		CString str;
		if(1)
		{
			using namespace helper_coffs;
			ffsco o;
			o.dirs(1);
			o.find(LPCSTR(m_path), LPCSTR("*.*"));
			ffsco::typeT coo;
			ffsco::typeT::iterator it;
			coo = o.co_dir();
			for (it = coo.begin();coo.end() != it;it ++) 
			{
				str=(*it).c_str();
				if(FindArray(&dirarray, str)==-1)
				{
					dirarray.Add (str);
				}
			}
		}
		if(1)
		{
			for(int i=0; i<dirarray.GetSize (); i++)
			{
				using namespace helper_coffs;
				ffsco o;
				o.dirs(0);
				o.find(LPCSTR(dirarray.ElementAt (i)), LPCSTR("*.jpg"));
				ffsco::typeT coo;
				ffsco::typeT::iterator it;
				coo = o.co_file();
				for (it = coo.begin();coo.end() != it;it ++) 
				{
					str=(*it).c_str();
					if(str.GetAt (str.ReverseFind ('\\')+1)=='s')continue;
					if(str.GetAt (str.ReverseFind ('\\')+1)=='m')continue;
					str.MakeLower ();
					m_patharray1.Add  (str); 
				}
			}
		}
	}

	CString str,temp;
	int oldcount=m_List1.GetItemCount ();
	for(int i=oldcount; i<(m_patharray1.GetSize ()+oldcount); i++)
	{
		str=m_patharray1.ElementAt (i-oldcount);
		temp.Empty ();
		if(str.Left (str.ReverseFind ('\\')+1)!=m_path)
		{
			temp=str.Left (str.ReverseFind ('\\')+1);
			temp= temp.Right (temp.GetLength ()-m_path.GetLength ());
		}
		str=str.Right (str.GetLength ()-str.ReverseFind ('\\')-1);
		str=temp+str.Left (str.GetLength ()-4);	
		m_List1.InsertItem(i, str);
		m_List1.SetItemText(i, 1, "1");
		str=m_patharray1.ElementAt (i-oldcount);

		Image *img=NULL;
		::LoadImageFromBuf (&img, str);
		if(img->GetWidth())
		{
			float area=(img->GetWidth()*img->GetHeight()*0.0254*0.0254)/(img->GetHorizontalResolution()*img->GetHorizontalResolution());
			str.Format ("%0.5f", area);
			m_List1.SetItemText(i, 2, str);
			delete img;
		}
	}
	if(m_patharray1.GetSize ()==0)
		AfxMessageBox("�˵�δ�������Ƭ!", MB_ICONINFORMATION);
	UpdateZ();
}

void outsourcingDlg2::OnBTNdel()	// ɾ����ť;
{
	// TODO: Add your control notification handler code here
	POSITION pos;
	pos=m_List1.GetFirstSelectedItemPosition();	
	if(pos==NULL)
	{
		AfxMessageBox("����ѡ����Ҫɾ������Ŀ!", MB_ICONINFORMATION);
		return;
	}
	int iItem=m_List1.GetNextSelectedItem(pos);	
	m_List1.DeleteItem (iItem);

	int count=m_List1.GetItemCount ();
	if(count==0)
	{
		UpdateZ();
		return;
	}
	if(iItem==count)
		iItem=0;
	m_List1.SetItemState (iItem, LVIS_SELECTED, LVIS_SELECTED);	
	UpdateZ();
}

void outsourcingDlg2::UpdateZ()
{
	CString str;
	str.Format ("ȫ����Ƭ��ϸ:%d", m_List1.GetItemCount());
	GetDlgItem(IDC_STATIC1)->SetWindowText( str);
}

void outsourcingDlg2::OnBTNdel2()	// �Ӳ�Ʒ�л�ȡ;
{
	// TODO: Add your control notification handler code here
	if(m_List1.GetItemCount ())
	{
		if(AfxMessageBox("�˵�������Ƭ��¼, �Ƿ��ٴλ�ȡ?", MB_YESNO|MB_ICONSTOP)!=IDYES)
			return;
	}

	int count = m_List1.GetItemCount();
#ifdef CHILD_VERSION
	CString filter="id='"+m_id+"' and name<>'���' and name<>'���'";
#else
	CString filter="id='"+m_id+"' and kind<>'2' and name<>'���' and name<>'���'";
#endif
	g_sendhead.code[0]=56;	// Table is dindansp;
	g_sendhead.tabcount=1;
	g_sendhead.bsql=0;
	g_pMainWnd->ProcessChatMessageRequest2(filter);if(g_bSendOK==0)return;
	CArray<CStringArray, CStringArray>List1array;
	DataToArray(&List1array);
	for(int i=0; i<List1array.GetSize(); i++)
	{
		m_List1.InsertItem(count+i, List1array.ElementAt(i).ElementAt(0));			// ��Ƭ����;
		m_List1.SetItemText(count+i, 1, List1array.ElementAt(i).ElementAt(1));		// ��Ƭ����;

		//printf("Jeff:%s,%s\n\n",List1array.ElementAt(i).ElementAt(0),List1array.ElementAt(i).ElementAt(1));
	}

	UpdateZ();	
}

void outsourcingDlg2::OnDestroy() 
{
	CDialog::OnDestroy();

	// TODO: Add your message handler code here
	if(g_bAllBranch)
	{
		g_pMainWnd->OnDisconnect();
		g_bBranchModify=0;g_branchip=g_branchname="";
	}	
}