// TestPubImportDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestPubImport.h"
#include "TestPubImportDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTestPubImportDlg dialog

CTestPubImportDlg::CTestPubImportDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestPubImportDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestPubImportDlg)
	m_Date = _T("20010101");
	m_Num_Message = 0;
	m_RepDestMessage = _T("");
	m_FichierDestMessage = _T("");
	m_Pointage_Id_Spot = 0;
	m_Pointage_NomZone = _T("");
	m_Pointage_Valide = 0;
	m_HeureDebut = _T("");
	m_HeureFin = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestPubImportDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestPubImportDlg)
	DDX_Control(pDX, IDC_PUBIMPORTCTRL1, m_PubImport);
	DDX_Control(pDX, IDC_EXPLORER1, m_Browser);
	DDX_Text(pDX, IDC_Date, m_Date);
	DDV_MaxChars(pDX, m_Date, 8);
	DDX_Text(pDX, IDC_NUM_MESSAGE, m_Num_Message);
	DDX_Text(pDX, IDC_REP_DEST_MESSAGE, m_RepDestMessage);
	DDX_Text(pDX, IDC_FICHIER_DEST_MESSAGE, m_FichierDestMessage);
	DDX_Text(pDX, IDC_POINTAGE_ID_SPOT, m_Pointage_Id_Spot);
	DDX_Text(pDX, IDC_POINTAGE_NOMZONE, m_Pointage_NomZone);
	DDX_Text(pDX, IDC_POINTAGE_VALIDE, m_Pointage_Valide);
	DDX_Text(pDX, IDC_Heure, m_HeureDebut);
	DDX_Text(pDX, IDC_Heure2, m_HeureFin);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestPubImportDlg, CDialog)
	//{{AFX_MSG_MAP(CTestPubImportDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CAMPAGNES_TOUTES, OnCampagnesToutes)
	ON_BN_CLICKED(IDC_MESSAGE_TOUS, OnMessageTous)
	ON_BN_CLICKED(IDC_CAMPAGNES_MAJ, OnCampagnesMaj)
	ON_BN_CLICKED(IDC_MESSAGE_MAJ, OnMessageMaj)
	ON_BN_CLICKED(IDC_CREER_CONDUCTEUR, OnCreerConducteur)
	ON_BN_CLICKED(IDC_Message, OnImporterMessage)
	ON_BN_CLICKED(IDC_POINTAGE_SPOT, OnPointageSpot)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestPubImportDlg message handlers

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_PubImport.InitRepertoire(".\\Datas");
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTestPubImportDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestPubImportDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



void CTestPubImportDlg::OnCampagnesToutes() 
{
	// TODO: Add your control notification handler code here
	m_PubImport.ListeCampagnes(".\\lst_cpgne.xml");
	
	char repertoire[1000];
	GetCurrentDirectory(1000,repertoire);
	m_Browser.Navigate(CString(repertoire)+"\\lst_cpgne.xml",NULL,NULL,NULL,NULL);
}

void CTestPubImportDlg::OnMessageTous() 
{
	// TODO: Add your control notification handler code here
	m_PubImport.ListeMessages(".\\lst_message.xml");
	
	char repertoire[1000];
	GetCurrentDirectory(1000,repertoire);
	m_Browser.Navigate(CString(repertoire)+"\\lst_message.xml",NULL,NULL,NULL,NULL);
}

void CTestPubImportDlg::OnCampagnesMaj() 
{
	// TODO: Add your control notification handler code here
	m_PubImport.ListeCampagnesModifiees(".\\lst_cpgne_maj.xml");
	
	char repertoire[1000];
	GetCurrentDirectory(1000,repertoire);
	m_Browser.Navigate(CString(repertoire)+"\\lst_cpgne_maj.xml",NULL,NULL,NULL,NULL);
}

void CTestPubImportDlg::OnMessageMaj() 
{
	// TODO: Add your control notification handler code here
		m_PubImport.ListeMessagesModifies(".\\lst_message_maj.xml");
	
	char repertoire[1000];
	GetCurrentDirectory(1000,repertoire);
	m_Browser.Navigate(CString(repertoire)+"\\lst_message_maj.xml",NULL,NULL,NULL,NULL);
}


long CTestPubImportDlg::SupprimerFichierDansRepertoire(CString Repertoire)
{
	CFileFind finder;
	
	BOOL bWorkingAnciennes = finder.FindFile(Repertoire+"\\*.*");
	while (bWorkingAnciennes)
	{
		bWorkingAnciennes = finder.FindNextFile();
		TRY
		{
			CFile::Remove(finder.GetFilePath());
		}
		CATCH (CFileException, pEx)
		{
		#ifdef _DEBUG
			afxDump << "File " << finder.GetFilePath() << " cannot be removed\n";
		#endif
		}
		END_CATCH
	}	
	return 0;
}

void CTestPubImportDlg::OnCreerConducteur() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	m_PubImport.Conducteur(".\\conduct.txt",m_Date,m_HeureDebut,m_HeureFin);
	
	char repertoire[1000];
	GetCurrentDirectory(1000,repertoire);
	m_Browser.Navigate(CString(repertoire)+"\\conduct.txt",NULL,NULL,NULL,NULL);
}

void CTestPubImportDlg::OnImporterMessage() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	if (m_PubImport.RecupererMessage(m_Num_Message,m_RepDestMessage,m_FichierDestMessage) == 0)
		AfxMessageBox("Son inexistant");
	else
		AfxMessageBox("Copie Terminée");
	
	char repertoire[1000];
	GetCurrentDirectory(1000,repertoire);

	CString FichierDest;
	if (CString(m_RepDestMessage).Right(1)=="\\")
		FichierDest=CString(m_RepDestMessage)+CString(m_FichierDestMessage);
	else
		FichierDest=CString(m_RepDestMessage)+"\\"+CString(m_FichierDestMessage);

//	m_Browser.Navigate(FichierDest,NULL,NULL,NULL,NULL);
}

void CTestPubImportDlg::OnPointageSpot() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	m_PubImport.PointageSpot(m_Pointage_Id_Spot,m_Pointage_Valide,m_Pointage_NomZone);
	
	
}

void CTestPubImportDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	
}
