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

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

#include "FRAME_LinkListF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSIfscF.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 "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#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 link "cxLabel"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"
//TFRAMELinkList *FRAMELinkList;
//---------------------------------------------------------------------------
__fastcall TFRAMELinkList::TFRAMELinkList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

    ColumnSel->Options->Filtering = false;
    m_pGDC = TvList->DataController;

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

int __fastcall TFRAMELinkList::AddLink(String ALinkId)
{
    TItsNode *pNode;

    int nRow = 0;
    TItsLink *pLink = ItsLinkManager->FLists.Find(ALinkId);
    if (!pLink) return nRow;

    try
    {
        TItsIfsc *pIfsc;

        TvList->BeginUpdate();

        //if (pLink->REGN_YN == "Y" || ChkDispExt->Checked == true)
        {
            nRow = m_pGDC->AppendRecord();

            m_pGDC->Values[nRow][ColumnSel->Index] = false;                   //선택
            m_pGDC->Values[nRow][Column00->Index]  = pLink->LINK_ID;
            m_pGDC->Values[nRow][Column01->Index]  = pLink->LINK_ID;
            m_pGDC->Values[nRow][Column20->Index]  = pLink->IFSC_ID;

            pIfsc = ItsIfscManager->FLists.Find(pLink->IFSC_ID);
            if (pIfsc) m_pGDC->Values[nRow][Column21->Index] = pIfsc->IFSC_NM;

            m_pGDC->Values[nRow][ColumnSeq->Index] = nRow+1;

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

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

            m_pGDC->Values[nRow][Column04->Index]  = pLink->ROAD_USE;         //'도로사용여부';
            m_pGDC->Values[nRow][Column05->Index]  = pLink->LANES;            //'차로수';
            m_pGDC->Values[nRow][Column06->Index]  = pLink->ROAD_RANK;        //'도로등급';
            m_pGDC->Values[nRow][Column07->Index]  = pLink->ROAD_TYPE;        //'도로유형';
            m_pGDC->Values[nRow][Column08->Index]  = pLink->ROAD_NO;          //'도로번호';
            m_pGDC->Values[nRow][Column09->Index]  = pLink->ROAD_NAME;        //'도로명';
            m_pGDC->Values[nRow][Column10->Index]  = pLink->MULTI_LINK;       //'중용구간여부';
            m_pGDC->Values[nRow][Column11->Index]  = pLink->LINK_CONNECT;     //'연결로코드';
            m_pGDC->Values[nRow][Column12->Index]  = pLink->MAX_SPD;          //'최고제한속도';
            m_pGDC->Values[nRow][Column13->Index]  = pLink->REST_VEH;         //'통행제한차량';
            m_pGDC->Values[nRow][Column14->Index]  = pLink->REST_W;           //'통과제한하중';
            m_pGDC->Values[nRow][Column15->Index]  = pLink->REST_H;           //'통과제한높이';
            m_pGDC->Values[nRow][Column16->Index]  = pLink->LINK_LENG;        //'연장';
            m_pGDC->Values[nRow][Column17->Index]  = pLink->RMRK;             //'비고';

            m_pGDC->Values[nRow][Column18->Index]  = 0;
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
    }
    return nRow;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMELinkList::UpdateList()
{
    CMM_ClearGridTableView(TvList);

    TItsNode *pNode;

    int nRow = 0;
    try
    {
        TItsIfsc *pIfsc;

        TvList->BeginUpdate();

        try
        {
            ItsLinkManager->FLists.Lock();
            FOR_STL(TItsLink*, pLink, ItsLinkManager->FLists)
            {
                if (pLink->REGN_YN == "Y" || ChkDispExt->Checked == true)
                {
                    nRow = m_pGDC->AppendRecord();

                    m_pGDC->Values[nRow][ColumnSel->Index] = false;                   //선택
                    m_pGDC->Values[nRow][Column00->Index]  = pLink->LINK_ID;
                    m_pGDC->Values[nRow][Column01->Index]  = pLink->LINK_ID;
                    m_pGDC->Values[nRow][Column20->Index]  = pLink->IFSC_ID;

                    pIfsc = ItsIfscManager->FLists.Find(pLink->IFSC_ID);
                    if (pIfsc) m_pGDC->Values[nRow][Column21->Index] = pIfsc->IFSC_NM;

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

                    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][Column03->Index]  = sTNodeName;              //'종료노드ID';

                    m_pGDC->Values[nRow][Column04->Index]  = pLink->ROAD_USE;         //'도로사용여부';
                    m_pGDC->Values[nRow][Column05->Index]  = pLink->LANES;            //'차로수';
                    m_pGDC->Values[nRow][Column06->Index]  = pLink->ROAD_RANK;        //'도로등급';
                    m_pGDC->Values[nRow][Column07->Index]  = pLink->ROAD_TYPE;        //'도로유형';
                    m_pGDC->Values[nRow][Column08->Index]  = pLink->ROAD_NO;          //'도로번호';
                    m_pGDC->Values[nRow][Column09->Index]  = pLink->ROAD_NAME;        //'도로명';
                    m_pGDC->Values[nRow][Column10->Index]  = pLink->MULTI_LINK;       //'중용구간여부';
                    m_pGDC->Values[nRow][Column11->Index]  = pLink->LINK_CONNECT;     //'연결로코드';
                    m_pGDC->Values[nRow][Column12->Index]  = pLink->MAX_SPD;          //'최고제한속도';
                    m_pGDC->Values[nRow][Column13->Index]  = pLink->REST_VEH;         //'통행제한차량';
                    m_pGDC->Values[nRow][Column14->Index]  = pLink->REST_W;           //'통과제한하중';
                    m_pGDC->Values[nRow][Column15->Index]  = pLink->REST_H;           //'통과제한높이';
                    m_pGDC->Values[nRow][Column16->Index]  = pLink->LINK_LENG;        //'연장';
                    m_pGDC->Values[nRow][Column17->Index]  = pLink->RMRK;             //'비고';
                    m_pGDC->Values[nRow][Column22->Index]  = pLink->PARA_ID;

                    m_pGDC->Values[nRow][Column99->Index]  = (int)pLink;
                    m_pGDC->Values[nRow][Column18->Index]  = 0;
                }
            }
        }
        __finally
        {
            ItsLinkManager->FLists.UnLock();
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMELinkList::UpdateParam()
{
    try
    {
        TcxDataController* pGDC = TvList->DataController;
        TvList->BeginUpdate();

        int nMemPtr;
        try
        {
            ItsLinkManager->FLists.Lock();
            for (int ii = 0; ii < pGDC->RecordCount; ii++)
            {
                nMemPtr = pGDC->Values[ii][Column99->Index];
                TItsLink* pLink = (TItsLink*)nMemPtr;
                if (!pLink) continue;

                pGDC->Values[ii][Column22->Index] = pLink->PARA_ID;
            }
        }
        __finally
        {
            ItsLinkManager->FLists.UnLock();
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMELinkList::ChkDispExtPropertiesChange(TObject *Sender)
{
    UpdateList();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMELinkList::BtnAllSelectClick(TObject *Sender)
{
    TcxButton *pBtn = (TcxButton*)Sender;
    CMM_CheckAllListItem(TvList, ColumnSel->Index, pBtn->Tag);
}
//---------------------------------------------------------------------------

int  __fastcall TFRAMELinkList::GetSelLinkIds(String &ALinkIds)
{
    ALinkIds = "";

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

    String sLinkId = VarToStr(m_pGDC->Values[nIndex][Column01->Index]);
    ALinkIds = sLinkId;
    return 1;
}
//---------------------------------------------------------------------------

int  __fastcall TFRAMELinkList::GetSelLinkIds(TStringList *AStringList)
{
    int nSelCnt = 0;

    try
    {
        TvList->BeginUpdate();

#if 0
        int nRowCnt = m_pGDC->RecordCount;
        for (int ii = 0; ii < nRowCnt; ii++)
        {
            if (((bool)m_pGDC->Values[ii][ColumnSel->Index]))
            {
                AStringList->Add(m_pGDC->Values[ii][Column00->Index]);
                nSelIdx++;
            }
        }
#else
        int nRowCnt = TvList->ViewData->RecordCount;

        for (int ii = 0; ii < nRowCnt; ii++)
        {
            TcxCustomGridRow* ARow = TvList->ViewData->Rows[ii];
            if (((bool)ARow->Values[ColumnSel->Index]))
            {
                AStringList->Add(ARow->Values[Column01->Index]);
                nSelCnt++;
            }
        }
#endif
    }
    __finally
    {
        TvList->EndUpdate();
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMELinkList::TvListDataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList);
    LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
}
//---------------------------------------------------------------------------

void __fastcall TFRAMELinkList::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= lblFileName->Caption;//"링크정보";

    CMM_ExportToExcelFile(sTitle, pGrid, pView, (TForm*)this);
}
//---------------------------------------------------------------------------

