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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "WindowMsgF.h"
#include "XiMapUtilF.h"
#include "CDSCodeF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSServiceLinkF.h"
#include "CDSRoadF.h"
#pragma hdrstop

#include "FrmGisSearchF.h"
#include "ITS00GEOF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#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 "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma resource "*.dfm"
TFrmGisSearch *FrmGisSearch = NULL;

//---------------------------------------------------------------------------
__fastcall TFrmGisSearch::TFrmGisSearch(TComponent* Owner)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);
    m_pGDC = TvList->DataController;
    m_bClose = false;
}
//---------------------------------------------------------------------------
void __fastcall TFrmGisSearch::FormClose(TObject *Sender, TCloseAction &Action)
{
    m_bClose = true;
    try 
    {
        if (TmrClear->Enabled == true)
        {
            TmrClear->Enabled = false;
//            ITS00GEO->xclntMap->ClearPathInfo();
//            ITS00GEO->xclntMap->ReDraw();
        }
        
        //if (m_bClose == false)
        {
            if (ITS00GEO) ITS00GEO->ChkGisSearch->Checked = false;
        }
    }
    catch(...)
    {
    }
    
    CMM_SaveForm(g_sFormsDir, this);

    Action = caFree;
}
//---------------------------------------------------------------------------
void __fastcall TFrmGisSearch::FormDestroy(TObject *Sender)
{
    FrmGisSearch = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TFrmGisSearch::TmrShowTimer(TObject *Sender)
{
    Enabled = true;
    EdSearch->Enabled = true; 
    EdSearch->BringToFront();
}
//---------------------------------------------------------------------------

void __fastcall TFrmGisSearch::BtnSearchClick(TObject *Sender)
{
    SelList();    
}
//---------------------------------------------------------------------------

void __fastcall TFrmGisSearch::EdSearchKeyPress(TObject *Sender, wchar_t &Key)
{
    if (Key == VK_RETURN)
    {
        Key = 0;
        BtnSearchClick(NULL);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmGisSearch::SelList()
{
    TSqlCursor crs;

    String sParam = "";

    m_nType = CbType->ItemIndex;
    sParam  = EdSearch->Text.Trim();

    switch(m_nType)
    {
    case 0: SelNode(sParam);        break;
    case 1: SelLink(sParam);        break;
    case 2: SelServiceLink(sParam); break;
    case 3: SelRoad(sParam);        break;
    }
    TvList->ApplyBestFit(NULL, false, false);
}
//---------------------------------------------------------------------------

void __fastcall TFrmGisSearch::SelNode(String AParam)
{
    Column01->Caption = "노드ID";
    Column02->Caption = "노드명";
    Column03->Caption = "노드유형";
    Column04->Visible = false;
    Column05->Visible = false;
    Column06->Visible = false;
    Column07->Visible = false;

    TItsSubCode *pSubCode;
    TItsCode    *FCodeNodeT = ItsCodeManager->FLists.Find("NDT"); //노드유형

    CMM_ClearGridTableView(TvList);

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

        try
        {
            ItsNodeManager->FLists.Lock();
            FOR_STL(TItsNode *, pNode, ItsNodeManager->FLists)
            {
                if (AParam == "" || pNode->NODE_ID.Pos(AParam)  ||
                                    pNode->NODE_NAME.Pos(AParam) )
                {
                    nRow = m_pGDC->AppendRecord();

                    m_pGDC->Values[nRow][Column01->Index] = pNode->NODE_ID;   //노드ID
                    m_pGDC->Values[nRow][Column02->Index] = pNode->NODE_NAME; //교차로명칭

                    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][Column03->Index] = sNodeType;        //노드유형

                    m_pGDC->Values[nRow][Column04->Index] = pNode->X_CRDN;    //X 좌표
                    m_pGDC->Values[nRow][Column05->Index] = pNode->Y_CRDN;    //Y 좌표

                    m_pGDC->Values[nRow][Column99->Index] = (int)pNode;
                }
            }
        }
        __finally
        {
            ItsNodeManager->FLists.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmGisSearch::SelLink(String AParam)
{
    Column01->Caption = "링크ID";
    Column02->Caption = "도로명";
    Column03->Caption = "시작노드";
    Column04->Caption = "종료노드";

    Column04->Visible = true;
    Column05->Visible = false;
    Column06->Visible = false;
    Column07->Visible = false;

    TItsNode *pNode;

    CMM_ClearGridTableView(TvList);

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

        try
        {
            ItsLinkManager->FLists.Lock();
            FOR_STL(TItsLink*, pLink, ItsLinkManager->FLists)
            {
                if (AParam == "" || pLink->LINK_ID.Pos(AParam)  ||
                                    pLink->ROAD_NAME.Pos(AParam) )
                {
                    nRow = m_pGDC->AppendRecord();

                    m_pGDC->Values[nRow][Column01->Index]  = pLink->LINK_ID;
                    m_pGDC->Values[nRow][Column02->Index]  = pLink->ROAD_NAME;        //'도로명';

                    String sFNodeName = "[" + pLink->F_NODEID + "] ";
                    pNode = ItsNodeManager->FLists.Find(pLink->F_NODEID);
                    if (pNode)
                        sFNodeName = sFNodeName + pNode->NODE_NAME;
                    m_pGDC->Values[nRow][Column03->Index]  = sFNodeName;              //'시작노드ID';

                    String sTNodeName = "[" + pLink->T_NODEID + "] ";
                    pNode = ItsNodeManager->FLists.Find(pLink->T_NODEID);
                    if (pNode)
                        sTNodeName = sTNodeName + pNode->NODE_NAME;
                    m_pGDC->Values[nRow][Column04->Index]  = sTNodeName;              //'종료노드ID';

                    m_pGDC->Values[nRow][Column99->Index]  = (int)pLink;
                }
            }
        }
        __finally
        {
            ItsLinkManager->FLists.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmGisSearch::SelServiceLink(String AParam)
{
    Column01->Caption = "ID";
    Column02->Caption = "명칭";
    Column03->Caption = "방향";
    Column04->Caption = "시점명";
    Column05->Caption = "종점명";

    Column04->Visible = true;
    Column05->Visible = true;

    Column06->Visible = false;
    Column07->Visible = false;

    TItsSubCode *pSubCode;
    TItsCode    *FCodeIFD = ItsCodeManager->FLists.Find("IFD"); // 방향코드

    CMM_ClearGridTableView(TvList);

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

        try
        {
            ItsServiceLinkManager->FLists.Lock();
            FOR_STL(TItsServiceLink*, pServiceLink, ItsServiceLinkManager->FLists)
            {
                if (AParam == "" || pServiceLink->IFSC_ID.Pos(AParam) ||
                                    pServiceLink->IFSC_NM.Pos(AParam) )
                {
                    nRow = m_pGDC->AppendRecord();

                    m_pGDC->Values[nRow][Column01->Index]  = pServiceLink->IFSC_ID;             //'정보제공구간 ID'
                    m_pGDC->Values[nRow][Column02->Index]  = pServiceLink->IFSC_NM;             //'정보제공구간 명'

                    String sIFD = "[" + pServiceLink->DRCT_CD  + "] ";
                    pSubCode = FCodeIFD->FSubLists.Find(pServiceLink->DRCT_CD);                 // 방향코드
                    if (pSubCode) sIFD = sIFD + pSubCode->CMMN_CD_KOR_NM;
                    m_pGDC->Values[nRow][Column03->Index]  = sIFD;                              //'방향 코드'


                    m_pGDC->Values[nRow][Column04->Index]  = pServiceLink->STRT_NM;             //'시점 명'
                    m_pGDC->Values[nRow][Column05->Index]  = pServiceLink->END_NM;              //'종점 명'

                    m_pGDC->Values[nRow][Column99->Index]  = (int)pServiceLink;
                }
            }
        }
        __finally
        {
            ItsServiceLinkManager->FLists.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmGisSearch::SelRoad(String AParam)
{
    Column01->Caption = "ID";
    Column02->Caption = "명칭";
    Column03->Caption = "방향";
    Column04->Caption = "시점명";
    Column05->Caption = "종점명";

    Column04->Visible = true;
    Column05->Visible = true;

    Column06->Visible = false;
    Column07->Visible = false;

    TItsSubCode *pSubCode;
	TItsCode    *FCodeIFD = ItsCodeManager->FLists.Find("IFD"); // 방향코드

    CMM_ClearGridTableView(TvList);

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

        try
        {
            ItsRoadManager->FLists.Lock();
            FOR_STL(TItsRoad*, pRoad, ItsRoadManager->FLists)
            {
                if (AParam == "" || pRoad->ROAD_ID.Pos(AParam)  ||
                                    pRoad->ROAD_NAME.Pos(AParam) )
                {
                    nRow = m_pGDC->AppendRecord();

                    m_pGDC->Values[nRow][Column01->Index]  = pRoad->ROAD_ID;             //'도로 ID'
                    m_pGDC->Values[nRow][Column02->Index]  = pRoad->ROAD_NAME;           //'도로명'

                    String sIFD = "[" + pRoad->DRCT_CD  + "] ";
                    pSubCode = FCodeIFD->FSubLists.Find(pRoad->DRCT_CD);                 // 방향코드
                    if (pSubCode) sIFD = sIFD + pSubCode->CMMN_CD_KOR_NM;
                    m_pGDC->Values[nRow][Column03->Index]  = sIFD;                       //'방향 코드'

                    m_pGDC->Values[nRow][Column04->Index]  = pRoad->STRT_NM;             //'시점 명'
                    m_pGDC->Values[nRow][Column05->Index]  = pRoad->END_NM;              //'종점 명'

                    m_pGDC->Values[nRow][Column99->Index]  = (int)pRoad;
                }
            }
        }
        __finally
        {
            ItsRoadManager->FLists.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmGisSearch::SelGisItem()
{
#if 0
    try
    {
        int nIndex = m_pGDC->FocusedRecordIndex;
        if (nIndex < 0) return;

        //int nMemPtr = m_pGDC->Values[nIndex][Column99->Index];
        String sId  = VarToStr(m_pGDC->Values[nIndex][Column01->Index]);

        TmrClear->Enabled = false;
        TXiMapUtil::ClearPath(ITS00GEO->xclntMap);
        switch(m_nType)
        {
        case 0: TXiMapUtil::MoveNode(ITS00GEO->xclntMap, sId);        break;
        case 1: TXiMapUtil::MoveLink(ITS00GEO->xclntMap, sId);        break;
        case 2: TXiMapUtil::MoveServiceLink(ITS00GEO->xclntMap, sId); break;
        case 3: TXiMapUtil::MoveRoad(ITS00GEO->xclntMap, sId);        break;
        default: return;
        }

        TmrClear->Enabled = true;
    }
    catch(...)
    {
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TFrmGisSearch::TmrClearTimer(TObject *Sender)
{
    try {
        //TXiMapUtil::ClearPath(ITS00MAP->xclntMap);
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmGisSearch::TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    if (ACellViewInfo)
    {
        SelGisItem();
    }
}
//---------------------------------------------------------------------------

