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

#include "stdafx.h"
#include "VCDemo.h"
#include "LoginDlg.h"
#include "VCDemoDlg.h"


// CLoginDlg 对话框

IMPLEMENT_DYNAMIC(CLoginDlg, CDialog)

CLoginDlg::CLoginDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLoginDlg::IDD, pParent)
{
	m_HaveLogin=0;
	m_parent=NULL;

	
	nPTZRight=0;
	nDevMNGRight=0;
	nCONMNGRight=0;
	nSURVEILRight=0;
	nUSRMNGRight=0;
	nVideoReplayRight=0;
	nAudioControlRight=0;
	nBCControlRight=0;
	nTVWallRight=0;
	nAlarmMngRight=0;
	nContentDeleteRight=0;


}

CLoginDlg::~CLoginDlg()
{
}

void CLoginDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CLoginDlg, CDialog)
	ON_BN_CLICKED(IDC_STARTLOGIN, &CLoginDlg::OnBnClickedStartlogin)
END_MESSAGE_MAP()





///单击登陆
void CLoginDlg::OnBnClickedStartlogin()
{
	if(m_parent && m_parent->m_plogObj)
	{
		int port=GetDlgItemInt(IDC_PORT);
		CString user;
		CString pass;
		CString ip;
		GetDlgItemText(IDC_USRE,user);
		GetDlgItemText(IDC_PASS,pass);
		GetDlgItemText(IDC_IPADDR,ip);

		if(user.GetLength()<=0)
		{
			MessageBox(_T("用户名不能为空"),_T("输入错误"));
			return ;
		}else if(pass.GetLength()<=0)
		{
			MessageBox(_T("密码不能为空"),_T("输入错误"));
			return ;
		}else if(ip.GetLength()<=0)
		{
			MessageBox(_T("IP地址不能为空"),_T("输入错误"));
			return ;
		}
		if(port<=0 || port>65535)
		{
			MessageBox(_T("端口错误，只能是1至65535之间"),_T("输入错误"));
			return ;
		}
		_bstr_t strXml=m_parent->m_plogObj->LoginPlat(_bstr_t(user),_bstr_t(pass),_bstr_t(ip),port,1);
		TiXmlDocument doc;
		doc.Parse(strXml);	
		if(!doc.Error())
		{
			//<Response Code="1" UserName="peter" PTZRight ="1" DevMNGRight ="1" CONMNGRight ="1" SURVEILRight ="1" USRMNGRight ="1" 
			//	VideoReplayRight ="1" AudioControlRight ="1" BCControlRight ="1" TVWallRight ="1" 
			//	AlarmMngRight ="1" ContentDeleteRight ="1" Result ="0"/>

			const char * retvalue=doc.RootElement()->Attribute("Result");
			if(_bstr_t(retvalue)!=_bstr_t("0"))
			{//
				int result=atol(retvalue);
				switch(result)
				{
					case 4902:
					{
						MessageBoxA(NULL,"此用户已被锁定,请联系管理员!","登录错误",0);
					}
					break;
					case 4912:
					{
						MessageBoxA(NULL,"登录用户数已超过最大值!","登录错误",0);
					}
					break;
					case 4883:
					{
						MessageBoxA(NULL,"服务响应超时！","登录错误",0);                
					}
					break;
					case 4886:
					{
						MessageBoxA(NULL,"用户名没有登录权限！","登录错误",0);
					}
					break;
					case 4888:
					{
						MessageBoxA(NULL,"用户名不存在！","登录错误",0);
					}
					break;
					case 4880:
					{
						MessageBoxA(NULL,"用户密码错误！","登录错误",0);
					}
					break;
					case 4901:
					{
						MessageBoxA(NULL,"此用户已在其它地方登录，请更换用户！","登录错误",0);
					}
					break;
					case -1:
					{
						MessageBoxA(NULL,"连接失败，请检查服务地址或端口是否正确！","登录错误",0);
					}
					break;
					case -3:
					{
						MessageBoxA(NULL,"登录超时，请重试！","登录错误",0);
					}
					break;
					default:
					{
						//alert("登录失败！错误代码:"+result,"登录错误");
						MessageBox(strXml,_T("登陆失败"));
					}
					break;
				}

			}else
			{
				m_HaveLogin=1;

				const char * buffer;
				buffer=doc.RootElement()->Attribute("PTZRight");
				nPTZRight=atol(buffer);

				buffer=doc.RootElement()->Attribute("DevMNGRight");
				nDevMNGRight=atol(buffer);

				buffer=doc.RootElement()->Attribute("CONMNGRight");
				nCONMNGRight=atol(buffer);

				buffer=doc.RootElement()->Attribute("SURVEILRight");
				nSURVEILRight=atol(buffer);

				buffer=doc.RootElement()->Attribute("USRMNGRight");
				nUSRMNGRight=atol(buffer);

				buffer=doc.RootElement()->Attribute("VideoReplayRight");
				nVideoReplayRight=atol(buffer);

				buffer=doc.RootElement()->Attribute("AudioControlRight");
				nAudioControlRight=atol(buffer);

				buffer=doc.RootElement()->Attribute("BCControlRight");
				nBCControlRight=atol(buffer);

				buffer=doc.RootElement()->Attribute("TVWallRight");
				nTVWallRight=atol(buffer);

				buffer=doc.RootElement()->Attribute("AlarmMngRight");
				nAlarmMngRight=atol(buffer);

				buffer=doc.RootElement()->Attribute("ContentDeleteRight");
				nContentDeleteRight=atol(buffer);

				this->EndModalLoop(0);
			}
		}else
		{
			MessageBox(strXml,_T("登陆失败"));
		}
	}
}





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

	///设备默认值
	SetDlgItemText(IDC_PORT,_T("20001"));
	SetDlgItemText(IDC_USRE,_T("zfw"));
	SetDlgItemText(IDC_PASS,_T("0"));
	SetDlgItemText(IDC_IPADDR,_T("192.168.88.132"));

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