// ShowEarlyWarning.cpp : implementation file
//

#include "stdafx.h"
#include "ylgl.h"
#include "ShowEarlyWarning.h"

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

/////////////////////////////////////////////////////////////////////////////
// ShowEarlyWarning dialog


ShowEarlyWarning::ShowEarlyWarning(CWnd* pParent /*=NULL*/)
	: CDialog(ShowEarlyWarning::IDD, pParent)
{
	//{{AFX_DATA_INIT(ShowEarlyWarning)
		// NOTE: the ClassWizard will add member initialization here
	m_dtdays=0;
	m_mode=0;
	m_nRet=0;
	m_nCheckPos=0;
	//}}AFX_DATA_INIT
}


void ShowEarlyWarning::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(ShowEarlyWarning)
	DDX_Control(pDX, IDC_LIST2, m_List1);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(ShowEarlyWarning, CDialog)
	//{{AFX_MSG_MAP(ShowEarlyWarning)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST2, OnCustomdrawList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// ShowEarlyWarning message handlers

BOOL ShowEarlyWarning::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_List1.SetHeadings("���,100;����,100;����,60;����,60;����,100;�绰,100;����,60;ȡ����,100;������,100;������,100;����,100");
	m_List1.m_bSortSupport =0;
	g_sendhead.bsql=0;
	g_sendhead.code[0]=222;
	g_sendhead.tabcount=1;
	g_pMainWnd->ProcessChatMessageRequest2("(hireorsale<>'��' or hireorsale is null) and (isreturn<>'�ѻ�' or isreturn is null) and no='"+m_no+"'");
	if(g_bSendOK==0)
	{
		CDialog::OnOK();
		return 1;
	}
	CArray<CStringArray, CStringArray>m_List1array;
	DataToArray(&m_List1array);
	if(m_List1array.GetSize()==0)
	{
		m_List1array.SetSize(1, 1);
		m_List1array.ElementAt(0).Add(m_no);
		m_List1array.ElementAt(0).Add(m_name);
		m_List1array.ElementAt(0).Add(m_cot1);
		m_List1array.ElementAt(0).Add(m_cot2);
		m_List1array.ElementAt(0).Add("��ǰ");
		m_List1array.ElementAt(0).Add("..");
		m_List1array.ElementAt(0).Add(m_cot3);
		m_List1array.ElementAt(0).Add(m_date1);
		m_List1array.ElementAt(0).Add("..");
		m_List1array.ElementAt(0).Add("..");
		m_List1array.ElementAt(0).Add("..");
		m_nCheckPos=0;
	}
	else
	{
		int size=m_List1array.GetSize();
		m_List1array.SetSize(size+1, 1);
		BOOL bInsert=0;
		for(int i=0; i<size; i++)
		{
			if(m_List1array.ElementAt(i).ElementAt(7)>m_date1)
			{
				for(int j=size; j>i; j--)
				{
					m_List1array.ElementAt(j).Copy(m_List1array.ElementAt(j-1));
				} 
				m_nCheckPos=i;
		m_List1array.ElementAt(i).RemoveAll();
		m_List1array.ElementAt(i).Add("");
		m_List1array.ElementAt(i).Add("..");
		m_List1array.ElementAt(i).Add("..");
		m_List1array.ElementAt(i).Add("..");
		m_List1array.ElementAt(i).Add("��ǰ");
		m_List1array.ElementAt(i).Add("..");
		m_List1array.ElementAt(i).Add(m_cot3);
		m_List1array.ElementAt(i).Add(m_date1);
		m_List1array.ElementAt(i).Add(m_date2);
		m_List1array.ElementAt(i).Add("..");
		m_List1array.ElementAt(i).Add("..");
		if(i==0)
		{
			m_List1array.ElementAt(i).SetAt(0, m_List1array.ElementAt(i+1).ElementAt(0));
			m_List1array.ElementAt(i).SetAt(1, m_List1array.ElementAt(i+1).ElementAt(1));
			m_List1array.ElementAt(i).SetAt(2, m_List1array.ElementAt(i+1).ElementAt(2));
			m_List1array.ElementAt(i).SetAt(3, m_List1array.ElementAt(i+1).ElementAt(3));

			m_List1array.ElementAt(i+1).SetAt(0, "");
			m_List1array.ElementAt(i+1).SetAt(1, "");
			m_List1array.ElementAt(i+1).SetAt(2, "");
			m_List1array.ElementAt(i+1).SetAt(3, "");
		}
		bInsert=1;break;
				 
			}
		}
		if(bInsert==0)
		{
		m_List1array.ElementAt(size).Add("");
		m_List1array.ElementAt(size).Add("..");
		m_List1array.ElementAt(size).Add("..");
		m_List1array.ElementAt(size).Add("..");
		m_List1array.ElementAt(size).Add("��ǰ");
		m_List1array.ElementAt(size).Add("..");
		m_List1array.ElementAt(size).Add(m_cot3);
		m_List1array.ElementAt(size).Add(m_date1);
		m_List1array.ElementAt(size).Add(m_date2);
		m_List1array.ElementAt(size).Add("..");
		m_List1array.ElementAt(size).Add("..");
		m_nCheckPos=size;
		}
	}

 



    CString version = _T ("Version 1.0");
	m_dtdays=AfxGetApp()->GetProfileInt (version, "dtdays", 2);
 

	m_List1.DeleteAllItems2 ();
	int ii=0;
	m_List1.m_arLabels.SetSize( m_List1array.GetSize (), 1 );
	int count=0;

	  for(ii=0; ii<m_List1.m_arLabels.GetSize (); ii++)
	  {
		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);
 


int pos=count-1;
 
		int posbase=pos;
		while(m_List1.m_arLabels.ElementAt (posbase).ElementAt (0).IsEmpty ())
		{
			posbase--;
			if(posbase<1)break;
		}
		int cot=atoi(m_List1.m_arLabels.ElementAt (posbase).ElementAt (2))-atoi(m_List1.m_arLabels.ElementAt (posbase).ElementAt (3));
        if(posbase==pos)
		{
			if(atoi(m_List1.m_arLabels.ElementAt (posbase).ElementAt (6))>cot)
			{
				if(m_mode)
				{
					if(pos==m_List1.GetItemCount()-1)
					{
						m_nRet=1;
					}
				}
			}
			else 
			{
				if(m_mode)
				{
					if(pos==m_List1.GetItemCount()-1)
					{
						CDialog::OnOK();return 1;
					}
				}
			}
	 
		}
		else
		{
			CTimeSpan dt(m_dtdays, 0, 0, 0);
			CString time1=m_List1.m_arLabels.ElementAt (pos).ElementAt (7);
			CTime taketm(atoi(time1.Mid (0,4)),atoi(time1.Mid (5,2)),atoi(time1.Mid (8,2)),0,0,0); //ȡ����
			CTime todaytm(atoi(g_date.Mid (0,4)),atoi(g_date.Mid (5,2)),atoi(g_date.Mid (8,2)),0,0,0); 
			todaytm+=dt;


		 

			for(int i=posbase; i<pos; i++)
			{
			    time1=m_List1.m_arLabels.ElementAt (i).ElementAt (8);
			    CTime returntm(atoi(time1.Mid (0,4)),atoi(time1.Mid (5,2)),atoi(time1.Mid (8,2)),0,0,0); 
		 
				if(returntm+dt>=taketm)
			      cot-=atoi(m_List1.m_arLabels.ElementAt (i).ElementAt (6));
			//	else if(returntm<=todaytm)
			    //  cot-=atoi(m_List1.m_arLabels.ElementAt (i).ElementAt (6));

			}
			int sign1=0;
			int sign2=0;

			if(cot>=atoi(m_List1.m_arLabels.ElementAt (pos).ElementAt (6)))
			{
				if(m_mode)
				{
					if(pos==m_nCheckPos)
					{
						sign1=1;

						//CDialog::OnOK();return 1;
					}
					if(pos==m_nCheckPos+1)
					{
						sign2=1;
					}
				}
			}
			else
			{
	 
				if(m_mode)
				{
					if(pos==m_nCheckPos)
					{
						sign1=2;
					//	m_nRet=1;
					}	
					if(pos==m_nCheckPos+1)
					{
						sign2=2;
					}
				}
			}

			if(m_mode)
			{
			if(m_nCheckPos<m_List1.GetItemCount()-1)
			{	
				if(sign1==1 && sign2==1)
				{
					CDialog::OnOK();return 1;
				}
				if(sign1==2 || sign2==2)
				{
					m_nRet=1;
				}
				else
				{
					CDialog::OnOK();return 1;
				}
			}
			else
			{
				if(sign1==1)
				{
					CDialog::OnOK();return 1;
				}	
				if(sign1==2)
				{
					m_nRet=1;
				}
			}
			}

	 
		}


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

void ShowEarlyWarning::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.
        COLORREF crText;
		int pos=pLVCD->nmcd.dwItemSpec;
		if(pos%2)
			pLVCD->clrTextBk = g_gridcol1;
		else
			pLVCD->clrTextBk = g_gridcol2;
		int posbase=pos;
		while(m_List1.m_arLabels.ElementAt (posbase).ElementAt (0).IsEmpty ())
		{
			posbase--;
			if(posbase<1)break;
		}
		int cot=atoi(m_List1.m_arLabels.ElementAt (posbase).ElementAt (2))-atoi(m_List1.m_arLabels.ElementAt (posbase).ElementAt (3));
	      BOOL bNeedReturn=0;
		CTimeSpan dt(m_dtdays, 0, 0, 0);
			CTime todaytm(atoi(g_date.Mid (0,4)),atoi(g_date.Mid (5,2)),atoi(g_date.Mid (8,2)),0,0,0); 
			todaytm+=dt;
		 

        if(posbase==pos)
		{
			if(atoi(m_List1.m_arLabels.ElementAt (posbase).ElementAt (6))>cot)
				crText = RGB(220,0,0);
			else 
				crText = RGB(20,133,20);  
			pLVCD->clrText = crText;
		}
		else
		{
			CTimeSpan dt(m_dtdays, 0, 0, 0);
			CString time1=m_List1.m_arLabels.ElementAt (pos).ElementAt (7);
			CTime taketm(atoi(time1.Mid (0,4)),atoi(time1.Mid (5,2)),atoi(time1.Mid (8,2)),0,0,0); //ȡ����
	 
			for(int i=posbase; i<pos; i++)
			{
			    time1=m_List1.m_arLabels.ElementAt (i).ElementAt (8);
			    CTime returntm(atoi(time1.Mid (0,4)),atoi(time1.Mid (5,2)),atoi(time1.Mid (8,2)),0,0,0); 
	 
				if(returntm+dt>=taketm)
			      cot-=atoi(m_List1.m_arLabels.ElementAt (i).ElementAt (6));	
			//	else if(returntm<=todaytm)
			   //   cot-=atoi(m_List1.m_arLabels.ElementAt (i).ElementAt (6));
			}
			if(cot>=atoi(m_List1.m_arLabels.ElementAt (pos).ElementAt (6)))
			{
				crText = RGB(20,133,20);	
		 
			}
			else
			{
				crText = RGB(220,0,0);
	 
			}
			pLVCD->clrText = crText;
		}

			CString time1=m_List1.m_arLabels.ElementAt (pos).ElementAt (8);
			if(time1.GetLength()>5)
			{
			CTime returntm(atoi(time1.Mid (0,4)),atoi(time1.Mid (5,2)),atoi(time1.Mid (8,2)),0,0,0); 
			bNeedReturn=0;
		    if(returntm<=todaytm)
			      bNeedReturn=1;
			if(bNeedReturn)
			{
				crText = RGB(220,0,0);pLVCD->clrText = crText;
			}}


        // Store the color back in the NMLVCUSTOMDRAW struct.
      

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