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

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

#include "VMSVIEWMF.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 link "cxColorComboBox"
#pragma link "cxCalc"
#pragma link "FRAME_VmsListF"
#pragma resource "*.dfm"

//TVMSVIEWM *VMSVIEWM = NULL;
//---------------------------------------------------------------------------

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

    FUpdate= false;
    FTitle = Caption;//"VMS 모니터링 화면 관리";

    EditMode = false;

    FRAMEVmsList1->PnlTop->Visible = false;
    FRAMEVmsList1->PnlBottom->Visible = false;
    FRAMEVmsList1->ColumnSel->Visible = false;
    FRAMEVmsList1->Column04->Visible = true;

    FRAMEVmsList1->TvList->OptionsView->ColumnAutoWidth = true;
    FRAMEVmsList1->TvList->OptionsSelection->MultiSelect = true;
    FRAMEVmsList1->TvList->OptionsSelection->CellSelect = false;
}
//---------------------------------------------------------------------------

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

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

void __fastcall TVMSVIEWM::FormInit()
{
    //TvList->OptionsView->NoDataToDisplayInfoText = "<등록되어 있는 정보가 없습니다>";
    //TvVms->OptionsView->NoDataToDisplayInfoText = "<등록되어 있는 정보가 없습니다>";

    FRAMEVmsList1->UpdateList();

    FNewDb = false;
}
//---------------------------------------------------------------------------

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

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

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

void __fastcall TVMSVIEWM::BtnEditClick(TObject *Sender)
{
    if (BtnEdit->Caption == FrmLang->lblEdit->Caption)//"편집")
    {
        ChangeEditMode(0);
        FNewDb = false;
    }
    else
    {
        String sName = EdName->Text;
        ChangeEditMode(1);

        if (CMM_FindGridData(TvList->DataController, sName, Col02->Index) >= 0)
        {
            CMM_SetGridRow(CxList, TvList, sName, Col02->Index);
            DisplayInfo();
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::ChangeEditMode(int AEdit)
{
    bool bEnabled = false;
    switch(AEdit)
    {
    case 0: //편집버튼클릭
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
            EditMode = true;
        }
        break;
    case 1: //편집[취소]버튼클릭
        {
            FormClear(); //화면을 모두 지운다.
            BtnEdit->Enabled = false;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = false;

            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
            //bEnabled = true;
            EditMode = false;
        }
        break;
    case 2: //추가
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
            EditMode = true;
        }
        break;
    case 3: //수정모드
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = true;
            BtnSave->Enabled = false;

            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
            bEnabled = true;
            EditMode = false;
        }
        break;
    }

    if (AEdit == 0 || AEdit == 2)
    {
        PnlCmd->Enabled = true;
    }
    else
    {
        PnlCmd->Enabled = false;
    }
}
//---------------------------------------------------------------------------

int __fastcall TVMSVIEWM::CheckDupFormName(String ANewName, String AOldName)
{
    int nDupCnt = 0;

    try
    {
        TvList->BeginUpdate();

        TcxGridDataController *pGDC = TvList->DataController;
        int nRows = pGDC->RecordCount;
        for (int ii = 0; ii < nRows; ii++)
        {
            String sFormName = VarToStr(pGDC->Values[ii][Col02->Index]);
            if (sFormName == AOldName)
            {
                continue;
            }
            if (sFormName == ANewName)
            {
                nDupCnt++;
            }
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
    return nDupCnt;
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::BtnSaveClick(TObject *Sender)
{
	if (EdName->Text.Trim().IsEmpty())
	{
        Application->MessageBox(lblText1->Caption.c_str(),//L"VMS 모니터링 화면 저장\r\nVMS 모니터링 화면명칭을 입력하세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}
    if (!FNewDb)
    {
        if (EdOrgName->Text.Trim().IsEmpty())
        {
            Application->MessageBox(lblText2->Caption.c_str(),//L"VMS 모니터링 화면 저장\r\nVMS 모니터링 화면을 목록에서 선택하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }
    }

    if (TvVms->DataController->RecordCount == 0)
    {
        Application->MessageBox(lblText3->Caption.c_str(),//L"VMS 모니터링 화면 저장\r\n모니터링 대상 VMS를 추가해 주세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

    String sOldName = EdOrgName->Text;
    String sNewName = EdName->Text.Trim();

    int nDupCnt;
    bool bResult = false;
    if (FNewDb)
    {
        nDupCnt = CheckDupFormName(sNewName, "");
    }
    else
    {
        nDupCnt = CheckDupFormName(sNewName, sOldName);
    }
    if (nDupCnt >= 1)
    {
        Application->MessageBox(lblText4->Caption.c_str(),//L"VMS 모니터링 화면 저장\r\n이미 사용중인 모니터링 화면명칭 입니다.\r\n사용하지 않는 명칭을 입력하세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

    bResult = DelVmsMonitoring(sOldName);
    if (!bResult)
    {
        return;
    }

    String sQry;
    TADOQuery *pADO = NULL;

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

    sQry = "INSERT INTO TB_VMS_MONITORING (     \r\n"
           "                MONITORING_TYPE,    \r\n"
           "                MONITORING_NM,      \r\n"
           "                MONITORING_SEQ,     \r\n"
           "                VMS_CTLR_NMBR,      \r\n"
           "                VIEW_MODE           \r\n"
           "                )                   \r\n"
           "  VALUES (                          \r\n"
           "                :p01,               \r\n"
           "                :p02,               \r\n"
           "                :p03,               \r\n"
           "                :p04,               \r\n"
           "                0                   \r\n"
           "                )                   \r\n";

            ITSDb_SQLText(pADO, sQry);

            ITSDb_GetConnection()->BeginTrans();

            TcxGridDataController *pGDC = TvVms->DataController;
            int nRows = pGDC->RecordCount;
            for (int ii = 0; ii < nRows; ii++)
            {
                String sVmsNmbr = VarToStr(pGDC->Values[ii][ColVms2->Index]);

                ITSDb_SQLBind(pADO, "p01", String(FMonitoringType));
                ITSDb_SQLBind(pADO, "p02", sNewName);
                ITSDb_SQLBind(pADO, "p03", String(ii+1));
                ITSDb_SQLBind(pADO, "p04", sVmsNmbr);

                ITSDb_SQLExec(pADO);
            }
            ITSDb_GetConnection()->CommitTrans();
            bResult = true;
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TVMSVIEWM::BtnSaveClick", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TVMSVIEWM::BtnSaveClick", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }

    if (bResult)
    {
        Application->MessageBox(lblText5->Caption.c_str(),//L"VMS 모니터링 화면 저장\r\nVMS 모니터링 화면 정보를 저장 하였습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

        FUpdate = true;

        LoadVmsMonitoring();
        CMM_SetGridRow(CxList, TvList, sNewName, Col02->Index);
        DisplayInfo();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::BtnInsertClick(TObject *Sender)
{
    FormClear();

    EdName->Text    = "";
    EdOrgName->Text = "";

    ChangeEditMode(2);
    FNewDb = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::BtnDeleteClick(TObject *Sender)
{
    int nIdx = TvList->DataController->FocusedRecordIndex;
    if (nIdx < 0) return;

    String sFormName = EdOrgName->Text;

    if (sFormName.IsEmpty())
    {
        Application->MessageBox(lblText6->Caption.c_str(),//L"VMS 모니터링 화면 삭제\r\n삭제할 VMS 모니터링 화면을 목록에서 선택하세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

    String sMsg;
    sMsg = lblText7->Caption + ": " + sFormName;//"VMS 모니터링 화면: " + sFormName;
    sMsg+= "\r\n" + lblText8->Caption;//"선택하신 VMS 모니터링 화면 정보를 삭제하시겠습니까?";

    if (Application->MessageBox(sMsg.c_str(), FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

    if (DelVmsMonitoring(sFormName))
    {
        FUpdate = true;

        Application->MessageBox(lblText9->Caption.c_str(),//L"VMS 모니터링 화면 삭제\r\nVMS 모니터링 화면 정보를 삭제 하였습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

        FormClear();
        ChangeEditMode(1);

        LoadVmsMonitoring();
	}
}
//---------------------------------------------------------------------------

bool __fastcall TVMSVIEWM::DelVmsMonitoring(String AFormName)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "DELETE TB_VMS_MONITORING      \r\n"
           " WHERE MONITORING_TYPE = :p01 \r\n"
           "   AND MONITORING_NM   = :p02 \r\n";

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

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", String(FMonitoringType));
            ITSDb_SQLBind(pADO, "p02", AFormName);
			pADO->ExecSQL();

            return true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS Monitoring Delete", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS Monitoring Delete", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    return true;
}
//---------------------------------------------------------------------------

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

    DisplayInfo();
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::TvListFocusedRecordChanged(TcxCustomGridTableView *Sender,
          TcxCustomGridRecord *APrevFocusedRecord, TcxCustomGridRecord *AFocusedRecord,
          bool ANewItemRecordFocusingChanged)
{
    if (!AFocusedRecord) return;
    DisplayInfo();
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::BtnSearchClick(TObject *Sender)
{
    RefreshData();

    EdFocus->SetFocus();
    ChangeEditMode(1);
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::RefreshData()
{
    // 검색조건에 의한 링크 데이터 조회
    FormClear();
    LoadVmsMonitoring();
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::LoadVmsMonitoring()
{
    String sQry;
    TADOQuery *pADO = NULL;

    CMM_ClearGridTableView(TvList);

    sQry = "SELECT MONITORING_NM, COUNT(1) AS CNT   \r\n"
           "  FROM TB_VMS_MONITORING                \r\n"
           " WHERE MONITORING_TYPE = :p01           \r\n"
           " GROUP BY MONITORING_NM                 \r\n"
           " ORDER BY MONITORING_NM DESC            \r\n";

    String sFirstFormId = "";
    try
    {
        int nRow;
        TcxDataController *pGDC = TvList->DataController;
        TvList->BeginUpdate();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", String(FMonitoringType));
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                nRow = pGDC->AppendRecord();

                pGDC->Values[nRow][Col01->Index] = "";
                pGDC->Values[nRow][Col02->Index] = pADO->FieldByName("MONITORING_NM")->AsString;
                pGDC->Values[nRow][Col03->Index] = pADO->FieldByName("CNT")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS monitoring screen query", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS monitoring screen query", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::FormClear()
{
    CMM_ClearGridTableView(TvVms);

    EdOrgName->Clear();
    EdName->Clear();

    FNewDb = false;
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::DisplayInfo()
{
    FormClear();

	int nSelIdx = TvList->DataController->FocusedRecordIndex;
	if( nSelIdx <= -1 )
		return;

    DisplayFormInfo(nSelIdx);

    ChangeEditMode(3);
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::DisplayFormInfo(int AIdx)
{
	TcxGridDataController *pDc = TvList->DataController;

    String sScrName = VarToStr(pDc->Values[AIdx][Col02->Index]);
    EdName->Text    = sScrName;
    EdOrgName->Text = sScrName;

    CMM_ClearGridTableView(TvVms);

    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.MONITORING_NM, A.MONITORING_SEQ,       \r\n"
           "       A.VMS_CTLR_NMBR, B.VMS_CTLR_ID, B.VMS_NM \r\n"
           "  FROM TB_VMS_MONITORING A,                     \r\n"
           "       TB_VMS_CTLR       B                      \r\n"
           " WHERE A.MONITORING_TYPE = :p01                 \r\n"
           "   AND A.MONITORING_NM   = :p02                 \r\n"
           "   AND A.VMS_CTLR_NMBR   = B.VMS_CTLR_NMBR(+)   \r\n"
           " ORDER BY A.MONITORING_NM, A.MONITORING_SEQ     \r\n";

    String sFirstFormId = "";
    try
    {
        int nRow;
        TcxDataController *pGDC = TvVms->DataController;
        TvVms->BeginUpdate();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", String(FMonitoringType));
            ITSDb_SQLBind(pADO, "p02", sScrName);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                nRow = pGDC->AppendRecord();

                pGDC->Values[nRow][ColVms1->Index] = pADO->FieldByName("MONITORING_SEQ")->AsString;
                pGDC->Values[nRow][ColVms2->Index] = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;
                pGDC->Values[nRow][ColVms3->Index] = pADO->FieldByName("VMS_NM")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS monitoring screen query", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS monitoring screen query", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvVms->EndUpdate();
    }
}
//---------------------------------------------------------------------------

bool __fastcall TVMSVIEWM::FindVmsInfo(String AVmsNmbr)
{
    try {
        TvVms->BeginUpdate();
        for (int ii = 0; ii < TvVms->DataController->RecordCount; ii++)
        {
            String sVmsNmbr = VarToStr(TvVms->DataController->Values[ii][ColVms2->Index]);
            if (sVmsNmbr == AVmsNmbr) return true;
        }
    }
    __finally
    {
        TvVms->EndUpdate();
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::UpdateVmsSeq()
{
    try {
        TvVms->BeginUpdate();
        for (int ii = 0; ii < TvVms->DataController->RecordCount; ii++)
        {
            TvVms->DataController->Values[ii][ColVms1->Index] = String(ii+1);
        }
    }
    __finally
    {
        TvVms->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::BtnListAddClick(TObject *Sender)
{
    TcxGridTableView *pSrcList = FRAMEVmsList1->TvList;
    TcxGridTableView *pDstList = TvVms;
    int nSelRows = pSrcList->Controller->SelectedRowCount;
    if (nSelRows <= 0)
    {
        return;
    }

    try
    {
        TcxGridDataController *pGDC = pDstList->DataController;
        try
        {
            for (int ii = 0; ii < nSelRows; ii++)
            {
                int nSelIdx = pSrcList->Controller->SelectedRows[ii]->RecordIndex;

                String sVmsNmbr = VarToStr(pSrcList->DataController->Values[nSelIdx][FRAMEVmsList1->Column01->Index]);
                String sVmsName = VarToStr(pSrcList->DataController->Values[nSelIdx][FRAMEVmsList1->Column03->Index]);

                if (FindVmsInfo(sVmsNmbr)) continue;

                int nRow = pGDC->AppendRecord();

                pGDC->Values[nRow][ColVms2->Index] = sVmsNmbr;
                pGDC->Values[nRow][ColVms3->Index] = sVmsName;
            }
        }
        catch(...)
        {
        }
    }
    __finally
    {
        UpdateVmsSeq();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::BtnListDeleteClick(TObject *Sender)
{
    TcxGridTableView *pSrcList = TvVms;
    int nFocused = pSrcList->Controller->FocusedRecordIndex;

    int nSelRows = pSrcList->Controller->SelectedRowCount;
    if (nSelRows <= 0)
    {
        return;
    }

    try
    {
        TcxGridDataController *pGDC = pSrcList->DataController;
        pSrcList->BeginUpdate();
        try
        {
            pSrcList->Controller->DeleteSelection();

            int nDataCnt = pSrcList->DataController->RecordCount;
            if (nDataCnt <= nFocused)
                nFocused = nDataCnt - 1;

            pSrcList->Controller->ClearSelection();
            if (nFocused >= 0)
            {
                pSrcList->Controller->FocusedRowIndex = nFocused;
                pSrcList->ViewData->Records[nFocused]->Focused = true;
                pSrcList->ViewData->Records[nFocused]->Selected = true;
            }
            //CxLink->SetFocus();
        }
        catch(...)
        {
        }
    }
    __finally
    {
        pSrcList->EndUpdate();
        UpdateVmsSeq();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::BtnUpClick(TObject *Sender)
{
    try
    {
        TvVms->BeginUpdate();

        int nCurIdx = TvVms->DataController->FocusedRowIndex;//FocusedRecordIndex;
        if (nCurIdx <= 0)
        {
            return;
        }

        int nUpIdx = nCurIdx-1;

        try
        {
            String sVal1 = VarToStr(TvVms->DataController->Values[nCurIdx][ColVms1->Index]);
            String sVal2 = VarToStr(TvVms->DataController->Values[nCurIdx][ColVms2->Index]);
            String sVal3 = VarToStr(TvVms->DataController->Values[nCurIdx][ColVms3->Index]);

            TvVms->DataController->Values[nCurIdx][ColVms1->Index] = TvVms->DataController->Values[nUpIdx][ColVms1->Index];
            TvVms->DataController->Values[nCurIdx][ColVms2->Index] = TvVms->DataController->Values[nUpIdx][ColVms2->Index];
            TvVms->DataController->Values[nCurIdx][ColVms3->Index] = TvVms->DataController->Values[nUpIdx][ColVms3->Index];

            TvVms->DataController->Values[nUpIdx][ColVms1->Index] = sVal1;
            TvVms->DataController->Values[nUpIdx][ColVms2->Index] = sVal2;
            TvVms->DataController->Values[nUpIdx][ColVms3->Index] = sVal3;

            TvVms->Controller->ClearSelection();
            TvVms->Controller->FocusedRowIndex = nUpIdx;
            TvVms->ViewData->Records[nUpIdx]->Focused = true;
            TvVms->ViewData->Records[nUpIdx]->Selected = true;
            CxVms->SetFocus();
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvVms->EndUpdate();
        UpdateVmsSeq();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSVIEWM::BtnDownClick(TObject *Sender)
{
    try
    {
        TvVms->BeginUpdate();

        int nCurIdx = TvVms->DataController->FocusedRowIndex;//FocusedRecordIndex;
        if (nCurIdx < 0)
        {
            return;
        }
        if (nCurIdx == TvVms->DataController->RecordCount-1)
        {
            return;
        }

        int nDnIdx = nCurIdx+1;

        try
        {
            String sVal1 = VarToStr(TvVms->DataController->Values[nCurIdx][ColVms1->Index]);
            String sVal2 = VarToStr(TvVms->DataController->Values[nCurIdx][ColVms2->Index]);
            String sVal3 = VarToStr(TvVms->DataController->Values[nCurIdx][ColVms3->Index]);

            TvVms->DataController->Values[nCurIdx][ColVms1->Index] = TvVms->DataController->Values[nDnIdx][ColVms1->Index];
            TvVms->DataController->Values[nCurIdx][ColVms2->Index] = TvVms->DataController->Values[nDnIdx][ColVms2->Index];
            TvVms->DataController->Values[nCurIdx][ColVms3->Index] = TvVms->DataController->Values[nDnIdx][ColVms3->Index];

            TvVms->DataController->Values[nDnIdx][ColVms1->Index] = sVal1;
            TvVms->DataController->Values[nDnIdx][ColVms2->Index] = sVal2;
            TvVms->DataController->Values[nDnIdx][ColVms3->Index] = sVal3;

            TvVms->Controller->ClearSelection();
            TvVms->Controller->FocusedRowIndex = nDnIdx;
            TvVms->ViewData->Records[nDnIdx]->Focused = true;
            TvVms->ViewData->Records[nDnIdx]->Selected = true;
            CxVms->SetFocus();
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvVms->EndUpdate();
        UpdateVmsSeq();
    }
}
//---------------------------------------------------------------------------

