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

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

#include "VMSM550MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxSplitter"
#pragma link "cxTextEdit"
#pragma link "cxTimeEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#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 "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "cxRadioGroup"
#pragma link "cxClasses"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxDBData"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridDBTableView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma link "cxCheckBox"
#pragma link "cxImage"
#pragma link "cxButtonEdit"
#pragma link "cxBlobEdit"
#pragma resource "*.dfm"

TVMSM550M *VMSM550M = NULL;
//---------------------------------------------------------------------------

__fastcall TVMSM550M::TVMSM550M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FUpdate= false;
    FTitle = Caption;//"VMS 제공구간 선택";

    EditMode = false;
    m_bSelected = false;

	EdNmbr->Text = "";
}
//---------------------------------------------------------------------------

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

void __fastcall TVMSM550M::FormShow(TObject *Sender)
{
    Refresh();

    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMSM550M::FormInit()
{
    FNewDb = false;
}
//---------------------------------------------------------------------------

void __fastcall TVMSM550M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;

    LoadPrltInfo();

    CMM_ExpandCollapseChk(TvAxis, true);

    if (EdNmbr->Text != "")
    {
        CMM_SetGridRow(CxAxis, TvAxis, EdNmbr->Text, Col01->Index);
    }
}
//---------------------------------------------------------------------------

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

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

void __fastcall TVMSM550M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    //
}
//---------------------------------------------------------------------------

void __fastcall TVMSM550M::DisplayInfo()
{
    FormInit();

	int nRow = TvAxis->DataController->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

	TcxGridDataController *pDc = TvAxis->DataController;

    EdNmbr->Text = VarToStr(pDc->Values[nRow][Col01->Index]);
}
//---------------------------------------------------------------------------

void __fastcall TVMSM550M::LoadPrltInfo()
{
    FormInit();

    CMM_ClearGridTableView(TvAxis);

    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT *                    \r\n"
           "  FROM TB_ATMP_PLTN_STTN    \r\n";
           " WHERE DEL_YN = 'N'         \r\n";

    try
    {
        int nRow;
        TcxDataController *pGDC = TvAxis->DataController;
        TvAxis->BeginUpdate();
        try
        {
#if 0
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                nRow = pGDC->AppendRecord();
                pGDC->Values[nRow][Col00->Index] = pADO->FieldByName("ATMP_STTN_NMBR")->AsString;
                pGDC->Values[nRow][Col01->Index] = pADO->FieldByName("ATMP_STTN_NMBR")->AsString;
                pGDC->Values[nRow][Col02->Index] = pADO->FieldByName("ATMP_STTN_NM")->AsString;
                pGDC->Values[nRow][Col03->Index] = pADO->FieldByName("VMS_DISP_NM")->AsString;
            }
#else
            FOR_STL(TCDSAtmp*, pObj, CDSAtmpManager->FLists)
            {
                if (pObj->DEL_YN != "N") continue;

                nRow = pGDC->AppendRecord();
                pGDC->Values[nRow][Col00->Index] = pObj->ATMP_STTN_NMBR;
                pGDC->Values[nRow][Col01->Index] = pObj->ATMP_STTN_NMBR;
                pGDC->Values[nRow][Col02->Index] = pObj->ATMP_STTN_NM;
                pGDC->Values[nRow][Col03->Index] = pObj->VMS_DISP_NM;
            }
#endif
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSM550M::LoadPrltInfo", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVMSM550M::LoadPrltInfo", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvAxis->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSM550M::TvAxisCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift,
          bool &AHandled)
{
	if (!ACellViewInfo) return;

    DisplayInfo();
    Sleep(200);
    BtnSelImageClick((TObject*)BtnSelImage);
}
//---------------------------------------------------------------------------

void __fastcall TVMSM550M::BtnSelImageClick(TObject *Sender)
{
    if (EdNmbr->Text == "" || EdNmbr->Text == "0")
    {
        Application->MessageBox(lblText1->Caption.c_str(),//L"VMS 제공구간 선택\r\nVMS 제공구간을 목록에서 선택하세요.",
                                FTitle.c_str(),
                                MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }
    m_bSelected = true;
    Close();
}
//---------------------------------------------------------------------------

