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

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

#include "FRAME_NodeListF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"
//TFRAMENodeList *FRAMENodeList;
//---------------------------------------------------------------------------
__fastcall TFRAMENodeList::TFRAMENodeList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

    ColumnSel->Options->Filtering = false;
    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<노드 정보가 없습니다>";

    FCodeTurnP = ItsCodeManager->FLists.Find("TLE"); //회전제한
    FCodeNodeT = ItsCodeManager->FLists.Find("NDT"); //노드유형
}
//---------------------------------------------------------------------------

void __fastcall TFRAMENodeList::UpdateList()
{
    TItsSubCode *pSubCode;

    CMM_ClearGridTableView(TvList);

    int nRow = 0;
    try
    {
        TvList->BeginUpdate();

        try
        {
            ItsNodeManager->FLists.Lock();
            FOR_STL(TItsNode *, pNode, ItsNodeManager->FLists)
            {
                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][Column00->Index] = pNode->NODE_ID;   //노드ID
                m_pGDC->Values[nRow][Column01->Index] = pNode->NODE_ID;   //노드ID

                String sNodeType = "[" + pNode->NODE_TYPE + "] ";
                if (FCodeNodeT)
                {
                    pSubCode = FCodeNodeT->FSubLists.Find(pNode->NODE_TYPE);
                    if (pSubCode) sNodeType = sNodeType + pSubCode->CMMN_CD_KOR_NM;
                }
                m_pGDC->Values[nRow][Column02->Index] = sNodeType;        //노드유형

                m_pGDC->Values[nRow][Column03->Index] = pNode->NODE_NAME; //교차로명칭

                String sTurnP = "[" + pNode->TURN_P + "] ";
                if (FCodeTurnP)
                {
                    pSubCode = FCodeTurnP->FSubLists.Find(pNode->TURN_P);
                    if (pSubCode) sTurnP = sTurnP + pSubCode->CMMN_CD_KOR_NM;
                }
                m_pGDC->Values[nRow][Column04->Index] = sTurnP;             //회전제한유무

                m_pGDC->Values[nRow][Column05->Index] = pNode->RMRK;      //비고
                m_pGDC->Values[nRow][Column06->Index] = String(pNode->X_CRDN);    //X 좌표
                m_pGDC->Values[nRow][Column07->Index] = String(pNode->Y_CRDN);    //Y 좌표
                m_pGDC->Values[nRow][Column18->Index] = 0;
            }
        }
        __finally
        {
            ItsNodeManager->FLists.UnLock();
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

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

int  __fastcall TFRAMENodeList::GetSelNodeIds(String &ANodeIds)
{
    ANodeIds = "";

    if (TvList->ViewData->RecordCount <= 0) return 0;
	int nIndex = m_pGDC->FocusedRecordIndex;
	if( nIndex < 0 )
		return 0;

    String sNodeId = VarToStr(m_pGDC->Values[nIndex][Column01->Index]);
    ANodeIds = sNodeId;
    return 1;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMENodeList::TvListDataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList);
    LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
}
//---------------------------------------------------------------------------

void __fastcall TFRAMENodeList::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= lblFileName->Caption;//"노드정보";

    CMM_ExportToExcelFile(sTitle, pGrid, pView, (TForm*)this);
}
//---------------------------------------------------------------------------


