/****************************************************************************
 * @source      :
 * @description :
 ****************************************************************************
 * DATE         AUTHOR    DESCRIPTION
 * --------------------------------------------------------------------------
 * 2016/07/10   shjung    최초작성
 *
 ****************************************************************************/
//---------------------------------------------------------------------------

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

#include "VMSM630MF.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"

TVMSM630M *VMSM630M = NULL;
//---------------------------------------------------------------------------

__fastcall TVMSM630M::TVMSM630M(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 = "";
}
//---------------------------------------------------------------------------

/*
 * 닫기버튼 이나 x버튼 클릭시 공통으로 처리하는 로직이들어간다..
 * Form과 DataModule class를 delete시킨다.
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TVMSM630M::CommClose()
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

/*
 * Form을 보여줄때 호출되는 event 메서드이다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TVMSM630M::FormShow(TObject *Sender)
{
    Refresh();

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

/*
 * form 초기화
 *
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TVMSM630M::FormInit()
{
    FNewDb = false;
}
//---------------------------------------------------------------------------

/*
 * Form이 Show되고 난 후 최초 1회 수행되는 타이머 이벤트
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TVMSM630M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;

    LoadAxisInfo();

    CMM_ExpandCollapseChk(TvAxis, true);

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

/*
 * Close 버튼 클릭 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TVMSM630M::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

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

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

void __fastcall TVMSM630M::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 TVMSM630M::LoadAxisInfo()
{
    FormInit();

    CMM_ClearGridTableView(TvAxis);

    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT C.VMS_CTLR_NMBR, C.VMS_CTLR_ID, C.VMS_NM,     \r\n"
           "       A.VMS_IFSC_ID, A.VMS_IFSC_NM,                 \r\n"
           "       A.DSPL_STRT_NODE_NM, A.DSPL_END_NODE_NM,      \r\n"
           "       A.AXIS_YN                                     \r\n"
           "  FROM TB_VMS_IFSC      A,                           \r\n"
           "       TB_VMS_RLTN_IFSC B,                           \r\n"
           "       (SELECT VMS_CTLR_NMBR,                        \r\n"
           "               VMS_CTLR_ID,                          \r\n"
           "               VMS_NM                                \r\n"
           "          FROM TB_VMS_CTLR                           \r\n"
           "         WHERE VMS_TYPE_CD = 'VMP1') C               \r\n"
           " WHERE A.USE_YN        = 'Y'                         \r\n"
           "   AND A.VMS_IFSC_ID   = B.VMS_IFSC_ID               \r\n"
           "   AND B.VMS_CTLR_NMBR = C.VMS_CTLR_NMBR             \r\n";

    try
    {
        int nRow;
        TcxDataController *pGDC = TvAxis->DataController;
        TvAxis->BeginUpdate();
        try
        {
            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("VMS_CTLR_ID")->AsString;
                pGDC->Values[nRow][Col01->Index] = pADO->FieldByName("VMS_IFSC_ID")->AsString;
                pGDC->Values[nRow][Col02->Index] = pADO->FieldByName("VMS_IFSC_NM")->AsString;
                pGDC->Values[nRow][Col03->Index] = pADO->FieldByName("DSPL_STRT_NODE_NM")->AsString;
                pGDC->Values[nRow][Col04->Index] = pADO->FieldByName("DSPL_END_NODE_NM")->AsString;
                pGDC->Values[nRow][Col05->Index] = pADO->FieldByName("AXIS_YN")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSM630M::LoadAxisInfo", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVMSM630M::LoadAxisInfo", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvAxis->EndUpdate();
    }
}
//---------------------------------------------------------------------------

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

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

void __fastcall TVMSM630M::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();
}
//---------------------------------------------------------------------------

