#include "CNewOS.h"
#include "resource.h"
#include "CMainEditor.h"
IMPLEMENT_DYNAMIC(CNewOS, CDialog)

BEGIN_MESSAGE_MAP(CNewOS, CDialog)
	ON_BN_CLICKED(IDOK, &CNewOS::OnBnClickedOk)
	ON_BN_CLICKED(IDC_DIR, &CNewOS::OnBnClickedDir)
END_MESSAGE_MAP()

CNewOS::CNewOS(CWnd* pParent)
	: CDialog(IDD_CMAINEDITOR_NEWOS, pParent)
{
}

CNewOS::~CNewOS()
{
}

void CNewOS::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_NAME, edit_label);
	DDX_Control(pDX, IDC_EDIT_AUTHOR, edit_author);
	DDX_Control(pDX, IDC_EDIT_DIR, edit_dir);
	DDX_Control(pDX, IDC_DIR, button_dir);
}

BOOL CNewOS::OnInitDialog()
{
	CDialog::OnInitDialog();
	//设置控件初始状态
	if (load)//如果是在编辑模式下
	{
		CMainEditor* mainEditor = (CMainEditor*)pMainEditor;
		edit_label.SetWindowTextW(mainEditor->osName.GetString());
		edit_author.SetWindowTextW(mainEditor->osAuthor.GetString());
		edit_dir.SetWindowTextW(mainEditor->osFilePath.GetString());
		edit_dir.EnableWindow(false);
		button_dir.EnableWindow(false);
		SetWindowText(L"编辑仓库信息");
	}
	return 0;
}


void CNewOS::OnBnClickedOk()
{
	
	//1.检查名字是否为空
	CStringW wstr;
	edit_label.GetWindowTextW(wstr);
	if (wstr.GetLength() == 0)
	{
		MessageBoxW(L"名字不能为空", L"填写信息未完成", MB_ICONINFORMATION);
		return;
	}
	//2.检查保存目录是否有效
	edit_dir.GetWindowTextW(wstr);
	if (wstr.GetLength() == 0)
	{
		MessageBoxW(L"必须选择一个保存目录", L"填写信息未完成", MB_ICONINFORMATION);
		return;
	}
	wchar_t buff[MAX_PATH];
	ZeroMemory(&buff, sizeof(buff));
	//去掉文件名，检查目录是否存在
	int len = wstr.ReverseFind('\\') + 1;
	CString cutStr = wstr.Left(len);
	wcscpy_s(buff, cutStr.GetString());
	if (!PathIsDirectoryW(buff))
	{
		MessageBoxW(L"目录不存在", L"填写信息错误", MB_ICONINFORMATION);
		return;
	}
	//返回数据
	edit_label.GetWindowTextW(outName);
	edit_author.GetWindowTextW(outAuthor);
	edit_dir.GetWindowTextW(outFilePath);
	CDialog::OnOK();
}


void CNewOS::OnBnClickedDir()
{
	
	CFileDialog fd(false, TEXT(""), TEXT("ocstore.db"), OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT, TEXT("数据库文件(*.db)|*.db|所有文件(*.*)|*.*||"), this);
	if (fd.DoModal() != IDOK)
		return;
	edit_dir.SetWindowText(fd.GetPathName());
}
