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

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

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

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_FacilityFailF"
#pragma link "FRAME_FacilityFailActionF"
#pragma link "cxButtons"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxSplitter"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma resource "*.dfm"

TIFM0030M *IFM0030M = NULL;

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

    BtnErrorHistory->Visible = true;

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

void __fastcall TIFM0030M::CommClose()
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIFM0030M::FormInit()
{
    m_pGDC = FRAMEFacilityList1->TvList->DataController;
}
//---------------------------------------------------------------------------

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

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

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

    RefreshData();
}
//---------------------------------------------------------------------------

void __fastcall TIFM0030M::RefreshData()
{
    FRAMEFacilityList1->UpdateList();
}
//---------------------------------------------------------------------------

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();
    }
}
//---------------------------------------------------------------------------

