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

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

#include "FRAME_RoadListF.h"
#include "CDSRoadF.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 resource "*.dfm"
//TFRAMERoadList *FRAMERoadList;
//---------------------------------------------------------------------------
__fastcall TFRAMERoadList::TFRAMERoadList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

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

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

	FCodeIFD = ItsCodeManager->FLists.Find("IFD"); // 방향코드
	FCodeSRT = ItsCodeManager->FLists.Find("SRT"); // 구간등급코드
}
//---------------------------------------------------------------------------

int __fastcall TFRAMERoadList::AddRoad(String ARoadId)
{
    int nRow = 0;
    TItsRoad *pRoad = ItsRoadManager->FLists.Find(ARoadId);
    if (!pRoad) return nRow;

    TItsSubCode *pSubCode;
    try
    {
        TvList->BeginUpdate();

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

            m_pGDC->Values[nRow][ColumnSel->Index] = false;                             //선택
            m_pGDC->Values[nRow][Column00->Index]  = pRoad->ROAD_ID;             //'도로 ID'
            m_pGDC->Values[nRow][Column01->Index]  = pRoad->ROAD_ID;             //'도로 ID'

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

            m_pGDC->Values[nRow][Column02->Index]  = pRoad->ROAD_NM;             //'도로명'

            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->RMRK;                //'비고'
            //m_pGDC->Values[nRow][Column05->Index]  = pRoad->EXTR_CNCT_SECT_YN;   //'외부 연계 구간 여부'
            m_pGDC->Values[nRow][Column06->Index]  = pRoad->DEL_YN;              //'삭제 여부'
            m_pGDC->Values[nRow][Column07->Index]  = pRoad->SECT_LNGT;           //'구간 길이'

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

            m_pGDC->Values[nRow][Column09->Index]  = pRoad->F_NODE_ID;           //'시작노드ID'
            m_pGDC->Values[nRow][Column10->Index]  = pRoad->T_NODE_ID;           //'종료노드ID'
            m_pGDC->Values[nRow][Column11->Index]  = pRoad->STRT_NM;             //'시점 명'
            m_pGDC->Values[nRow][Column12->Index]  = pRoad->END_NM;              //'종점 명'

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

void __fastcall TFRAMERoadList::UpdateList()
{
    TItsSubCode *pSubCode;

    CMM_ClearGridTableView(TvList);

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

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

                    m_pGDC->Values[nRow][ColumnSel->Index] = false;                             //선택
                    m_pGDC->Values[nRow][Column00->Index]  = pRoad->ROAD_ID;             //'도로 ID'
                    m_pGDC->Values[nRow][Column01->Index]  = pRoad->ROAD_ID;             //'도로 ID'

                    m_pGDC->Values[nRow][Column02->Index]  = pRoad->ROAD_NM;             //'도로명'

                    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->RMRK;                //'비고'
                    //m_pGDC->Values[nRow][Column05->Index]  = pRoad->EXTR_CNCT_SECT_YN;   //'외부 연계 구간 여부'
                    m_pGDC->Values[nRow][Column06->Index]  = pRoad->DEL_YN;              //'삭제 여부'
                    m_pGDC->Values[nRow][Column07->Index]  = pRoad->SECT_LNGT;           //'구간 길이'

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

                    m_pGDC->Values[nRow][Column09->Index]  = pRoad->F_NODE_ID;           //'시작노드ID'
                    m_pGDC->Values[nRow][Column10->Index]  = pRoad->T_NODE_ID;           //'종료노드ID'
                    m_pGDC->Values[nRow][Column11->Index]  = pRoad->STRT_NM;             //'시점 명'
                    m_pGDC->Values[nRow][Column12->Index]  = pRoad->END_NM;              //'종점 명'

                    m_pGDC->Values[nRow][Column18->Index]  = (int)pRoad;
                }
            }
        }
        __finally
        {
            ItsRoadManager->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 TFRAMERoadList::ChkDispExtPropertiesChange(TObject *Sender)
{
    UpdateList();
}
//---------------------------------------------------------------------------

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

int  __fastcall TFRAMERoadList::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 TFRAMERoadList::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 TFRAMERoadList::TvListDataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList);
    LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
}
//---------------------------------------------------------------------------

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

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

