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

#include "pch.h"
#include "ZGKJ.h"
#include "afxdialogex.h"
#include "Personnel.h"
#include "AddDep.h"
#include "AddPost.h"


// Personnel 对话框

IMPLEMENT_DYNAMIC(Personnel, CDialogEx)

Personnel::Personnel(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PERSONNEL, pParent)
	, awatiName(_T(""))
	, awatiPhone(_T(""))
	, depName(_T(""))
	, postName(_T(""))
{
	//pParent = new Personnel;
}

Personnel::~Personnel()
{
	
}

void Personnel::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_DEPARMENT, ListDeparment);
	DDX_Control(pDX, IDC_LIST_AWATI, ListAwati);
	DDX_Control(pDX, IDC_LIST3_STAFF, ListStaff);
	DDX_Control(pDX, IDC_COMBO_DEP, DepName);
	DDX_Control(pDX, IDC_LIST_POST, ListPost);
	DDX_Control(pDX, IDC_COMBO2_POST, PostName);
	DDX_Text(pDX, IDC_PER_NAME, awatiName);
	DDX_Text(pDX, IDC_PER_PHONE, awatiPhone);
	DDX_CBString(pDX, IDC_COMBO_DEP, depName);
	DDX_CBString(pDX, IDC_COMBO2_POST, postName);
}


BEGIN_MESSAGE_MAP(Personnel, CDialogEx)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDOWN()
	ON_NOTIFY(NM_CLICK, IDC_LIST_AWATI, &Personnel::OnNMClickListAwati)
	ON_BN_CLICKED(IDC_ADD_DEP, &Personnel::OnBnClickedAddDep)
	ON_BN_CLICKED(IDC_PER_DELETE, &Personnel::OnBnClickedPerDelete)
	ON_BN_CLICKED(IDC_ENTRY, &Personnel::OnBnClickedEntry)
	ON_BN_CLICKED(IDC_ADD_POST, &Personnel::OnBnClickedAddPost)
	ON_BN_CLICKED(IDC_DIMISSION, &Personnel::OnBnClickedDimission)
	ON_WM_TIMER()
END_MESSAGE_MAP()

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

	// TODO:  在此添加额外的初始化
	InitListDeparment();
	LoadDepartement();
	InitListAwati();
	LoadAwatiForReview();
	InitList3Staff(); 
	LoadSaff();
	InitListPost();
	LoadPost();
	SetTimer(1, 120000, NULL);

	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
// Personnel 消息处理程序


void Personnel::InitListDeparment()
{
	// TODO: 在此处添加实现代码.
	ListDeparment.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	ListDeparment.InsertColumn(0, _T("部门ID"), LVCFMT_LEFT, 70);
	ListDeparment.InsertColumn(1, _T("部门名称"), LVCFMT_LEFT, 90);
}

void Personnel::InitList3Staff()
{
	ListStaff.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES| WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_REPORT | LVS_EDITLABELS);
	//ListStaff.InsertColumn(0, _T("员工ID"), LVCFMT_LEFT, 60);
	ListStaff.InsertColumn(0, _T("员工姓名"), LVCFMT_LEFT, 100);
	ListStaff.InsertColumn(1, _T("员工电话"), LVCFMT_LEFT, 150);
	ListStaff.InsertColumn(2, _T("部门"), LVCFMT_LEFT, 105);
	ListStaff.InsertColumn(3, _T("职务"), LVCFMT_LEFT, 105);
}

void Personnel::InitListPost()
{
	ListPost.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	ListPost.InsertColumn(0, _T("职务ID"), LVCFMT_LEFT, 70);
	ListPost.InsertColumn(1, _T("职务名称"), LVCFMT_LEFT, 90);
}

void Personnel::InitListAwati()
{
	// TODO: 在此处添加实现代码.
	ListAwati.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	ListAwati.InsertColumn(0, _T("待审核人姓名"), LVCFMT_LEFT, 130);
	ListAwati.InsertColumn(1, _T("待审核人电话"), LVCFMT_LEFT, 160);
}

void Personnel::LoadAwatiForReview()
{
	ListAwati.DeleteAllItems();
	char* strSql = "SELECT name,phone FROM tb_wait_for_review;";
	if (!theApp.sql->ExecutiveCommand(strSql))
	{
		int i = 0;
		MYSQL_RES* res = theApp.sql->GetResultSet();
		while (theApp.sql->row = mysql_fetch_row(res))
		{
			CString str,str2;
			str = theApp.MyAtoT(theApp.sql->row[0]);
			str2 = theApp.MyAtoT(theApp.sql->row[1]);
			ListAwati.InsertItem(i, str);
			ListAwati.SetItemText(i, 1, str2);
			i++;
		}
	}
	else 
	{
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()), MB_OK | MB_ICONERROR);
	}
	return;
}

void Personnel::LoadDepartement()
{
	//"INSERT INTO tb_department (department_id,department_name)VALUES (%d,'%s')", depID, depName);
	ListDeparment.DeleteAllItems();
	DepName.ResetContent();
	char* strSql = "SELECT department_id,department_name FROM tb_department;";
	if (!theApp.sql->ExecutiveCommand(strSql))
	{
		int i = 0;
		MYSQL_RES* res = theApp.sql->GetResultSet();
		while (theApp.sql->row = mysql_fetch_row(res))
		{
			CString str, str2;
			str = theApp.MyAtoT(theApp.sql->row[0]);
			str2 = theApp.MyAtoT(theApp.sql->row[1]);
			ListDeparment.InsertItem(i, str);
			ListDeparment.SetItemText(i, 1, str2);
			DepName.AddString(str2);
			i++;
		}
	}
	else
	{
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()), MB_OK | MB_ICONERROR);
	}
	return;
}

void Personnel::LoadPost()
{
	ListPost.DeleteAllItems();
	PostName.ResetContent();
	char* strSql = "SELECT post_id,post_name FROM tb_post;";
	//char* strSql = "SELECT department_id,department_name FROM tb_department;";
	if (!theApp.sql->ExecutiveCommand(strSql))
	{
		int i = 0;
		MYSQL_RES* res = theApp.sql->GetResultSet();
		while (theApp.sql->row = mysql_fetch_row(res))
		{
			CString str, str2;
			str = theApp.MyAtoT(theApp.sql->row[0]);
			str2 = theApp.MyAtoT(theApp.sql->row[1]);
			ListPost.InsertItem(i, str);
			ListPost.SetItemText(i, 1, str2);
			PostName.AddString(str2);
			i++;
		}
	}
	else
	{
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()), MB_OK | MB_ICONERROR);
	}
	return;
}

void Personnel::LoadSaff()
{
	ListStaff.DeleteAllItems();
	int i = 0;
	//char deparment[STR_NUM], post[STR_NUM];
	CString Phone, Name, Department, Post;
	char order[STR_NUM];
	sprintf_s(order, STR_NUM - 1, "SELECT tb_staff.name,tb_staff.phone,tb_department.department_name,tb_post.post_name FROM tb_staff INNER JOIN tb_department INNER JOIN tb_post ON tb_staff.department_id =tb_department.department_id AND tb_staff.post_id=tb_post.post_id;");
	if (!theApp.sql->ExecutiveCommand(order))
	{
		
		MYSQL_RES* res = theApp.sql->GetResultSet();
		while (theApp.sql->row = mysql_fetch_row(res))
		{
			Phone = theApp.MyAtoT(theApp.sql->row[0]);
			Name = theApp.MyAtoT(theApp.sql->row[1]);
			Department = theApp.MyAtoT(theApp.sql->row[2]);
			Post = theApp.MyAtoT(theApp.sql->row[3]);
			ListStaff.InsertItem(i, Phone);
			ListStaff.SetItemText(i, 1, Name);
			ListStaff.SetItemText(i, 2, Department);
			ListStaff.SetItemText(i, 3, Post);
			i++;
		}
	}
	else
	{
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()), MB_OK | MB_ICONERROR);
	}
	return;
}



void Personnel::OnNMClickListAwati(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	POSITION pos = ListAwati.GetFirstSelectedItemPosition();
	int i = ListAwati.GetNextSelectedItem(pos);
	CString str,str2;
	str = ListAwati.GetItemText(i, 0);
	str2 = ListAwati.GetItemText(i, 1);
	SetDlgItemText(IDC_PER_NAME,str);
	SetDlgItemText(IDC_PER_PHONE, str2);
	*pResult = 0;
}


void Personnel::OnBnClickedAddDep()
{
	// TODO: 在此添加控件通知处理程序代码
	AddDep adddep;
	if(adddep.DoModal()==IDOK)
	{
		LoadDepartement();
	}
	
}


void Personnel::OnBnClickedPerDelete()
{
	// TODO: 在此添加控件通知处理程序代码
	CString name, phone, str;
	char order[STR_NUM], * str1, * str2;
	GetDlgItemText(IDC_PER_NAME, name);
	GetDlgItemText(IDC_PER_PHONE, phone);
	str.Format(_T("确认删除待入职人员：%s 吗？"), name);
	str1 = CZGKJApp::MyTtoA(name);
	str2 = CZGKJApp::MyTtoA(phone);
	sprintf_s(order, STR_NUM - 1, "DELETE FROM tb_wait_for_review WHERE  name = '%s' AND phone = '%s' ", str1, str2);
	if (AfxMessageBox(str, MB_YESNO) == IDNO)
	{
		return;
	}
	else if (!theApp.sql->ExecutiveCommand(order))
	{
		str.Format(_T("%s 已删除成功！"), name);
		AfxMessageBox(str);
		LoadAwatiForReview();
		return;
	}
	else
	{
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()), MB_OK | MB_ICONERROR);
	}
}



void Personnel::OnBnClickedEntry()
{
	UpdateData(TRUE);
	if (depName.IsEmpty() || postName.IsEmpty())
	{
		AfxMessageBox(_T("必须指定待入职人员部门和职务！"));
		return;
	}
	char pass[STR_NUM];
	char key[STR_NUM];
	char order[STR_NUM];
	int key1;
	int depID;
	int postID;
	char* phone = CZGKJApp::MyTtoA(awatiPhone);
	char* name = CZGKJApp::MyTtoA(awatiName);
	sprintf_s(order, STR_NUM - 1, "SELECT pass, secret_key FROM tb_wait_for_review WHERE phone = '%s'", phone);
	if (!theApp.sql->ExecutiveCommand(order))
	{
		//if(theApp.sql.)
		MYSQL_RES* res = theApp.sql->GetResultSet();
		if (res->row_count == 0)
		{
			AfxMessageBox(_T("无此用户注册信息！"));
			return; 
		}
		theApp.sql->row = mysql_fetch_row(res);
		strcpy_s(pass, STR_NUM, theApp.sql->row[0]);
		strcpy_s(key, STR_NUM, theApp.sql->row[1]);
		key1 = atoi(key);	
	}
	else
	{
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()));
		return;
	}


	sprintf_s(order, STR_NUM, "SELECT department_id FROM tb_department WHERE department_name = '%s';",CZGKJApp::MyTtoA(depName));
	if (!theApp.sql->ExecutiveCommand(order))
	{
		MYSQL_RES* res = theApp.sql->GetResultSet();
		theApp.sql->row = mysql_fetch_row(res);
		depID = atoi(theApp.sql->row[0]);
		
	}
	else
	{
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()), MB_OK | MB_ICONERROR);
		return;
	}

	sprintf_s(order, STR_NUM, "SELECT post_id FROM tb_post WHERE post_name = '%s';", CZGKJApp::MyTtoA(postName));
	if (!theApp.sql->ExecutiveCommand(order))
	{
		MYSQL_RES* res = theApp.sql->GetResultSet();
		theApp.sql->row = mysql_fetch_row(res);
		postID = atoi(theApp.sql->row[0]);

	}
	else
	{
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()));
		return;
	}

	
	sprintf_s(order, STR_NUM, "INSERT INTO tb_staff (phone,name,department_id,password,secret_key,post_id)VALUES ('%s','%s',%d,'%s',%d,%d)",phone, name, depID, pass, key1,postID);
	
	if (!theApp.sql->ExecutiveCommand(order))
	{

		sprintf_s(order, STR_NUM - 1, "DELETE FROM tb_wait_for_review WHERE  name = '%s' AND phone = '%s' ", name, phone);
		theApp.sql->ExecutiveCommand(order);
		AfxMessageBox(awatiName + _T("入职成功！"));
		LoadAwatiForReview();
		LoadSaff();
	}
	else
	{
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()), MB_OK | MB_ICONERROR);
		return;
	}
	SetDlgItemText(IDC_PER_NAME,_T(""));
	SetDlgItemText(IDC_PER_PHONE, _T(""));
	SetDlgItemText(IDC_COMBO_DEP, _T(""));
	SetDlgItemText(IDC_COMBO2_POST, _T(""));
	return;
}


void Personnel::OnBnClickedAddPost()
{
	// TODO: 在此添加控件通知处理程序代码
	AddPost addPost;
	if(addPost.DoModal()==IDOK)
	{ 
		LoadPost();
	}
	
}


void Personnel::OnBnClickedDimission()
{
	// TODO: 在此添加控件通知处理程序代码
	CString phone, name,str;
	int i = ListStaff.GetSelectionMark();
	if (i < 0)
	{
		AfxMessageBox(_T("请选中要离职的人员"));
		return;
	}
	
	name = ListStaff.GetItemText(i, 0);
	phone = ListStaff.GetItemText(i, 1);

	str.Format(_T("请确认 %s 离职手续已办理完毕！"), name);
;
	if (AfxMessageBox(str, MB_YESNO) == IDNO)
	{
		return;
	}

	char order[STR_NUM];
	sprintf_s(order, STR_NUM, "DELETE FROM tb_staff WHERE phone = '%s'", CZGKJApp::MyTtoA(phone));
	if (theApp.sql->ExecutiveCommand(order))
	{
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()), MB_OK | MB_ICONERROR);
	}
	LoadSaff();
	return;
}



void Personnel::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	char order[STR_NUM] = "SELECT 1";
	if (nIDEvent == 1) 
	{
		if (theApp.sql->ExecutiveCommand(order))
		{
			AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()), MB_OK | MB_ICONERROR);
		}
		MYSQL_RES* res = theApp.sql->GetResultSet();
		
	}
	
	CDialogEx::OnTimer(nIDEvent);
}
