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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "CDSFacilityF.h"
#include "CDSFacilityFailF.h"
#pragma hdrstop

#include "IFM0030MF.h"
#include "IHS0050MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#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 "cxLabel"
#pragma link "cxStyles"
#pragma link "dxSkinscxPCPainter"
#pragma link "FRAME_FacilityFailF"
#pragma link "FRAME_FacilityFailActionF"
#pragma link "dxSkinsForm"
#pragma resource "*.dfm"

TIFM0030M *IFM0030M = NULL;

//---------------------------------------------------------------------------
__fastcall TIFM0030M::TIFM0030M(TComponent* Owner)
    : TForm(Owner)
{
#ifdef D_GJ
    BtnErrorHistory->Visible = true;
#else
    BtnErrorHistory->Visible = false;
#endif
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FRAMEFacilityList1->PnlTop->Visible = false;
    FRAMEFacilityList1->TvList->OptionsData->Editing = false;
}
//---------------------------------------------------------------------------

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

/*
 * form 초기화
 *
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TIFM0030M::FormInit()
{
    m_pGDC = FRAMEFacilityList1->TvList->DataController;
}
//---------------------------------------------------------------------------

/*
 * Form을 보여줄때 호출되는 event 메서드이다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TIFM0030M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

/*
 * Form이 Show되고 난 후 최초 1회 수행되는 타이머 이벤트
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TIFM0030M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    BtnSearchClick((TObject*)BtnSearch);
}
//---------------------------------------------------------------------------

/*
 * Search 버튼 클릭 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TIFM0030M::BtnSearchClick(TObject *Sender)
{
    RefreshData();
}
//---------------------------------------------------------------------------

/*
 * Refresh Data Event Function
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TIFM0030M::RefreshData()
{
    FRAMEFacilityList1->UpdateList();
}
//---------------------------------------------------------------------------

/*
 * Close 버튼 클릭 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TIFM0030M::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

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

void __fastcall TIFM0030M::FRAMEFacilityList1TvListFocusedRecordChanged(TcxCustomGridTableView *Sender, TcxCustomGridRecord *APrevFocusedRecord,
          TcxCustomGridRecord *AFocusedRecord, bool ANewItemRecordFocusingChanged)
{
	if (!AFocusedRecord)
	{
        FRAMEFacilityFail1->UpdateCheck();
		FRAMEFacilityFail1->UpdateList(NULL);
		return;
	}

    if (FRAMEFacilityList1->TvList->ViewData->RecordCount <= 0) return;
    int nIndex = m_pGDC->FocusedRecordIndex;
    if (nIndex < 0) return;

    int nMemPtr = m_pGDC->Values[nIndex][FRAMEFacilityList1->Column99->Index];
    if (nMemPtr == 0) return;

    FRAMEFacilityFail1->UpdateCheck();
    FRAMEFacilityFail1->UpdateList((TItsFacility*)nMemPtr);
}
//---------------------------------------------------------------------------

void __fastcall TIFM0030M::FRAMEFacilityFail1TvListFocusedRecordChanged(TcxCustomGridTableView *Sender, TcxCustomGridRecord *APrevFocusedRecord,
          TcxCustomGridRecord *AFocusedRecord, bool ANewItemRecordFocusingChanged)
{
	if (!AFocusedRecord)
	{
        FRAMEFacilityFailAction1->UpdateCheck();
		FRAMEFacilityFailAction1->UpdateList(NULL);
		return;
	}

    if (FRAMEFacilityFail1->TvList->ViewData->RecordCount <= 0) return;
    int nIndex = FRAMEFacilityFail1->TvList->DataController->FocusedRecordIndex;
    if (nIndex < 0) return;

    int nMemPtr = FRAMEFacilityFail1->TvList->DataController->Values[nIndex][FRAMEFacilityFail1->Column99->Index];
    if (nMemPtr == 0) return;

    FRAMEFacilityFailAction1->UpdateCheck();
    FRAMEFacilityFailAction1->UpdateList((TItsFacilityFail*)nMemPtr);
}
//---------------------------------------------------------------------------

void __fastcall TIFM0030M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    FRAMEFacilityFailAction1->OnCloseQuery(CanClose);
    FRAMEFacilityFail1->OnCloseQuery(CanClose);

    try
    {
        if (ItsFacilityFailManager)
        {
            delete ItsFacilityFailManager;
            ItsFacilityFailManager = NULL;
        }
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIFM0030M::BtnErrorHistoryClick(TObject *Sender)
{
    if (IHS0050M)
    {
        try {
        ShowWindow(IHS0050M->Handle, SW_RESTORE);
        IHS0050M->BringToFront();
        } catch(...) {}
    }
    else
    {
        Application->CreateForm(__classid(TIHS0050M), &IHS0050M);
        if (IHS0050M) IHS0050M->Show();
    }
}
//---------------------------------------------------------------------------
DWORD  CUSTOM_ITEMS[2] = { COLOR_ACTIVECAPTION, COLOR_CAPTIONTEXT };
TColor CUSTOM_COLORS[2] = { clRed, clYellow };
TColor FStandardColors[2];
void __fastcall TIFM0030M::FormActivate(TObject *Sender)
{
#if 0
    FStandardColors[0] = GetSysColor(COLOR_ACTIVECAPTION);
    FStandardColors[1] = GetSysColor(COLOR_CAPTIONTEXT  );
    SetSysColors(2, (CONST INT *)CUSTOM_ITEMS, (CONST COLORREF *)CUSTOM_COLORS);
#endif
}
//---------------------------------------------------------------------------

