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

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

#include "VMS0200MF.h"
#include "VMSVIEWMF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSplitter"
#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 "cxClasses"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "cxGridCardView"
#pragma link "cxGridCustomLayoutView"
#pragma link "cxCheckBox"
#pragma link "cxDropDownEdit"
#pragma link "cxImage"
#pragma link "cxButtons"
#pragma resource "*.dfm"
TVMS0200M *VMS0200M = NULL;

//---------------------------------------------------------------------------
__fastcall TVMS0200M::TVMS0200M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    LoadLocalSkin();

    FMonitoringType = enMonitoringScenario;
    TsList01->TabVisible = false;

    if (FormStyle != fsMDIChild) CMM_LoadForm(g_sFormsDir, this);

	FListForm = new TList;
    FSelVmsObj= NULL;
    FSelForm  = NULL;
    //PgVmsState->Properties->HideTabs = true;
    //PgVmsIfscTraf->Properties->HideTabs = true;
    FReady = false;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::CommClose()
{
    try
    {
        ClearFormList();
        SAFE_DELETE(FListForm);

        if (FormStyle != fsMDIChild) CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::ClearFormList()
{
    try
    {
        LockWindowUpdate(Handle);
        PgFormTab->HideTabs = true;
        try
        {
            for(int ii = 0; ii < FListForm->Count; ii++)
            {
                TVMS0201M *pForm = (TVMS0201M*)FListForm->Items[ii];
                pForm->Hide();
                SAFE_DELETE(pForm);
            }
            FListForm->Clear();

            int nForms = PgFormTab->PageCount;
            for (int ii = 1; ii < nForms; ii++)
            {
                TcxTabSheet *pTmpSheet = PgFormTab->Pages[1];
                if (pTmpSheet) delete pTmpSheet;
            }
            PgFormTab->ActivePageIndex = 0;
        }
        catch(Exception &e)
        {
            TcxTabSheet *pTmpSheet = PgFormTab->Pages[1];
            if (pTmpSheet) delete pTmpSheet;
        }
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::FormCreate(TObject *Sender)
{
    InitMonitoringForm();
    CreateVmsMsgForm(VmsCtlrManager->FMaxWidth);
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::CreateVmsMsgForm(int AWidth)
{
    FReady = false;

    ClearFormList();

    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT MONITORING_NM, MONITORING_SEQ, VMS_CTLR_NMBR  \r\n"
           "  FROM TB_VMS_MONITORING                             \r\n"
           " WHERE MONITORING_TYPE = :p01                        \r\n"
           " ORDER BY MONITORING_NM DESC, MONITORING_SEQ         \r\n";

    TcxTabSheet *pSheet = NULL;
    TVMS0201M   *pScrForm = NULL;
    String       sOldName = "";
    int          nIndex   = 0;
    try
    {
        LockWindowUpdate(Handle);

        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())
            {
                String sScrName = pADO->FieldByName("MONITORING_NM")->AsString;
                String sScrSeq  = pADO->FieldByName("MONITORING_SEQ")->AsString;
                String sVmsNmbr = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;
                if (sOldName != sScrName)
                {
                    //신규 화면목록 추가
                    pSheet = CMM_AddTabSheet(PgFormTab, "  " + sScrName + "  ");
                    if (!pSheet) continue;

                    TVMS0201M *pNewForm = new TVMS0201M(this, Handle, sScrName, nIndex++);
                    pNewForm->Parent = pSheet;
                    pNewForm->Show();
                    pScrForm = pNewForm;
                    sOldName = sScrName;
                    FListForm->Add(pNewForm);
                }
                pScrForm->AddVmsForm(sVmsNmbr);
            }
        }
        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;
        }
        if (PgFormTab->PageCount == 1)
            PgFormTab->ActivePageIndex = 0;
        else
            PgFormTab->ActivePageIndex = 1;
        LockWindowUpdate(0);
    }

    RefreshScenario();

    if (FListForm->Count <= 1)
    {
        PgFormTab->HideTabs = true;
    }
    else
    {
        PgFormTab->HideTabs = false;
    }

    FReady = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::RefreshScenario()
{
    try
    {
        LockWindowUpdate(Handle);
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMS0201M *pForm = (TVMS0201M*)FListForm->Items[ii];
            pForm->RefreshScenario();
        }
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::FormShow(TObject *Sender)
{
    Refresh();
    if (PgFormTab->PageCount == 1)
        PgFormTab->ActivePageIndex = 0;
    else
        PgFormTab->ActivePageIndex = 1;

    Application->ProcessMessages();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::FormActivate(TObject *Sender)
{
    ActivateForm(true);
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::FormDeactivate(TObject *Sender)
{
    ActivateForm(false);
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::LoadLocalSkin()
{
}
//---------------------------------------------------------------------------

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

void __fastcall TVMS0200M::FormClose(TObject *Sender, TCloseAction &Action)
{
    try {
        CommClose();

        VMS0200M = NULL;
        Action = caFree;
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::OnMainFormMessage(TMessage &Msg)
{
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::OnVmsStateRefreshMessage(TMessage &Msg)
{
    for(int ii = 0; ii < FListForm->Count; ii++)
    {
        TVMS0201M *pForm = (TVMS0201M*)FListForm->Items[ii];
        //pForm->RefreshScenario();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::OnVmsFormRefreshMessage(TMessage &Msg)
{
    for(int ii = 0; ii < FListForm->Count; ii++)
    {
        TVMS0201M *pForm = (TVMS0201M*)FListForm->Items[ii];
        pForm->RefreshScenario();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::OnVmsSelectMessage(TMessage &Msg)
{
    try {
        FSelVmsObj = (TVmsCtlr*)Msg.WParam;
        if (!FSelVmsObj) return;

        TVMSFORM0 *pSelForm = (TVMSFORM0*)Msg.LParam;
        if (pSelForm == FSelForm) return;

        if (FSelForm) FSelForm->SelectForm(false);

        FSelForm = pSelForm;
        if (FSelForm) FSelForm->SelectForm(true);
    } catch(...) {};
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::BtnMonitoringMngrClick(TObject *Sender)
{
    TVMSVIEWM *pForm = new TVMSVIEWM(this);
    pForm->FMonitoringType = FMonitoringType;
    pForm->ShowModal();
    bool bUpdate = pForm->FUpdate;
    SAFE_DELETE(pForm);

    if (bUpdate)
    {
        if (FSelForm) FSelForm->SelectForm(false);
        FSelVmsObj = NULL;
        FSelForm   = NULL;

        CreateVmsMsgForm(VmsCtlrManager->FMaxWidth);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::InitMonitoringForm()
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT COUNT(1) AS CNT          \r\n"
           "  FROM TB_VMS_MONITORING        \r\n"
           " WHERE MONITORING_TYPE = :p01   \r\n";

    VmsCtlrManager->FLists.Lock();
    try
    {
        int nFormSeq;
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", String(FMonitoringType));
            ITSDb_SQLOpen(pADO);
            int nFormCnt = pADO->FieldByName("CNT")->AsInteger;
            if (nFormCnt > 0) return;

    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);

            nFormSeq = 0;
            FOR_STL(TVmsCtlr*, pObj, VmsCtlrManager->FLists)
            {
                if (pObj->DEL_YN == "Y") continue;

                String sFormName = "Default";
                ITSDb_SQLBind(pADO, "p01", String(FMonitoringType));
                ITSDb_SQLBind(pADO, "p02", sFormName);
                ITSDb_SQLBind(pADO, "p03", String(++nFormSeq));
                ITSDb_SQLBind(pADO, "p04", String(pObj->VMS_CTLR_NMBR));

                ITSDb_SQLExec(pADO);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TVMS0200M::InitMonitoringForm", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TVMS0200M::InitMonitoringForm", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        VmsCtlrManager->FLists.UnLock();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::ActivateForm(bool AActivate)
{
    try
    {
        int nTabIndex = PgFormTab->ActivePageIndex;
        nTabIndex--;    //ActivePageIndex 으로 구간값은 숨어있는 탭을 포함한 인덱스
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMS0201M *pForm = (TVMS0201M*)FListForm->Items[ii];
            if (AActivate)
            {
                if (ii == nTabIndex) pForm->Play = true;
                else                 pForm->Play = false;
            }
            else
            {
                //비활성이면 모든 화면 비활성으로
                pForm->Play = false;
            }
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::PgFormTabPageChanging(TObject *Sender, TcxTabSheet *NewPage, bool &AllowChange)
{
    if (!FReady) return;

    try
    {
        int nTabIndex = NewPage->TabIndex;  //숨어있는 탭을 제외한 인덱스
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMS0201M *pForm = (TVMS0201M*)FListForm->Items[ii];
            if (nTabIndex == ii) pForm->Play = true;
            else                 pForm->Play = false;
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::BtnSearchClick(TObject *Sender)
{
    VmsCtlrManager->LoadVmsMsg();
    try
    {
        LockWindowUpdate(Handle);
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMS0201M *pForm = (TVMS0201M*)FListForm->Items[ii];
            pForm->RefreshScenario();
        }
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::ChkAutoWidthPropertiesChange(TObject *Sender)
{
    try
    {
        LockWindowUpdate(Handle);
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMS0201M *pForm = (TVMS0201M*)FListForm->Items[ii];

            pForm->TvVmsMsg->BeginUpdate();
            pForm->TvVmsMsg->OptionsView->ColumnAutoWidth = ChkAutoWidth->Checked;
            pForm->TvVmsMsg->EndUpdate();
        }
    }
    __finally
    {
        LockWindowUpdate(0);
        //BtnBestFit->Visible = !ChkAutoWidth->Checked;
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0200M::BtnBestFitClick(TObject *Sender)
{
    try
    {
        LockWindowUpdate(Handle);
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMS0201M *pForm = (TVMS0201M*)FListForm->Items[ii];

            pForm->TvVmsMsg->BeginUpdate();
            pForm->TvVmsMsg->ApplyBestFit(NULL, false, false);
            pForm->TvVmsMsg->EndUpdate();
        }
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

