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

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

#include "FRAME_UnRegFacilityListF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "cxContainer"
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
__fastcall TFRAMEUnRegFacilityList::TFRAMEUnRegFacilityList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

    ColumnSel->Options->Filtering = false;
    m_pGDC = TvList->DataController;

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

void __fastcall TFRAMEUnRegFacilityList::UpdateList()
{
    CMM_ClearGridTableView(TvList);

    int nRow = 0;
    try
    {
        TvList->BeginUpdate();

        try
        {
            ItsFacilityManager->FRegLists.Lock();
            FOR_STL(TItsRegFacility*, pObj, ItsFacilityManager->FRegLists)
            {
                if (pObj->UnRegisterd == "N") continue;

                nRow = m_pGDC->AppendRecord();

                pObj->GridIndex = nRow;

                m_pGDC->Values[nRow][ColumnSel->Index] = true;                 //선택

                m_pGDC->Values[nRow][Column01->Index] = pObj->Type;
                m_pGDC->Values[nRow][Column02->Index] = pObj->Id;
                m_pGDC->Values[nRow][Column03->Index] = pObj->Name;

                m_pGDC->Values[nRow][Column99->Index] = (int)pObj;
            }
        }
        __finally
        {
            ItsFacilityManager->FRegLists.UnLock();
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

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

int  __fastcall TFRAMEUnRegFacilityList::GetSelectIds(String &ALinkIds)
{
    ALinkIds = "";

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

    String sLinkId = VarToStr(m_pGDC->Values[nIndex][Column01->Index]);
    ALinkIds = sLinkId;
    return 1;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEUnRegFacilityList::TvListDataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList);
    LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEUnRegFacilityList::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= lblTitle->Caption;//"미등록시설물정보";

    CMM_ExportToExcelFile(sTitle, pGrid, pView, (TForm*)this);
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEUnRegFacilityList::TvListMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
    try
    {
        TcxGridSite *ASite = (TcxGridSite *)Sender;

        //Screen->Cursor = crDefault;

        //TcxCustomGridHitTest *AHitTest = tvSel->GetHitTest(X, Y);
        TcxCustomGridHitTest *AHitTest = dynamic_cast<TcxGridRecordHitTest*>(ASite->ViewInfo->GetHitTest(X, Y));
        if (dynamic_cast<TcxGridRecordHitTest*>(ASite->ViewInfo->GetHitTest(X, Y)))
        {
            int ARow = ((TcxGridRecordCellHitTest*)AHitTest)->GridRecord->Index;
            if (((TcxGridRecordCellHitTest*)AHitTest)->Item == NULL) return;
            int ACol = ((TcxGridRecordCellHitTest*)AHitTest)->Item->Index;
            if (ACol == ColumnSel->Index &&
                ARow < ASite->GridView->DataController->RecordCount-1)
            {
                Screen->Cursor = crHandPoint;
            }
        }
        else
        {
            Screen->Cursor = crDefault;
        }
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

