// SalarySet4.cpp : implementation file
//

#include "stdafx.h"
#include "ylgl.h"
#include "SalarySet4.h"
#include "SelExpendType.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// SalarySet4 dialog


SalarySet4::SalarySet4(CWnd* pParent /*=NULL*/)
	: CDialog(SalarySet4::IDD, pParent)
{
	//{{AFX_DATA_INIT(SalarySet4)
	m_money1 = 0;
	m_money2 = 0;
	m_rate = 0.0f;
	m_check1 = FALSE;
	m_testmoney = 0.0f;
	//}}AFX_DATA_INIT
}


void SalarySet4::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(SalarySet4)
	DDX_Control(pDX, IDC_EDIT8, m_numctrl4);
	DDX_Control(pDX, IDC_LIST1, m_list1);
	DDX_Control(pDX, IDC_EDIT3, m_numctrl3);
	DDX_Control(pDX, IDC_EDIT2, m_numctrl2);
	DDX_Control(pDX, IDC_EDIT1, m_numctrl1);
	DDX_Text(pDX, IDC_EDIT1, m_money1);
	DDX_Text(pDX, IDC_EDIT2, m_money2);
	DDX_Text(pDX, IDC_EDIT3, m_rate);
	DDX_Check(pDX, IDC_CHECK1, m_check1);
	DDX_Text(pDX, IDC_EDIT8, m_testmoney);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(SalarySet4, CDialog)
	//{{AFX_MSG_MAP(SalarySet4)
	ON_BN_CLICKED(IDC_BUTTON1, OnAdd)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON14, OnButton14)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// SalarySet4 message handlers

BOOL SalarySet4::OnInitDialog()
{
	CDialog::OnInitDialog();
	// TODO: Add extra initialization here
	if (IsHasRights2new(25) == 0)
	{
		GetDlgItem(IDOK)->EnableWindow(0);
	}
	CString str = g_cominfoarray.ElementAt(0).ElementAt(77);
	int pos = str.Find(m_header);
	int headleng = m_header.GetLength();
	if (pos != -1)
	{
		str = str.Right(str.GetLength() - pos);
		str.Delete(0, headleng);
		pos = str.Find("$");
		if (pos != -1)
			str = str.Left(pos);
		InitRule(str);
	}

	str = m_header;
	str.TrimLeft("$");
	str.TrimRight(":");
	SetWindowText("������ɷ���-" + str);


	//////////////////////////////////////////////////////
	g_sendhead.bsql = 0;
	g_sendhead.code[0] = 160;
	g_sendhead.code[1] = 169;
	g_sendhead.tabcount = 2;
	g_pMainWnd->ProcessChatMessageRequest2(1);
	if (g_bSendOK == 0)
	{
		CDialog::OnCancel();
		return 0;
	}
	CArray<CStringArray, CStringArray>expendtypearray;
	CArray<CStringArray, CStringArray>baseexpendtypearray;
	DataToArray(&expendtypearray, &baseexpendtypearray);
	for (int i = 0; i < baseexpendtypearray.GetSize(); i++)
	{
		m_expendtypearray.Add(baseexpendtypearray.ElementAt(i).ElementAt(0));
	}
	for (i = 0; i < expendtypearray.GetSize(); i++)
	{
		m_expendtypearray.Add(expendtypearray.ElementAt(i).ElementAt(0));
	}
	m_expendtypearray.Add("��Ӱ����");
#ifdef CHILD_VERSION
	m_expendtypearray.Add("��������");
#else
	m_expendtypearray.Add("��ױ����");
#endif // CHILD_VERSION
	m_expendtypearray.Add("ѡƬ����");
	m_expendtypearray.Add("��ӰѡƬ����");

#ifdef CHILD_VERSION
	m_expendtypearray.Add("����ѡƬ����");
#else
	m_expendtypearray.Add("��ױѡƬ����");
#endif // CHILD_VERSION

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

void SalarySet4::OnAdd()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if (CheckRange() == 0)
		return;
	CString str, temp;
	if (m_money1 >= m_money2)
	{
		AfxMessageBox("ҵ����Χ����!", MB_ICONINFORMATION);
		return;
	}
	if (m_list1.GetCount() == 0 && m_money1 != 0)
	{
		AfxMessageBox("��ʼҵ��Ӧ��0��ʼ!", MB_ICONINFORMATION);
		return;
	}

	str.Format("%d-%d ��:%0.1f", m_money1, m_money2, m_rate); str += "%";
	for (int i = 0; i < m_list1.GetCount(); i++)
	{
		m_list1.GetText(i, temp);
		if (str == temp)
		{
			AfxMessageBox("���д˹���!", MB_ICONINFORMATION);
			return;
		}
	}
	m_list1.AddString(str);
}

BOOL SalarySet4::CheckRange()
{
	CString str, temp;
	int money1, money2;
	for (int i = 0; i < m_list1.GetCount(); i++)
	{
		m_list1.GetText(i, temp);
		int pos = temp.Find(" ");
		temp = temp.Left(pos);
		pos = temp.Find("-");
		money1 = atoi(temp.Left(pos));
		money2 = atoi(temp.Right(temp.GetLength() - pos - 1));
		if (m_money1 >= money1 && m_money1 <= money2)
		{
			AfxMessageBox("��ʼҵ���Ѱ��������еĹ�����!", MB_ICONINFORMATION);
			return 0;
		}
		if (m_money2 >= money1 && m_money2 <= money2)
		{
			AfxMessageBox("��ֹҵ���Ѱ��������еĹ�����!", MB_ICONINFORMATION);
			return 0;
		}
	}
	if (m_list1.GetCount())
	{
		m_list1.GetText(m_list1.GetCount() - 1, temp);
		int pos = temp.Find(" ");
		temp = temp.Left(pos);
		pos = temp.Find("-");
		money2 = atoi(temp.Right(temp.GetLength() - pos - 1));
		if (m_money1 != money2 + 1)
		{
			AfxMessageBox("ҵ���β�����,��ʼҵ��Ӧ����һ�������ֹҵ����1", MB_ICONINFORMATION);
			return 0;
		}
	}
	return 1;
}

void SalarySet4::OnButton3()
{
	// TODO: Add your control notification handler code here
	if (AfxMessageBox("ȷ��Ҫɾ��ȫ��������?", MB_YESNO | MB_ICONINFORMATION) != IDYES)return;
	m_list1.ResetContent();
}

void SalarySet4::OnOK()
{
	// TODO: Add extra validation here
	UpdateData();
	CString str, temp;
	str = m_header;
	for (int i = 0; i < m_list1.GetCount(); i++)
	{
		m_list1.GetText(i, temp);
		str += temp;
		str += ";";
	}
	temp.Format("%d", m_check1);
	str += temp;

	CString oldrule = g_cominfoarray.ElementAt(0).ElementAt(77);
	CString left, right;
	int pos = oldrule.Find(m_header);
	if (pos != -1)
	{
		left = oldrule.Left(pos);
		right = oldrule.Right(oldrule.GetLength() - pos - m_header.GetLength());
		pos = right.Find("$");
		if (pos != -1)
			right = right.Right(right.GetLength() - pos);
		else
			right.Empty();
		str = left + str + right;
	}
	else
		str += oldrule;


	CString name;
	temp.Empty();
	pos = str.Find("$");
	while (pos != -1)
	{
		int pos2 = str.Find(":");
		name = str.Mid(pos + 1, pos2 - pos - 1);
		BOOL bFind = 0;
		for (int i = 0; i < m_expendtypearray.GetSize(); i++)
		{
			if (name == m_expendtypearray.ElementAt(i))
			{
				bFind = 1;
				break;
			}
		}
		if (bFind)
		{
			temp += str.Left(pos2);
			str = str.Right(str.GetLength() - pos2);
			pos = str.Find("$");
			if (pos != -1)
			{
				temp += str.Left(pos);
				str = str.Right(str.GetLength() - pos);
			}
			else
				temp += str;
			pos = str.Find("$");
		}
		else
		{
			str = str.Right(str.GetLength() - pos2);
			pos = str.Find("$");
			if (pos != -1)
			{
				str = str.Right(str.GetLength() - pos);
			}
			pos = str.Find("$");
		}
	}
	str = temp;

	//$��Ӱʦǰ��:0-1000 ��:1.0%;0$����ǰ��:0-1000 ��:1.0%;0$��Ӱ����:0$ǰ��:0$��ױ����:0$ѡƬ����:1
	//	m_expendtypearray





	CString sql;
	sql.Format("update version set [salaryset]='%s'", str);
	g_sendhead.bsql = 1;
	g_pMainWnd->ProcessChatMessageRequest2(sql); if (g_bSendOK == 0)return;
	AfxMessageBox("����ɹ�!", MB_ICONINFORMATION);

	g_cominfoarray.ElementAt(0).SetAt(77, str);
	CDialog::OnOK();
}

float GetSalaryFromRule(float fmoney, CString rule)
{
	float fresult = 0;
	int pos = rule.Find(":");
	if (pos == -1)return 0;
	rule = rule.Right(rule.GetLength() - pos - 1);
	CStringArray rulearray;
	pos = rule.Find(";");
	while (pos != -1)
	{
		rulearray.Add(rule.Left(pos));
		rule = rule.Right(rule.GetLength() - pos - 1);
		pos = rule.Find(";");
	}
	BOOL check1 = atoi(rule);
	if (rulearray.GetSize() == 0)return 0;

	CString str, temp;
	int money1, money2;
	float frate;

	if (check1 == 0)
	{
		for (int i = 0; i < rulearray.GetSize(); i++)
		{
			temp = rulearray.ElementAt(i);
			int pos = temp.Find(" ");
			temp = temp.Left(pos);
			pos = temp.Find("-");
			money1 = atoi(temp.Left(pos));
			money2 = atoi(temp.Right(temp.GetLength() - pos - 1));
			if ((i == rulearray.GetSize() - 1) || (fmoney >= money1 && fmoney <= money2))
			{
				temp = rulearray.ElementAt(i);
				pos = temp.Find(":");
				temp = temp.Right(temp.GetLength() - pos - 1);
				temp.TrimRight("%");
				frate = atof(temp) / 100.0;
				fresult = frate*fmoney;
				return fresult;
			}
		}
	}
	else//////
	{
		for (int i = 0; i < rulearray.GetSize(); i++)
		{
			temp = rulearray.ElementAt(i);
			int pos = temp.Find(" ");
			temp = temp.Left(pos);
			pos = temp.Find("-");
			money1 = atoi(temp.Left(pos));
			money2 = atoi(temp.Right(temp.GetLength() - pos - 1));
			if ((i == rulearray.GetSize() - 1) || (fmoney >= money1 && fmoney <= money2))
			{
				temp = rulearray.ElementAt(i);
				pos = temp.Find(":");
				temp = temp.Right(temp.GetLength() - pos - 1);
				temp.TrimRight("%");
				frate = atof(temp) / 100.0;
				float dtmoney = fmoney - money1;
				fresult += frate*dtmoney;
				if (i == 0)//ֻ��һ������
				{
					fresult = frate*fmoney;
					return fresult;
				}

				while (i)
				{
					i--;
					temp = rulearray.ElementAt(i);
					pos = temp.Find(" ");
					temp = temp.Left(pos);
					pos = temp.Find("-");
					money1 = atoi(temp.Left(pos));
					money2 = atoi(temp.Right(temp.GetLength() - pos - 1));

					temp = rulearray.ElementAt(i);
					pos = temp.Find(":");
					temp = temp.Right(temp.GetLength() - pos - 1);
					temp.TrimRight("%");
					frate = atof(temp) / 100.0;
					dtmoney = money2 - money1;
					fresult += frate*dtmoney;
				}
				return fresult;
			}
		}
	}

	return fresult;
}
/*
float GetSalaryFromRule(float fmoney, CString rule)
{
float fresult=0;
int pos=rule.Find (":");
if(pos==-1)return 0;
rule=rule.Right (rule.GetLength ()-pos-1);
CStringArray rulearray;
pos=rule.Find (";");
while(pos!=-1)
{
rulearray.Add (rule.Left (pos));
rule=rule.Right (rule.GetLength ()-pos-1);
pos=rule.Find (";");
}
BOOL check1=atoi(rule);
if(rulearray.GetSize ()==0)return 0;

CString str,temp;
int money1,money2;
float frate;
for(int i=0; i<rulearray.GetSize (); i++)
{
temp=rulearray.ElementAt (i);
int pos=temp.Find(" ");
temp=temp.Left(pos);
pos=temp.Find("-");
money1=atoi(temp.Left(pos));
money2=atoi(temp.Right(temp.GetLength()-pos-1));
if( (i==rulearray.GetSize ()-1) || (fmoney>=money1 && fmoney<=money2) )
{
temp=rulearray.ElementAt (i);
pos=temp.Find (":");
temp=temp.Right (temp.GetLength ()-pos-1);
temp.TrimRight ("%");
frate=atof(temp)/100.0;
if(check1 && i>0)
{
temp=rulearray.ElementAt (i-1);
pos=temp.Find (":");
temp=temp.Right (temp.GetLength ()-pos-1);
temp.TrimRight ("%");
float frate2=atof(temp)/100.0;
fresult=(money1-1)*frate2+frate*(fmoney-money1+1);
}
else
{
fresult=frate*fmoney;
}
return fresult;
}
}
return fresult;
}
*/
void SalarySet4::OnButton2()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if (m_testmoney < 1)
	{
		m_testmoney = 100000;
		UpdateData(0);
	}
	CString rule = GetRule();
	float fresult = GetSalaryFromRule(m_testmoney, rule);
	CString temp;
	temp.Format("%0.2f", fresult);
	SetDlgItemText(IDC_EDIT4, temp);

	/*	CString str,temp;
		int money1,money2;
		float frate;
		for(int i=0; i<m_list1.GetCount (); i++)
		{
		m_list1.GetText (i, temp);
		int pos=temp.Find(" ");
		temp=temp.Left(pos);
		pos=temp.Find("-");
		money1=atoi(temp.Left(pos));
		money2=atoi(temp.Right(temp.GetLength()-pos-1));
		if( (i==m_list1.GetCount ()-1) || (m_testmoney>=money1 && m_testmoney<=money2) )
		{
		m_list1.GetText (i, temp);
		pos=temp.Find (":");
		temp=temp.Right (temp.GetLength ()-pos-1);
		temp.TrimRight ("%");
		frate=atof(temp)/100.0;
		float fresult;
		if(m_check1 && i>0)
		{
		m_list1.GetText (i-1, temp);
		pos=temp.Find (":");
		temp=temp.Right (temp.GetLength ()-pos-1);
		temp.TrimRight ("%");
		float frate2=atof(temp)/100.0;
		fresult=(money1-1)*frate2+frate*(m_testmoney-money1+1);
		}
		else
		{
		fresult=frate*m_testmoney;
		}
		temp.Format ("%0.2f", fresult);
		SetDlgItemText(IDC_EDIT4, temp);
		return;
		}
		}
		AfxMessageBox("���������!", MB_ICONINFORMATION);*/
}

CString SalarySet4::GetRule()
{
	CString str, temp;
	str = m_header;//"$ǰ��:";
	for (int i = 0; i < m_list1.GetCount(); i++)
	{
		m_list1.GetText(i, temp);
		str += temp;
		str += ";";
	}
	temp.Format("%d", m_check1);
	str += temp;
	return str;
}

void SalarySet4::OnButton14()
{
	// TODO: Add your control notification handler code here
	SelExpendType seldlg;
	seldlg.m_mode = 1;
	seldlg.m_bSecond = 1;
	if (seldlg.DoModal() != IDOK)return;
	CString oldrule = g_cominfoarray.ElementAt(0).ElementAt(77);
	CString header = "$" + seldlg.m_sel + ":";

	CString left, right;
	int pos = oldrule.Find(header);
	if (pos != -1)
	{
		right = oldrule.Right(oldrule.GetLength() - pos - header.GetLength());
		pos = right.Find("$");
		if (pos != -1)
			right = right.Left(pos);
		InitRule(right);
	}
	else
	{
		AfxMessageBox(seldlg.m_sel + "δ���ù���!", MB_ICONINFORMATION);
	}
}

void SalarySet4::InitRule(CString str)
{
	m_list1.ResetContent();
	int pos = str.Find(";");
	while (pos != -1)
	{
		m_list1.AddString(str.Left(pos));
		str = str.Right(str.GetLength() - pos - 1);
		pos = str.Find(";");
	}
	m_check1 = atoi(str);
	UpdateData(false);
}