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

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

#include "FRAME_IfscListF.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 resource "*.dfm"
//TFRAMEIfscList *FRAMEIfscList;
//---------------------------------------------------------------------------
__fastcall TFRAMEIfscList::TFRAMEIfscList(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 TFRAMEIfscList::AddIfsc(String AIfscId)
{
    TItsSubCode *pSubCode;

    int nRow = 0;
    TItsIfsc *pIfsc = ItsIfscManager->FLists.Find(AIfscId);
    if (!pIfsc) return nRow;

    try
    {
        TvList->BeginUpdate();

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

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

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

            m_pGDC->Values[nRow][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][Column03->Index]  = sIFD;                              //'방향 코드'

            m_pGDC->Values[nRow][Column04->Index]  = pIfsc->RMRK;                //'비고'
            m_pGDC->Values[nRow][Column05->Index]  = pIfsc->EXTR_CNCT_SECT_YN;   //'외부 연계 구간 여부'
            m_pGDC->Values[nRow][Column06->Index]  = pIfsc->DEL_YN;              //'삭제 여부'
            m_pGDC->Values[nRow][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][Column08->Index]  = sSRT;                              //'구간 등급 코드'

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

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

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

    CMM_ClearGridTableView(TvList);


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

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

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

                    m_pGDC->Values[nRow][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][Column03->Index]  = sIFD;                              //'방향 코드'

                    m_pGDC->Values[nRow][Column04->Index]  = pIfsc->RMRK;                //'비고'
                    m_pGDC->Values[nRow][Column05->Index]  = pIfsc->EXTR_CNCT_SECT_YN;   //'외부 연계 구간 여부'
                    m_pGDC->Values[nRow][Column06->Index]  = pIfsc->DEL_YN;              //'삭제 여부'
                    m_pGDC->Values[nRow][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][Column08->Index]  = sSRT;                              //'구간 등급 코드'

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

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

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

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

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

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

