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

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

#include "IHS0020MF.h"
#include "IHS00201F.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 "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

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

    FTitle = Caption;//"프로세스 상태이력";
    m_pFormList = new TList(); 
    TsList01->TabVisible = false;
}
//---------------------------------------------------------------------------

void __fastcall TIHS0020M::CommClose()
{
    try
    {
        for (int idx = m_pFormList->Count-1; idx >= 0; idx--)
        {
            m_pFormList->Delete(idx);
        }
        delete m_pFormList;

        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

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

void __fastcall TIHS0020M::FormInit()
{
    DtStDate->Date = Now();// - 1;
    DtEdDate->Date = Now();// - 1;
    DtStTime->EditValue = "00";//Now().FormatString("hh");
    DtEdTime->EditValue = "23";//Now().FormatString("hh");

    CbType->Properties->Items->Clear();
    try
    {
        ItsProcessManager->FLists.Lock();
        FOR_STL(TItsProcess*, pObj, ItsProcessManager->FLists)
        {
            if (pObj->DEL_YN == "Y") continue;
            if (pObj->STTS_DSPL_YN == "N") continue;

            String sSystType = pObj->SYST_TYPE;
            String sSystName = pObj->SYST_NM;
            String sSystId   = pObj->SYST_ID;
            CbType->Properties->Items->Add("[" + sSystId + "] " + sSystName);
        }
    }
    __finally
    {
        ItsProcessManager->FLists.UnLock();
    }
    CbType->ItemIndex = 0;
}
//---------------------------------------------------------------------------

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

void __fastcall TIHS0020M::RefreshData()
{
    // 검색조건에 의한 링크 데이터 조회
    try
    {
        //SelListData();
	}
	__finally
	{
	}
}
//---------------------------------------------------------------------------

void __fastcall TIHS0020M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

	String sStDateTime = DtStDate->Date.FormatString("yyyymmdd");
	String sEdDateTime = DtEdDate->Date.FormatString("yyyymmdd") + DtEdTime->Time.FormatString("hh");
#if 0
    if (sStDateTime > sEdDateTime)
    {
        Application->MessageBox(FrmLang->lblQryDtErr->Caption.c_str(),//L"검색 시작시각이 검색 종료시각 보다 큽니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStDate;
        return;
    }
    FStDateTime = sStDateTime + "0000";
    FEdDateTime = sEdDateTime + "5959";
#endif
    FStDateTime = sStDateTime + "000000";
    FEdDateTime = sStDateTime + "235959";

    FIdList = "";

    int FRptType = CbType->ItemIndex;
    String sType = CbType->Text;
    String sTitle;
    sTitle = sType + "[" + sStDateTime + "]";

    String sSystId = ITSUtil_GetCode(CbType->Text);
    try
    {
        Application->ProcessMessages();
        LockWindowUpdate(Handle);

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

        IHS00201 = new TIHS00201(this, Handle, FRptType, sSystId, FStDateTime, FEdDateTime, sTitle);
        IHS00201->Parent = pSheet;
        IHS00201->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        m_pFormList->Add(IHS00201);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

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

void __fastcall TIHS0020M::OnSubFormClose(TMessage Msg)
{
    int nActiveIdx = PgTab->ActivePageIndex;
    if (nActiveIdx <= 0) return;

    LockWindowUpdate(Handle);

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

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

void __fastcall TIHS0020M::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 TIHS0020M::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    IHS0020M = NULL;
    Action = caFree;    
}
//---------------------------------------------------------------------------

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

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

