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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "WindowMsgF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSIfscF.h"
#include "CDSRoadF.h"
#include "CDSTrafficGradeF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "FrmLinkGradeSetF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxDropDownEdit"
#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 "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"
TFrmLinkGradeSet *FrmLinkGradeSet = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmLinkGradeSet::TFrmLinkGradeSet(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FTitle = Caption;//"구간 소통등급 변경";
    Caption = "  " + FTitle;//"  구간 소통등급 변경";

    m_sLinkId    = "";
    m_nLinkLevel = 0;

    CbTrfGrade->Properties->OnChange = NULL;
    FCodeLTC = ItsCodeManager->FLists.Find("LTC");	//소통등급
    try
    {
        FOR_STL(TItsTrafficGrade *, pTrfGrade, ItsTrafficGradeManager->FLists)
        {
            CbTrfGrade->Properties->Items->Add("[" + pTrfGrade->SECT_GRAD_CD + "] " + pTrfGrade->SECT_GRAD_NM);
        }
    }
    __finally
    {
    }
    CbTrfGrade->ItemIndex = 0;
    CbTrfGrade->Properties->OnChange = CbTrfGradePropertiesChange;
    DisplayTrafficGrade();
}
//---------------------------------------------------------------------------
void __fastcall TFrmLinkGradeSet::FormClose(TObject *Sender, TCloseAction &Action)
{
    CMM_SaveForm(g_sFormsDir, this);
    FrmLinkGradeSet = NULL;
    Action = caFree;
}
//---------------------------------------------------------------------------
void __fastcall TFrmLinkGradeSet::FormDestroy(TObject *Sender)
{
    FrmLinkGradeSet = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TFrmLinkGradeSet::SetInfo(String sLinkId, int nLinkLevel)
{
    m_sLinkId    = sLinkId;
    m_nLinkLevel = nLinkLevel;

    EdId->Text     = sLinkId;
    EdLevel->Text  = String(m_nLinkLevel);
    EdLength->Text = FormatFloat("##,##0", 0);

    EdStId->Text = "";
    EdEdId->Text = "";
    EdStName->Text = FrmLang->lblNoName->Caption;//"명칭없음";
    EdEdName->Text = FrmLang->lblNoName->Caption;//"명칭없음";

    switch(m_nLinkLevel)
    {
    case 1: //레벨1 링크(표준링크)
        {
            TItsNode *pNode;
            TItsLink *pLink;

            pLink = ItsLinkManager->FLists.Find(m_sLinkId);
            if (!pLink) return;

            EdStId->Text = pLink->F_NODE_ID;
            EdEdId->Text = pLink->T_NODE_ID;
            EdLength->Text = FormatFloat("##,##0", StrToFloat(pLink->LINK_LENG));

            pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
            if (pNode) EdStName->Text = pNode->NODE_NAME;
            pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
            if (pNode) EdEdName->Text = pNode->NODE_NAME;

            TItsTrafficGrade *pGrade = ItsTrafficGradeManager->FLists.Find(pLink->SECT_GRAD_CD);
            if (pGrade) CbTrfGrade->Text = "[" + pLink->SECT_GRAD_CD + "] " + pGrade->SECT_GRAD_NM;
            else        CbTrfGrade->Text = "";
        }
        break;
    case 2: //레벨2 링크(서비스링크)
        {
            TItsNode *pNode;
            TItsIfsc *pLink;

            pLink = ItsIfscManager->FLists.Find(m_sLinkId);
            if (!pLink) return;

            EdStId->Text = pLink->F_NODE_ID;
            EdEdId->Text = pLink->T_NODE_ID;
            EdLength->Text = FormatFloat("##,##0", StrToFloat(pLink->SECT_LNGT));

            pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
            if (pNode) EdStName->Text = pNode->NODE_NAME;
            pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
            if (pNode) EdEdName->Text = pNode->NODE_NAME;

            TItsTrafficGrade *pGrade = ItsTrafficGradeManager->FLists.Find(pLink->SECT_GRAD_CD);
            if (pGrade) CbTrfGrade->Text = "[" + pLink->SECT_GRAD_CD + "] " + pGrade->SECT_GRAD_NM;
            else        CbTrfGrade->Text = "";
        }
        break;
    case 3: //도로
        {
            TItsNode *pNode;
            TItsRoad *pLink;

            pLink = ItsRoadManager->FLists.Find(m_sLinkId);
            if (!pLink) return;

            EdStId->Text = pLink->F_NODE_ID;
            EdEdId->Text = pLink->T_NODE_ID;
            EdLength->Text = FormatFloat("##,##0", StrToFloat(pLink->SECT_LNGT));

            pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
            if (pNode) EdStName->Text = pNode->NODE_NAME;
            pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
            if (pNode) EdEdName->Text = pNode->NODE_NAME;

            TItsTrafficGrade *pGrade = ItsTrafficGradeManager->FLists.Find(pLink->SECT_GRAD_CD);
            if (pGrade) CbTrfGrade->Text = "[" + pLink->SECT_GRAD_CD + "] " + pGrade->SECT_GRAD_NM;
            else        CbTrfGrade->Text = "";
        }
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmLinkGradeSet::CbTrfGradePropertiesChange(TObject *Sender)
{
    DisplayTrafficGrade();
}
//---------------------------------------------------------------------------

void __fastcall TFrmLinkGradeSet::DisplayTrafficGrade()
{
    CMM_ClearGridTableView(TvList);
    try
    {
        TvList->BeginUpdate();
        TcxDataController *pGDC = TvList->DataController;

        String sGradeCode = ITSUtil_GetCode(CbTrfGrade->Text);

        TItsTrafficGrade *pGrade = ItsTrafficGradeManager->FLists.Find(sGradeCode);
        if (!pGrade) return;

        int nRow;
        try
        {
            FOR_STL(TItsTrafficSubGrade *, pTrfSubGrade, pGrade->FSubLists)
            {
                nRow = pGDC->AppendRecord();

                String sLTC = pTrfSubGrade->CMTR_GRAD_CD;
                TItsSubCode *pSubCode = FCodeLTC->FSubLists.Find(pTrfSubGrade->CMTR_GRAD_CD);
                if (pSubCode) sLTC = pSubCode->CMMN_CD_KOR_NM;

                pGDC->Values[nRow][Column01->Index] = pTrfSubGrade->CMTR_GRAD_CD;   //코드
                pGDC->Values[nRow][Column02->Index] = sLTC;                         //명칭
                pGDC->Values[nRow][Column03->Index] = pTrfSubGrade->LWST_TRVL_SPED; //속도(Low)
                pGDC->Values[nRow][Column04->Index] = pTrfSubGrade->HGHS_TRVL_SPED; //속도(High)
            }
        }
        __finally
        {
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmLinkGradeSet::BtnApplyClick(TObject *Sender)
{
    if (Application->MessageBox(lblCnfm->Caption.c_str(),//L"소통등급의 변경은 소통정보에 큰 영향을 미칩니다.\r\n계속하시겠습니까?\r\n\r\n[주의 - 반드시 정보를 확인후 진행하세요!!!]",
                                FTitle.c_str(),
                                MB_YESNO|MB_ICONWARNING|MB_ICONQUESTION) != IDYES)
    {
        return;
    }

    String sQry;
    TADOQuery *pADO = NULL;
    String sGradeCode = ITSUtil_GetCode(CbTrfGrade->Text);

    try
    {
        pADO = new TADOQuery(NULL);
        pADO->Close();
        pADO->Connection = ITSDb_GetConnection();

        try
        {
            switch(m_nLinkLevel)
            {
            case 1: //레벨1 링크(표준링크)
                sQry = "UPDATE TB_LINK SET SECT_GRAD_CD = :p01 WHERE LINK_ID = :p02";
                break;
            case 2: //레벨2 링크(서비스링크)
                sQry = "UPDATE TB_IFSC SET SECT_GRAD_CD = :p01 WHERE IFSC_ID = :p02";
                break;
            case 3: //도로
                sQry = "UPDATE TB_ROAD SET SECT_GRAD_CD = :p01 WHERE ROAD_ID = :p02";
                break;
            default: return;
            }

            ITSDb_GetConnection()->BeginTrans();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", sGradeCode);
            ITSDb_SQLBind(pADO, "p02", m_sLinkId);
            ITSDb_SQLExec(pADO);

            ITSDb_GetConnection()->CommitTrans();

            switch(m_nLinkLevel)
            {
            case 1: //레벨1 링크(표준링크)
                {
                    TItsLink *pLink = ItsLinkManager->FLists.Find(m_sLinkId);
                    if (!pLink) return;
                    pLink->SECT_GRAD_CD = sGradeCode;
                }
                break;
            case 2: //레벨2 링크(서비스링크)
                {
                    TItsIfsc *pIfsc = ItsIfscManager->FLists.Find(m_sLinkId);
                    if (!pIfsc) return;
                    pIfsc->SECT_GRAD_CD = sGradeCode;
                }
                break;
            case 3: //도로
                {
                    TItsRoad *pRoad = ItsRoadManager->FLists.Find(m_sLinkId);
                    if (!pRoad) return;
                    pRoad->SECT_GRAD_CD = sGradeCode;
                }
                break;
            }
            Application->MessageBox(lblOk->Caption.c_str(),//L"소통정보 등급구분을 적용 하였습니다.",
                                    FTitle.c_str(),
                                    MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

            POST_MSG(Application->MainForm->Handle, WM_DATABASE_REFRESH, WP_MSG_05, 0);
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ITSDb_GetConnection()->RollbackTrans();
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            ITSDb_GetConnection()->RollbackTrans();
            throw Exception(FrmLang->lblDbErr->Caption);//"[구간 소통등급 변경] 알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
}
//---------------------------------------------------------------------------

