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

#include "pch.h"
#include "ZGKJ.h"
#include "afxdialogex.h"
#include "SupportStaff.h"
#include <afxinet.h>

// SupportStaff 对话框

IMPLEMENT_DYNAMIC(SupportStaff, CDialogEx)

SupportStaff::SupportStaff(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SUPPORTSTAFF, pParent)
	, ProValue(_T(""))
	, CityValue(_T(""))
	, CountyValue(_T(""))
	, ErrorDescription(_T(""))
	, CompanyName(_T(""))
	, ContactNumber(_T(""))
	, Linkman(_T(""))
{
	
}

SupportStaff::~SupportStaff()
{
}

void SupportStaff::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PROVINCE, ProItem);
	DDX_CBString(pDX, IDC_PROVINCE, ProValue);
	DDX_Control(pDX, IDC_CITY, CityItem);
	DDX_CBString(pDX, IDC_CITY, CityValue);
	DDX_Control(pDX, IDC_COUNTY, CountyItem);
	DDX_CBString(pDX, IDC_COUNTY, CountyValue);
	DDX_Text(pDX, IDC_ERROR_DESCRIPTION, ErrorDescription);
	DDX_Text(pDX, IDC_EDIT1, CompanyName);
	DDX_Text(pDX, IDC_EDIT3, ContactNumber);
	DDX_Text(pDX, IDC_EDIT4, Linkman);
}


BEGIN_MESSAGE_MAP(SupportStaff, CDialogEx)
	ON_WM_TIMER()
	ON_CBN_SELCHANGE(IDC_PROVINCE, &SupportStaff::OnSelchangeProvince)
	ON_CBN_SELCHANGE(IDC_CITY, &SupportStaff::OnSelchangeCity)
	ON_BN_CLICKED(IDC_LOADIMAGE, &SupportStaff::OnBnClickedLoadimage)
	ON_WM_PAINT()

	ON_BN_CLICKED(IDC_CREATE_TICKET, &SupportStaff::OnBnClickedCreateTicket)
	ON_BN_CLICKED(IDC_BUTTON1, &SupportStaff::OnBnClickedButton1)
END_MESSAGE_MAP()


// SupportStaff 消息处理程序


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

	// TODO:  在此添加额外的初始化
	LoadProvince();
	CityItem.EnableWindow(FALSE);
	CountyItem.EnableWindow(FALSE);
	SetTimer(1, 120000, NULL);
	return TRUE;
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

void SupportStaff::LoadProvince()
{
	char order[STR_NUM];
	sprintf_s(order, STR_NUM - 1, "SELECT province.name FROM province");
	if (!theApp.sql->ExecutiveCommand(order))
	{
		MYSQL_RES* res = theApp.sql->GetResultSet();
		while (theApp.sql->row = mysql_fetch_row(res))
		{
			CString temp = theApp.MyAtoT(theApp.sql->row[0]);
			ProItem.AddString(temp);
		}
	}
}
void SupportStaff::OnTimer(UINT_PTR nIDEvent)
{
	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();

	}
	TRACE(_T("哈哈，我就是定时器\n"));
	CDialogEx::OnTimer(nIDEvent);
}


void SupportStaff::OnSelchangeProvince()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData();
	CityItem.ResetContent();
	char order[STR_NUM];
	sprintf_s(order, STR_NUM - 1, "SELECT city.name FROM city INNER JOIN province ON province.id = city.provinceid WHERE province.name='%s'", CZGKJApp::MyTtoA(ProValue));
	if (!theApp.sql->ExecutiveCommand(order))
	{
		MYSQL_RES* res = theApp.sql->GetResultSet();
		while (theApp.sql->row = mysql_fetch_row(res))
		{
			CString temp = theApp.MyAtoT(theApp.sql->row[0]);
			CityItem.AddString(temp);
		}
	}
	CityItem.EnableWindow(TRUE);

}


void SupportStaff::OnSelchangeCity()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData();
	CountyItem.ResetContent();
	char order[STR_NUM];
	sprintf_s(order, STR_NUM - 1, "SELECT area.name FROM area INNER JOIN city ON city.id = area.cityid WHERE city.name = '%s'", CZGKJApp::MyTtoA(CityValue));
	if (!theApp.sql->ExecutiveCommand(order))
	{
		MYSQL_RES* res = theApp.sql->GetResultSet();
		while (theApp.sql->row = mysql_fetch_row(res))
		{
			CString temp = theApp.MyAtoT(theApp.sql->row[0]);
			CountyItem.AddString(temp);
		}
	}
	CountyItem.EnableWindow(TRUE);

}


void SupportStaff::OnBnClickedLoadimage()
{
	// TODO: 在此添加控件通知处理程序代码
	int i = 0;
	CFileDialog fileDlg(TRUE, NULL, _T("D:\\Pictruer\\"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT, _T("Text Files (*.jpg)|*.jpg||"), NULL);
	fileDlg.DoModal();
	fileNum = GetSelectedFileCount(fileDlg);
	if (fileNum > 4)
	{
		AfxMessageBox(_T("最多只能选中4张图片！"));
		return;
	}
	CString pathName = fileDlg.GetPathName(); // 获取第一个文件的完整路径（包括文件名）
	//CString fileName = fileDlg.GetFileName(); // 获取第一个文件的名称
	POSITION pos = fileDlg.GetStartPosition();
	while (pos != NULL)
	{
		pathName = fileDlg.GetNextPathName(pos);// 获取下一个文件的完整路径
		//TRACE(_T("哈哈，我是文件路径：") + pathName + _T("\n"));
		// 处理pathName，例如将其输出到列表控件或者其他控件
		FilePath.push_back(pathName);
		Image[i].Load(pathName);
		i++;
	}
	Invalidate();
	UpdateWindow();	
}

void SupportStaff::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: 在此处添加消息处理程序代码
	// 不为绘图消息调用 CDialogEx::OnPaint()
	int j = IDC_PICTURE;
	CRect rect;
	for (int i = 0; i < fileNum; i++)
	{
		if (!Image[i].IsNull())
		{
			GetDlgItem(j++)->GetWindowRect(rect);
			ScreenToClient(rect);
			Image[i].Draw(dc.m_hDC, rect);
		}
	}

}

int SupportStaff::GetSelectedFileCount(CFileDialog& fileDlg)
{
	int FileCount = 0;
	POSITION filePosition = fileDlg.GetStartPosition();
	while (filePosition != nullptr)
	{
		fileDlg.GetNextPathName(filePosition);
		FileCount++;
	}

	return FileCount;

}

void SupportStaff::OnBnClickedCreateTicket()
{
	
	UpdateData();
	//PreparedStatement* pstmt;
	char order[STR_NUM];
	CString str;
	CString str2;
	CString FileName;
	SYSTEMTIME localTime;
	GetLocalTime(&localTime);
	CTime lt(localTime);
	str += lt.Format(_T("%Y%m%d%H%M%S"));
	str2 = lt.Format(_T("%Y-%m-%d %H:%M:%S"));
	//"INSERT INTO tb_staff (phone,name,department_id,password,secret_key,post_id)VALUES ('%s','%s',%d,'%s',%d,%d)"

	sprintf_s(order, STR_NUM - 1, "INSERT INTO tb_after_sale_ticket (ticketid,error_description,time_begin,province,city,area,company_name,contact_number,linkman,Customer_call) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
		CZGKJApp::MyTtoA(str), CZGKJApp::MyTtoA(ErrorDescription), CZGKJApp::MyTtoA(str2), CZGKJApp::MyTtoA(ProValue), CZGKJApp::MyTtoA(CityValue), CZGKJApp::MyTtoA(CountyValue), CZGKJApp::MyTtoA(CompanyName), CZGKJApp::MyTtoA(ContactNumber), CZGKJApp::MyTtoA(Linkman), CZGKJApp::MyTtoA(theApp.Phone));
	if (theApp.sql->ExecutiveCommand(order))
	{	
		AfxMessageBox(theApp.MyAtoT(theApp.sql->GetErrorInformation()), MB_OK | MB_ICONERROR);
		return;
	}
	else
	{
		if (!ConnectFtpServer(_T("118.24.178.150"), _T("htl"), _T("ycJDHC1602")))
		{
			int a = GetLastError();
			AfxMessageBox(_T("链接FTP服务器失败! 错误代码：") + CZGKJApp::MyItoT(a));
			return;
		}
		if (!pFtpConnection->CreateDirectory(str))
		{
			int a = GetLastError();
			AfxMessageBox(_T("文件夹创建错误！错误码:") + CZGKJApp::MyItoT(a));
			return;
		}
		if (!pFtpConnection->SetCurrentDirectory(str))
		{
			int a = GetLastError();			
			AfxMessageBox(_T("当前路径错误！错误码：")+ CZGKJApp::MyItoT(a));
			return;
		}
		for (int i = 0; i < FilePath.size(); i++)
		{
			CString FileName = str;
			FileName += CZGKJApp::MyItoT(i) + _T(".jpg");
			if (!pFtpConnection->PutFile(FilePath[i],FileName ))
			{ 
				int a = GetLastError();
				AfxMessageBox(_T("文件上传错误！错误码:") + CZGKJApp::MyItoT(a));
				return;
			}
		}
	}
	AfxMessageBox(_T("售后工单已生成，工单编号：" + str));
	SetDlgItemText(IDC_ERROR_DESCRIPTION, _T(""));
	return;
}

void SupportStaff::OnBnClickedButton1()
{
}

BOOL SupportStaff::ConnectFtpServer(CString ServerIP, CString UserName, CString PassWord)
{
	pInternetSession = new CInternetSession; 
	pFtpConnection = new CFtpConnection(pInternetSession, ServerIP,UserName, PassWord);
	if (!pFtpConnection)
	{
		return FALSE;
	}
	return TRUE;
}
