/****************************************************************************
 * @source      : FrmVmsOpMainF.cpp
 * @description : 어플리케이션 메인폼
 ****************************************************************************
 * DATE         AUTHOR    DESCRIPTION
 * --------------------------------------------------------------------------
 * 2011/11/22   정승호    최초작성
 *
 ****************************************************************************/
//---------------------------------------------------------------------------

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

#include "FrmVmsLogSttsF.h"

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

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxHint"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxTextEdit"
#pragma link "dxBar"
#pragma link "dxCustomHint"
#pragma link "dxRibbon"
#pragma link "dxRibbonGallery"
#pragma link "dxRibbonSkins"
#pragma link "dxRibbonStatusBar"
#pragma link "dxScreenTip"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinChooserGallery"
#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 "dxSkinsdxBarPainter"
#pragma link "dxSkinsdxRibbonPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinsForm"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "dxStatusBar"
#pragma link "dxTabbedMDI"
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxDropDownEdit"
#pragma link "cxGroupBox"
#pragma link "cxSplitter"
#pragma link "cxTimeEdit"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#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 resource "*.dfm"

TFrmVmsLogStts *FrmVmsLogStts = NULL;

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

__fastcall TFrmVmsLogStts::TFrmVmsLogStts(TComponent* Owner)
    : TForm(Owner)//TdxCustomRibbonForm(Owner)//TForm(Owner)
{
    DoubleBuffered = true;

    g_AppCfg.lMainWinHandle = (long)Handle;

    LangTrans->Translate(this, ITSDb_GetConnection());

    CMM_SetFormPosition(g_AppCfg.bSaveForm);

    ITSSkin_Initialize(Application);
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

#if 0
    String sCaption = "";
    ReadConfigInfo(g_AppCfg.sConfigFile, "APPLICATION", "CAPTION", sCaption);
    ITSSkin_Caption(sCaption == "1" ? true : false);
#endif

    SetColorScheme(g_AppCfg.sSkinName);

    FFormList = new TList();
    TsList01->TabVisible = false;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogStts::FormCreate(TObject *Sender)
{
    Application->ShowMainForm = false;

    try {
        Application->Icon->LoadFromResourceName(((unsigned int)HInstance), "MAINICON");
    } catch(...) { ShowMessage("LoadFromResourceName failed"); }

    Application->ShowMainForm = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogStts::FormShow(TObject *Sender)
{
    Application->ProcessMessages();
    FormInit();
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmVmsLogStts::CommClose()
{
    try
    {
        for (int idx = FFormList->Count-1; idx >= 0; idx--)
        {
            TFrmVmsLogSttsSub *pLogForm = (TFrmVmsLogSttsSub*)FFormList->Items[idx];
            delete pLogForm;
            FFormList->Delete(idx);
        }
        delete FFormList;

        ITSSkin_Term();
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogStts::SetColorScheme(String ASkinName)
{
    dxSkinController1->SkinName = ASkinName;
    dxSkinController1->Refresh();
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmVmsLogStts::FormInit()
{
    DtStrDate->Date = Now();
    DtStrTime->DateTime = StrToDateTime("00:00");//Now();   //오늘 00시00분부터
    //DtStrTime->DateTime = Now();
    DtEndDate->Date = Now();
    DtEndTime->DateTime = Now();

    FillVmsList();
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogStts::FillVmsList()
{
    String     sQry;
    TADOQuery *pADO = NULL;

    CMM_ClearGridTableView(TvList);

    sQry = "SELECT A.*,                               \r\n"
           "       B.VMS_TYPE_NM,                     \r\n"
           "       B.VMS_WDTH,     B.VMS_HGHT,        \r\n"
           "       B.ROW_NUM,      B.COL_NUM,         \r\n"
           "       B.MODL_ROW_NUM, B.MODL_COL_NUM,    \r\n"
           "       B.POWR_ROW_NUM, B.POWR_COL_NUM     \r\n"
           "  FROM TB_VMS_CTLR A,                     \r\n"
           "       TB_VMS_TYPE B                      \r\n"
           " WHERE A.VMS_TYPE_CD = B.VMS_TYPE_CD      \r\n"
           "   AND A.DEL_YN      = 'N'                \r\n"
           " ORDER BY A.VMS_CTLR_NMBR                 \r\n";

    int nRow = 0;
    TcxDataController *pGDC = TvList->DataController;
    try
    {
        TvList->BeginUpdate(lsimImmediate);
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                String DEL_YN = pADO->FieldByName("DEL_YN")->AsString;
                if (DEL_YN == "Y") continue;

                String VMS_CTLR_NMBR    = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;
                String VMS_CTLR_ID      = pADO->FieldByName("VMS_CTLR_ID")->AsString;
                String VMS_NM           = pADO->FieldByName("VMS_NM")->AsString;
                String VMS_TYPE_CD      = pADO->FieldByName("VMS_TYPE_NM")->AsString;

                nRow = pGDC->AppendRecord();

                pGDC->Values[nRow][ColumnSel->Index] = false;
                pGDC->Values[nRow][Column00->Index]  = "-";
                pGDC->Values[nRow][Column01->Index]  = VMS_CTLR_NMBR;
                pGDC->Values[nRow][Column02->Index]  = VMS_CTLR_ID;
                pGDC->Values[nRow][Column03->Index]  = VMS_NM;
                pGDC->Values[nRow][Column11->Index]  = VMS_CTLR_NMBR;
                pGDC->Values[nRow][Column12->Index]  = VMS_CTLR_ID;
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS Information Query", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS Information 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 TFrmVmsLogStts::OnSubFormClose(TMessage Msg)
{
    int nActiveIdx = PgTab->ActivePageIndex;
    if (nActiveIdx <= 0) return;

    LockWindowUpdate(Handle);

    TcxTabSheet *pSheet = PgTab->ActivePage;
    if (pSheet)
    {
        delete pSheet;
    }
    nActiveIdx--;   // 첫번째 탭은 화면에 숨겨져 있다.
    FFormList->Delete(nActiveIdx);

    LockWindowUpdate(0);
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogStts::PgTabDblClick(TObject *Sender)
{
    TPoint APoint;
    APoint = PgTab->MouseDownPos;
    if (PgTab->IndexOfTabAt(APoint.x, APoint.y) != -1)
        POST_MSG(Handle, WM_SUBFORM_CLOSE, 0, 0);
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmVmsLogStts::BtnSearchClick(TObject *Sender)
{
    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(lblText3->Caption.c_str(),//L"검색 시작시각이 검색 종료시각 보다 큽니다.",
                                Caption.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStrDate;
        return;
    }
    FStrTime = FStrTime + "00";
    FEndTime = FEndTime + "59";

    FIdList = "";
    int nSelects = CMM_GetSelectCount(TvList, ColumnSel->Index, Column01->Index, FIdList);
    if (0 == nSelects)
    {
        Application->MessageBox(lblText4->Caption.c_str(),//L"VMS가 선택되지 않았습니다.\r\nVMS 목록에서 VMS를 선택하십시요.",
                                Caption.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        CxList->SetFocus();
        return;
    }

    String sTitle = "[" + FStrTime.SubString(1,10) + "-" + FEndTime.SubString(1,10) + "]";

    try
    {
        Application->ProcessMessages();
        LockWindowUpdate(Handle);

        TcxTabSheet *pSheet = CMM_AddTabSheet(PgTab, sTitle);
        if (!pSheet) return;

        TFrmVmsLogSttsSub *pLogForm = new TFrmVmsLogSttsSub(this, Handle, FStrTime, FEndTime, FIdList);
        pLogForm->Caption= Caption;
        pLogForm->Parent = pSheet;
        pLogForm->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        FFormList->Add(pLogForm);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmVmsLogStts::PgTabCanCloseEx(TObject *Sender, int ATabIndex, bool &ACanClose)
{
    int nActiveIdx = ATabIndex;
    if (nActiveIdx <= 0) return;

    nActiveIdx--;   // 첫번째 탭은 화면에 숨겨져 있다.
    FFormList->Delete(nActiveIdx);
}
//---------------------------------------------------------------------------

