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

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

#include "FrmVmsIfscTrafF.h"

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

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxHint"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxTextEdit"
#pragma link "dxBar"
#pragma link "dxCustomHint"
#pragma link "dxRibbon"
#pragma link "dxRibbonGallery"
#pragma link "dxRibbonSkins"
#pragma link "dxRibbonStatusBar"
#pragma link "dxScreenTip"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinChooserGallery"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinsdxBarPainter"
#pragma link "dxSkinsdxRibbonPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinsForm"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "dxStatusBar"
#pragma link "dxTabbedMDI"
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxDropDownEdit"
#pragma link "cxGroupBox"
#pragma link "cxSplitter"
#pragma link "cxTimeEdit"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma link "cxGridBandedTableView"
#pragma resource "*.dfm"

TFrmVmsIfscTraf *FrmVmsIfscTraf = NULL;

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

__fastcall TFrmVmsIfscTraf::TFrmVmsIfscTraf(TComponent* Owner)
    : TForm(Owner)//TdxCustomRibbonForm(Owner)//TForm(Owner)
{
    CMM_SetFormPosition(g_AppCfg.bSaveForm);
    ITSSkin_Initialize(Application, g_AppCfg.sLang);

    DoubleBuffered = true;

    g_AppCfg.lMainWinHandle = (long)Handle;

    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    //CMM_LoadForm(g_sFormsDir, this);

    SetColorScheme(g_AppCfg.sSkinName);

    TvList->OptionsView->ColumnAutoWidth = false;

    VmsIfscManager = new TVmsIfscManager();
    FTitle = Caption;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsIfscTraf::FormCreate(TObject *Sender)
{
    Application->ShowMainForm = false;

    try {
        Application->Icon->LoadFromResourceName(((unsigned int)HInstance), "MAINICON");
    } catch(...) { ShowMessage("LoadFromResourceName failed"); }

    Application->ShowMainForm = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsIfscTraf::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsIfscTraf::FormInit()
{
    VmsIfscManager->LoadFromDb();
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmVmsIfscTraf::CommClose()
{
    try
    {
        ITSSkin_Term();
        //CMM_SaveForm(g_sFormsDir, this);
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsIfscTraf::SetColorScheme(String ASkinName)
{
    dxSkinController1->SkinName = ASkinName;
    dxSkinController1->Refresh();
}
//---------------------------------------------------------------------------

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

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

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

    RefreshData();
    CxList->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TFrmVmsIfscTraf::RefreshData()
{
    // 검색조건에 의한 링크 데이터 조회
    SelListData();
}
//---------------------------------------------------------------------------
void __fastcall TFrmVmsIfscTraf::SelListData()
{
    CMM_ClearGridTableView(TvList);
    CMM_ClearGridTableView(TvIfsc);

    // VMS IFSC 소통정보 조회
    VmsIfscManager->LoadTraffic();

    int nRow = 0;
    TcxDataController *pGDC = TvList->DataController;
    try
    {
        //TvList->BeginUpdate(lsimImmediate);
        TvList->BeginUpdate();
        try
        {
            FOR_STL(TVmsIfsc*, pObj, VmsIfscManager->FLists)
            {
                nRow = pGDC->AppendRecord();
                pGDC->Values[nRow][Col00->Index] = pObj->VMS_IFSC_ID;
                pGDC->Values[nRow][Col01->Index] = pObj->VMS_IFSC_NM;
                pGDC->Values[nRow][Col02->Index] = pObj->DSPL_STRT_NODE_NM;
                pGDC->Values[nRow][Col03->Index] = pObj->DSPL_END_NODE_NM;
                pGDC->Values[nRow][Col04->Index] = pObj->ROAD_NM;
                pGDC->Values[nRow][Col05->Index] = pObj->SPOT_NM;
                pGDC->Values[nRow][Col06->Index] = pObj->AXIS_YN;
                pGDC->Values[nRow][Col07->Index] = pObj->CNGS_SPD;
                pGDC->Values[nRow][Col08->Index] = pObj->SECT_LNGT;

                pGDC->Values[nRow][Col11->Index] = pObj->MIN[2];
                pGDC->Values[nRow][Col12->Index] = pObj->MAX[2];
                pGDC->Values[nRow][Col21->Index] = pObj->MIN[1];
                pGDC->Values[nRow][Col22->Index] = pObj->MAX[1];
                pGDC->Values[nRow][Col31->Index] = pObj->MIN[0];
                pGDC->Values[nRow][Col32->Index] = pObj->MAX[0];

                pGDC->Values[nRow][Col41->Index] = ITSUtil_FormatStr(pObj->PRCN_DT, STR_DATETIME);
                pGDC->Values[nRow][Col42->Index] = pObj->SPED;

                String sVmsGrad;
                if      (pObj->CMTR_GRAD_CD == "1") sVmsGrad = "원활";
                else if (pObj->CMTR_GRAD_CD == "2") sVmsGrad = "지체";
                else if (pObj->CMTR_GRAD_CD == "3") sVmsGrad = "정체";
                else                                sVmsGrad = "정보없음";


                pGDC->Values[nRow][Col43->Index] = sVmsGrad;
                pGDC->Values[nRow][Col44->Index] = pObj->TRVL_HH;
                pGDC->Values[nRow][ColMem->Index] = (int)pObj;
            }
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS Information Query", String(e.ClassName()), e.Message, "");
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }

}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsIfscTraf::Col06CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    int nRecordIdx = AViewInfo->GridRecord->RecordIndex;
    if (nRecordIdx < 0)
    {
        return;
    }

    try
    {
        int nItemIdx = AViewInfo->Item->Index;
    	if (VarIsNull(AViewInfo->GridRecord->DisplayTexts[nItemIdx])) return;

        String sData = AViewInfo->GridRecord->DisplayTexts[nItemIdx];
        if (sData == "Y")
        {
            ACanvas->Canvas->Brush->Color = clLime;
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsIfscTraf::Col43CustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    try
    {
        TColor tColor = ACanvas->Brush->Color;
        String sGrade = AViewInfo->GridRecord->DisplayTexts[AViewInfo->Item->Index];

        if      (sGrade.Pos("원활")) tColor = clLime;
        else if (sGrade.Pos("지체")) tColor = clYellow;
        else if (sGrade.Pos("정체")) tColor = clRed;
        else                         tColor = clGray;

        ACanvas->SetBrushColor(tColor);
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsIfscTraf::TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
	if (!ACellViewInfo) return;
	int nRow = TvList->DataController->FocusedRecordIndex;
	if( nRow <= -1 ) return;

    try
    {
        int nMemPtr = (int)(TvList->DataController->Values[nRow][ColMem->Index]);
        TVmsIfsc *pObj = (TVmsIfsc*)nMemPtr;
        DispIfscLinkTraf(pObj);


    } catch(Exception &e) {}
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsIfscTraf::DispIfscLinkTraf(TVmsIfsc* AVmsIfsc)
{
    if (!AVmsIfsc)
    {
        return;
    }

    CMM_ClearGridTableView(TvIfsc);
    int nRow = 0;
    TcxDataController *pGDC = TvIfsc->DataController;
    try
    {
        //TvIfsc->BeginUpdate(lsimImmediate);
        TvIfsc->BeginUpdate();
        try
        {
            FOR_STL(TVmsLinkIfsc*, pObj, AVmsIfsc->FLists)
            {
                nRow = pGDC->AppendRecord();
                pGDC->Values[nRow][ColI00->Index] = nRow+1;

                pGDC->Values[nRow][ColI01->Index] = pObj->IFSC_ID;
                pGDC->Values[nRow][ColI02->Index] = pObj->IFSC_NM;
                pGDC->Values[nRow][ColI03->Index] = pObj->STRT_NM;
                pGDC->Values[nRow][ColI04->Index] = pObj->END_NM;
                pGDC->Values[nRow][ColI05->Index] = pObj->SECT_LNGT;

                TIfscTraf* pTraf = VmsIfscManager->FIfscLists.Find(pObj->IFSC_ID);
                if (pTraf != NULL) {
                    pGDC->Values[nRow][ColI06->Index] = ITSUtil_FormatStr(pTraf->PRCN_DT, STR_DATETIME);

                    String sVmsGrad;
                    if      (pTraf->CMTR_GRAD_CD == "LTC1") sVmsGrad = "원활";
                    else if (pTraf->CMTR_GRAD_CD == "LTC2") sVmsGrad = "지체";
                    else if (pTraf->CMTR_GRAD_CD == "LTC3") sVmsGrad = "정체";
                    else                                   sVmsGrad = "정보없음";
                    pGDC->Values[nRow][ColI07->Index] = sVmsGrad;
                    pGDC->Values[nRow][ColI08->Index] = pTraf->SPED;
                    pGDC->Values[nRow][ColI09->Index] = pTraf->TRVL_HH;
                }
                else {
                    pGDC->Values[nRow][ColI06->Index] = "1970-01-01 00:00:00";
                    pGDC->Values[nRow][ColI07->Index] = "정보없음";
                    pGDC->Values[nRow][ColI08->Index] = 0;
                    pGDC->Values[nRow][ColI09->Index] = 0;
                }
            }
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS Information Query", String(e.ClassName()), e.Message, "");
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        TvIfsc->EndUpdate();
        TvIfsc->DataController->GotoFirst();
        TvIfsc->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

