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

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

#include "FRAME_FacilityListF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCalendar"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxDropDownEdit"
#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 "cxSpinEdit"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "cxContainer"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

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

    ColumnSel->Options->Filtering = false;
    //TvList->DataController->Filter->SupportedLike = false;
    m_pGDC = TvList->DataController;

//    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<시설물 정보가 없습니다>";

    if (!ItsCompanyManager)
    {
        ItsCompanyManager = new TItsCompanyManager();
    }
    ItsCompanyManager->LoadFromDb();

    //현장자산유무
	TcxComboBoxProperties *pcxCboProperties06 = (TcxComboBoxProperties*)Column06->Properties;
    pcxCboProperties06->Items->Clear();
	pcxCboProperties06->Items->Add("Y");
	pcxCboProperties06->Items->Add("N");

    //업체
    TcxComboBoxProperties *pcxCboProperties22 = (TcxComboBoxProperties *)Column22->Properties;
    TcxComboBoxProperties *pcxCboProperties23 = (TcxComboBoxProperties *)Column23->Properties;
    pcxCboProperties22->Items->Clear();
    pcxCboProperties23->Items->Clear();

    try
    {
        FOR_STL(TItsCompany*, pObj, ItsCompanyManager->FLists)
        {
            pcxCboProperties22->Items->Add("[" + pObj->CMPY_ID + "] " + pObj->CMPY_NM);
            pcxCboProperties23->Items->Add("[" + pObj->CMPY_ID + "] " + pObj->CMPY_NM);
        }
    }
    __finally
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFacilityList::UpdateList()
{
    TItsCompany *pComp;
	TDateTime    dtNull = NULL;

    CMM_ClearGridTableView(TvList);

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

        try
        {
            ItsFacilityManager->FLists.Lock();
            FOR_STL(TItsFacility*, pObj, ItsFacilityManager->FLists)
            {
                pObj->EditMode = DB_INIT;   //미리 초기화해준다(업데이트 확인을 위해)
                if (pObj->DEL_YN == "Y") continue;

                nRow = m_pGDC->AppendRecord();

                pObj->GridIndex = nRow;

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

                m_pGDC->Values[nRow][Column00->Index] = pObj->Deleted;
                m_pGDC->Values[nRow][Column01->Index] = "-";
                m_pGDC->Values[nRow][Column02->Index] = pObj->FCLT_ID;                          // FCLT_ID VARCHAR(50 BYTE) NOT NULL,    '시설물 ID';
                m_pGDC->Values[nRow][Column03->Index] = pObj->RFID_ID;                          // RFID_ID VARCHAR(50 BYTE) NOT NULL,    '시설물 ID';
                pLayer = ItsLayerManager->FLists.Find(pObj->FCLT_TYPE);
                if (pLayer)
                {
                    m_pGDC->Values[nRow][Column04->Index] = pLayer->Name;                        // FCLT_TYPE VARCHAR(7 BYTE) NOT NULL, '시설물유형';
                }
                else
                {
                    m_pGDC->Values[nRow][Column04->Index] = pObj->FCLT_TYPE;                        // FCLT_TYPE VARCHAR(7 BYTE) NOT NULL, '시설물유형';
                }
                m_pGDC->Values[nRow][Column05->Index] = pObj->FCLT_LCTN;                        // FCLT_LCTN VARCHAR(100 BYTE)  '시설물 위치';

                if(pObj->RSCE_YN.IsEmpty())
                m_pGDC->Values[nRow][Column06->Index] = "N";
                else
                m_pGDC->Values[nRow][Column06->Index] = pObj->RSCE_YN;                          // RSCE_YN CHAR(1 BYTE) DEFAULT 'N',   '현장자산유무';

                m_pGDC->Values[nRow][Column07->Index] = pObj->MDL_NM;                           // MDL_NM VARCHAR(30 BYTE)       '모델명';
                m_pGDC->Values[nRow][Column08->Index] = pObj->USE_YEAR;                         // USE_YEAR NUMBER(3)            '사용년수';

#if 0
                m_pGDC->Values[nRow][Column09->Index] = dtNull;   // INWR_DT VARCHAR(14 BYTE)      '입고일시';
                m_pGDC->Values[nRow][Column10->Index] = dtNull;   // OTWR_DT VARCHAR(14 BYTE)      '출고일시';
                m_pGDC->Values[nRow][Column11->Index] = dtNull;   // ISTL_DT VARCHAR(14 BYTE)      '설치일시';
                m_pGDC->Values[nRow][Column12->Index] = dtNull;   // TAKT_DT VARCHAR(14 BYTE)      '반출일시';
                m_pGDC->Values[nRow][Column14->Index] = dtNull;   // TAKN_DT VARCHAR(14 BYTE)      '반입일시';
                m_pGDC->Values[nRow][Column15->Index] = dtNull;   // ABDN_DT VARCHAR(14 BYTE)      '폐기일시';
                m_pGDC->Values[nRow][Column17->Index] = dtNull;   // ADTN_DT VARCHAR(14 BYTE)      '추가일시';
                m_pGDC->Values[nRow][Column19->Index] = dtNull;   // CHNG_DT VARCHAR(14 BYTE)      '변경일시';
#else
#if 0
                m_pGDC->Values[nRow][Column09->Index] = "";   // INWR_DT VARCHAR(14 BYTE)      '입고일시';
                m_pGDC->Values[nRow][Column10->Index] = "";   // OTWR_DT VARCHAR(14 BYTE)      '출고일시';
                m_pGDC->Values[nRow][Column11->Index] = "";   // ISTL_DT VARCHAR(14 BYTE)      '설치일시';
                m_pGDC->Values[nRow][Column12->Index] = "";   // TAKT_DT VARCHAR(14 BYTE)      '반출일시';
                m_pGDC->Values[nRow][Column14->Index] = "";   // TAKN_DT VARCHAR(14 BYTE)      '반입일시';
                m_pGDC->Values[nRow][Column15->Index] = "";   // ABDN_DT VARCHAR(14 BYTE)      '폐기일시';
                m_pGDC->Values[nRow][Column17->Index] = "";   // ADTN_DT VARCHAR(14 BYTE)      '추가일시';
                m_pGDC->Values[nRow][Column19->Index] = "";   // CHNG_DT VARCHAR(14 BYTE)      '변경일시';
#endif
#endif
                m_pGDC->Values[nRow][Column22->Index] = "";   // ISTL_CMPY_ID VARCHAR(30 BYTE) '설치업체ID';
                m_pGDC->Values[nRow][Column23->Index] = "";   // PRDC_CMPY_ID VARCHAR(30 BYTE) '제조업체ID';

                if (!pObj->INWR_DT.IsEmpty())
                m_pGDC->Values[nRow][Column09->Index] = ITSUtil_StrToTime(pObj->INWR_DT);   // INWR_DT VARCHAR(14 BYTE)      '입고일시';

                if (!pObj->OTWR_DT.IsEmpty())
                m_pGDC->Values[nRow][Column10->Index] = ITSUtil_StrToTime(pObj->OTWR_DT);   // OTWR_DT VARCHAR(14 BYTE)      '출고일시';

                if (!pObj->ISTL_DT.IsEmpty())
                m_pGDC->Values[nRow][Column11->Index] = ITSUtil_StrToTime(pObj->ISTL_DT);   // ISTL_DT VARCHAR(14 BYTE)      '설치일시';

                if (!pObj->TAKT_DT.IsEmpty())
                m_pGDC->Values[nRow][Column12->Index] = ITSUtil_StrToTime(pObj->TAKT_DT);   // TAKT_DT VARCHAR(14 BYTE)      '반출일시';

                m_pGDC->Values[nRow][Column13->Index] = pObj->TAKT_RESN;                        // TAKT_RESN VARCHAR(100 BYTE)   '반출사유';

                if (!pObj->TAKN_DT.IsEmpty())
                m_pGDC->Values[nRow][Column14->Index] = ITSUtil_StrToTime(pObj->TAKN_DT);   // TAKN_DT VARCHAR(14 BYTE)      '반입일시';

                if (!pObj->ABDN_DT.IsEmpty())
                m_pGDC->Values[nRow][Column15->Index] = ITSUtil_StrToTime(pObj->ABDN_DT);   // ABDN_DT VARCHAR(14 BYTE)      '폐기일시';

                m_pGDC->Values[nRow][Column16->Index] = pObj->ADTN_NM;                          // ADTN_NM VARCHAR(30 BYTE)      '추가자명';

                if (!pObj->ADTN_DT.IsEmpty())
                m_pGDC->Values[nRow][Column17->Index] = ITSUtil_StrToTime(pObj->ADTN_DT);   // ADTN_DT VARCHAR(14 BYTE)      '추가일시';

                m_pGDC->Values[nRow][Column18->Index] = pObj->CHNG_NM;                          // CHNG_NM VARCHAR(30 BYTE)      '변경자명';

                if (!pObj->CHNG_DT.IsEmpty())
                m_pGDC->Values[nRow][Column19->Index] = ITSUtil_StrToTime(pObj->CHNG_DT);   // CHNG_DT VARCHAR(14 BYTE)      '변경일시';

                m_pGDC->Values[nRow][Column20->Index] = pObj->AMT;                              // AMT NUMBER(15),               '금액';
                m_pGDC->Values[nRow][Column21->Index] = pObj->DEL_YN;                           // DEL_YN CHAR(1 BYTE)           '삭제여부';
                m_pGDC->Values[nRow][Column24->Index] = pObj->ISTL_LCTN_ADDR;

                if (!pObj->ISTL_CMPY_ID.IsEmpty())
                {
                    String sComp1 = "[" + pObj->ISTL_CMPY_ID + "] ";
                    pComp = ItsCompanyManager->FLists.Find(pObj->ISTL_CMPY_ID);
                    if (pComp) sComp1 = sComp1 + pComp->CMPY_NM;
                    m_pGDC->Values[nRow][Column22->Index] = sComp1;                                 // ISTL_CMPY_ID VARCHAR(30 BYTE) '설치업체ID';
                }
                if (!pObj->MNFC_CMPY_ID.IsEmpty())
                {
                    String sComp2 = "[" + pObj->MNFC_CMPY_ID + "] ";
                    pComp = ItsCompanyManager->FLists.Find(pObj->MNFC_CMPY_ID);
                    if (pComp) sComp2 = sComp2 + pComp->CMPY_NM;
                    m_pGDC->Values[nRow][Column23->Index] = sComp2;                                 // PRDC_CMPY_ID VARCHAR(30 BYTE) '제조업체ID';
                }

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

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

int  __fastcall TFRAMEFacilityList::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 TFRAMEFacilityList::TvListDataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList);
    LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
}
//---------------------------------------------------------------------------

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

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


