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

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

#include "FRAME_TrafficIfscLinkListF.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"
//TFRAMETrafficServiceLinkList *FRAMEServiceLinkList;
//---------------------------------------------------------------------------
__fastcall TFRAMETrafficServiceLinkList::TFRAMETrafficServiceLinkList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

    m_pGDC = TvList->DataController;
    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<서비스링크 소통정보가 없습니다>";

    FCodeLTC = ItsCodeManager->FLists.Find("LTC");
}
//---------------------------------------------------------------------------

void __fastcall TFRAMETrafficServiceLinkList::UpdateList()
{
    TItsSubCode *pSubCode;
    TItsTraffic *pTraf;

    CMM_ClearGridTableView(TvList);

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

		TDateTime dtBefore15MM = Now() - (double)15 / (double)1440;

        try
        {
            ItsIfscManager->FLists.Lock();
            FOR_STL(TItsIfsc*, pIfsc, ItsIfscManager->FLists)
            {
                pTraf = pIfsc->TRAF;
                if (pIfsc->REGN_YN == "Y" || ChkDispExt->Checked == true)
                {
                    String sLTC = "[" + pTraf->CMTR_GRAD_CD + "] ";
                    if (FCodeLTC)
                    {
                        pSubCode = FCodeLTC->FSubLists.Find(pTraf->CMTR_GRAD_CD);
                        if (pSubCode) sLTC = sLTC + pSubCode->CMMN_CD_KOR_NM;
                        else          sLTC = FrmLang->lblMissing->Caption;//"결측";
                    }

                    String sMissing = "";
                    if (pTraf->LASTANS == "N")
                        sMissing = FrmLang->lblMissing->Caption;//"결측";

                    if (FLastAnsTime == "")
                        FLastAnsTime = ITSUtil_StrToTime(pTraf->MPRCN_DT);

                    nRow = m_pGDC->AppendRecord();

                    m_pGDC->Values[nRow][Column00->Index]  = pIfsc->IFSC_NM;                    //'정보제공구간 명'
                    m_pGDC->Values[nRow][Column19->Index]  = pIfsc->DRCT_CD == "0" ? FrmLang->lblUp->Caption : FrmLang->lblDown->Caption;//"상행" : "하행";                    //'방향 명'
                    m_pGDC->Values[nRow][Column01->Index]  = pIfsc->IFSC_ID;                    //'정보제공구간 ID'
                    m_pGDC->Values[nRow][Column02->Index]  = pIfsc->STRT_NM;                    //'시점명';
                    m_pGDC->Values[nRow][Column03->Index]  = pIfsc->END_NM;                     //'종점명';
                    m_pGDC->Values[nRow][Column04->Index]  = ITSUtil_StrToTime(pTraf->PRCN_DT); //'가공 일시';

                    //if (pLink->LASTANS == "N")  // 최신 15분 이전 기준
                    {
                        m_pGDC->Values[nRow][Column05->Index]  = pTraf->TFVL;                   //'교통량';
                        m_pGDC->Values[nRow][Column06->Index]  = pTraf->SPED;                   //'속도';
                        m_pGDC->Values[nRow][Column07->Index]  = pTraf->OCPY_RATE;              //'점유율';
                        m_pGDC->Values[nRow][Column08->Index]  = pTraf->TRVL_HH;                //'통행시간';
                        m_pGDC->Values[nRow][Column09->Index]  = sLTC;                             //'소통등급';
                        m_pGDC->Values[nRow][Column10->Index]  = pTraf->CMTR_GRAD_CD;           //'소통등급코드';
                        m_pGDC->Values[nRow][Column11->Index]  = sMissing;                         //'결측여부';
                        //m_pGDC->Values[nRow][Column12->Index]  = pTraf->PRCN_SPED;            //'가공속도';
                        //m_pGDC->Values[nRow][Column13->Index]  = pTraf->SMTH_SPED;            //'평활화속도';
                        //m_pGDC->Values[nRow][Column14->Index]  = pTraf->ADJS_SPED;            //'보정속도';
                        m_pGDC->Values[nRow][Column15->Index]  = pTraf->PTRN_SPED;              //'패턴속도';
                        //m_pGDC->Values[nRow][Column16->Index]  = sCST;                           //'가공소스';
                        //m_pGDC->Values[nRow][Column17->Index]  = sMCS;                           //'보정소스';
                        m_pGDC->Values[nRow][Column18->Index]  = pTraf->DATA_NUM;               //'데이터개수'
                    }
                }
            }
        }
        __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;
    }
}
//---------------------------------------------------------------------------

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

int  __fastcall TFRAMETrafficServiceLinkList::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;
}
//---------------------------------------------------------------------------

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

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

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

void __fastcall TFRAMETrafficServiceLinkList::Column09CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sGrade = AViewInfo->GridRecord->DisplayTexts[Column10->Index];

        if (sGrade == "LTC1")
        {
            ACanvas->Canvas->Brush->Color = clLime;
        }
        else if (sGrade == "LTC2")
        {
            ACanvas->Canvas->Brush->Color = clYellow;
        }
        else if (sGrade == "LTC3")
        {
            ACanvas->Canvas->Brush->Color = clRed;
        }
        else
        {
            ACanvas->Canvas->Brush->Color = clGray;
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMETrafficServiceLinkList::Column11CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sMiss = AViewInfo->GridRecord->DisplayTexts[Column11->Index];

        if (sMiss == FrmLang->lblMissing->Caption)//"결측")
        {
            ACanvas->Canvas->Font->Color = clRed;
        }
    }
}
//---------------------------------------------------------------------------

