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

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

#include "VMSEDT0MF.h"
#include "VMSSEL0MF.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 resource "*.dfm"

TVMSEDT0M *VMSEDT0M = NULL;
//---------------------------------------------------------------------------

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

    FUpdate= false;
    FTitle = Caption;//"VMS 폼 스케줄 일괄 관리";

    TvForm->OptionsView->DataRowHeight  = 0;
    TvForm->OptionsView->CellAutoHeight = true;
    TvForm->OptionsView->Indicator      = false;

    TvForm->OptionsCustomize->ColumnMoving  = false;
    TvForm->OptionsCustomize->ColumnSorting = false;

    //TvForm->Columns[0]->Width = 40;
    //TvForm->Columns[0]->Options->HorzSizing = false;

    FStrTime = "";
    FEndTime = "";
    FSelected = false;
}
//---------------------------------------------------------------------------

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

void __fastcall TVMSEDT0M::FormCreate(TObject *Sender)
{
    //
}
//---------------------------------------------------------------------------

/*
 * Form을 보여줄때 호출되는 event 메서드이다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TVMSEDT0M::FormShow(TObject *Sender)
{
#if 1
    if (FMode == "A")
    {
        FTitle = lblText1->Caption;//"VMS 폼 스케줄 일괄 등록";
        GrpObj->Caption = lblText2->Caption;//"▶ VMS 목록";
        BtnAllDel->Visible = false;
    }
    else
    {
        FTitle = lblText3->Caption;//"VMS 폼 스케줄 일괄 편집";
        GrpObj->Caption = lblText4->Caption;//"▶ 선택폼을 스케줄에 사용중인 VMS 목록";
    }
#else
    if (FMode == "A")
    {
        FTitle = "Batch add of VMS form schedule";
        GrpObj->Caption = "▶ VMS List";
        BtnAllDel->Visible = false;
    }
    else
    {
        FTitle = "Batch edit VMS form schedule";
        GrpObj->Caption = "▶ List the VMS using to schedule the selection form";
    }
#endif

    Caption = FTitle;

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

void __fastcall TVMSEDT0M::FormInit()
{
    FormObjectInit();

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

void __fastcall TVMSEDT0M::FormObjectInit()
{
    //TvList->OptionsView->NoDataToDisplayInfoText = "<관련 VMS 정보가 없습니다>";
}
//---------------------------------------------------------------------------

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

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

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

void __fastcall TVMSEDT0M::ChangeEditMode(int AEdit)
{
}
//---------------------------------------------------------------------------


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

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

    if (FStrTime == "")
    {
        DtStrDate->Date = Now();
    }
    else
    {
        TDateTime dtStrDate = APP_StrToDateTime(FStrTime);
        DtStrDate->Date = dtStrDate;
        DtStrTime->Date = dtStrDate;
        DtStrTime->Time = dtStrDate;
    }
    if (FEndTime == "")
    {
        TDateTime dtEndDate = EncodeDate(2099, 12, 31);
        TDateTime dtEndTime = EncodeTime(23, 59, 59, 0);
        DtEndDate->Date = dtEndDate;
        DtEndTime->Date = dtEndDate;
        DtEndTime->Time = dtEndTime;
    }
    else
    {
        TDateTime dtEndDate = APP_StrToDateTime(FEndTime);
        DtEndDate->Date = dtEndDate;
        DtEndTime->Date = dtEndDate;
        DtEndTime->Time = dtEndDate;
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSEDT0M::RefreshData()
{
    // 검색조건에 의한 링크 데이터 조회
    FormClear();
    DisplayFormInfo(0);
    DisplayVmsInfo(0);
}
//---------------------------------------------------------------------------
void __fastcall TVMSEDT0M::FormClear()
{
    EdFormId->Clear();
    EdObjId->Clear();

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

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

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

void __fastcall TVMSEDT0M::DisplayFormInfo(int AIdx)
{
    CMM_ClearGridTableView(TvForm);

    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.*                   \r\n"
           "  FROM TB_VMS_FORM A         \r\n"
           " WHERE A.VMS_FORM_ID = :p01  \r\n"
           " ORDER BY A.VMS_FORM_ID      \r\n";

    try
    {
        TvForm->BeginUpdate();

        TcxDataController *pGDC = TvForm->DataController;
        int nRow;
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", FFormId);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                nRow = pGDC->AppendRecord();
                pGDC->Values[nRow][0] = pADO->FieldByName("VMS_FORM_ID")->AsString;
                pGDC->Values[nRow][1] = pADO->FieldByName("VMS_FORM_IMAG")->AsVariant;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSEDT0M::DisplayFormInfo", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVMSEDT0M::DisplayFormInfo", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvForm->ApplyBestFit(NULL, false, false);
        TvForm->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSEDT0M::DisplayVmsInfo(int AIdx)
{
    CMM_ClearGridTableView(TvList);

    String sQry;
    TADOQuery *pADO = NULL;

    String sWhere;
    if (FMode == "A")
    {
        sWhere = "   AND A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR(+)          \r\n";
    }
    else
    {
        sWhere = "   AND A.VMS_CTLR_NMBR = B.VMS_CTLR_NMBR             \r\n";
    }

    sQry = "SELECT A.VMS_CTLR_NMBR, A.VMS_CTLR_ID, A.VMS_NM,           \r\n"
           "       B.PHASE, B.DSPL_HH, B.DSPL_STRT_HH, B.DSPL_END_HH,  \r\n"
           "       B.USE_YN, B.VMS_FORM_ID,                            \r\n"
           "       DECODE(B.VMS_CTLR_NMBR, NULL, 0, 1) AS FG           \r\n"
           "  FROM TB_VMS_CTLR A,                                      \r\n"
           "       (SELECT X.VMS_CTLR_NMBR,                            \r\n"
           "               X.VMS_SCH_TYPE,                             \r\n"
           "               X.VMS_SCH_FORM_TYPE,                        \r\n"
           "               X.PHASE,                                    \r\n"
           "               X.VMS_FORM_ID,                              \r\n"
           "               X.DSPL_STRT_HH,                             \r\n"
           "               X.DSPL_END_HH,                              \r\n"
           "               X.DSPL_HH,                                  \r\n"
           "               X.FRST_VMS_IFSC_ID,                         \r\n"
           "               X.SECD_VMS_IFSC_ID,                         \r\n"
           "               X.THIR_VMS_IFSC_ID,                         \r\n"
           "               X.FOUR_VMS_IFSC_ID,                         \r\n"
           "               X.FRST_IMG_IFSC_ID,                         \r\n"
           "               X.SECD_IMG_IFSC_ID,                         \r\n"
           "               X.THIR_IMG_IFSC_ID,                         \r\n"
           "               X.FOUR_IMG_IFSC_ID,                         \r\n"
           "               X.SYMB_LIB_NMBR,                            \r\n"
           "               X.STRM_ADDR,                                \r\n"
           "               X.USE_YN,                                   \r\n"
           "               Y.VMS_TYPE_CD,                              \r\n"
           "               Y.VMS_FORM_TYPE_CD,                         \r\n"
           "               Y.VMS_FORM_NM                               \r\n"
           "          FROM TB_VMS_DSPL_SCH X,                          \r\n"
           "               TB_VMS_FORM     Y                           \r\n"
           "         WHERE X.VMS_FORM_ID       = Y.VMS_FORM_ID         \r\n"
           "           AND X.VMS_SCH_TYPE      = :p02                  \r\n"
           "           AND X.VMS_SCH_FORM_TYPE = :p03                  \r\n"
           "           AND X.VMS_FORM_ID       = :p04) B               \r\n"
           " WHERE A.VMS_TYPE_CD   = :p01                              \r\n";
    sQry+= sWhere;

    try
    {
        TvList->BeginUpdate();

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

            ITSDb_SQLText(pADO, sQry);

            ITSDb_SQLBind(pADO, "p01", FVmsTypeCd);
            ITSDb_SQLBind(pADO, "p02", FScheType);
            ITSDb_SQLBind(pADO, "p03", FScheFormType);
            ITSDb_SQLBind(pADO, "p04", FFormId);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                String sFormId = pADO->FieldByName("VMS_FORM_ID")->AsString;
                if (FMode == "A")
                {
                    if (sFormId == FFormId)
                    {
                        //전체추가인데 기존에 등록되어 있으면 추가하지 않는다.
                        //continue;
                    }
                }
                nRow = pGDC->AppendRecord();

                pGDC->Values[nRow][Col00->Index] = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;
                pGDC->Values[nRow][Col01->Index] = pADO->FieldByName("VMS_CTLR_ID")->AsString;
                pGDC->Values[nRow][Col02->Index] = pADO->FieldByName("VMS_NM")->AsString;

                String sFlag = pADO->FieldByName("FG")->AsString;
                if (sFlag == "0")
                {
                    pGDC->Values[nRow][ColumnSel->Index] = true;
                    pGDC->Values[nRow][Col03->Index] = "4";
                    pGDC->Values[nRow][Col04->Index] = Now().FormatString(STR_HHNN);
                    pGDC->Values[nRow][Col05->Index] = "2099-12-31 23:59";
                    pGDC->Values[nRow][Col06->Index] = "Y";
                    pGDC->Values[nRow][Col07->Index] = "1";
                    pGDC->Values[nRow][Col08->Index] = Now().FormatString("YYYYMMDDHHNN") + "00";
                    pGDC->Values[nRow][Col09->Index] = "20991231235959";
                    continue;
                }

                if (FMode == "A")
                {
                    pGDC->Values[nRow][ColumnSel->Index] = false;
                }
                else
                {
                    pGDC->Values[nRow][ColumnSel->Index] = true;
                }

                String sStrDate = pADO->FieldByName("DSPL_STRT_HH")->AsString;
                String sEndDate = pADO->FieldByName("DSPL_END_HH")->AsString;

                pGDC->Values[nRow][Col03->Index] = pADO->FieldByName("DSPL_HH")->AsString;
                pGDC->Values[nRow][Col04->Index] = ITSUtil_FormatStr(sStrDate, STR_HHNN);
                pGDC->Values[nRow][Col05->Index] = ITSUtil_FormatStr(sEndDate, STR_HHNN);
                pGDC->Values[nRow][Col06->Index] = pADO->FieldByName("USE_YN")->AsString;
                pGDC->Values[nRow][Col07->Index] = pADO->FieldByName("PHASE")->AsString;
                pGDC->Values[nRow][Col08->Index] = sStrDate;
                pGDC->Values[nRow][Col09->Index] = sEndDate;

                FStrTime = sStrDate;
                FEndTime = sEndDate;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMSEDT0M::DisplayVmsInfo", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVMSEDT0M::DisplayVmsInfo", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSEDT0M::BtnAllSelectClick(TObject *Sender)
{
    TcxButton *pBtn = (TcxButton*)Sender;
    CMM_CheckAllListItem(TvList, ColumnSel->Index, pBtn->Tag);
}
//---------------------------------------------------------------------------

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

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

    TcxGridDataController *pDc = (TcxGridDataController*)Sender->DataController;

    int nColIdx = ACellViewInfo->Item->Index;

    String sStrDate = VarToStr(pDc->Values[nRow][Col08->Index]);
    String sEndDate = VarToStr(pDc->Values[nRow][Col09->Index]);

    {
        //표출기간설정
        TVMSSEL0M *pSelDate = new TVMSSEL0M(this);
        pSelDate->FStrTime = sStrDate;
        pSelDate->FEndTime = sEndDate;

        pSelDate->ShowModal();
        bool bSelected = pSelDate->FSelected;
        sStrDate  = pSelDate->FStrTime;
        sEndDate  = pSelDate->FEndTime;
        delete pSelDate;
        pSelDate = NULL;

        if (bSelected)
        {
            pDc->Values[nRow][Col04->Index] = ITSUtil_FormatStr(sStrDate, STR_HHNN);;
            pDc->Values[nRow][Col05->Index] = ITSUtil_FormatStr(sEndDate, STR_HHNN);;
            pDc->Values[nRow][Col08->Index] = sStrDate;
            pDc->Values[nRow][Col09->Index] = sEndDate;
        }
        return;
    }
}
//---------------------------------------------------------------------------

bool __fastcall TVMSEDT0M::CheckData(bool ACheckData)
{
    int nSelCnt = CMM_GetCheckCount(TvList, ColumnSel->Index);
    if (nSelCnt <= 0)
    {
        //Application->MessageBox(L"VMS Schedule Batch Management\r\nSelect the VMS you want to work on from the VMS list.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        Application->MessageBox(lblText5->Caption.c_str(),//L"VMS 스케줄 일괄 관리\r\nVMS 목록에서 작업대상 VMS를 먼저 선택하세요.",
                                FTitle.c_str(),
                                MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return false;
    }

    if (!ACheckData) return true;

    String sStrDate = DtStrDate->Date.FormatString("yyyymmdd");
    String sStrTime = DtStrTime->DateTime.FormatString("HHnn");
    String sEndDate = DtEndDate->Date.FormatString("yyyymmdd");
    String sEndTime = DtEndTime->DateTime.FormatString("HHnn");
    FStrTime = sStrDate + sStrTime;
    FEndTime = sEndDate + sEndTime;

    if (FStrTime > FEndTime)
    {
        //Application->MessageBox(L"The display start time is larger than the display end time.", L"Set VMS form expiration period", MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        Application->MessageBox(lblText6->Caption.c_str(),//L"표출 시작시각이 표출 종료시각 보다 큽니다.",
                                lblText7->Caption.c_str(),//L"VMS 폼 표출기간 설정",
                                MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStrDate;
        return false;
    }
    FStrTime = FStrTime + "00";
    FEndTime = FEndTime + "59";

    return true;
}
//---------------------------------------------------------------------------

void __fastcall TVMSEDT0M::UpdateEditData()
{
    try
    {
        TvList->BeginUpdate();

        int nRowCnt = TvList->ViewData->RecordCount;
        for (int ii = 0; ii < nRowCnt; ii++)
        {
            TcxCustomGridRow* ARow = TvList->ViewData->Rows[ii];
            if (!ARow->IsData) continue;
            if (!((bool)ARow->Values[ColumnSel->Index]))
            {
                continue;
            }

            ARow->Values[Col03->Index] = CbCycle->Text.Trim();
            ARow->Values[Col04->Index] = ITSUtil_FormatStr(FStrTime, STR_HHNN);
            ARow->Values[Col05->Index] = ITSUtil_FormatStr(FEndTime, STR_HHNN);
            ARow->Values[Col06->Index] = CbUse->Text.Trim();
            //ARow->Values[Col07->Index] = "";    //phase
            ARow->Values[Col08->Index] = FStrTime;
            ARow->Values[Col09->Index] = FEndTime;
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSEDT0M::BtnAllAddClick(TObject *Sender)
{
    //등록 - 등록 또는 수정 INSERT or UPDATE
    if (!CheckData(true)) return;

#if 1
    if (Application->MessageBox(lblText8->Caption.c_str(),//L"VMS 스케줄 일괄 관리\r\n선택한 VMS에 대하여 스케줄 정보를 [[[동일내용으로]]] 저장 하시겠습니까?",
                                FTitle.c_str(),
                                MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;
#else
    if (Application->MessageBox(L"VMS schedule management\r\nDo you want to save schedule information for selected VMS [[[Same as above]]]?",
                                FTitle.c_str(),
                                MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;
#endif

    UpdateEditData();   //동일 편집내용을 VMS목록에 적용

    bool bResult;
    if (FMode == "A")
    {
        //INSERT
        bResult = InsertVmsSchedule();
    }
    else
    {
        //UPDATE
        bResult = UpdateVmsSchedule();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSEDT0M::BtnAllEdtClick(TObject *Sender)
{
    //수정 - 등록 또는 수정 INSERT or UPDATE
    if (!CheckData(true)) return;

#if 1
    if (Application->MessageBox(lblText9->Caption.c_str(),//L"VMS 스케줄 일괄 관리\r\n선택한 VMS에 대하여 스케줄 정보를 저장 하시겠습니까?",
                                FTitle.c_str(),
                                MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;
#else
    if (Application->MessageBox(L"VMS schedule management\r\nDo you want to save schedule information for selected VMS?",
                                FTitle.c_str(),
                                MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;
#endif

    bool bResult;
    if (FMode == "A")
    {
        //INSERT
        bResult = InsertVmsSchedule();
    }
    else
    {
        //UPDATE
        bResult = UpdateVmsSchedule();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMSEDT0M::BtnAllDelClick(TObject *Sender)
{
    //삭제 - 무조건 DELETE
    if (!CheckData(false)) return;

#if 1
    if (Application->MessageBox(lblText10->Caption.c_str(),//L"VMS 스케줄 일괄 관리\r\n선택한 VMS에 대하여 스케줄 정보를 삭제하시겠습니까?",
                                FTitle.c_str(),
                                MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;
#else
    if (Application->MessageBox(L"VMS schedule management\r\nDo you want to delete the schedule information for the selected VMS?",
                                FTitle.c_str(),
                                MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;
#endif

    bool bResult = true;
    if (bResult)
    {
        bResult = DeleteVmsSchedule();
    }
}
//---------------------------------------------------------------------------

bool __fastcall TVMSEDT0M::DeleteVmsSchedule()
{
    bool       bResult = false;
    String     sQry;
    TADOQuery *pADO = NULL;


    sQry = "DELETE TB_VMS_DSPL_SCH                 \r\n"
           "       WHERE VMS_CTLR_NMBR     = :p01  \r\n"
           "         AND VMS_SCH_TYPE      = :p02  \r\n"
           "         AND VMS_SCH_FORM_TYPE = :p03  \r\n"
           "         AND PHASE             = :p04  \r\n"
           "         AND VMS_FORM_ID       = :p05  \r\n";

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

            ITSDb_SQLText(pADO, sQry);

            //ITSDb_GetConnection()->BeginTrans();

            int nRowCnt = TvList->ViewData->RecordCount;
            for (int ii = 0; ii < nRowCnt; ii++)
            {
                TcxCustomGridRow* ARow = TvList->ViewData->Rows[ii];
                if (!ARow->IsData) continue;
                if (!((bool)ARow->Values[ColumnSel->Index]))
                {
                    continue;
                }

                String sVmsNmbr      = VarToStr(ARow->Values[Col00->Index]);
                String sScheType     = FScheType;
                String sScheFormType = FScheFormType;
                String sPhase        = VarToStr(ARow->Values[Col07->Index]);
                String sFormId       = FFormId;

                ITSDb_SQLBind(pADO, "p01", sVmsNmbr);
                ITSDb_SQLBind(pADO, "p02", sScheType);
                ITSDb_SQLBind(pADO, "p03", sScheFormType);
                ITSDb_SQLBind(pADO, "p04", sPhase);
                ITSDb_SQLBind(pADO, "p05", sFormId);

                ITSDb_SQLExec(pADO);
            }

            //ITSDb_GetConnection()->CommitTrans();
            bResult = true;
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TVMSEDT0M::DeleteVmsSchedule", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TVMSEDT0M::DeleteVmsSchedule", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    if (bResult)
    {
#if 1
        Application->MessageBox(lblText11->Caption.c_str(),//L"VMS 스케줄 일괄 관리\r\nVMS 스케줄 정보를 삭제 하였습니다.",
                                FTitle.c_str(),
                                MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
#else
        Application->MessageBox(L"VMS schedule management\r\nDeleted the VMS schedule information.",
                                FTitle.c_str(),
                                MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
#endif

        FSelected = true;
        Close();
    }

    return bResult;
}
//---------------------------------------------------------------------------

bool __fastcall TVMSEDT0M::UpdateVmsSchedule()
{
    bool       bResult = false;
    String     sQry;
    TADOQuery *pADO = NULL;


    sQry = "UPDATE TB_VMS_DSPL_SCH           \r\n"
           "   SET DSPL_HH      = :p05,      \r\n"
           "       DSPL_STRT_HH = :p06,      \r\n"
           "       DSPL_END_HH  = :p07,      \r\n"
           "       USE_YN       = :p08       \r\n"
           " WHERE VMS_CTLR_NMBR     = :p01  \r\n"
           "   AND VMS_SCH_TYPE      = :p09  \r\n"
           "   AND VMS_SCH_FORM_TYPE = :p02  \r\n"
           "   AND PHASE             = :p03  \r\n"
           "   AND VMS_FORM_ID       = :p04  \r\n";

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

            ITSDb_SQLText(pADO, sQry);

            //ITSDb_GetConnection()->BeginTrans();

            int nRowCnt = TvList->ViewData->RecordCount;
            for (int ii = 0; ii < nRowCnt; ii++)
            {
                TcxCustomGridRow* ARow = TvList->ViewData->Rows[ii];
                if (!ARow->IsData) continue;
                if (!((bool)ARow->Values[ColumnSel->Index]))
                {
                    continue;
                }

                String sVmsNmbr      = VarToStr(ARow->Values[Col00->Index]);
                String sScheType     = FScheType;
                String sScheFormType = FScheFormType;
                String sPhase        = VarToStr(ARow->Values[Col07->Index]);
                String sFormId       = FFormId;

                String sDispHH   = VarToStr(ARow->Values[Col03->Index]);
                String sStrTime  = VarToStr(ARow->Values[Col08->Index]);
                String sEndTime  = VarToStr(ARow->Values[Col09->Index]);
                String sUseYn    = VarToStr(ARow->Values[Col06->Index]);

                ITSDb_SQLBind(pADO, "p01", sVmsNmbr);
                ITSDb_SQLBind(pADO, "p02", sScheFormType);
                ITSDb_SQLBind(pADO, "p03", sPhase);
                ITSDb_SQLBind(pADO, "p04", sFormId);
                ITSDb_SQLBind(pADO, "p05", sDispHH);
                ITSDb_SQLBind(pADO, "p06", sStrTime);
                ITSDb_SQLBind(pADO, "p07", sEndTime);
                ITSDb_SQLBind(pADO, "p08", sUseYn);
                ITSDb_SQLBind(pADO, "p09", sScheType);

                ITSDb_SQLExec(pADO);
            }

            //ITSDb_GetConnection()->CommitTrans();
            bResult = true;
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TVMSEDT0M::UpdateVmsSchedule", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TVMSEDT0M::UpdateVmsSchedule", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    if (bResult)
    {
#if 1
        Application->MessageBox(lblText12->Caption.c_str(),//L"VMS 스케줄 일괄 관리\r\nVMS 스케줄 정보를 수정 하였습니다.",
                                FTitle.c_str(),
                                MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
#else
        Application->MessageBox(L"VMS schedule management\r\Edited the VMS schedule information.",
                                FTitle.c_str(),
                                MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
#endif

        FSelected = true;
        Close();
    }

    return bResult;
}
//---------------------------------------------------------------------------

bool __fastcall TVMSEDT0M::InsertVmsSchedule()
{
    bool       bResult = false;
    String     sQry;
    TADOQuery *pADO = NULL;

#if 0
    sQry = "INSERT INTO TB_VMS_DSPL_SCH (                   \r\n"
           "             VMS_CTLR_NMBR,                     \r\n"
           "             VMS_SCH_TYPE,                      \r\n"
           "             VMS_SCH_FORM_TYPE,                 \r\n"
           "             PHASE,                             \r\n"
           "             VMS_FORM_ID,                       \r\n"
           "             DSPL_HH,                           \r\n"
           "             DSPL_STRT_HH,                      \r\n"
           "             DSPL_END_HH,                       \r\n"
           "             SYMB_LIB_NMBR,                     \r\n"
           "             STRM_ADDR,                         \r\n"
           "             USE_YN                             \r\n"
           "             )                                  \r\n"
           "     VALUES (                                   \r\n"
           "             :p01,                              \r\n"
           "             :p02,                              \r\n"
           "             :p11,                              \r\n"
           "             (SELECT NVL(MAX(PHASE), 0) + 1     \r\n"
           "                FROM TB_VMS_DSPL_SCH            \r\n"
           "               WHERE VMS_CTLR_NMBR     = :p08   \r\n"
           "                 AND VMS_SCH_TYPE      = :p09   \r\n"
           "                 AND VMS_SCH_FORM_TYPE = :p10), \r\n"
           "             :p03,                              \r\n"
           "             :p04,                              \r\n"
           "             :p05,                              \r\n"
           "             :p06,                              \r\n"
           "             :p12,                              \r\n"
           "             :p13,                              \r\n"
           "             :p07                               \r\n"
           "            )                                   \r\n";
#else
    sQry = "INSERT INTO TB_VMS_DSPL_SCH (                   \r\n"
           "             VMS_CTLR_NMBR,                     \r\n"
           "             VMS_SCH_TYPE,                      \r\n"
           "             VMS_SCH_FORM_TYPE,                 \r\n"
           "             PHASE,                             \r\n"
           "             VMS_FORM_ID,                       \r\n"
           "             DSPL_HH,                           \r\n"
           "             DSPL_STRT_HH,                      \r\n"
           "             DSPL_END_HH,                       \r\n"
           "             SYMB_LIB_NMBR,                     \r\n"
           "             STRM_ADDR,                         \r\n"
           "             USE_YN                             \r\n"
           "             )                                  \r\n"
           "     VALUES (                                   \r\n"
           "             :p01,                              \r\n"
           "             :p02,                              \r\n"
           "             :p11,                              \r\n"
           "             (SELECT NVL(MAX(PHASE), 0) + 1     \r\n"
           "                FROM TB_VMS_DSPL_SCH            \r\n"
           "               WHERE VMS_CTLR_NMBR     = :p08   \r\n"
           "                 AND VMS_SCH_TYPE      = :p09), \r\n"
           "             :p03,                              \r\n"
           "             :p04,                              \r\n"
           "             :p05,                              \r\n"
           "             :p06,                              \r\n"
           "             :p12,                              \r\n"
           "             :p13,                              \r\n"
           "             :p07                               \r\n"
           "            )                                   \r\n";
#endif

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

            ITSDb_SQLText(pADO, sQry);

            //ITSDb_GetConnection()->BeginTrans();

            int nRowCnt = TvList->ViewData->RecordCount;
            for (int ii = 0; ii < nRowCnt; ii++)
            {
                TcxCustomGridRow* ARow = TvList->ViewData->Rows[ii];
                if (!ARow->IsData) continue;
                if (!((bool)ARow->Values[ColumnSel->Index]))
                {
                    continue;
                }

                String sVmsNmbr      = VarToStr(ARow->Values[Col00->Index]);
                String sScheType     = FScheType;
                String sScheFormType = FScheFormType;
                String sFormId       = FFormId;
                String sDispHH       = VarToStr(ARow->Values[Col03->Index]);
                String sStrTime      = VarToStr(ARow->Values[Col08->Index]);
                String sEndTime      = VarToStr(ARow->Values[Col09->Index]);
                String sUseYn        = VarToStr(ARow->Values[Col06->Index]);

                ITSDb_SQLBind(pADO, "p01", sVmsNmbr);
                ITSDb_SQLBind(pADO, "p02", sScheType);
                ITSDb_SQLBind(pADO, "p03", sFormId);
                ITSDb_SQLBind(pADO, "p04", sDispHH);
                ITSDb_SQLBind(pADO, "p05", sStrTime);
                ITSDb_SQLBind(pADO, "p06", sEndTime);
                ITSDb_SQLBind(pADO, "p07", sUseYn);
                ITSDb_SQLBind(pADO, "p08", sVmsNmbr);
                ITSDb_SQLBind(pADO, "p09", sScheType);
                //ITSDb_SQLBind(pADO, "p10", sScheFormType);
                ITSDb_SQLBind(pADO, "p11", sScheFormType);
                ITSDb_SQLBind(pADO, "p12", FSymbLibNmbr);
                ITSDb_SQLBind(pADO, "p13", FStreamAddr);

                ITSDb_SQLExec(pADO);
            }

            //ITSDb_GetConnection()->CommitTrans();
            bResult = true;
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TVMSEDT0M::InsertVmsSchedule", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TVMSEDT0M::InsertVmsSchedule", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
    if (bResult)
    {
#if 1
        Application->MessageBox(lblText13->Caption.c_str(),//L"VMS 스케줄 일괄 관리\r\nVMS 스케줄 정보를 저장 하였습니다.",
                                FTitle.c_str(),
                                MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
#else
        Application->MessageBox(L"VMS schedule management\r\nVMS schedule information saved.",
                                FTitle.c_str(),
                                MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
#endif

        FSelected = true;
        Close();
    }

    return bResult;
}
//---------------------------------------------------------------------------
bool __fastcall TVMSEDT0M::InsertVmsScheduleWeek()
{
    bool       bResult = false;
#if 0
    String     sQry;
    TADOQuery *pADO = NULL;

    sQry = "INSERT INTO TB_VMS_DSPL_SCH_WEEK (                                  \r\n"
           "                              VMS_CTLR_NMBR,                        \r\n"
           "                              VMS_SCH_TYPE,                         \r\n"
           "                              PHASE,                                \r\n"
           "                              WEEK_CD,                              \r\n"
           "                              SCH_HH                                \r\n"
           "                             )                                      \r\n"
           "                      VALUES(                                       \r\n"
           "                             :p01, :p02,                            \r\n"
           "                              (SELECT NVL(MAX(PHASE), 0)            \r\n"
           "                                 FROM TB_VMS_DSPL_SCH               \r\n"
           "                                WHERE VMS_CTLR_NMBR     = :p05      \r\n"
           "                                  AND VMS_SCH_TYPE      = :p06      \r\n"
           "                                  AND VMS_SCH_FORM_TYPE = :p07),    \r\n"
           "                             :p03, :p04                             \r\n"
           "                             )                                      \r\n";

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

            ITSDb_SQLText(pADO, sQry);

            int nRowCnt = TvList->ViewData->RecordCount;
            for (int ii = 0; ii < nRowCnt; ii++)
            {
                TcxCustomGridRow* ARow = TvList->ViewData->Rows[ii];
                if (!ARow->IsData) continue;
                if (!((bool)ARow->Values[ColumnSel->Index]))
                {
                    continue;
                }

                String sVmsNmbr      = VarToStr(ARow->Values[Col00->Index]);
                String sScheType     = FScheType;
                String sScheFormType = FScheFormType;

                for (int jj = 1; jj <= 7; jj++)
                {
                    ITSDb_SQLBind(pADO, "p01", sVmsNmbr);
                    ITSDb_SQLBind(pADO, "p02", sScheType);
                    ITSDb_SQLBind(pADO, "p03", String(jj));
                    ITSDb_SQLBind(pADO, "p04", "111111111111111111111111");
                    ITSDb_SQLBind(pADO, "p05", sVmsNmbr);
                    ITSDb_SQLBind(pADO, "p06", sScheType);
                    ITSDb_SQLBind(pADO, "p07", sScheFormType);

                    pADO->ExecSQL();
                }
            }

            bResult = true;
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TVMSEDT0M::InsertVmsScheduleWeek", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            DBERRORMSG("TVMSEDT0M::InsertVmsScheduleWeek", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
    }
#endif
    return bResult;
}
//---------------------------------------------------------------------------

