// DLG_Corp_User_Add.cpp : implementation file
//

#include "stdafx.h"
#include "smsmanager.h"
#include "DLG_Corp_User_Add.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// DLG_Corp_User_Add dialog
#include "DLG_Corp_User.h"


DLG_Corp_User_Add::DLG_Corp_User_Add(CWnd* pParent /*=NULL*/)
	: CDialog(DLG_Corp_User_Add::IDD, pParent)
{
	//{{AFX_DATA_INIT(DLG_Corp_User_Add)
	m_strLoginName = _T("");
	m_strMsg = _T("");
	m_strName = _T("");
	m_strPasswd = _T("");
	m_bSDK = FALSE;
	//}}AFX_DATA_INIT
	m_pUserDlg = (DLG_Corp_User *)pParent;
	memset( &m_User,0,sizeof(m_User));
	m_Pos = 0;
	m_bAdd = true;
	m_lCorpID = 0;
}


void DLG_Corp_User_Add::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DLG_Corp_User_Add)
	DDX_Text(pDX, IDC_CORP_USERADD_LOGINNAME, m_strLoginName);
	DDV_MaxChars(pDX, m_strLoginName, 16);
	DDX_Text(pDX, IDC_CORP_USERADD_MSG, m_strMsg);
	DDX_Text(pDX, IDC_CORP_USERADD_NAME, m_strName);
	DDV_MaxChars(pDX, m_strName, 16);
	DDX_Text(pDX, IDC_CORP_USERADD_PASSWD, m_strPasswd);
	DDX_Check(pDX, IDC_CORP_USERADD_SDK, m_bSDK);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(DLG_Corp_User_Add, CDialog)
	//{{AFX_MSG_MAP(DLG_Corp_User_Add)
	ON_BN_CLICKED(IDC_CORP_USERADD_RESETPASSWD, OnCorpUseraddResetpasswd)
	ON_EN_CHANGE(IDC_CORP_USERADD_LOGINNAME, OnChangeCorpUseraddLoginname)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DLG_Corp_User_Add message handlers

BOOL DLG_Corp_User_Add::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_strLoginName = m_User.szLoginName;
	m_strName      = m_User.szName;

	OnCorpUseraddResetpasswd();   //��������
	if ( !m_bAdd )  //��������
	{
		GetDlgItem(IDC_CORP_USERADD_LOGINNAME)->EnableWindow(false);
		GetDlgItem(IDC_CORP_USERADD_NAME)->EnableWindow(false);
	}
	OnChangeCorpUseraddLoginname();  //��ʾ��Ϣ
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void DLG_Corp_User_Add::SetParam(ANS_Corp_UserData2 User, POSITION pos, long lCorpID , BOOL bAdd)
{
	m_User = User;
	m_Pos = pos;
	m_lCorpID = lCorpID;
	m_bAdd  = bAdd;
}

ANS_Corp_UserData2 DLG_Corp_User_Add::GetParam()
{
	return m_User;
}

void DLG_Corp_User_Add::OnCorpUseraddResetpasswd() 
{
	/*
	srand(::GetTickCount());
	CString strTemp;
	strTemp.Format( _T("%03d%03d") , rand(),rand());
	strTemp.SetAt( 6 , 0 );
	m_strPasswd = strTemp;
	*/
	m_strPasswd = ::LuGetRandPassword();
	UpdateData( false );
	OnChangeCorpUseraddLoginname();   //��������
}

void DLG_Corp_User_Add::OnChangeCorpUseraddLoginname() 
{
	UpdateData(true);
	m_strMsg.Format( _T("��ҵID:%d\r\n��¼�û�:%s\r\n����:%s\r\n"),m_lCorpID,m_strLoginName,m_strPasswd );
	UpdateData(false);
}

void DLG_Corp_User_Add::OnOK() 
{
	UpdateData(true);
	if ( m_strLoginName.GetLength() <= 0 )
	{
		MessageBox( _T("��¼������Ϊ�գ�������!") , _T("����") , MB_ICONWARNING );
		GetDlgItem(IDC_CORP_USERADD_LOGINNAME)->SetFocus();
		return ;
	}
	if ( m_pUserDlg->FindLoginName( m_strLoginName,m_Pos ) )
	{
		MessageBox( _T("�˵�¼�������룬������������¼��!") , _T("����") , MB_ICONWARNING );
		GetDlgItem( IDC_CORP_USERADD_LOGINNAME )->SetFocus();
		return ;
	}
	memset( &m_User , 0 , sizeof(m_User) );
	_tcscpy( m_User.szLoginName , m_strLoginName );
	_tcscpy( m_User.szName , m_strName );
	m_User.bSDK = m_bSDK;
	//����

		char szUser[32]={0};
		char szPasswd[64]={0};
		char szPasswd2[64]={0};
#ifdef _UNICODE
		strcpy(szUser , CW2A(m_User.szLoginName) );
		strcpy(szPasswd , CW2A(m_strPasswd));
#else
		strcpy(szUser , m_User.szLoginName );
		strcpy(szPasswd , m_strPasswd);
#endif
		CDes des;
		long lOut=sizeof(szPasswd2);
		des.Encrypt( szPasswd , strlen(szPasswd) , szPasswd2 , lOut , szUser , 8 );
#ifdef _UNICODE
		_tcscpy(m_User.szPasswd , CA2W(szPasswd2));
#else
		strcpy(m_User.szPasswd , szPasswd2);
#endif
	CDialog::OnOK();
}