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

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

#include "IDB0010MF.h"
#include "IDB00101F.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 "cxLabel"
#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 resource "*.dfm"

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

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

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

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

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

    FCodeTurnP = ItsCodeManager->FLists.Find("TLE"); //회전제한
    FCodeNodeT = ItsCodeManager->FLists.Find("NDT"); //노드유형
}
//---------------------------------------------------------------------------

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

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

void __fastcall TIDB0010M::SelListData()
{
    TItsSubCode *pSubCode;

    CMM_ClearGridTableView(TvList);

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

        try
        {
            ItsNodeManager->FLists.Lock();
            FOR_STL(TItsNode *, pNode, ItsNodeManager->FLists)
            {
                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][Column00->Index] = pNode->NODE_ID;   //노드ID
                m_pGDC->Values[nRow][Column01->Index] = pNode->NODE_ID;   //노드ID

                String sNodeType = "[" + pNode->NODE_TYPE + "] ";
                if (FCodeNodeT)
                {
                    pSubCode = FCodeNodeT->FSubLists.Find(pNode->NODE_TYPE);
                    if (pSubCode) sNodeType = sNodeType + pSubCode->CMMN_CD_KOR_NM;
                }
                m_pGDC->Values[nRow][Column02->Index] = sNodeType;        //노드유형

                m_pGDC->Values[nRow][Column03->Index] = pNode->NODE_NAME; //교차로명칭

                String sTurnP = "[" + pNode->TURN_P + "] ";
                if (FCodeTurnP)
                {
                    pSubCode = FCodeTurnP->FSubLists.Find(pNode->TURN_P);
                    if (pSubCode) sTurnP = sTurnP + pSubCode->CMMN_CD_KOR_NM;
                }
                m_pGDC->Values[nRow][Column04->Index] = sTurnP;             //회전제한유무

                m_pGDC->Values[nRow][Column05->Index] = pNode->RMRK;      //비고
                m_pGDC->Values[nRow][Column06->Index] = String(pNode->X_CRDN);    //X 좌표
                m_pGDC->Values[nRow][Column07->Index] = String(pNode->Y_CRDN);    //Y 좌표
                m_pGDC->Values[nRow][Column08->Index] = (int)pNode;
            }
        }
        __finally
        {
            ItsNodeManager->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 TIDB0010M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

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

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

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

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

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

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

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

    String sNodeId = m_pGDC->Values[nRow][Column01->Index];   //노드ID
    String sNodeNm = m_pGDC->Values[nRow][Column03->Index]; //교차로명칭
    //String sRemark = m_pGDC->Values[nRow][Column05->Index];      //비고
    //String sPosX = VarToStrDef(m_pGDC->Values[nRow][Column06->Index], "0");
    //String sPosY = VarToStrDef(m_pGDC->Values[nRow][Column07->Index], "0");

    try {
        TIDB00101 *pDlg = new TIDB00101(this);
        pDlg->EdId->Text = sNodeId;
        pDlg->EdNm->Text = sNodeNm;
        pDlg->ShowModal();
        if (pDlg->FUpdated)
        {
            sNodeNm = pDlg->EdNm->Text.Trim();
            m_pGDC->Values[nRow][Column03->Index] = sNodeNm;
            int nMemPtr = m_pGDC->Values[nRow][Column08->Index];
            TItsNode *pObj = (TItsNode*)nMemPtr;
            pObj->NODE_NAME = sNodeNm;
        }
        delete pDlg;
    } catch(...) { }
}
//---------------------------------------------------------------------------

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

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

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

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

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

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

void __fastcall TIDB0010M::DisplayListData()
{
    if (TvList->ViewData->RecordCount <= 0) return;
	int nIndex = m_pGDC->FocusedRecordIndex;
	if( nIndex < 0 )
		return;

    String sPosX = VarToStrDef(m_pGDC->Values[nIndex][Column06->Index], "0");
    String sPosY = VarToStrDef(m_pGDC->Values[nIndex][Column07->Index], "0");

    //ShowMessage(sPosX + "  " + sPosY);    
#if 0
	double dTempX = strTempX.ToDouble();
	unsigned int nTempX = dTempX * 10000000;

	double dTempY = cxGrid_DataTV->DataController->Values[nIndex][enGridPosition_Y_CRDN];
	unsigned int nTempY = dTempY * 100000000;
#endif
}
//---------------------------------------------------------------------------

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

