//---------------------------------------------------------------------------

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "AppGlobalF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "ITSSEL4MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLabel"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"

TITSSEL4M *ITSSEL4M = NULL;
//---------------------------------------------------------------------------
__fastcall TITSSEL4M::TITSSEL4M(TComponent* Owner, TItsAtrdManager* AAtrdManager)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    m_bSelected = false;
    m_pGDC = TvList->DataController;
    MyItsAtrdManager = AAtrdManager;
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL4M::CommClose()
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL4M::FormShow(TObject *Sender)
{
    Refresh();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL4M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    BtnSearchClick((TObject*)BtnSearch);   
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL4M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

    RefreshData();
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL4M::RefreshData()
{
    // 검색조건에 의한 링크 데이터 조회
    try
    {
        SelListData();
	}
	__finally
	{
		//TvList->ApplyBestFit(NULL, false, false);
	}
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL4M::SelListData()
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT DISTINCT TRIM(ATRD_NM) AS ATRD_NM  \r\n"
           "  FROM TB_ATRD                            \r\n"
           " ORDER BY ATRD_NM                         \r\n";

    CMM_ClearGridTableView(TvList);
    int nRow = 0;

    TvList->BeginUpdate();
    try
    {
        try
        {
#if 0
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String ATRD_ID = pADO->FieldByName("ATRD_NM")->AsString;
                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][ColumnSel->Index] = false;
                m_pGDC->Values[nRow][Column02->Index]  = pADO->FieldByName("ATRD_NM")->AsString;
            }
#else
            FOR_STL(TCDSAtrdName*, pObj, MyItsAtrdManager->FCDSNameLists)
            {
                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][ColumnSel->Index] = pObj->IsSelected;
                m_pGDC->Values[nRow][Column02->Index]  = pObj->ATRD_NM;
            }
#endif
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TITSSEL4M::SelListData", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TITSSEL4M::SelListData", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL4M::BtnCloseClick(TObject *Sender)
{
    FIdList = "";
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL4M::BtnSelectClick(TObject *Sender)
{
    FIdList = "";
    int nSelects = CMM_GetSelectCount(TvList, ColumnSel->Index, Column02->Index, FIdList);
    if (0 == nSelects)
    {
        Application->MessageBox(FrmLang->lblSelErr->Caption.c_str(),//L"목록에서 도로를 먼저 선택 하세요.",
                                cxGroupBox1->Caption.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        return;
    }

    MyItsAtrdManager->FCDSNameSelCnt = 0;
    FOR_STL(TCDSAtrdName*, pObj, MyItsAtrdManager->FCDSNameLists)
    {
        pObj->IsSelected = false;
    }

    TvList->BeginUpdate();
    try
    {
        int nRowCnt = TvList->ViewData->RecordCount;
        for (int ii = 0; ii < nRowCnt; ii++)
        {
            TcxCustomGridRow* ARow = TvList->ViewData->Rows[ii];
            if (!ARow->IsData) continue;
            if (((bool)ARow->Values[ColumnSel->Index]))
            {
                String ATRD_NM = ARow->Values[Column02->Index];
                TCDSAtrdName *pObj = MyItsAtrdManager->FCDSNameLists.Find(ATRD_NM);
                if (pObj)
                {
                    pObj->IsSelected = true;
                    MyItsAtrdManager->FCDSNameSelCnt++;
                }
            }
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
    m_bSelected = true;

    Close();
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL4M::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    ITSSEL4M = NULL;
    //Action = caFree;
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL4M::SetMultiSelect(bool AMultiSelect)
{
    TvList->OptionsSelection->MultiSelect = AMultiSelect;
}
//---------------------------------------------------------------------------

bool __fastcall TITSSEL4M::GetMultiSelect()
{
    return TvList->OptionsSelection->MultiSelect;
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL4M::BtnAllSelectClick(TObject *Sender)
{
    TcxButton *pBtn = (TcxButton*)Sender;
    CMM_CheckAllListItem(TvList, ColumnSel->Index, pBtn->Tag);
}
//---------------------------------------------------------------------------

