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

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

#include "DSRH070MF.h"
#include "DSRH0701F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_DsrcListF"
#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"

TDSRH070M *DSRH070M = NULL;
//---------------------------------------------------------------------------
__fastcall TDSRH070M::TDSRH070M(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;
    FRAMEDsrcList1->PnlBottom->Visible = false;
}
//---------------------------------------------------------------------------

void __fastcall TDSRH070M::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 TDSRH070M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

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

    InitCode();
}
//---------------------------------------------------------------------------

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

    FRAMEDsrcList1->UpdateList();
}
//---------------------------------------------------------------------------

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

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

    int STAT = CbStat->ItemIndex;

    if (STAT == 0)
    {
#if 0
        FStDateTime = DtStDate->Date.FormatString("yyyymmdd") + DtStTime->Time.FormatString("hh");
        FEdDateTime = DtEdDate->Date.FormatString("yyyymmdd") + DtEdTime->Time.FormatString("hh");
#else
        FStDateTime = DtStDate->Date.FormatString("yyyymmdd") + DtStTime->Time.FormatString("hhnn");
        FEdDateTime = DtEdDate->Date.FormatString("yyyymmdd") + DtEdTime->Time.FormatString("hhnn");
#endif
    }
    else
    {
        FStDateTime = DtStDate->Date.FormatString("yyyymmdd");
        FEdDateTime = DtEdDate->Date.FormatString("yyyymmdd");
    }

    if (FStDateTime > FEdDateTime)
    {
        Application->MessageBox(FrmLang->lblQryDtErr->Caption.c_str(),//L"검색 시작시각이 검색 종료시각 보다 큽니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStDate;
        return;
    }

    String sTitle;
    if (STAT == 0)
    {
        sTitle = "시간통계 [" + FStDateTime + " ~ " + FEdDateTime + "]";
#if 0
        FStDateTime = FStDateTime + "0000";
        FEdDateTime = FEdDateTime + "5959";
#else
        FStDateTime = FStDateTime + "00";
        FEdDateTime = FEdDateTime + "59";
#endif
    }
    else
    {
        sTitle = "일통계 [" + FStDateTime + " ~ " + FEdDateTime + "]";
        FStDateTime = FStDateTime + "000000";
        FEdDateTime = FEdDateTime + "235959";
    }

    FIdList = "";
    int nSelects = CMM_GetSelectCount(FRAMEDsrcList1->TvList, FRAMEDsrcList1->ColumnSel->Index, FRAMEDsrcList1->Column01->Index, FIdList);
    if (0 == nSelects)
    {
        Application->MessageBox(FrmLang->lblSelLstErr->Caption.c_str(),//L"검색 목록을 선택하지 않았습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        FRAMEDsrcList1->CxList->SetFocus();
        return;
    }

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

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

        DSRH0701 = new TDSRH0701(this, Handle, FStDateTime, FEdDateTime, FIdList, STAT);
        DSRH0701->Parent = pSheet;
        DSRH0701->Show();
        PgTab->ActivePage = pSheet;
        pSheet = PgTab->ActivePage;
        m_pFormList->Add(DSRH0701);
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

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

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

void __fastcall TDSRH070M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    FRAMEDsrcList1->OnCloseQuery(CanClose);
}
//---------------------------------------------------------------------------

void __fastcall TDSRH070M::InitCode()
{
#if 0
    TItsCode *FCodeRCT;
    TItsCode *FCodeOBU;
    FCodeRCT = ItsCodeManager->FLists.Find("RCT");
    FCodeOBU = ItsCodeManager->FLists.Find("OBU");

    // 차종
    CbCTyp->Properties->Items->Clear();
    CbCTyp->Properties->Items->Add(" [ALL] 전체");
	if (FCodeRCT)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeRCT->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			CbCTyp->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbCTyp->ItemIndex = 0;

    // OBU 종류
    CbObuKind->Properties->Items->Clear();
    CbObuKind->Properties->Items->Add(" [ALL] 전체");
	if (FCodeOBU)
	{
		FOR_STL(TItsSubCode *, pSubCode, FCodeOBU->FSubLists)
		{
			if (pSubCode->USE_YN == "N") continue;
			CbObuKind->Properties->Items->Add(" [" + pSubCode->CMMN_CD + "] " + pSubCode->CMMN_CD_KOR_NM);
		}
	}
    CbObuKind->ItemIndex = 0;
#endif
}
//---------------------------------------------------------------------------


void __fastcall TDSRH070M::CbStatPropertiesChange(TObject *Sender)
{
    switch(CbStat->ItemIndex)
    {
    case 0: //시간 통계
        DtStTime->Visible = true;
        DtEdTime->Visible = true;
        break;
    case 1: //일 통계
        DtStTime->Visible = false;
        DtEdTime->Visible = false;
        break;
    }
}
//---------------------------------------------------------------------------

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

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

