// AreaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ylgl.h"
#include "AreaDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// AreaDlg dialog
AreaDlg::AreaDlg(CWnd* pParent /*=NULL*/)
	: CDialog(AreaDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(AreaDlg)
	m_edit1 = _T("");
	//}}AFX_DATA_INIT
}

void AreaDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(AreaDlg)
	DDX_Control(pDX, IDC_LIST1, m_List1);
	DDX_Text(pDX, IDC_EDIT1, m_edit1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(AreaDlg, CDialog)
	//{{AFX_MSG_MAP(AreaDlg)
	ON_BN_CLICKED(IDC_BUTadd, OnBUTadd)
	ON_BN_CLICKED(IDC_BUTdel, OnBUTdel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
//AreaDlg message handlers
//
BOOL AreaDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	// TODO: Add extra initialization here
	CRect rc;
	m_List1.GetClientRect(rc);
	CString strHeadings;
	strHeadings.Format("��������,%d", rc.Width() - 18);
	m_List1.SetHeadings(strHeadings);
	FillGrid();
	this->CenterWindow(g_pMainWnd);
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void AreaDlg::FillGrid()
{
	g_sendhead.bsql = 0;
	g_sendhead.code[0] = 82;
	g_sendhead.tabcount = 1;
	g_pMainWnd->ProcessChatMessageRequest2(2); if (g_bSendOK == 0)return;
	DataToArray(&g_List1array);

	m_List1.DeleteAllItems2();
	int ii = 0;
	{
		m_List1.m_arLabels.SetSize(g_List1array.GetSize(), 1);
		for (ii = 0; ii < m_List1.m_arLabels.GetSize(); ii++)
			m_List1.m_arLabels.ElementAt(ii).Copy(g_List1array.ElementAt(ii));
	}
	m_List1.m_LabelCount = ii;
	m_List1.SetItemCountEx(ii);
}

void AreaDlg::OnBUTadd()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_edit1.TrimLeft();
	m_edit1.TrimRight();
	if (m_edit1.IsEmpty())
	{
		AfxMessageBox("������������!", MB_ICONINFORMATION);
		this->GetDlgItem(IDC_EDIT1)->SetFocus();
		return;
	}
	CString sql;
	sql.Format("insert into [area] (name)values('%s')", m_edit1);
	g_sendhead.bsql = 1;
	g_pMainWnd->ProcessChatMessageRequest2(sql); if (g_bSendOK == 0)return;
	FillGrid();
}

void AreaDlg::OnBUTdel()
{
	// TODO: Add your control notification handler code here
	POSITION pos;
	pos = m_List1.GetFirstSelectedItemPosition();
	if (pos == NULL)
	{
		AfxMessageBox("��ѡ����Ҫɾ�������!", MB_ICONINFORMATION);
		return;
	}

	if (AfxMessageBox("ȷ��ɾ����?", MB_YESNO | MB_ICONINFORMATION) != IDYES)return;
	int iItem = m_List1.GetNextSelectedItem(pos);
	CString name = m_List1.GetItemText(iItem, 0);

	CString sql;
	sql.Format("delete from area where name='%s'", name);
	g_sendhead.bsql = 1;
	g_pMainWnd->ProcessChatMessageRequest2(sql); if (g_bSendOK == 0)return;
	FillGrid();
}