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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "CDSNodeF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "IDB0020MF.h"
#include "IDB00201F.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 "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 "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"

TIDB0020M *IDB0020M = NULL;
//---------------------------------------------------------------------------
__fastcall TIDB0020M::TIDB0020M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0020M::CommClose()
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0020M::FormInit()
{
    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = lblNoInfo->Caption;//"<링크 정보가 없습니다>";

    FCodeRDU = ItsCodeManager->FLists.Find("RDU");	// 도로사용여부
    FCodeRDR = ItsCodeManager->FLists.Find("RDR");	// 도로등급
    FCodeRDT = ItsCodeManager->FLists.Find("RDT");	// 도로유형
    FCodeMLT = ItsCodeManager->FLists.Find("MLT");	// 중용구간
    FCodeCTR = ItsCodeManager->FLists.Find("CTR");	// 연결로
    FCodePLV = ItsCodeManager->FLists.Find("PLV");	// 통행제한차량
}
//---------------------------------------------------------------------------

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

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

void __fastcall TIDB0020M::RefreshData()
{
    SelListData();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0020M::SelListData()
{
    TItsNode    *pNode;
    TItsSubCode *pSubCode;

    CMM_ClearGridTableView(TvList);

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

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

                    m_pGDC->Values[nRow][Column00->Index] = pLink->LINK_ID;
                    m_pGDC->Values[nRow][Column01->Index] = pLink->LINK_ID;

                    String sFNodeName = "[" + pLink->F_NODE_ID + "] ";
                    pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
                    if (pNode) sFNodeName = sFNodeName + pNode->NODE_NAME;

                    String sTNodeName = "[" + pLink->T_NODE_ID + "] ";
                    pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
                    if (pNode) sTNodeName = sTNodeName + pNode->NODE_NAME;

                    m_pGDC->Values[nRow][Column02->Index]  = sFNodeName;              //'시작노드ID';
                    m_pGDC->Values[nRow][Column03->Index]  = sTNodeName;              //'종료노드ID';

                    String sRDU = "[" + pLink->ROAD_USE + "] ";
                    if (FCodeRDU)
                    {
                        pSubCode = FCodeRDU->FSubLists.Find(pLink->ROAD_USE);
                        if (pSubCode) sRDU = sRDU + pSubCode->CMMN_CD_KOR_NM;
                    }
                    m_pGDC->Values[nRow][Column04->Index]  = sRDU;                    //'도로사용여부';

                    m_pGDC->Values[nRow][Column05->Index]  = pLink->LANES;            //'차로수';

                    String sRDR = "[" + pLink->ROAD_RANK + "] ";
                    if (FCodeRDR)
                    {
                        pSubCode = FCodeRDR->FSubLists.Find(pLink->ROAD_RANK);
                        if (pSubCode) sRDR = sRDR + pSubCode->CMMN_CD_KOR_NM;
                    }
                    m_pGDC->Values[nRow][Column06->Index]  = sRDR;                   //'도로등급';

                    String sRDT = "[" + pLink->ROAD_TYPE + "] ";
                    if (FCodeRDT)
                    {
                        pSubCode = FCodeRDT->FSubLists.Find(pLink->ROAD_TYPE);
                        if (pSubCode) sRDT = sRDT + pSubCode->CMMN_CD_KOR_NM;
                    }
                    m_pGDC->Values[nRow][Column07->Index]  = sRDT;                    //'도로유형';

                    m_pGDC->Values[nRow][Column08->Index]  = pLink->ROAD_NO;          //'도로번호';
                    m_pGDC->Values[nRow][Column09->Index]  = pLink->ROAD_NAME;        //'도로명';

                    String sMLT = "[" + pLink->MULTI_LINK + "] ";
                    if (FCodeMLT)
                    {
                        pSubCode = FCodeMLT->FSubLists.Find(pLink->MULTI_LINK);
                        if (pSubCode) sMLT = sMLT + pSubCode->CMMN_CD_KOR_NM;
                    }
                    m_pGDC->Values[nRow][Column10->Index]  = sMLT;                    //'중용구간여부';

                    String sCTR = "[" + pLink->LINK_CONNECT + "] ";
                    if (FCodeCTR)
                    {
                        pSubCode = FCodeCTR->FSubLists.Find(pLink->LINK_CONNECT);
                        if (pSubCode) sCTR = sCTR + pSubCode->CMMN_CD_KOR_NM;
                    }
                    m_pGDC->Values[nRow][Column11->Index]  = sCTR;                    //'연결로코드';

                    m_pGDC->Values[nRow][Column12->Index]  = pLink->MAX_SPD;          //'최고제한속도';

                    String sPLV = "[" + pLink->REST_VEH + "] ";
                    if (FCodePLV)
                    {
                        pSubCode = FCodePLV->FSubLists.Find(pLink->REST_VEH);
                        if (pSubCode) sPLV = sPLV + pSubCode->CMMN_CD_KOR_NM;
                    }
                    m_pGDC->Values[nRow][Column13->Index]  = sPLV;                    //'통행제한차량';

                    m_pGDC->Values[nRow][Column14->Index]  = pLink->REST_W;           //'통과제한하중';
                    m_pGDC->Values[nRow][Column15->Index]  = pLink->REST_H;           //'통과제한높이';

                    m_pGDC->Values[nRow][Column16->Index]  = pLink->LINK_LENG;        //'연장';
                    m_pGDC->Values[nRow][Column17->Index]  = pLink->RMRK;             //'비고';

                    //m_pGDC->Values[nRow][Column02->Index]  = pLink->DEL_YN;             //'삭제 여부';
                    //m_pGDC->Values[nRow][Column02->Index]  = pLink->DFLT_SPED;          //'디폴트 속도';
                    //m_pGDC->Values[nRow][Column02->Index]  = pLink->DFLT_SPED_USE_YN;   //'디폴트 속도 사용 여부';
                    //m_pGDC->Values[nRow][Column02->Index]  = pLink->SECT_GRAD_CD;       //'구간 등급 코드';
                    //m_pGDC->Values[nRow][Column02->Index]  = pLink->AREA_CD;            //'지역 코드';

                    m_pGDC->Values[nRow][Column18->Index] = (int)pLink;
                }
            }
        }
        __finally
        {
            ItsLinkManager->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 TIDB0020M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

    RefreshData();
    CxList->SetFocus();
}
//---------------------------------------------------------------------------

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

void __fastcall TIDB0020M::ChkExpandClick(TObject *Sender)
{
    CMM_ExpandCollapseChk(TvList, ChkExpand->Checked);
}
//---------------------------------------------------------------------------

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

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

void __fastcall TIDB0020M::BtnCancelClick(TObject *Sender)
{
    //
}
//---------------------------------------------------------------------------

void __fastcall TIDB0020M::BtnInsertClick(TObject *Sender)
{
    //
}
//---------------------------------------------------------------------------

void __fastcall TIDB0020M::BtnEditClick(TObject *Sender)
{
    if (TvList->ViewData->RecordCount <= 0) return;
	int nRow = m_pGDC->FocusedRecordIndex;
	if( nRow < 0 )
		return;

    String sId = m_pGDC->Values[nRow][Column01->Index]; //ID
    String sNm = m_pGDC->Values[nRow][Column09->Index]; //도로명

    try {
        TIDB00201 *pDlg = new TIDB00201(this);
        pDlg->EdId->Text = sId;
        pDlg->EdNm->Text = sNm;
        pDlg->ShowModal();
        if (pDlg->FUpdated)
        {
            sNm = pDlg->EdNm->Text.Trim();
            m_pGDC->Values[nRow][Column09->Index] = sNm;
            int nMemPtr = m_pGDC->Values[nRow][Column18->Index];
            TItsLink *pObj = (TItsLink*)nMemPtr;
            pObj->ROAD_NAME = sNm;
        }
        delete pDlg;
    } catch(...) { }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0020M::BtnDeleteClick(TObject *Sender)
{
    //
}
//---------------------------------------------------------------------------

void __fastcall TIDB0020M::DisplayListData()
{
}
//---------------------------------------------------------------------------

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

void __fastcall TIDB0020M::BtnApplyClick(TObject *Sender)
{
    //
}
//---------------------------------------------------------------------------

void __fastcall TIDB0020M::BtnBestFitClick(TObject *Sender)
{
    try
    {
        TvList->BeginUpdate();
		TvList->ApplyBestFit(NULL, false, false);
	}
	__finally
	{
        TvList->EndUpdate();
	}
}
//---------------------------------------------------------------------------

void __fastcall TIDB0020M::ChkAutoWidthPropertiesChange(TObject *Sender)
{
    try
    {
        TvList->BeginUpdate();
		TvList->OptionsView->ColumnAutoWidth = ChkAutoWidth->Checked;
        BtnBestFit->Visible = !ChkAutoWidth->Checked;
	}
	__finally
	{
        TvList->EndUpdate();
	}
}
//---------------------------------------------------------------------------

void __fastcall TIDB0020M::TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    DisplayListData();
    BtnEditClick((TObject*)BtnEdit);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0020M::MnuMovePosClick(TObject *Sender)
{
    TMenuItem *pMenu = (TMenuItem *)Sender;

    if (TvList->ViewData->RecordCount <= 0) return;
	int nIndex = m_pGDC->FocusedRecordIndex;
	if( nIndex < 0 )
		return;

    String sLinkId = VarToStr(m_pGDC->Values[nIndex][Column01->Index]);

    if (pMenu->Tag == 0)
    {
        //메인지도
    }
    else
    {
        //새지도
    }
}
//---------------------------------------------------------------------------

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

