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

/////////////////////////////////////////////////////////////////////////////
// PhotoPrintFrom2
PhotoPrintFrom2::PhotoPrintFrom2(CWnd* pParent /*=NULL*/)
	: CDialog(PhotoPrintFrom2::IDD)
{
	//{{AFX_DATA_INIT(PhotoPrintFrom2)
	m_year = atoi(g_date.Mid(0, 4));
	m_month = atoi(g_date.Mid(5, 2));
	m_day = atoi(g_date.Mid(8, 2));
	m_bInit = 0;
	m_timestmap = 0;
	m_radio1 = 0;
	//}}AFX_DATA_INIT
}

void PhotoPrintFrom2::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(PhotoPrintFrom2)
	DDX_Control(pDX, IDC_COMBObranch, m_combobranch);
	DDX_Control(pDX, IDC_COMBO6, m_combo1);
	DDX_Control(pDX, IDC_SPIN3, m_spinday);
	DDX_Control(pDX, IDC_SPIN2, m_spinmonth);
	DDX_Control(pDX, IDC_SPIN1, m_spinyear);
	DDX_Control(pDX, IDC_LIST2, m_List1);
	DDX_Control(pDX, IDC_STATIC1, m_static1);
	DDX_Text(pDX, IDC_EDITyear, m_year);
	DDV_MinMaxUInt(pDX, m_year, 1900, 3000);
	DDX_Text(pDX, IDC_EDITmonth, m_month);
	DDV_MinMaxUInt(pDX, m_month, 1, 12);
	DDX_Text(pDX, IDC_EDITday, m_day);
	DDV_MinMaxUInt(pDX, m_day, 1, 31);
	DDX_Radio(pDX, IDC_RADIO3, m_radio1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(PhotoPrintFrom2, CDialog)
	//{{AFX_MSG_MAP(PhotoPrintFrom2)
	ON_BN_CLICKED(IDC_BUTclose, OnBUTclose)
	ON_EN_CHANGE(IDC_EDITyear, OnChangeEDITyear)
	ON_EN_CHANGE(IDC_EDITmonth, OnChangeEDITmonth)
	ON_EN_CHANGE(IDC_EDITday, OnChangeEDITday)
	ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
	ON_BN_CLICKED(IDC_RADIO4, OnRadio4)
	ON_EN_CHANGE(IDC_EDITmoney2, OnChangeEDITmoney2)
	ON_WM_DESTROY()
	ON_CBN_SELCHANGE(IDC_COMBO6, OnSelchangeCombo6)
	ON_CBN_SELCHANGE(IDC_COMBObranch, OnSelchangeCOMBObranch)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// PhotoPrintFrom2 diagnostics

#ifdef _DEBUG
void PhotoPrintFrom2::AssertValid() const
{
	CDialog::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// PhotoPrintFrom2 message handlers
extern BOOL CALLBACK EnumChildProc2(HWND hwnd, LPARAM lParam);

BOOL PhotoPrintFrom2::OnInitDialog()
{
	CDialog::OnInitDialog();
	// TODO: Add your specialized code here and/or call the base class
	if (g_bAllBranch)
	{
		GetDlgItem(IDC_STATIC12)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_COMBObranch)->ShowWindow(SW_SHOW);

		//	m_combobranch.AddString ("");
		for (int i = 0; i < g_brancharray.GetSize(); i++)
		{
			m_combobranch.AddString(g_brancharray.ElementAt(i).ElementAt(0));
		}
	}
	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);

	CString version = _T("Version 1.0");
	CString str = AfxGetApp()->GetProfileString(version, "printprice", "20");
	SetDlgItemText(IDC_EDITmoney2, str);

	m_static1.SetFont(&g_titlefont);

#ifdef CHILD_VERSION
	m_List1.SetHeadings("������,100;�ҳ�����,100;��������,100;��Ƭ����,100;����,100;���m2,100;�Ƿ񷢳�,100;��������,100;�Ƿ����,100;��������,100");
#else
	m_List1.SetHeadings("������,100;�б�����,100;Ů������,100;��Ƭ����,100;����,100;���m2,100;�Ƿ񷢳�,100;��������,100;�Ƿ����,100;��������,100");
#endif
	//m_List1.m_bSortSupport=0;
	m_List1.LoadColumnInfo(178);
	m_spinyear.SetRange(1900, 3000);
	m_spinmonth.SetRange(1, 12);
	m_spinday.SetRange(1, 31);

	g_sendhead.bsql = 0;
	g_sendhead.code[0] = 9;
	g_sendhead.tabcount = 1;
	CString sql;
	g_pMainWnd->ProcessChatMessageRequest2(1);
	if (g_bSendOK == 0)
	{
		CDialog::OnOK();
		return 0;
	}
	DataToArray(&g_List1array);

	if (m_combo1.GetCount() == 0)
	{
		m_combo1.ResetContent(); m_combo1.AddString("ȫ��");
		for (int i = 0; i < g_List1array.GetSize(); i++)
		{
			m_combo1.AddString(g_List1array.ElementAt(i).ElementAt(1));
		}
		m_combo1.SetCurSel(0);
	}


	m_bInit = 1;
	DateChange();
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void PhotoPrintFrom2::FillGrid()
{
	CString splb = "ȫ��";
	int pos = m_combo1.GetCurSel();
	if (pos != -1)
	{
		m_combo1.GetLBText(pos, splb);
	}


	m_List1.DeleteAllItems2();
	int ii = 0;
	m_List1.m_arLabels.SetSize(m_List1array.GetSize(), 1);
	int count = 0;
	int sum1 = 0;
	float sum2 = 0;
	if (splb == "ȫ��")
	{
		for (ii = 0; ii < m_List1.m_arLabels.GetSize(); ii++)
		{
			m_List1.m_arLabels.ElementAt(count++).Copy(m_List1array.ElementAt(ii));
			sum1 += atoi(m_List1array.ElementAt(ii).ElementAt(4));
			sum2 += atoi(m_List1array.ElementAt(ii).ElementAt(4))*atof(m_List1array.ElementAt(ii).ElementAt(5));
		}
	}
	else
	{
		for (ii = 0; ii < m_List1.m_arLabels.GetSize(); ii++)
		{

			if (m_List1array.ElementAt(ii).ElementAt(6).GetLength() < 4)continue;
			if (m_List1array.ElementAt(ii).ElementAt(6).Right(m_List1array.ElementAt(ii).ElementAt(6).GetLength() - 4) != splb)continue;

			m_List1.m_arLabels.ElementAt(count++).Copy(m_List1array.ElementAt(ii));
			sum1 += atoi(m_List1array.ElementAt(ii).ElementAt(4));
			sum2 += atoi(m_List1array.ElementAt(ii).ElementAt(4))*atof(m_List1array.ElementAt(ii).ElementAt(5));
		}
	}


	m_List1.m_arLabels.SetSize(count, 1);
	ii = count;
	m_List1.m_LabelCount = ii;
	m_List1.SetItemCountEx(ii);
	CString str;
	str.Format("����:%d", ii);
	SetDlgItemText(IDC_STATIC2, str);

	str.Format("%d", sum1);
	SetDlgItemText(IDC_EDITmoney1, str);

	str.Format("%0.5f", sum2);
	SetDlgItemText(IDC_EDITmoney5, str);

	OnChangeEDITmoney2();
}

void PhotoPrintFrom2::OnBUTclose()
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}

void PhotoPrintFrom2::OnChangeEDITyear()
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	DateChange();
	// TODO: Add your control notification handler code here

}

void PhotoPrintFrom2::OnChangeEDITmonth()
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	DateChange();
	// TODO: Add your control notification handler code here

}

void PhotoPrintFrom2::OnChangeEDITday()
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	//    DateChange();
	// TODO: Add your control notification handler code here

}

void PhotoPrintFrom2::DateChange()
{
	if (m_bInit == 0)return;
	m_spinyear.EnableWindow(0);
	m_spinmonth.EnableWindow(0);
	m_spinday.EnableWindow(0);
	UpdateData();
	if (m_branch != "")
	{
		g_pMainWnd->OnDisconnect();
		g_branchip = GetIP(m_branch);
		g_pMainWnd->OnDisconnect();
		g_branchname = m_branch;
		g_bBranchModify = 1;
	}
	int i;
	int g_nYearposTemp = g_nYearpos;
	BOOL bLastYear = 0;
	BOOL bThisYear = 0;
#if JEFF_TEST_ON	// 2015��ʼ���ÿ�������;
	int g_nYearposPre = -1;
	if (m_year >= 2014 && m_year <= atoi(g_date.Left(4)) && g_hisyeararray.GetSize())
	{
		g_nYearpos = -1;
		bThisYear = 1;
		if (m_year == 2014)
		{
			g_nYearposPre = GetYearPos(m_year - 1);
		}
	}
	else if (m_year <= 2013 && g_hisyeararray.GetSize())
	{
		g_nYearpos = GetYearPos(m_year);
		if (g_nYearpos != -1)
			g_nYearposPre = GetYearPos(m_year - 1);
		bLastYear = 1;
	}
#else
	if (m_year == atoi(g_date.Left(4)) && g_hisyeararray.GetSize())
		bThisYear = 1;
	else if (m_year <= atoi(g_date.Left(4)) - 1 && g_hisyeararray.GetSize())
		bLastYear = 1;
	g_nYearpos = GetYearPos(m_year);
	int g_nYearposPre = GetYearPos(m_year - 1);
#endif

	g_sendhead.bsql = 0;
	g_sendhead.code[0] = 147;
	g_sendhead.tabcount = 1;
	CString sql, strdate, strdate2;
	strdate.Format("%04d-%02d-%02d", m_year, m_month, 1);
	strdate2.Format("%04d-%02d-%02d", m_year, m_month, 31);
	m_static1.SetWindowText(strdate.Left(7) + "��ӡ����");
	if (m_radio1 == 0)
		sql.Format("[time1]>='" + strdate + "' and [time1]<='" + strdate2 + "'");
	else
		sql.Format("[time2]>='" + strdate + "' and [time2]<='" + strdate2 + "'");
	g_pMainWnd->ProcessChatMessageRequest2(sql);
	if (g_bSendOK == 0)
	{
		m_spinyear.EnableWindow(1);
		m_spinmonth.EnableWindow(1);
		m_spinday.EnableWindow(1);
		g_nYearpos = g_nYearposTemp;
		return;
	}
	DataToArray(&m_List1array);

	if (bLastYear)//�����ȥ��, ��ӽ���IJ���
	{
		g_nYearpos = -1;//����
		g_sendhead.bsql = 0;
		g_sendhead.code[0] = 147;
		g_sendhead.tabcount = 1;
		g_pMainWnd->ProcessChatMessageRequest2(sql);
		if (g_bSendOK == 0)
		{
			m_spinyear.EnableWindow(1);
			m_spinmonth.EnableWindow(1);
			m_spinday.EnableWindow(1);
			g_nYearpos = g_nYearposTemp;
			return;
		}
		CArray<CStringArray, CStringArray>ThisYearList1array;
		DataToArray(&ThisYearList1array);
		int oldsize = m_List1array.GetSize();
		m_List1array.SetSize(oldsize + ThisYearList1array.GetSize());
		for (i = oldsize; i < oldsize + ThisYearList1array.GetSize(); i++)
		{
			m_List1array.ElementAt(i).Copy(ThisYearList1array.ElementAt(i - oldsize));
		}
	}
	if (g_nYearposPre != -1)//����ǽ���, ���ȥ��IJ���
	{
		g_nYearpos = g_nYearposPre;//ȥ��
		g_sendhead.bsql = 0;
		g_sendhead.code[0] = 147;
		g_sendhead.tabcount = 1;
		g_pMainWnd->ProcessChatMessageRequest2(sql);
		if (g_bSendOK == 0)
		{
			m_spinyear.EnableWindow(1);
			m_spinmonth.EnableWindow(1);
			m_spinday.EnableWindow(1);
			g_nYearpos = g_nYearposTemp;
			return;
		}
		CArray<CStringArray, CStringArray>ThisYearList1array;
		DataToArray(&ThisYearList1array);
		int oldsize = m_List1array.GetSize();
		m_List1array.SetSize(oldsize + ThisYearList1array.GetSize());
		for (i = oldsize; i < oldsize + ThisYearList1array.GetSize(); i++)
		{
			m_List1array.ElementAt(i).Copy(ThisYearList1array.ElementAt(i - oldsize));
		}
	}
	g_nYearpos = g_nYearposTemp;

	FillGrid();
	m_spinyear.EnableWindow(1);
	m_spinmonth.EnableWindow(1);
	m_spinday.EnableWindow(1);
}

void PhotoPrintFrom2::OnRadio3()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	DateChange();
}

void PhotoPrintFrom2::OnRadio4()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	DateChange();
}

void PhotoPrintFrom2::OnChangeEDITmoney2()
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	if (m_bInit == 0)return;
	CString str, str2;
	GetDlgItem(IDC_EDITmoney5)->GetWindowText(str);
	GetDlgItem(IDC_EDITmoney2)->GetWindowText(str2);
	str.Format("%0.5f", atof(str)*atof(str2));
	SetDlgItemText(IDC_EDITmoney3, str);
	// TODO: Add your control notification handler code here

}

void PhotoPrintFrom2::OnDestroy()
{
	// TODO: Add your message handler code here
	CString str;
	GetDlgItem(IDC_EDITmoney2)->GetWindowText(str);
	CString version = _T("Version 1.0");
	AfxGetApp()->WriteProfileString(version, "printprice", str);
	if (g_bAllBranch)
	{
		g_pMainWnd->OnDisconnect();
		g_bBranchModify = 0; g_branchip = g_branchname = "";
	}
	CDialog::OnDestroy();
}

void PhotoPrintFrom2::OnSelchangeCombo6()
{
	// TODO: Add your control notification handler code here
	FillGrid();
}

void PhotoPrintFrom2::OnSelchangeCOMBObranch()
{
	// TODO: Add your control notification handler code here
	int pos = m_combobranch.GetCurSel();
	if (pos == -1)return;
	m_combobranch.GetLBText(pos, m_branch);
	DateChange();

	m_combo1.ResetContent();
	g_sendhead.bsql = 0;
	g_sendhead.code[0] = 9;
	g_sendhead.tabcount = 1;
	CString sql;
	g_pMainWnd->ProcessChatMessageRequest2(1);
	if (g_bSendOK == 0)
	{

		return;
	}
	DataToArray(&g_List1array);

	if (m_combo1.GetCount() == 0)
	{
		m_combo1.ResetContent(); m_combo1.AddString("ȫ��");
		for (int i = 0; i < g_List1array.GetSize(); i++)
		{
			m_combo1.AddString(g_List1array.ElementAt(i).ElementAt(1));
		}
		m_combo1.SetCurSel(0);
	}
}