//---------------------- CSPDBControls.h ----------------------

//*************************************************************
//*****       Copyright ©2002 - 2004 Micronovae Ltd       *****
//*****                All rights reserved                *****
//*************************************************************


#ifndef _CSPDBCONTROLS
#define _CSPDBCONTROLS

#include "CSPConf.h"
#include "CSPLib.h"
#include "CSPString.h"
#include "CSPWString.h"
#include "CSPODBCRecordset.h"
#include "CSPHttpObjects.h"
#include "CSPTemplate.h"

#include <vector>


//------------- class CSPDBControl -----------------

class CSPDBFORM_CLASS_EXPORT CSPDBControl : public CSPObject
{
public:
	
	enum { DEFAULT = 0xFFFFFFFFUL };
	enum { TYPE_UNKNOWN = 0, TYPE_EDIT = 1, TYPE_DATE, TYPE_SELECT, TYPE_CHECKBOX, TYPE_MEMO };

	CSPDBControl();
	CSPDBControl( unsigned long ulCtrlType );
	virtual ~CSPDBControl();

	operator const CSPString& () const;
	operator const char*() const;

	const CSPString& GetOutput() const;
	unsigned long GetType() const;

	virtual void Render() = 0;
	void SetRecordset( CSPODBCRecordset& Set );
	virtual CSPVariant GetUpdateValue( const CSPRequest& Request ) const = 0;
	
	CSPString FieldName;
	CSPString ControlName;
	CSPString Attributes;

protected:
	CSPString m_strOutput;
	const unsigned long m_ulType;
	CSPODBCRecordset* m_pSet;
};



//------------- class CSPDBEditCtrl -----------------

class CSPDBFORM_CLASS_EXPORT CSPDBEditCtrl : public CSPDBControl
{
public:
	unsigned long Width;
	bool Password;

	CSPDBEditCtrl();
	virtual ~CSPDBEditCtrl();

	void Render();
	CSPVariant GetUpdateValue( const CSPRequest& Request ) const;

protected:

};


//------------- class CSPDBDateCtrl -----------------

class CSPDBFORM_CLASS_EXPORT CSPDBDateCtrl : public CSPDBControl
{
public:

	CSPDBDateCtrl();
	virtual ~CSPDBDateCtrl();

	void Render();
	CSPVariant GetUpdateValue( const CSPRequest& Request ) const;

protected:

};


//------------- class CSPDBSelectCtrl -----------------

class CSPDBFORM_CLASS_EXPORT CSPDBSelectCtrl : public CSPDBControl
{
public:

	CSPString KeyFieldName;
	CSPString ValueFieldName;


	CSPDBSelectCtrl();
	virtual ~CSPDBSelectCtrl();

	void Render();
	CSPVariant GetUpdateValue( const CSPRequest& Request ) const;
	void SetLookupRecordset( CSPODBCRecordset& LookupSet );

protected:
	CSPODBCRecordset* m_pLookupSet;
};


//------------- class CSPDBCheckboxCtrl -----------------

class CSPDBFORM_CLASS_EXPORT CSPDBCheckboxCtrl : public CSPDBControl
{
public:

	CSPDBCheckboxCtrl();
	virtual ~CSPDBCheckboxCtrl();

	void Render();
	CSPVariant GetUpdateValue( const CSPRequest& Request ) const;

protected:

};

//------------- class CSPDBMemoCtrl -----------------

class CSPDBFORM_CLASS_EXPORT CSPDBMemoCtrl : public CSPDBControl
{
public:

	unsigned long Columns;
	unsigned long Rows;


	CSPDBMemoCtrl();
	virtual ~CSPDBMemoCtrl();

	void Render();
	CSPVariant GetUpdateValue( const CSPRequest& Request ) const;

protected:

};


//-------------- class CSPDBControlForm ------------------

class CSPDBFORM_CLASS_EXPORT CSPDBControlForm : public CSPObject
{
public:

	CSPDBControlForm( CSPHttpObjects& HttpObjects );
	virtual ~CSPDBControlForm();

	void SetRecordset( CSPODBCRecordset& Set );

	CSPDBEditCtrl& AddEditControl( const CSPString& strCtrlName, const CSPString& strFieldName, const CSPString& strAttributes );
	CSPDBDateCtrl& AddDateControl( const CSPString& strCtrlName, const CSPString& strFieldName = CSPString(), const CSPString& strAttributes = CSPString() );
	CSPDBCheckboxCtrl& AddCheckboxControl( const CSPString& strCtrlName, const CSPString& strFieldName = CSPString(), const CSPString& strAttributes = CSPString() );
	CSPDBSelectCtrl& AddSelectControl( const CSPString& strCtrlName, const CSPString& strFieldName, CSPODBCRecordset& LookupSet, const CSPString& strKeyFieldName, const CSPString& strValueFieldName, const CSPString& strAttributes = CSPString() );
	CSPDBMemoCtrl& AddMemoControl( const CSPString& strCtrlName, const CSPString& strFieldName, const CSPString& strAttributes );
	bool Render( bool bClear = true );
	const CSPString& GetOutput() const;
	operator const CSPString& () const;
	bool Update();
	bool Insert();

	void LoadForm( const CSPTemplate& Templ );
	void LoadForm( const CSPString& strFile );
	CSPTemplate& GetTemplate();

protected:
	std::vector<CSPDBControl*> vecControls;
	CSPODBCRecordset* m_pSet;
	const CSPRequest& Request;
	const CSPPage& Page;
	CSPTemplate Template;
};
#endif
