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

IMPLEMENT_DYNCREATE(ShowGoods, MyFormView)
ShowGoods::ShowGoods()
: MyFormView(ShowGoods::IDD)
{
	//{{AFX_DATA_INIT(ShowGoods)
	m_filter = _T("");
	m_radio1 = 0;
	//}}AFX_DATA_INIT
}

ShowGoods::~ShowGoods()
{
}

void ShowGoods::DoDataExchange(CDataExchange* pDX)
{
	MyFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(ShowGoods)
	DDX_Control(pDX, IDC_LIST4, m_List2);
	DDX_Control(pDX, IDC_COMBO1, m_combo1);
	DDX_Control(pDX, IDC_LIST2, m_List1);
	DDX_Control(pDX, IDC_STATIC1, m_static1);
	DDX_CBString(pDX, IDC_COMBO1, m_filter);
	DDX_Radio(pDX, IDC_RADIO1, m_radio1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(ShowGoods, MyFormView)
	//{{AFX_MSG_MAP(ShowGoods)
	ON_BN_CLICKED(IDC_BUTclose, OnBUTclose)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_TIMER()
	ON_NOTIFY(NM_DBLCLK, IDC_LIST2, OnDblclkList2)
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	ON_BN_CLICKED(IDC_BUTdel, OnBUTdel)
	ON_BN_CLICKED(IDC_BUTdel6, OnBUTdel6)
	ON_BN_CLICKED(IDC_BUTdel2, OnBUTdel2)
	//}}AFX_MSG_MAP
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST2, OnCustomdrawList)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// ShowGoods diagnostics

#ifdef _DEBUG
void ShowGoods::AssertValid() const
{
	MyFormView::AssertValid();
}

void ShowGoods::Dump(CDumpContext& dc) const
{
	MyFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// ShowGoods message handlers

void ShowGoods::OnInitialUpdate()
{
	MyFormView::OnInitialUpdate();
	GetDlgItem(IDC_BUTdel)->EnableWindow(IsHasRights2new(49));
	GetDlgItem(IDC_BUTdel6)->EnableWindow(IsHasRights2new(49));

	// TODO: Add your specialized code here and/or call the base class
	CMyMdi Mdi;
	Mdi.SetSubView((CWnd*)GetParent(), (CWnd*)this);

	// Here we create the outbar control using the splitter as its parent
	// and setting its id to the first pane.
	CRect rc2;
	GetWindowRect(rc2);
	::MoveWindow(m_hWnd, g_rc.left, g_rc.top, g_rc.Width(), g_rc.Height(), TRUE);
	EnumChildWindows(m_hWnd, (WNDENUMPROC)EnumChildProc, 0);
	m_static1.SetFont(&g_titlefont);
	m_List1.SetHeadings("��Ʒ���,200;��Ʒ����,200;���,100");
	m_List1.LoadColumnInfo(135);

	m_List2.SetHeadings("����,120;��Ʒ���,100;��Ʒ����,120;����,60;����,100;������,80;���,60;��ע,150");
	m_List2.LoadColumnInfo(136);

	GetDlgItem(IDC_LIST2)->ShowWindow(SW_SHOW);
	GetDlgItem(IDC_LIST4)->ShowWindow(SW_HIDE);

	g_sendhead.bsql = 0;
	g_sendhead.code[0] = 76;
	g_sendhead.tabcount = 1;
	g_pMainWnd->ProcessChatMessageRequest2(1); if (g_bSendOK == 0)return;
	DataToArray(&g_List1array);

	int i = 0;
	int size1 = 0;
	int size2 = 0;
	for ( i = 0; i < g_List1array.GetSize(); i++)
	{
		if (g_List1array.ElementAt(i).GetSize() == 8)
		{// Ԫ�ش�С = 8, ��ʾ���dz�����¼;
			size2++;
			m_List2array.SetSize(size2, 1);
			size2--;
			m_List2array.ElementAt(size2).Copy(g_List1array.ElementAt(i));
			size2++;
		}
		else if (g_List1array.ElementAt(i).GetSize() == 3)
		{// Ԫ�ش�С = 3; ��ʾ���ļ�����;
			size1++;
			m_List1array.SetSize(size1, 1);
			size1--;
			m_List1array.ElementAt(size1).Copy(g_List1array.ElementAt(i));
			size1++;
		}
	}
	FillGrid();
	m_combo1.GetWindowRect(rc2);
	ScreenToClient(rc2);
	rc2.bottom += 200;
	m_combo1.MoveWindow(rc2);
	GetDlgItem(IDC_BUTclose)->GetWindowRect(rc2);
	SetComboHei(&m_combo1, rc2.Height());

	for (i = 0; i < m_List1array.GetSize(); i++)
	{
		if (m_combo1.FindString(0, m_List1array.ElementAt(i).ElementAt(0)) == -1)
			m_combo1.AddString(m_List1array.ElementAt(i).ElementAt(0));
	}
}

void ShowGoods::FillGrid()
{
	int ii = 0;
	if (m_radio1 == 0)
	{
		m_List1.DeleteAllItems2();
		m_List1.m_arLabels.SetSize(m_List1array.GetSize(), 1);
		int count = 0;
		if (m_filter.IsEmpty())
		{
			for (ii = 0; ii < m_List1.m_arLabels.GetSize(); ii++)
			{
				m_List1.m_arLabels.ElementAt(count++).Copy(m_List1array.ElementAt(ii));
			}
		}
		else
		{
			for (ii = 0; ii < m_List1.m_arLabels.GetSize(); ii++)
			{
				if (m_List1array.ElementAt(ii).ElementAt(0).Find(m_filter) != -1 || \
					m_List1array.ElementAt(ii).ElementAt(1).Find(m_filter) != -1 || \
					m_List1array.ElementAt(ii).ElementAt(2).Find(m_filter) != -1)
				{
					m_List1.m_arLabels.ElementAt(count++).Copy(m_List1array.ElementAt(ii));
				}
			}
		}
		m_List1.m_arLabels.SetSize(count, 1);
		ii = count;
		m_List1.m_LabelCount = ii;
		m_List1.SetItemCountEx(ii);
	}
	else
	{
		m_List2.DeleteAllItems2();
		m_List2.m_arLabels.SetSize(m_List2array.GetSize(), 1);
		int count = 0;
		if (m_filter.IsEmpty())
		{
			for (ii = 0; ii < m_List2.m_arLabels.GetSize(); ii++)
			{
				m_List2.m_arLabels.ElementAt(count++).Copy(m_List2array.ElementAt(ii));
			}
		}
		else
		{
			for (ii = 0; ii < m_List2.m_arLabels.GetSize(); ii++)
			{
				if (m_List2array.ElementAt(ii).ElementAt(0).Find(m_filter) != -1 || \
					m_List2array.ElementAt(ii).ElementAt(1).Find(m_filter) != -1 || \
					m_List2array.ElementAt(ii).ElementAt(2).Find(m_filter) != -1 || \
					m_List2array.ElementAt(ii).ElementAt(3).Find(m_filter) != -1 || \
					m_List2array.ElementAt(ii).ElementAt(4).Find(m_filter) != -1 || \
					m_List2array.ElementAt(ii).ElementAt(5).Find(m_filter) != -1 || \
					m_List2array.ElementAt(ii).ElementAt(6).Find(m_filter) != -1 || \
					m_List2array.ElementAt(ii).ElementAt(7).Find(m_filter) != -1)
				{
					m_List2.m_arLabels.ElementAt(count++).Copy(m_List2array.ElementAt(ii));
				}
			}
		}
		m_List2.m_arLabels.SetSize(count, 1);
		ii = count;
		m_List2.m_LabelCount = ii;
		m_List2.SetItemCountEx(ii);
	}
	CString str;
	str.Format("��Ŀ��:%d", ii);
	SetDlgItemText(IDC_STATIC2, str);

	CRect rc;
	GetDlgItem(IDC_STATIC2)->GetWindowRect(rc);
	ScreenToClient(rc);
	InvalidateRect(rc);
}

void ShowGoods::OnBUTclose()
{
	// TODO: Add your control notification handler code here
	GetParent()->SendMessage(WM_CLOSE);
}

void ShowGoods::OnTimer(UINT nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	KillTimer(nIDEvent);
	OnButton1();
}

BOOL ShowGoods::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	try
	{
		if (pMsg->message == WM_KEYDOWN)
		{
			switch (pMsg->wParam)
			{
			case VK_RETURN:
				OnButton1();
				return 1;
			case 0x43: // copy
				if ((GetKeyState(VK_CONTROL) & 0x80))
				{
					GetFocus()->SendMessage(WM_COPY);
					return TRUE;
				}
				break;
			case 0x56: //Ctrl + V:
				if ((GetKeyState(VK_CONTROL) & 0x80))
				{
					GetFocus()->SendMessage(WM_PASTE);
					return TRUE;
				}
				break;
			case 0x58: // cut
				if ((GetKeyState(VK_CONTROL) & 0x80))
				{
					GetFocus()->SendMessage(WM_CUT);
					return TRUE;
				}
				break;
			case 0x5A: //undo
			case 0x59: //redo
				if ((GetKeyState(VK_CONTROL) & 0x80))
				{
					GetFocus()->SendMessage(WM_UNDO);
					return TRUE;
				}
				break;
			}
		}
		return MyFormView::PreTranslateMessage(pMsg);
	}
	catch (...)
	{
	}
	return true;
}

void ShowGoods::OnButton1()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_filter.TrimLeft();
	m_filter.TrimRight();
	FillGrid();
}

void ShowGoods::OnDblclkList2(NMHDR* pNMHDR, LRESULT* pResult)
{
	// TODO: Add your control notification handler code here
	/* POSITION pos;
	 pos=m_List1.GetFirstSelectedItemPosition();
	 if(pos==NULL)
	 {
	 return;
	 }
	 int iItem=m_List1.GetNextSelectedItem(pos);
	 CString memberno=m_List1.GetItemText (iItem, 1);
	 AddMember dlg;
	 dlg.m_memberno =memberno;
	 dlg.m_bAdd=0;
	 if(dlg.DoModal ()==IDOK)
	 {
	 g_sendhead.bsql=0;
	 g_sendhead.code[0]=68;
	 g_sendhead.tabcount=1;
	 g_pMainWnd->ProcessChatMessageRequest2(1);if(g_bSendOK==0)return;
	 DataToArray(&m_List1array);
	 OnButton1();
	 }*/
	*pResult = 0;
}

void ShowGoods::OnSelchangeCombo1()
{
	// TODO: Add your control notification handler code here
	SetTimer(1, 100, NULL);
}

void ShowGoods::GetData()
{
}

void ShowGoods::OnCustomdrawList(NMHDR* pNMHDR, LRESULT* pResult)
{
	NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
	// Take the default processing unless we set this to something else below.
	*pResult = 0;

	// First thing - check the draw stage. If it's the control's prepaint
	// stage, then tell Windows we want messages for every item.
	if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
	}
	else if (CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage)
	{
		// This is the prepaint stage for an item. Here's where we set the
		// item's text color. Our return value will tell Windows to draw the
		// item itself, but it will use the new color we set here.
		// We'll cycle the colors through red, green, and light blue.


		if (atof(m_List1.m_arLabels.ElementAt(pLVCD->nmcd.dwItemSpec).ElementAt(2)) < 1)
			pLVCD->clrText = RGB(220, 0, 0);
		else
			pLVCD->clrText = RGB(20, 133, 20);

		if (pLVCD->nmcd.dwItemSpec % 2)
			pLVCD->clrTextBk = g_gridcol1;
		else
			pLVCD->clrTextBk = g_gridcol2;
		// Store the color back in the NMLVCUSTOMDRAW struct.


		// Tell Windows to paint the control itself.
		*pResult = CDRF_DODEFAULT;
	}
}

void ShowGoods::OnRadio1()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if (m_radio1 == 0)
	{
		GetDlgItem(IDC_LIST2)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_LIST4)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_BUTdel)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_BUTdel2)->ShowWindow(SW_SHOW);
	}
	else
	{
		GetDlgItem(IDC_LIST4)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_LIST2)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_BUTdel)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_BUTdel2)->ShowWindow(SW_HIDE);
	}
	OnButton1();
}

void ShowGoods::OnRadio2()
{
	// TODO: Add your control notification handler code here
	OnRadio1();
}

void ShowGoods::OnBUTdel()
{
	// TODO: Add your control notification handler code here
	if (IsHasRights2new(49) == 0)return;
	POSITION pos;
	pos = m_List2.GetFirstSelectedItemPosition();
	if (pos == NULL)
	{
		AfxMessageBox("����ѡ����Ҫɾ������Ŀ!", MB_ICONINFORMATION);
		return;
	}
	if (AfxMessageBox("ɾ�����޷��ָ�, ������?", MB_YESNO | MB_ICONSTOP) != IDYES)return;
	int iItem;
	CArray<int, int>array;
	while (pos)
	{
		iItem = m_List2.GetNextSelectedItem(pos);
		array.Add(iItem);
	}

	int i = 0;
	CString sql;
	for ( i = 0; i < array.GetSize(); i++)
	{
		iItem = array.ElementAt(i);
		CString id = m_List2.GetItemText(iItem, 0);
		CString type = m_List2.GetItemText(iItem, 6);
		if (type == "���")
			sql += "***delete from storeinfo where id='" + id + "'";
		else if (type == "����")
			sql += "***delete from storeinfo2 where id='" + id + "'";
	}
	sql.TrimLeft("***");

	g_sendhead.bsql = 1;
	g_pMainWnd->ProcessChatMessageRequest2(sql); if (g_bSendOK == 0)return;

	g_sendhead.bsql = 0;
	g_sendhead.code[0] = 76;
	g_sendhead.tabcount = 1;
	g_pMainWnd->ProcessChatMessageRequest2(1); if (g_bSendOK == 0)return;
	DataToArray(&g_List1array);
	m_List1array.RemoveAll();
	m_List2array.RemoveAll();
	int size1 = 0;
	int size2 = 0;
	for (i = 0; i < g_List1array.GetSize(); i++)
	{
		if (g_List1array.ElementAt(i).GetSize() == 8)
		{
			size2++;
			m_List2array.SetSize(size2, 1);
			size2--;
			m_List2array.ElementAt(size2).Copy(g_List1array.ElementAt(i));
			size2++;
		}
		else if (g_List1array.ElementAt(i).GetSize() == 3)
		{
			size1++;
			m_List1array.SetSize(size1, 1);
			size1--;
			m_List1array.ElementAt(size1).Copy(g_List1array.ElementAt(i));
			size1++;
		}
	}
	FillGrid();
}

void ShowGoods::OnBUTdel6()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if (m_radio1 == 0)
		ListToXLS(&m_List1, "c:\\����¼.xls", 0);
	else
		ListToXLS(&m_List2, "c:\\�����.xls", 0);
}

void ShowGoods::OnBUTdel2()
{
	// 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);
	CString type = m_List1.GetItemText(iItem, 0);
	CString name = m_List1.GetItemText(iItem, 1);

	InputName dlg;
	dlg.m_mode = 1;
	if (dlg.DoModal() != IDOK)return;
	if (AfxMessageBox("ע�⣺�̵�����ȫ��������¼, ������?", MB_YESNO | MB_ICONSTOP) != IDYES)return;
	CString sql;
	sql.Format("delete from [storeinfo] where [type]='%s' and [name]='%s'***delete from [storeinfo2] where [type]='%s' and [name]='%s'***insert into [storeinfo]([id],[type],[name],[count],[date],[remark],[clerk])values('�̵�','%s','%s','%d','%s','%s','%s')", type, name, type, name, type, name, atoi(dlg.m_name), g_date, "�̵�", g_user.name);


	m_List1array.RemoveAll();
	m_List2array.RemoveAll();
	g_sendhead.bsql = 1;
	g_pMainWnd->ProcessChatMessageRequest2(sql); if (g_bSendOK == 0)return;
	AfxMessageBox("����ɹ�!", MB_ICONINFORMATION);
	g_sendhead.bsql = 0;
	g_sendhead.code[0] = 76;
	g_sendhead.tabcount = 1;
	g_pMainWnd->ProcessChatMessageRequest2(1); if (g_bSendOK == 0)return;
	DataToArray(&g_List1array);

	int size1 = 0;
	int size2 = 0;
	for (int i = 0; i < g_List1array.GetSize(); i++)
	{
		if (g_List1array.ElementAt(i).GetSize() == 8)
		{
			size2++;
			m_List2array.SetSize(size2, 1);
			size2--;
			m_List2array.ElementAt(size2).Copy(g_List1array.ElementAt(i));
			size2++;
		}
		else if (g_List1array.ElementAt(i).GetSize() == 3)
		{
			size1++;
			m_List1array.SetSize(size1, 1);
			size1--;
			m_List1array.ElementAt(size1).Copy(g_List1array.ElementAt(i));
			size1++;
		}
	}
	FillGrid();
}