// CFindAdjustClassifDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CFindAdjustClassifDlg.h"


// CFindAdjustClassifDlg dialog

IMPLEMENT_DYNAMIC(CFindAdjustClassifDlg, CaplTranslateDialog)

CFindAdjustClassifDlg::CFindAdjustClassifDlg(CWnd* pParent /*=NULL*/)
	: CaplTranslateDialog(CFindAdjustClassifDlg::IDD, pParent)
	, m_Name(_T(""))
{
	m_api = 0;
	m_classif = 0;
	m_el=0;
	m_read_only=false;
}

CFindAdjustClassifDlg::~CFindAdjustClassifDlg()
{
}

void CFindAdjustClassifDlg::DoDataExchange(CDataExchange* pDX)
{
	CaplTranslateDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EXIST_DOC, m_CtrlExist);
	DDX_Control(pDX, IDC_BTN_SELECT_CLASSIF, m_BtnSelect);
	DDX_Text(pDX, IDC_NAME, m_Name);
	DDX_Control(pDX, IDOK, m_BtnOk);
	DDX_Control(pDX, IDCANCEL, m_BtnCancel);
}


BEGIN_MESSAGE_MAP(CFindAdjustClassifDlg, CaplTranslateDialog)
	ON_BN_CLICKED(IDC_BTN_SELECT_CLASSIF, &CFindAdjustClassifDlg::OnBnClickedBtnSelectClassif)
	ON_BN_CLICKED(IDOK, &CFindAdjustClassifDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CFindAdjustClassifDlg message handlers

void CFindAdjustClassifDlg::OnBnClickedBtnSelectClassif()
{
	// TODO: Add your control notification handler code here
	if(m_api!=0){
		

		// ЯАИ: Этот выбор может вызываться при поиске из SelectInstance, а SelectInstance не переживает рекурсивный вызов
		/*m_classif = m_api->m_classifier_mgr.SelectInstanceEx(APL_MODE_SELECT_CLASSIF_ELEMENT | APL_MODE_SELECT_CLASSIF_SYSTEM,APL_T("Выбор справочника"));
		if(m_classif!=0){
			m_Name = m_api->GetItemName(m_classif);
		}
		else
		{
			m_Name = _T("");
		}*/

		CaplInstance *item=0;
		m_api->m_classifier_mgr.SelectItem(&item,0,APL_SELECT_CLASSIFIER_SYSTEM|APL_SELECT_CLASSIFIER_LEVEL);
		if(0!=item)
		{
			m_classif=item;
			m_Name = m_api->GetItemName(m_classif);
		}
		else m_Name = _T("");

		UpdateData(FALSE);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CFindAdjustDocDlg message handlers
BOOL CFindAdjustClassifDlg::OnInitDialog() 
{
	CaplTranslateDialog::OnInitDialog();
	aplExtent ext0;
	//int i;
	CString buf;

	m_CtrlExist.ResetContent();
	m_CtrlExist.InsertString(0,APL_T("Входит в справочник"));
	m_CtrlExist.InsertString(1,APL_T("Не входит в справочник"));

	m_BtnOk.SetIcon(GetAplStdIco16(APLSTDICO_OK));
	m_BtnOk.SetFlat(FALSE);
	m_BtnCancel.SetIcon(GetAplStdIco16(APLSTDICO_CANCEL));
	m_BtnCancel.SetFlat(FALSE);
	
	m_CtrlExist.SetCurSel(0);
	// TODO: Add extra initialization here
	if(m_api!=0){
		if(m_el!=0){
			if(m_el->is_exist){
				m_CtrlExist.SetCurSel(0);
			}else{
				m_CtrlExist.SetCurSel(1);
			}
			m_classif = m_el->classif;
			if(m_classif!=0){
				m_Name = m_api->GetItemName(m_classif);
			}
		}
	}
	UpdateData(FALSE);

	if(m_read_only){
		m_CtrlExist.EnableWindow(FALSE);
		m_BtnSelect.EnableWindow(FALSE);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void CFindAdjustClassifDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	if(!m_read_only && m_api!=0){
		if(m_classif==0){
			AfxMessageBox( APL_T("Выберите справочник или элемент справочника!"));
			return;
		}
		if(m_el==0){
			m_el=new ElValClass();
		}
		m_el->is_exist=m_CtrlExist.GetCurSel()==0;
		m_el->classif=m_classif;
	}
 
	OnOK();
}
