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

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

#include "ITSSEL5MF.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"

TITSSEL5M *ITSSEL5M = NULL;
//---------------------------------------------------------------------------
__fastcall TITSSEL5M::TITSSEL5M(TComponent* Owner, TVdsCtlrManager* AVdsCtlrManager)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    m_bSelected = false;
    m_pGDC = TvList->DataController;
    MyVdsCtlrManager = AVdsCtlrManager;
}
//---------------------------------------------------------------------------

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

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

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

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

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

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

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

    sQry = "SELECT A.LINK_ID, C.NODE_NAME AS STR_NAME, D.NODE_NAME AS END_NAME,         \r\n"
           "       F.VDS_CTLR_NMBR, F.VDS_NM                                            \r\n"
           "  FROM TB_VDS_DTCT_RLTN_LINK A,                                             \r\n"
           "       TB_LINK               B,                                             \r\n"
           "       TB_NODE               C,                                             \r\n"
           "       TB_NODE               D,                                             \r\n"
           "       TB_VDS_DTCT           E,                                             \r\n"
           "       TB_VDS_CTLR           F                                              \r\n"
           " WHERE A.LINK_ID   = B.LINK_ID                                              \r\n"
           "   AND B.F_NODE_ID = C.NODE_ID                                              \r\n"
           "   AND B.T_NODE_ID = D.NODE_ID                                              \r\n"
           "   AND A.VDS_DTCT_NMBR = E.VDS_DTCT_NMBR                                    \r\n"
           "   AND E.VDS_CTLR_NMBR = F.VDS_CTLR_NMBR                                    \r\n"
           "   AND F.VDS_TYPE_CD   = 'I'                                                \r\n"
           " GROUP BY A.LINK_ID, C.NODE_NAME, D.NODE_NAME, F.VDS_CTLR_NMBR, F.VDS_NM    \r\n"
           " ORDER BY F.VDS_CTLR_NMBR, A.LINK_ID                                        \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())
            {
                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][ColumnSel->Index] = false;
                m_pGDC->Values[nRow][Column01->Index]  = pADO->FieldByName("VDS_CTLR_NMBR")->AsString;
                m_pGDC->Values[nRow][Column02->Index]  = pADO->FieldByName("VDS_NM")->AsString;
                m_pGDC->Values[nRow][Column03->Index]  = pADO->FieldByName("LINK_ID")->AsString;
                m_pGDC->Values[nRow][Column04->Index]  = pADO->FieldByName("STR_NAME")->AsString;
                m_pGDC->Values[nRow][Column05->Index]  = pADO->FieldByName("END_NAME")->AsString;
            }
#else
            FOR_STL(TVdsLink*, pObj, MyVdsCtlrManager->FLinkLists)
            {
                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][ColumnSel->Index] = pObj->IsSelected;
                m_pGDC->Values[nRow][Column01->Index]  = pObj->VDS_CTLR_NMBR;
                m_pGDC->Values[nRow][Column02->Index]  = pObj->VDS_NM;
                m_pGDC->Values[nRow][Column03->Index]  = pObj->LINK_ID;
                m_pGDC->Values[nRow][Column04->Index]  = pObj->STR_NAME;
                m_pGDC->Values[nRow][Column05->Index]  = pObj->END_NAME;
            }
#endif
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TITSSEL5M::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("TITSSEL5M::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 TITSSEL5M::BtnCloseClick(TObject *Sender)
{
    FIdList = "";
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TITSSEL5M::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;
    }

    MyVdsCtlrManager->FLinkSelCnt = 0;
    FOR_STL(TVdsLink*, pObj, MyVdsCtlrManager->FLinkLists)
    {
        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 LINK_ID = ARow->Values[Column03->Index];
                TVdsLink *pObj = MyVdsCtlrManager->FLinkLists.Find(LINK_ID);
                if (pObj)
                {
                    pObj->IsSelected = true;
                    MyVdsCtlrManager->FLinkSelCnt++;
                }
            }
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
    m_bSelected = true;

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

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

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

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

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

