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

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

#include "FRAME_ParkingListF.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 resource "*.dfm"
//TFRAMEParkingList *FRAMEParkingList;
//---------------------------------------------------------------------------
__fastcall TFRAMEParkingList::TFRAMEParkingList(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ColumnSel->Options->Filtering = false;
    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<주차장 정보가 없습니다>";

	FCodeOper = ItsCodeManager->FLists.Find("POT");
	FCodeType = ItsCodeManager->FLists.Find("PFT");
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEParkingList::UpdateList(bool ARealYn/*=false*/)
{
    CMM_ClearGridTableView(TvList);

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

        TItsSubCode *pSubCode;
        try
        {
            PrltCtlrManager->FLists.Lock();
            FOR_STL(TPrltCtlr*, pObj, PrltCtlrManager->FLists)
            {
                if (pObj->DEL_YN != "N") continue;

                if (ARealYn && pObj->REAL_DATA_YN != "Y") continue;

                nRow = m_pGDC->AppendRecord();

                m_pGDC->Values[nRow][ColumnSel->Index] = false;                 //선택
                m_pGDC->Values[nRow][Column00->Index]  = "-";                   //편집
                m_pGDC->Values[nRow][Column01->Index]  = pObj->PRLT_CTLR_NMBR;
                m_pGDC->Values[nRow][Column11->Index]  = pObj->PRLT_CTLR_ID;

                m_pGDC->Values[nRow][Column02->Index] = "";
                if (FCodeOper)
                {
                    pSubCode = FCodeOper->FSubLists.Find(pObj->PRLT_OPER_DVSN_CD);
                    if (pSubCode) m_pGDC->Values[nRow][Column02->Index] = pSubCode->CMMN_CD_KOR_NM;
                }

                m_pGDC->Values[nRow][Column03->Index] = "";
                if (FCodeType)
                {
                    pSubCode = FCodeType->FSubLists.Find(pObj->PRLT_TYPE_DVSN_CD);
                    if (pSubCode) m_pGDC->Values[nRow][Column03->Index] = pSubCode->CMMN_CD_KOR_NM;
                }

                m_pGDC->Values[nRow][Column04->Index]  = pObj->PRLT_NM;
                //m_pGDC->Values[nRow][Column05->Index]  = pObj->VALD_YN == "Y" ? "사용" : "사용안함";
                m_pGDC->Values[nRow][Column06->Index]  = pObj->PRLT_CTLR_IP;
                m_pGDC->Values[nRow][Column07->Index]  = pObj->PRLT_CTLR_PORT;
                m_pGDC->Values[nRow][Column08->Index]  = pObj->CTLR_CLCT_CYCL;
                m_pGDC->Values[nRow][Column09->Index]  = pObj->CTLR_STTS_CYCL;
                m_pGDC->Values[nRow][Column10->Index]  = pObj->WHOL_NPLS;
                m_pGDC->Values[nRow][Column13->Index]  = pObj->REAL_DATA_YN == "Y" ? "사용" : "사용안함";
                m_pGDC->Values[nRow][Column99->Index]  = (int)pObj;
            }
        }
        __finally
        {
            PrltCtlrManager->FLists.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 TFRAMEParkingList::BtnAllSelectClick(TObject *Sender)
{
    TcxButton *pBtn = (TcxButton*)Sender;
    CMM_CheckAllListItem(TvList, ColumnSel->Index, pBtn->Tag);
}
//---------------------------------------------------------------------------

int  __fastcall TFRAMEParkingList::GetSelIds(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;
}
//---------------------------------------------------------------------------

int  __fastcall TFRAMEParkingList::GetSelIds(TStringList *AStringList)
{
    int nSelCnt = 0;

    try
    {
        TvList->BeginUpdate();

        int nRowCnt = TvList->ViewData->RecordCount;

        for (int ii = 0; ii < nRowCnt; ii++)
        {
            TcxCustomGridRow* ARow = TvList->ViewData->Rows[ii];
            if (((bool)ARow->Values[ColumnSel->Index]))
            {
                AStringList->Add(ARow->Values[Column01->Index]);
                nSelCnt++;
            }
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------

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

void __fastcall TFRAMEParkingList::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= "주차장정보";

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

void __fastcall TFRAMEParkingList::OnCloseQuery(bool &CanClose)
{
    //
}
//---------------------------------------------------------------------------

