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

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

#include "WindowMsgF.h"

#include "FrmIncidVmsInfoF.h"
#include "FrmLinkGradeSetF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSIfscF.h"
#include "CDSRoadF.h"
#include "CDSTrafficGradeF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#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 "cxGroupBox"
#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 resource "*.dfm"
TFrmIncidVmsInfo *FrmIncidVmsInfo = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmIncidVmsInfo::TFrmIncidVmsInfo(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

Width = 474;
Height= 495;
int nHeight = Height;
int nWidth  = Width;

    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);
Height = nHeight;
Width  = nWidth;

    Caption = " " + Caption;//" 돌발구간 VMS 정보";
    m_sLinkId    = "";

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<해당 구간에 VMS 정보가 없습니다>";

    FCodeRDR = ItsCodeManager->FLists.Find("RDR");	// 도로등급
}
//---------------------------------------------------------------------------

void __fastcall TFrmIncidVmsInfo::FormClose(TObject *Sender, TCloseAction &Action)
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
    Action = caFree;
    FrmIncidVmsInfo = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TFrmIncidVmsInfo::RefreshInfo()
{
    ShowIncidVmsInfo(m_sLinkId);
}
//---------------------------------------------------------------------------

void __fastcall TFrmIncidVmsInfo::ShowIncidVmsInfo(String ALinkId)
{
    m_sLinkId    = ALinkId;

    EdId->Text     = ALinkId;
    EdLevel->Text  = String(1);
    EdLength->Text = FormatFloat("##,##0", 0);

    EdStId->Text = "";
    EdEdId->Text = "";
    EdStName->Text = FrmLang->lblNoName->Caption;//"명칭없음";
    EdEdName->Text = FrmLang->lblNoName->Caption;//"명칭없음";

    EdRoadName->Text = "";
    EdRoadRank->Text = "";

    EdRoadRank->Visible = false;

    TItsNode *pNode;
    TItsLink *pLink;

    pLink = ItsLinkManager->FLists.Find(m_sLinkId);
    if (!pLink) return;

    EdStId->Text = pLink->F_NODE_ID;
    EdEdId->Text = pLink->T_NODE_ID;
    EdLength->Text = FormatFloat("##,##0", StrToFloat(pLink->LINK_LENG));

    pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
    if (pNode) EdStName->Text = pNode->NODE_NAME.Trim();
    pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
    if (pNode) EdEdName->Text = pNode->NODE_NAME.Trim();

    EdRoadName->Text = pLink->ROAD_NAME;

    String sRDR = "[" + pLink->ROAD_RANK + "] ";
    if (FCodeRDR)
    {
        TItsSubCode *pSubCode = FCodeRDR->FSubLists.Find(pLink->ROAD_RANK);
        if (pSubCode) sRDR = sRDR + pSubCode->CMMN_CD_KOR_NM;
    }
    EdRoadRank->Text = sRDR;

    //TItsTrafficLink *pTraf = ItsTrafficLinkManager->FList.Find(m_sLinkId);
    //if (pTraf)
    //{
    //}
    EdRoadRank->Visible = true;

    SelVmsInfo(ALinkId);
}
//---------------------------------------------------------------------------

void __fastcall TFrmIncidVmsInfo::SelVmsInfo(String ALinkId)
{
    String sQry;
    TADOQuery *pADO = NULL;

    CMM_ClearGridTableView(TvList);
    try
    {
        TvList->BeginUpdate();
        TcxDataController *pGDC = TvList->DataController;

        pADO = new TADOQuery(NULL);
        pADO->Close();
        pADO->Connection = ITSDb_GetConnection();

        try
        {
#if 0
//구간이 여러개 있기때문에 VMS가 중복으로 표출된다.
            sQry = "SELECT A.LINK_ID, A.IFSC_ID, B.VMS_IFSC_ID, C.VMS_CTLR_NMBR, \r\n"
                   "       D.VMS_CTLR_ID, D.VMS_NM                               \r\n"
                   "  FROM TB_IFSC_RLTN_LINK      A,                             \r\n"
                   "       TB_VMS_IFSC_RLTN_IFSC  B,                             \r\n"
                   "       TB_VMS_RLTN_IFSC       C,                             \r\n"
                   "       TB_VMS_CTLR            D                              \r\n"
                   " WHERE A.IFSC_ID       = B.IFSC_ID                           \r\n"
                   "   AND C.VMS_IFSC_ID   = B.VMS_IFSC_ID                       \r\n"
                   "   AND C.VMS_CTLR_NMBR = D.VMS_CTLR_NMBR                     \r\n"
                   "   AND A.LINK_ID       = :p01                                \r\n"
                   " ORDER BY C.VMS_CTLR_NMBR                                    \r\n";
#else
            sQry = "SELECT VMS_CTLR_NMBR, VMS_CTLR_ID, VMS_NM                                   \r\n"
                   "  FROM TB_VMS_CTLR                                                          \r\n"
                   " WHERE VMS_CTLR_NMBR IN (SELECT DISTINCT C.VMS_CTLR_NMBR AS VMS_CTLR_NMBR   \r\n"
                   "                          FROM TB_IFSC_RLTN_LINK      A,                    \r\n"
                   "                               TB_VMS_IFSC_RLTN_IFSC  B,                    \r\n"
                   "                               TB_VMS_RLTN_IFSC       C                     \r\n"
                   "                         WHERE A.IFSC_ID       = B.IFSC_ID                  \r\n"
                   "                           AND C.VMS_IFSC_ID   = B.VMS_IFSC_ID              \r\n"
                   "                           AND A.LINK_ID       = :p01 )                     \r\n"
                   "   AND DEL_YN = 'N'                                                         \r\n"
                   " ORDER BY VMS_CTLR_NMBR                                                     \r\n";
#endif

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", ALinkId);
            ITSDb_SQLOpen(pADO);
            int nRow;
            for( ; !pADO->Eof; pADO->Next())
            {
                nRow = pGDC->AppendRecord();
                pGDC->Values[nRow][Column01->Index] = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;
                pGDC->Values[nRow][Column12->Index] = pADO->FieldByName("VMS_CTLR_ID")->AsString;
                pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("VMS_NM")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

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

