// DlgDataManage.cpp : 实现文件
//

#include "stdafx.h"
#include "Draw.h"
#include "DlgDataManage.h"
#include "DBOperate.h"


// CDlgDataManage 对话框

IMPLEMENT_DYNAMIC(CDlgDataManage, CDialog)

CDlgDataManage::CDlgDataManage(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgDataManage::IDD, pParent)
	, m_pMap(NULL)
{

}

CDlgDataManage::~CDlgDataManage()
{
}

void CDlgDataManage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_DATA, m_datalist);
}


BEGIN_MESSAGE_MAP(CDlgDataManage, CDialog)
	ON_BN_CLICKED(IDC_BTN_LOAD, &CDlgDataManage::OnBnClickedBtnLoad)
	ON_BN_CLICKED(IDC_BTN_DELETE, &CDlgDataManage::OnBnClickedBtnDelete)
END_MESSAGE_MAP()


// CDlgDataManage 消息处理程序

BOOL CDlgDataManage::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_datalist.ModifyStyle(0L,LVS_REPORT);
	m_datalist.SetExtendedStyle(m_datalist.GetExtendedStyle()
		|LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_SINGLESEL&~LVS_EX_CHECKBOXES);
	m_datalist.InsertColumn(0,_T("图幅名字"));
	m_datalist.InsertColumn(1,_T("时间"));

	CRect rect;
	m_datalist.GetClientRect(rect);
	m_datalist.SetColumnWidth(0,rect.Width()*2/5);
	m_datalist.SetColumnWidth(1,rect.Width()*3/5);

	CStringArray arMapName,arTime;
	CDBOperate db;
	db.m_pConnection = g_pConnection;
	db.GetMapName(arMapName,arTime);

	for (int i=0; i!=arMapName.GetCount(); ++i)
	{
		m_datalist.InsertItem(i,"");
		m_datalist.SetItemText(i,0,arMapName.GetAt(i));
		m_datalist.SetItemText(i,1,arTime.GetAt(i));
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

void CDlgDataManage::OnBnClickedBtnLoad()
{
	// TODO: 在此添加控件通知处理程序代码
	int nItem = m_datalist.GetSelectionMark();
	if (nItem == -1)
	{
		AfxMessageBox(_T("请选择要查看的行！"));
		return;
	}
	CString strMap = m_datalist.GetItemText(nItem,0);
	ASSERT(m_pMap);
	CDBOperate db;
	db.m_pConnection = g_pConnection;
	C2DMap* pMap = NULL;
	pMap = db.CreateMap(strMap);
	if (pMap != NULL)
	{
		delete m_pMap;
		m_pMap = pMap;
		OnOK();
	}
}

void CDlgDataManage::OnBnClickedBtnDelete()
{
	// TODO: 在此添加控件通知处理程序代码
	int nItem = m_datalist.GetSelectionMark();
	if (nItem == -1)
	{
		AfxMessageBox(_T("请选择要删除的行！"));
		return;
	}
	CString strMap = m_datalist.GetItemText(nItem,0);
	CString strMsg;
	strMsg.Format(_T("确定要删除图幅：%s"),strMap);
	int nRes = AfxMessageBox(strMsg,MB_YESNO);
	if (nRes == IDYES)
	{
		CDBOperate db;
		db.m_pConnection = g_pConnection;
		if (db.DeleteMap(strMap))
		{
			m_datalist.DeleteItem(nItem);
			AfxMessageBox(_T("删除成功！"));
		}
	}
}
