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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "CDSCodeF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSIfscF.h"
#include "CDSRoadF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "IDB0040MF.h"
#include "IDB00401F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_RoadListF"
#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 "cxSplitter"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "FRAME_IfscListF"
#pragma resource "*.dfm"

TIDB0040M *IDB0040M = NULL;
//---------------------------------------------------------------------------
__fastcall TIDB0040M::TIDB0040M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FRAMERoadList1->PnlTop->Visible = false;
    FRAMERoadList1->ChkDispExt->Visible = false;

    FRAMEIfscList1->PnlTop->Visible = false;
    FRAMEIfscList1->ChkDispExt->Visible = false;
    FRAMEIfscList1->ColumnSeq->Visible = true;
}
//---------------------------------------------------------------------------

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

void __fastcall TIDB0040M::FormInit()
{
    //m_pGDC = TvList->DataController;

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

    FCodeRDU = ItsCodeManager->FLists.Find("RDU");	// 도로사용여부
    FCodeRDR = ItsCodeManager->FLists.Find("RDR");	// 도로등급
    FCodeRDT = ItsCodeManager->FLists.Find("RDT");	// 도로유형
    FCodeMLT = ItsCodeManager->FLists.Find("MLT");	// 중용구간
    FCodeCTR = ItsCodeManager->FLists.Find("CTR");	// 연결로
    FCodePLV = ItsCodeManager->FLists.Find("PLV");	// 통행제한차량
}
//---------------------------------------------------------------------------

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

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

void __fastcall TIDB0040M::RefreshData()
{
}
//---------------------------------------------------------------------------

void __fastcall TIDB0040M::SelListData()
{
}
//---------------------------------------------------------------------------

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

    RefreshData();
    //CxList->SetFocus();
}
//---------------------------------------------------------------------------

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

void __fastcall TIDB0040M::DisplayListData()
{
}
//---------------------------------------------------------------------------

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

void __fastcall TIDB0040M::MnuMovePosClick(TObject *Sender)
{
#if 0
    TMenuItem *pMenu = (TMenuItem *)Sender;

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

    String sLinkId = VarToStr(m_pGDC->Values[nIndex][Column01->Index]);
#endif
}
//---------------------------------------------------------------------------

void __fastcall TIDB0040M::FRAMERoadList1TvListFocusedRecordChanged(TcxCustomGridTableView *Sender, TcxCustomGridRecord *APrevFocusedRecord,
          TcxCustomGridRecord *AFocusedRecord, bool ANewItemRecordFocusingChanged)
{
    int nIndex = FRAMERoadList1->TvList->DataController->FocusedRecordIndex;
    if (nIndex < 0) return;

    TLabel            *pLabel = FRAMEIfscList1->LblRecords;
    TcxGridTableView  *TvList = FRAMEIfscList1->TvList;
    TcxDataController *m_pGDC = TvList->DataController;

    pLabel->Caption = "0 건";

    CMM_ClearGridTableView(TvList);

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

        try
        {
            TItsSubCode *pSubCode;
            TItsCode    *FCodeIFD = ItsCodeManager->FLists.Find("IFD"); // 방향코드
            TItsCode    *FCodeSRT = ItsCodeManager->FLists.Find("SRT"); // 구간등급코드

            String sRoadId = VarToStr(FRAMERoadList1->TvList->DataController->Values[nIndex][FRAMERoadList1->Column01->Index]);
            TItsRoad *pRoad = ItsRoadManager->FLists.Find(sRoadId);
            if (!pRoad) return;

            TItsIfsc *pIfsc;
            String    sFNodeName, sTNodeName;
            int nSeq;
            std::map<int, String>::iterator itPos;
            for(itPos = pRoad->FIfscs.begin(); itPos != pRoad->FIfscs.end(); ++itPos)
            {
                nRow = m_pGDC->AppendRecord();

                nSeq              = itPos->first;
                String sSvcLinkId = itPos->second;

                m_pGDC->Values[nRow][FRAMEIfscList1->ColumnSeq->Index] = nSeq;
                m_pGDC->Values[nRow][FRAMEIfscList1->Column00->Index]  = sSvcLinkId;
                m_pGDC->Values[nRow][FRAMEIfscList1->Column01->Index]  = sSvcLinkId;

                pIfsc = ItsIfscManager->FLists.Find(sSvcLinkId);
                if (!pIfsc)
                {
                    continue;
                }

                m_pGDC->Values[nRow][FRAMEIfscList1->Column00->Index]  = pIfsc->IFSC_ID;             //'정보제공구간 ID'
                m_pGDC->Values[nRow][FRAMEIfscList1->Column01->Index]  = pIfsc->IFSC_ID;             //'정보제공구간 ID'

                m_pGDC->Values[nRow][FRAMEIfscList1->Column02->Index]  = pIfsc->IFSC_NM;             //'정보제공구간 명'

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

                m_pGDC->Values[nRow][FRAMEIfscList1->Column04->Index]  = pIfsc->RMRK;                //'비고'
                m_pGDC->Values[nRow][FRAMEIfscList1->Column05->Index]  = pIfsc->EXTR_CNCT_SECT_YN;   //'외부 연계 구간 여부'
                m_pGDC->Values[nRow][FRAMEIfscList1->Column06->Index]  = pIfsc->DEL_YN;              //'삭제 여부'
                m_pGDC->Values[nRow][FRAMEIfscList1->Column07->Index]  = pIfsc->SECT_LNGT;           //'구간 길이'

                String sSRT = "[" + pIfsc->SECT_GRAD_CD  + "] ";
                pSubCode = FCodeSRT->FSubLists.Find(pIfsc->SECT_GRAD_CD);                                   // 구간등급코드
                if (pSubCode) sSRT = sSRT + pSubCode->CMMN_CD_KOR_NM;
                m_pGDC->Values[nRow][FRAMEIfscList1->Column08->Index]  = sSRT;                              //'구간 등급 코드'

                m_pGDC->Values[nRow][FRAMEIfscList1->Column09->Index]  = pIfsc->F_NODE_ID;           //'시작노드ID'
                m_pGDC->Values[nRow][FRAMEIfscList1->Column10->Index]  = pIfsc->T_NODE_ID;           //'종료노드ID'
                m_pGDC->Values[nRow][FRAMEIfscList1->Column11->Index]  = pIfsc->STRT_NM;             //'시점 명'
                m_pGDC->Values[nRow][FRAMEIfscList1->Column12->Index]  = pIfsc->END_NM;              //'종점 명'
            }
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvList->EndUpdate();
        pLabel->Caption = FormatFloat("##,##0", m_pGDC->RecordCount) + " 건";
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0040M::FRAMERoadList1TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    BtnEditClick((TObject*)BtnEdit);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0040M::BtnEditClick(TObject *Sender)
{
    TcxGridTableView  *TvList = FRAMERoadList1->TvList;
    TcxDataController *m_pGDC = TvList->DataController;

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

    String sId = m_pGDC->Values[nRow][FRAMERoadList1->Column01->Index]; //ID
    String sNm = m_pGDC->Values[nRow][FRAMERoadList1->Column02->Index]; //명칭
    String sStNm = m_pGDC->Values[nRow][FRAMERoadList1->Column11->Index]; //시점명
    String sEdNm = m_pGDC->Values[nRow][FRAMERoadList1->Column12->Index]; //종점명

    try {
        TIDB00401 *pDlg = new TIDB00401(this);
        pDlg->EdId->Text = sId;
        pDlg->EdNm->Text = sNm;
        pDlg->EdStNm->Text = sStNm;
        pDlg->EdEdNm->Text = sEdNm;
        pDlg->ShowModal();
        if (pDlg->FUpdated)
        {
            sNm = pDlg->EdNm->Text.Trim();
            sStNm = pDlg->EdStNm->Text.Trim();
            sEdNm = pDlg->EdEdNm->Text.Trim();
            m_pGDC->Values[nRow][FRAMERoadList1->Column02->Index] = sNm;
            m_pGDC->Values[nRow][FRAMERoadList1->Column11->Index] = sStNm;
            m_pGDC->Values[nRow][FRAMERoadList1->Column12->Index] = sEdNm;

            int nMemPtr = m_pGDC->Values[nRow][FRAMERoadList1->Column18->Index];
            TItsRoad *pObj = (TItsRoad*)nMemPtr;
            pObj->ROAD_NM = sNm;
            pObj->STRT_NM = sStNm;
            pObj->END_NM  = sEdNm;
        }
        delete pDlg;
    } catch(...) { }
}
//---------------------------------------------------------------------------

