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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "AppGlobalF.h"
#include "CDSCodeF.h"
#include "CDSFacilityF.h"
#include "CDSNodeF.h"
#include "CDSRoadF.h"
#include "ITSLangTransF.h"
#include "ITSMapF.h"
#include "ITS_OPLibF.h"
#pragma hdrstop

#include "IDB0120MF.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 "cxDropDownEdit"
#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 "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxSplitter"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "FRAME_AtrdListF"
#pragma link "FRAME_OpenMapF"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

TIDB0120M *IDB0120M = NULL;
//---------------------------------------------------------------------------
void TIDB0120M_MapCallback(TObject *ASender, const String AFunc, const String AArgs, const bool AFirst)
{
    try
    {
        if (!IDB0120M) return;
        IDB0120M->OpenMapCallback(ASender, AFunc, AArgs, AFirst);
    }
    catch(Exception &exception)
    {
    }
}
//---------------------------------------------------------------------------

__fastcall TIDB0120M::TIDB0120M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FUpdate= false;
    FTitle = Caption;//"간선도로 관리";

    m_pGDC = FRAMEAtrdList1->TvList->DataController;

    FRAMEAtrdList1->PnlTop->Visible = false;
    //FRAMEAtrdList1->PnlBottom->Visible = false;
    FRAMEAtrdList1->ColumnSel->Visible = false;

    TvLink->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<간선도로 링크구성정보>";

    FLayerIdx = LyrTp_Node;
    FRAMEOpenMap1->LayerIdx = FLayerIdx;
    FRAMEOpenMap1->ChkShowNmbr->Visible = false;
    FRAMEOpenMap1->BtnMoveMapToFit->Visible = false;
    FRAMEOpenMap1->MapCallbackFunc = TIDB0120M_MapCallback;

    EditMode = false;
}
//---------------------------------------------------------------------------

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

void __fastcall TIDB0120M::FormShow(TObject *Sender)
{
    Refresh();

    FRAMEOpenMap1->OpenMap();

    CbDRCT_CD->ItemIndex = 0;
    //CbDRCT_CD->Enabled = false;
    CbDRCT_CD->Properties->ReadOnly = true;
    EdId->Properties->ReadOnly      = true;
    EdATRD_ID->Properties->ReadOnly = true;
    EdATRD_NM->Properties->ReadOnly = true;
    EdDRCT_NM->Properties->ReadOnly = true;
    EdAREA_CD->Properties->ReadOnly = true;

    FormInit();
    ChangeEditMode(1);
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::FormInit()
{
    if (FRAMEOpenMap1->MapStart)
    {
        int nRow;
        TcxDataController *pGDC = TvLink->DataController;
        try
        {
            int nSeq = 1;
            for (int ii = 0; ii < pGDC->RecordCount; ii++)
            {
                String sLinkId = pGDC->Values[ii][ColLink2->Index];

                sJs.printf(L"updateEditLink('%s', 0)", sLinkId.c_str());
                FRAMEOpenMap1->ExecScript(sJs);
            }
        }
        catch(...)
        {
        }
    }

    EdId->Text           = "";
    EdATRD_ID->Text      = "";
    EdATRD_NM->Text      = "";
    EdDRCT_NM->Text      = "";
    //CbDRCT_CD->ItemIndex = 0;
    EdAREA_CD->Text      = "";

    CMM_ClearGridTableView(TvLink);

    FNewDb = false;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;

    UpdateList();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::UpdateEditLink()
{
    if (!FRAMEOpenMap1->MapStart) return;

    try
    {
        ItsRoadManager->FLists.Lock();
        try
        {
            FOR_STL(TItsRoad*, pRoad, ItsRoadManager->FLists)
            {
                if (pRoad->DEL_YN == "Y")   continue;

                if (pRoad->IsVertex)
                {
                    sJs.printf(L"addEditLink('%s', '%s', '%s', '%s', '%s', '%s')",
                        pRoad->ROAD_ID.c_str(),
                        pRoad->ROAD_NM.c_str(),
                        pRoad->STRT_NM.c_str(),
                        pRoad->END_NM.c_str(),
                        pRoad->X_CRDN_ARR.c_str(),
                        pRoad->Y_CRDN_ARR.c_str());

                    FRAMEOpenMap1->ExecScript(sJs);
                }
            }
        }
        __finally
        {
            ItsRoadManager->FLists.UnLock();
        }
        FRAMEOpenMap1->ExecScript("makeEditLinkLayer()");
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::UpdateList()
{
    FRAMEAtrdList1->UpdateList();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

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

void __fastcall TIDB0120M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    if (FUpdate)
    {
        POST_MSG(Application->MainForm->Handle, WM_DATABASE_REFRESH, WP_MSG_14, 0);
    }

    FRAMEAtrdList1->OnCloseQuery(CanClose);
    FRAMEOpenMap1->OnCloseQuery(CanClose);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::DisplayInfo(TItsAtrd* AObj)
{
    FormInit();

    if (!AObj) return;

    EdId->Text           = AObj->ATRD_ID;
    EdATRD_ID->Text      = AObj->ATRD_ID;
    EdATRD_NM->Text      = AObj->ATRD_NM;
    EdDRCT_NM->Text      = AObj->DRCT_NM;
    //CbDRCT_CD->Properties->ReadOnly = false;
    CbDRCT_CD->ItemIndex = (AObj->DRCT_CD.Trim() == "0") ? 0 : 1;
    //CbDRCT_CD->Properties->ReadOnly = true;
    EdAREA_CD->Text      = AObj->AREA_CD;

    ChangeEditMode(3);

    DisplayLink(AObj);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::DisplayLink(TItsAtrd* AObj)
{
    if (!AObj) return;

    CMM_ClearGridTableView(TvLink);

    String sLinkIds = "";
    try
    {
        int nRow;
        TcxDataController *pGDC = TvLink->DataController;
        TvLink->BeginUpdate();

        FOR_STL(TItsAtrdRoad *, pObj, AObj->FRoads)
        {
            String sLinkId = pObj->ROAD_ID;
            nRow = pGDC->AppendRecord();
            pGDC->Values[nRow][ColLink2->Index] = sLinkId;

            TItsRoad *pLink = ItsRoadManager->FLists.Find(sLinkId);
            if (pLink)
            {
                TItsNode *pNode;
                pGDC->Values[nRow][ColLink1->Index] = String(pObj->ORD);

                pGDC->Values[nRow][ColLink3->Index] = pLink->F_NODE_ID;
                pGDC->Values[nRow][ColLink5->Index] = pLink->T_NODE_ID;
                pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
                if (pNode)
                {
                    pGDC->Values[nRow][ColLink4->Index] = pNode->NODE_NAME;
                }
                pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
                if (pNode)
                {
                    pGDC->Values[nRow][ColLink6->Index] = pNode->NODE_NAME;
                }
            }

            if (sLinkId != "")
            {
                if (sLinkIds == "") sLinkIds = sLinkId;
                else                sLinkIds = sLinkIds+","+sLinkId;
            }
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }

    RefreshLink();

    if (sLinkIds != "")
    {
        sJs.printf(L"extentLayerObject(%d, '%s')", LyrTp_Traffic, sLinkIds.c_str());
        FRAMEOpenMap1->ExecScript(sJs);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::BtnEditClick(TObject *Sender)
{
    if (BtnEdit->Caption == FrmLang->lblEdit->Caption)//"편집")
    {
        ChangeEditMode(0);
        FNewDb = false;
    }
    else
    {
        String sId = EdId->Text;

        ChangeEditMode(1);

        CMM_SetGridRow(FRAMEAtrdList1->CxList, FRAMEAtrdList1->TvList, sId, FRAMEAtrdList1->Column01->Index);
        int nRow = FRAMEAtrdList1->TvList->DataController->FocusedRecordIndex;
        if( nRow <= -1 )
            return;

        int nMemPtr = FRAMEAtrdList1->TvList->DataController->Values[nRow][FRAMEAtrdList1->Column99->Index];
        TItsAtrd* pObj = (TItsAtrd*)nMemPtr;
        DisplayInfo(pObj);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::ChangeEditMode(int AEdit)
{
    bool bGisEdit = true;
    bool bEnabled = false;

    switch(AEdit)
    {
    case 0: //편집버튼클릭
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
            EditMode = true;
        }
        break;
    case 1: //편집[취소]버튼클릭
        {
            FormInit(); //화면을 모두 지운다.
            BtnEdit->Enabled = false;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = false;

            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
            bEnabled = true;
            bGisEdit = false;
            EditMode = false;
        }
        break;
    case 2: //추가
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
            EditMode = true;
        }
        break;
    case 3: //수정모드
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = true;
            BtnSave->Enabled = false;

            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
            bEnabled = true;
            EditMode = true;
        }
        break;
    }

    if (AEdit == 0 || AEdit == 2)
    {
        FRAMEAtrdList1->CxList->Enabled = false;
    }
    else
    {
        FRAMEAtrdList1->CxList->Enabled = true;
    }

    //CbAVI_CTLR_MNGM_NMBR->ItemIndex = -1;
    EdId->Properties->ReadOnly      = bEnabled;
    EdATRD_ID->Properties->ReadOnly = bEnabled;
    EdATRD_NM->Properties->ReadOnly = bEnabled;
    EdDRCT_NM->Properties->ReadOnly = bEnabled;
    //CbDRCT_CD->ItemIndex = 0;
    //CbDRCT_CD->Enabled = !bEnabled;
    CbDRCT_CD->Properties->ReadOnly = bEnabled;
    EdAREA_CD->Properties->ReadOnly = bEnabled;

    BtnLinkAdd->Enabled = !bEnabled;
    BtnLinkDel->Enabled = !bEnabled;

    BtnUp->Enabled = !bEnabled;
    BtnDown->Enabled = !bEnabled;

//    RefreshLink();
    PnlEdit->Enabled = EditMode;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::BtnInsertClick(TObject *Sender)
{
    FormInit();
    //신규아이디를 얻어와서 화면을 초기화한다.
    String sId;
	sId = InputBox(lblInput->Caption,//"간선도로 ID",
                   lblInQry->Caption,//"신규 간선도로 ID를 입력하세요.",
                   lblInput->Caption);//"간선도로 ID" );

    sId = sId.Trim();
	if (sId == lblInput->Caption//"간선도로 ID"
     || sId == "")
	{
		return;
	}
	if (AnsiString(sId).Length() > 10)
	{
        Application->MessageBox(lblDigitErr->Caption.c_str(),//L"간선도로 ID는  10Byte를 초과할 수 없습니다.",
                                FTitle.c_str(),
                                MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}

    FRAMEAtrdList1->MyItsAtrdManager->FLists.Lock();
    try
    {
        TItsAtrd *pDb = FRAMEAtrdList1->MyItsAtrdManager->FLists.Find(sId);
        if (pDb)
        {
            String sErr = FrmLang->lbIdErr->Caption + "[" + sId + "]";
            Application->MessageBox(sErr.c_str(),//("[" + sId + "]는 사용할 수 없는 간선도로 ID 입니다.").c_str(),
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }
    }
    __finally
    {
        FRAMEAtrdList1->MyItsAtrdManager->FLists.UnLock();
    }

    EdId->Text   = "";
    EdATRD_ID->Text = sId;

    ChangeEditMode(2);
    FNewDb = true;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::BtnDeleteClick(TObject *Sender)
{
    int nIdx = m_pGDC->FocusedRecordIndex;
    if (nIdx < 0) return;

    String sId = EdATRD_ID->Text;

    if (sId.IsEmpty())
    {
        Application->MessageBox(lblText1->Caption.c_str(),//L"삭제할 간선도로 정보를 목록에서 선택하세요.\r\n목록에서 더블클릭하여 선택합니다.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }
    String sMsg;
    sMsg = lblInput->Caption + ": " + sId;//"간선도로 ID: " + sId;
    sMsg+= "\r\n" + lblText2->Caption;//"선택하신 간선도로 정보를 삭제하시겠습니까?\r\n관련정보도 함께 삭제 됩니다.";

    if (Application->MessageBox(sMsg.c_str(), FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    TItsAtrd *pObj = NULL;
    try
    {
        FRAMEAtrdList1->MyItsAtrdManager->FLists.Lock();
        pObj = FRAMEAtrdList1->MyItsAtrdManager->FLists.Find(sId);
    }
    __finally
    {
        FRAMEAtrdList1->MyItsAtrdManager->FLists.UnLock();
    }

	if (pObj)
	{
		if (FRAMEAtrdList1->MyItsAtrdManager->DeleteAtrd(pObj))
		{
            pObj->DEL_YN = "Y";
            UpdateList();
		}

        Application->MessageBox(lblText3->Caption.c_str(),//L"간선도로 정보 삭제\r\n간선도로 정보를 삭제 하였습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

        FUpdate = true;

        FormInit();
        ChangeEditMode(1);
	}
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::BtnSaveClick(TObject *Sender)
{
    TItsAtrd *pObj;

    String sId = EdATRD_ID->Text;

    if (EdATRD_ID->Text.IsEmpty()        ||
        EdATRD_NM->Text.Trim().IsEmpty() ||
        CbDRCT_CD->ItemIndex < 0)
	{
        Application->MessageBox(FrmLang->lblCnfm->Caption.c_str(),//L"★항목은 필수 입력 항목입니다.\r\n모두 입력후 진행해 주세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}


    if (FNewDb)
    {
        pObj = new TItsAtrd();
    }
    else
        pObj = FRAMEAtrdList1->MyItsAtrdManager->FLists.Find(sId);

    if (pObj)
    {
        pObj->ATRD_ID = sId;
        pObj->ATRD_NM = EdATRD_NM->Text.Trim();
        pObj->DRCT_NM = EdDRCT_NM->Text.Trim();
        pObj->DRCT_CD = CbDRCT_CD->ItemIndex == 0 ? "0" : "1";
        pObj->AREA_CD = EdAREA_CD->Text.Trim();
        pObj->DEL_YN  = "N";

        pObj->Completed = true;
        pObj->MEM_DEL   = "N";

        pObj->FRoads.RemoveAll();

        TvLink->BeginUpdate();
        try {
            for (int ii = 0; ii < TvLink->DataController->RecordCount; ii++)
            {
                String sLinkId = TvLink->DataController->Values[ii][ColLink2->Index];

				TItsAtrdRoad *pObjLnk = new TItsAtrdRoad();

                pObjLnk->ATRD_ID = pObj->ATRD_ID;
                pObjLnk->ROAD_ID = sLinkId;
                pObjLnk->ORD     = ii+1;

				pObj->FRoads.Push(pObjLnk->ORD, pObjLnk);

                pObjLnk->Completed = true;
                pObjLnk->MEM_DEL   = "N";
            }
        }
        __finally
        {
            TvLink->EndUpdate();
        }

        bool bResult;
        if (FNewDb)
            bResult = FRAMEAtrdList1->MyItsAtrdManager->InsertAtrd(pObj);
        else
            bResult = FRAMEAtrdList1->MyItsAtrdManager->UpdateAtrd(pObj);

        if (bResult)
        {
            Application->MessageBox(FrmLang->lblDbSave->Caption.c_str(),//L"간선도로 정보저장\r\n간선도로 정보를 저장 하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

            if (FNewDb)
            {
                FRAMEAtrdList1->MyItsAtrdManager->FLists.Push(pObj->ATRD_ID, pObj);
            }

            FUpdate = true;

            UpdateList();
            CMM_SetGridRow(FRAMEAtrdList1->CxList, FRAMEAtrdList1->TvList, sId, FRAMEAtrdList1->Column01->Index);
#if 0
            DisplayInfo(pObj);
#else
            int nRow = FRAMEAtrdList1->TvList->DataController->FocusedRecordIndex;
            if( nRow <= -1 )
                return;

            int nMemPtr = FRAMEAtrdList1->TvList->DataController->Values[nRow][FRAMEAtrdList1->Column99->Index];
            TItsAtrd* pObj = (TItsAtrd*)nMemPtr;
            DisplayInfo(pObj);
#endif
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::RefreshLink()
{
    if (!FRAMEOpenMap1->MapStart) return;

    try
    {
        int nRow;
        TcxDataController *pGDC = TvLink->DataController;
        TvLink->BeginUpdate();
        try
        {
            int nSeq = 1;
            for (int ii = 0; ii < pGDC->RecordCount; ii++)
            {
                pGDC->Values[ii][ColSeq->Index] = nSeq;
                String sLinkId = pGDC->Values[ii][ColLink2->Index];

                sJs.printf(L"updateEditLink('%s', %d)", sLinkId.c_str(), nSeq);
                FRAMEOpenMap1->ExecScript(sJs);
                if (nSeq++ > ITSMAP_MAX_DISPCOLOR) nSeq = 1;
            }
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::BtnLinkDelClick(TObject *Sender)
{
    int nSelRows = TvLink->Controller->SelectedRowCount;
    if (nSelRows <= 0)
    {
        Application->MessageBox(lblText4->Caption.c_str(),//L"삭제를 원하는 구간을 목록에서 선택하세요.",
                                FTitle.c_str(),
                                MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

    try
    {
        TcxDataController *pGDC = TvLink->DataController;
        TvLink->BeginUpdate();
        try
        {
            for (int ii = 0; ii < pGDC->RecordCount; ii++)
            {
                String sLinkId = pGDC->Values[ii][ColLink2->Index];
                sJs.printf(L"updateEditLink('%s', 0)", sLinkId.c_str());
                FRAMEOpenMap1->ExecScript(sJs);
            }
            TvLink->Controller->DeleteSelection();
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }
    RefreshLink();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::BtnLinkAddClick(TObject *Sender)
{
    if (EdLinkId->Text.IsEmpty()) return;

    String sNewId = EdLinkId->Text;
    bool bFound = false;
    try
    {
        int nRow;
        TcxDataController *pGDC = TvLink->DataController;
        TvLink->BeginUpdate();

        for (int ii = 0; ii < pGDC->RecordCount; ii++)
        {
            String sLinkId = pGDC->Values[ii][ColLink2->Index];
            if (sNewId == sLinkId)
            {
                bFound = true;
                break;
            }
        }
        if (bFound)
        {
            Application->MessageBox(lblText5->Caption.c_str(),//L"이미 등록되어 있는 구간 입니다.\r\n구간은 중복해서 등록 할 수 없습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }

        nRow = pGDC->AppendRecord();
        pGDC->Values[nRow][ColLink2->Index] = sNewId;
        TItsRoad *pLink = ItsRoadManager->FLists.Find(sNewId);
        if (pLink)
        {
            TItsNode *pNode;
            pGDC->Values[nRow][ColLink3->Index] = pLink->F_NODE_ID;
            pGDC->Values[nRow][ColLink5->Index] = pLink->T_NODE_ID;
            pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
            if (pNode)
            {
                pGDC->Values[nRow][ColLink4->Index] = pNode->NODE_NAME;
            }
            pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
            if (pNode)
            {
                pGDC->Values[nRow][ColLink6->Index] = pNode->NODE_NAME;
            }
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }

    RefreshLink();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::TvLinkCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    if (!FRAMEOpenMap1->MapStart) return;

	if (!ACellViewInfo) return;
	int nRow = TvLink->DataController->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    String sLinkId = TvLink->DataController->Values[nRow][ColLink2->Index];
    sJs.printf(L"extentLayerObject(%d, '%s')", LyrTp_Traffic, sLinkId.c_str());
    FRAMEOpenMap1->ExecScript(sJs);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::FRAMEAtrdList1TvListCellDblClickSect(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
	if (!ACellViewInfo) return;
	int nRow = FRAMEAtrdList1->TvList->DataController->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    int nMemPtr = FRAMEAtrdList1->TvList->DataController->Values[nRow][FRAMEAtrdList1->Column99->Index];
    TItsAtrd* pObj = (TItsAtrd*)nMemPtr;
    DisplayInfo(pObj);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::ChkGisZoomClick(TObject *Sender)
{
    PnlEdit->Visible = !ChkGisZoom->Checked;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::BtnUpClick(TObject *Sender)
{
    try
    {
        TvLink->BeginUpdate();

        int nCurIdx = TvLink->DataController->FocusedRowIndex;//FocusedRecordIndex;
        if (nCurIdx <= 0)
        {
            return;
        }

        int nUpIdx = nCurIdx-1;

        try
        {
            String sVal1 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink1->Index]);
            String sVal2 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink2->Index]);
            String sVal3 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink3->Index]);
            String sVal4 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink4->Index]);
            String sVal5 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink5->Index]);
            String sVal6 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink6->Index]);

            TvLink->DataController->Values[nCurIdx][ColLink1->Index] = TvLink->DataController->Values[nUpIdx][ColLink1->Index];
            TvLink->DataController->Values[nCurIdx][ColLink2->Index] = TvLink->DataController->Values[nUpIdx][ColLink2->Index];
            TvLink->DataController->Values[nCurIdx][ColLink3->Index] = TvLink->DataController->Values[nUpIdx][ColLink3->Index];
            TvLink->DataController->Values[nCurIdx][ColLink4->Index] = TvLink->DataController->Values[nUpIdx][ColLink4->Index];
            TvLink->DataController->Values[nCurIdx][ColLink5->Index] = TvLink->DataController->Values[nUpIdx][ColLink5->Index];
            TvLink->DataController->Values[nCurIdx][ColLink6->Index] = TvLink->DataController->Values[nUpIdx][ColLink6->Index];

            TvLink->DataController->Values[nUpIdx][ColLink1->Index] = sVal1;
            TvLink->DataController->Values[nUpIdx][ColLink2->Index] = sVal2;
            TvLink->DataController->Values[nUpIdx][ColLink3->Index] = sVal3;
            TvLink->DataController->Values[nUpIdx][ColLink4->Index] = sVal4;
            TvLink->DataController->Values[nUpIdx][ColLink5->Index] = sVal5;
            TvLink->DataController->Values[nUpIdx][ColLink6->Index] = sVal6;

            TvLink->Controller->ClearSelection();
            TvLink->Controller->FocusedRowIndex = nUpIdx;
            //TvLink->Controller->FocusedRecordIndex = nUpIdx;
            TvLink->ViewData->Records[nUpIdx]->Focused = true;
            TvLink->ViewData->Records[nUpIdx]->Selected = true;
            CxLink->SetFocus();
            //TcxCustomGridRow* ARow = TvLink->ViewData->Rows[TvLink->DataController->FocusedRowIndex];
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }
    RefreshLink();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::BtnDownClick(TObject *Sender)
{
    try
    {
        TvLink->BeginUpdate();

        int nCurIdx = TvLink->DataController->FocusedRowIndex;//FocusedRecordIndex;
        //int nCurIdx = TvLink->DataController->FocusedRecordIndex;
        if (nCurIdx < 0)
        {
            return;
        }
        if (nCurIdx == TvLink->DataController->RecordCount-1)
        {
            return;
        }

        int nDnIdx = nCurIdx+1;

        try
        {
            String sVal1 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink1->Index]);
            String sVal2 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink2->Index]);
            String sVal3 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink3->Index]);
            String sVal4 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink4->Index]);
            String sVal5 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink5->Index]);
            String sVal6 = VarToStr(TvLink->DataController->Values[nCurIdx][ColLink6->Index]);

            TvLink->DataController->Values[nCurIdx][ColLink1->Index] = TvLink->DataController->Values[nDnIdx][ColLink1->Index];
            TvLink->DataController->Values[nCurIdx][ColLink2->Index] = TvLink->DataController->Values[nDnIdx][ColLink2->Index];
            TvLink->DataController->Values[nCurIdx][ColLink3->Index] = TvLink->DataController->Values[nDnIdx][ColLink3->Index];
            TvLink->DataController->Values[nCurIdx][ColLink4->Index] = TvLink->DataController->Values[nDnIdx][ColLink4->Index];
            TvLink->DataController->Values[nCurIdx][ColLink5->Index] = TvLink->DataController->Values[nDnIdx][ColLink5->Index];
            TvLink->DataController->Values[nCurIdx][ColLink6->Index] = TvLink->DataController->Values[nDnIdx][ColLink6->Index];

            TvLink->DataController->Values[nDnIdx][ColLink1->Index] = sVal1;
            TvLink->DataController->Values[nDnIdx][ColLink2->Index] = sVal2;
            TvLink->DataController->Values[nDnIdx][ColLink3->Index] = sVal3;
            TvLink->DataController->Values[nDnIdx][ColLink4->Index] = sVal4;
            TvLink->DataController->Values[nDnIdx][ColLink5->Index] = sVal5;
            TvLink->DataController->Values[nDnIdx][ColLink6->Index] = sVal6;

            TvLink->Controller->ClearSelection();
            TvLink->Controller->FocusedRowIndex = nDnIdx;
            //TvLink->Controller->FocusedRecordIndex = nDnIdx;
            TvLink->ViewData->Records[nDnIdx]->Focused = true;
            TvLink->ViewData->Records[nDnIdx]->Selected = true;
            CxLink->SetFocus();
            //TcxCustomGridRow* ARow = TvLink->ViewData->Rows[TvLink->DataController->FocusedRowIndex];
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }
    RefreshLink();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::UpdateNode()
{
    if (!FRAMEOpenMap1->MapStart) return;

    sJs.printf(L"removeLayerObject(%d)", FLayerIdx);
    FRAMEOpenMap1->ExecScript(sJs);

    String arrObj = "";
    String sTemp;

    int cnt = 0;
    ItsNodeManager->FLists.Lock();
    try
    {
        FOR_STL(TItsNode*, pObj, ItsNodeManager->FLists)
        {
            if (arrObj != "") arrObj += "|";

            if (cnt == 0)
            {
                sTemp.printf(L"%s,%s,%s,%.6f,%.6f,0,0,0,0", pObj->NODE_ID.c_str(), pObj->NODE_ID.c_str(), pObj->NODE_NAME.c_str(), pObj->X_CRDN, pObj->Y_CRDN);
                arrObj += sTemp;
                sJs.printf(L"addFacility(%d, '%s')", FLayerIdx, arrObj.c_str());
                FRAMEOpenMap1->ExecScript(sJs);
            }
            else
            {
                sTemp.printf(L"%s,%s,%s,%.6f,%.6f,0,0,0,0", pObj->NODE_ID.c_str(), pObj->NODE_ID.c_str(), pObj->NODE_NAME.c_str(), pObj->X_CRDN, pObj->Y_CRDN);
                sJs.printf(L"addFacilityObject(%d, '%s')", FLayerIdx, sTemp.c_str());
                FRAMEOpenMap1->ExecScript(sJs);
            }
            cnt++;
        }
    }
    __finally
    {
        ItsNodeManager->FLists.UnLock();
#if 0
        if (arrObj != "")
        {
            sJs.printf(L"addFacility(%d, '%s')", FLayerIdx, arrObj.c_str());
            FRAMEOpenMap1->ExecScript(sJs);
        }
#endif
        sJs.printf(L"showLayer(%d, 0)", FLayerIdx);
        FRAMEOpenMap1->ExecScript(sJs);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::OpenMapCallback(TObject *ASender, const String AFunc, const String AArgs, const bool AFirst)
{
    try
    {
        if (AFirst)
        {
            //지도를 편집모드로 변경
            sJs.printf(L"setEditMode(%d, 1)", FLayerIdx);
            FRAMEOpenMap1->ExecScript(sJs);

            //FRAMEOpenMap1->ExecScript("setMouseClick(1)");

            //노드아이콘 등록
            //UpdateNode();

            //편집레이어 생성
            FRAMEOpenMap1->ExecScript("initEditLayer()");

            //배경링크레이어 생성
            UpdateEditLink();

            //sJs.printf(L"extentLayer(%d)", FLayerIdx);
            //FRAMEOpenMap1->ExecScript(sJs);

            return;
        }

        if (AFunc.Pos("onlinkclick"))
        {
            TStringList *pStringList = new TStringList;
            pStringList->CommaText = AArgs;

            try
            {
                String sLyrType = pStringList->Strings[0];
                String sLinkId  = pStringList->Strings[1];
#if 0
                String sLinkX   = pStringList->Strings[2];
                String sLinkY   = pStringList->Strings[3];
                String sX       = pStringList->Strings[4];
                String sY       = pStringList->Strings[5];
#endif
                if (EditMode)
                {
                    EdLinkId->Text = sLinkId;
                }
            }
            catch(Exception &e)
            {
            }
            delete pStringList;
        }
    }
    catch(Exception &exception)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0120M::ColSeqCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
	if (AViewInfo->GridRecord->RecordIndex < 0)
		return;
	if (VarIsNull(AViewInfo->Value))
		return;

	try
	{
		int nIdx = AViewInfo->Value;
		if (nIdx < 0)
			return;

		ACanvas->Brush->Color = ITSMap_GetDisplayColor(nIdx);
		ACanvas->Font->Color  = ACanvas->Brush->Color;
	}
	catch(...)
	{
	}
}
//---------------------------------------------------------------------------

