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

#include "stdafx.h"
#include "HealthHutUi.h"
#include "HealthHutUiDlg.h"
#include "afxdialogex.h"
#include "SqliteUtil.h"
#include "RegegitKey.h"
#include "Upload.h"
#include "utils.h"

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()

CHealthHutUiDlg::CHealthHutUiDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CHealthHutUiDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHealthHutUiDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, mList);
	DDX_Control(pDX, IDC_EDIT1, mName);
	DDX_Control(pDX, IDC_EDIT2, mCard);
}

BEGIN_MESSAGE_MAP(CHealthHutUiDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDOK, &CHealthHutUiDlg::OnBnClickedOk)
	ON_WM_CREATE()
	ON_WM_CLOSE()
	ON_NOTIFY(NM_CLICK, IDC_LIST1, &CHealthHutUiDlg::OnNMClickList1)
END_MESSAGE_MAP()

// CHealthHutUiDlg 消息处理程序

BOOL CHealthHutUiDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	// 执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	TCHAR rgtsz[3][10] = {_T("姓名"), _T("身份证"), _T(" ")};
	LV_COLUMN lvcolumn;
	CRect rect;
	mList.GetWindowRect(&rect);
	for(int i=0;i<3;i++)  
	{
		lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH | LVCF_ORDER|LVS_EX_FULLROWSELECT;  
		lvcolumn.fmt = LVCFMT_CENTER;
		lvcolumn.pszText = rgtsz[i];
		lvcolumn.iSubItem = i;lvcolumn.iOrder = i;
		if(i==0) {
			  lvcolumn.cx = rect.Width()*1/5 ;
		}
		else
			lvcolumn.cx = rect.Width()*2/5;
		mList.InsertColumn(i, &lvcolumn);
	}

	int row=0, column=0;
	char **result = 0;

	result = CSqliteUtil::instance()->read_all(&row, &column);
	int l = 0;
	for (int j = 0; j < row; j++) {
		mList.InsertItem(j , CString(result[(j+1) * column + 2]).GetString());
		mList.SetItemText(j, 1, CString(result[(j+1) * column + 3]).GetString());
		mList.SetItemText(j, 2, _T("选择"));
	}

	CSqliteUtil::instance()->clear(result);
	if (CRegegitKey::check_reg()) {
		MessageBox(_T("初始化完成"),_T("欢迎使用"),MB_OK);
		CDialogEx::OnOK();
		return TRUE;
	}

	if (0 != Login()) {
		MessageBox(_T("登录失败，请检查网络，如仍有错误，请联系技术支持！"),_T("错误"),MB_OK);
		CDialogEx::OnOK();
		return TRUE;
	}

	if (mMain.start()) {
		MessageBox(_T("当前没有文件需要上传"),_T("提示信息"),MB_OK);
		CDialogEx::OnOK();
	}

	return TRUE;
}

void CHealthHutUiDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。
void CHealthHutUiDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CHealthHutUiDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CHealthHutUiDlg::OnBnClickedOk()
{
	CString name;
	mName.GetWindowText(name);
	CString idCard;
	mCard.GetWindowText(idCard);

	if (name.GetLength() == 0)
	{
		MessageBox(_T("请填写姓名"),_T("提示"),MB_OK);
		return;
	}

	if (idCard.GetLength() == 0)
	{
		MessageBox(_T("请填写身份证"),_T("提示"),MB_OK);
		return;
	}

	int ret = search(name, idCard);
	if (1 == ret)
	{
		MessageBox(_T("查询信息失败"),_T("提示"),MB_OK);
	}
	
	std::vector<CString> names = getNames();
	std::vector<CString> ids = getIds();
	if (0 == names.size() || 0 == ids.size())
	{
		MessageBox(_T("抱歉，机构内无此长者，请确认！"),_T("提示"),MB_OK);
		mName.SetWindowTextW(_T(""));
		mCard.SetWindowTextW(_T(""));
		return;
	}

	mList.DeleteAllItems();
	for (int i = 0; i < names.size(); i++)
	{
		mList.InsertItem(i, names.at(i));
		mList.SetItemText(i, 1, ids.at(i));
		mList.SetItemText(i, 2, _T("选择"));
	}
}

void CHealthHutUiDlg::OnTimer(UINT_PTR nIDEvent)
{
}

int CHealthHutUiDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialogEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	SetTimer(LOCAL_TIMER, 3000, 0);

	mMain.init();
	CSqliteUtil::instance()->init();

	return 0;
}

void CHealthHutUiDlg::OnClose()
{
	CDialogEx::OnOK();
}

void CHealthHutUiDlg::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	DWORD dwPos = GetMessagePos();
	CPoint point( LOWORD(dwPos), HIWORD(dwPos) );

	mList.ScreenToClient(&point);

	LVHITTESTINFO lvinfo;
	lvinfo.pt = point;
	lvinfo.flags = LVHT_ABOVE;

	int nItem = mList.SubItemHitTest(&lvinfo);
	if(nItem != -1)
	{
		if (lvinfo.iSubItem != 2) {
			return;
		}

		CString name = mList.GetItemText(lvinfo.iItem, 0);
		CString idCard = mList.GetItemText(lvinfo.iItem, 1);
		CString path = mMain.getPath();
		if (CUpload::upload(path, name, idCard)) {
			MessageBox(_T("数据上传成功"),_T("提示"),MB_OK);
		}
		else {
			MessageBox(_T("数据上传失败"),_T("提示"),MB_OK);
		}
	}

	CDialogEx::OnOK();
	*pResult = 0;
}
