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

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

#include "IDB0030MF.h"
#include "IDB00301F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_LinkListF"
#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"

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

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

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

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

void __fastcall TIDB0030M::FormInit()
{
    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 TIDB0030M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
    FRAMEIfscList1->UpdateList();
}
//---------------------------------------------------------------------------

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

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

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

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

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

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

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

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

void __fastcall TIDB0030M::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 TIDB0030M::FRAMEIfscList1TvListFocusedRecordChanged(TcxCustomGridTableView *Sender, TcxCustomGridRecord *APrevFocusedRecord,
          TcxCustomGridRecord *AFocusedRecord, bool ANewItemRecordFocusingChanged)
{
    int nIndex = FRAMEIfscList1->TvList->DataController->FocusedRecordIndex;
    if (nIndex < 0) return;

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

    pLabel->Caption = "0 건";

    CMM_ClearGridTableView(TvList);

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

        try
        {
            String sSvcLinkId = VarToStr(FRAMEIfscList1->TvList->DataController->Values[nIndex][FRAMEIfscList1->Column01->Index]);
            TItsIfsc *pIfsc = ItsIfscManager->FLists.Find(sSvcLinkId);
            if (!pIfsc) return;

            TItsNode *pNode;
            TItsLink *pLink;
            String    sFNodeName, sTNodeName;
            int nSeq;
            std::map<int, String>::iterator itPos;
            for(itPos = pIfsc->FLinks.begin(); itPos != pIfsc->FLinks.end(); ++itPos)
            {
                nRow = m_pGDC->AppendRecord();

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

                m_pGDC->Values[nRow][FRAMELinkList1->ColumnSeq->Index] = nSeq;
                m_pGDC->Values[nRow][FRAMELinkList1->Column00->Index]  = sLinkId;
                m_pGDC->Values[nRow][FRAMELinkList1->Column01->Index]  = sLinkId;

                pLink = ItsLinkManager->FLists.Find(sLinkId);
                if (!pLink)
                {
                    continue;
                }
                String sFNodeName = "[" + pLink->F_NODE_ID + "] ";
                pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
                if (pNode)
                    sFNodeName = sFNodeName + pNode->NODE_NAME;

                String sTNodeName = "[" + pLink->T_NODE_ID + "] ";
                pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
                if (pNode)
                    sTNodeName = sTNodeName + pNode->NODE_NAME;

                m_pGDC->Values[nRow][FRAMELinkList1->Column02->Index]  = sFNodeName;              //'시작노드ID';
                m_pGDC->Values[nRow][FRAMELinkList1->Column03->Index]  = sTNodeName;              //'종료노드ID';
                m_pGDC->Values[nRow][FRAMELinkList1->Column04->Index]  = pLink->ROAD_USE;         //'도로사용여부';
                m_pGDC->Values[nRow][FRAMELinkList1->Column05->Index]  = pLink->LANES;            //'차로수';
                m_pGDC->Values[nRow][FRAMELinkList1->Column06->Index]  = pLink->ROAD_RANK;        //'도로등급';
                m_pGDC->Values[nRow][FRAMELinkList1->Column07->Index]  = pLink->ROAD_TYPE;        //'도로유형';
                m_pGDC->Values[nRow][FRAMELinkList1->Column08->Index]  = pLink->ROAD_NO;          //'도로번호';
                m_pGDC->Values[nRow][FRAMELinkList1->Column09->Index]  = pLink->ROAD_NAME;        //'도로명';
                m_pGDC->Values[nRow][FRAMELinkList1->Column10->Index]  = pLink->MULTI_LINK;       //'중용구간여부';
                m_pGDC->Values[nRow][FRAMELinkList1->Column11->Index]  = pLink->LINK_CONNECT;     //'연결로코드';
                m_pGDC->Values[nRow][FRAMELinkList1->Column12->Index]  = pLink->MAX_SPD;          //'최고제한속도';
                m_pGDC->Values[nRow][FRAMELinkList1->Column13->Index]  = pLink->REST_VEH;         //'통행제한차량';
                m_pGDC->Values[nRow][FRAMELinkList1->Column14->Index]  = pLink->REST_W;           //'통과제한하중';
                m_pGDC->Values[nRow][FRAMELinkList1->Column15->Index]  = pLink->REST_H;           //'통과제한높이';
                m_pGDC->Values[nRow][FRAMELinkList1->Column16->Index]  = pLink->LINK_LENG;        //'연장';
                m_pGDC->Values[nRow][FRAMELinkList1->Column17->Index]  = pLink->RMRK;             //'비고';
            }
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvList->EndUpdate();
        pLabel->Caption = FormatFloat("##,##0", m_pGDC->RecordCount) + " 건";
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0030M::BtnEditClick(TObject *Sender)
{
    TcxGridTableView  *TvList = FRAMEIfscList1->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][FRAMEIfscList1->Column01->Index]; //ID
    String sNm = m_pGDC->Values[nRow][FRAMEIfscList1->Column02->Index]; //명칭
    String sStNm = m_pGDC->Values[nRow][FRAMEIfscList1->Column11->Index]; //시점명
    String sEdNm = m_pGDC->Values[nRow][FRAMEIfscList1->Column12->Index]; //종점명

    try {
        TIDB00301 *pDlg = new TIDB00301(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][FRAMEIfscList1->Column02->Index] = sNm;
            m_pGDC->Values[nRow][FRAMEIfscList1->Column11->Index] = sStNm;
            m_pGDC->Values[nRow][FRAMEIfscList1->Column12->Index] = sEdNm;

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

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

