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

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

#include "FRAME_AviSectList2F.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxContainer"
#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 "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma resource "*.dfm"
//TFRAMEAviRseList *FRAMEAviRseList;
//---------------------------------------------------------------------------
__fastcall TFRAMEAviSectList2::TFRAMEAviSectList2(TComponent* Owner)
    : TFrame(Owner)
{
    m_pGDC = TvList->DataController;

    TvList->OptionsView->GroupByBox = false;
    TvList->OptionsView->NoDataToDisplayInfoText = "<AVI 구간 정보가 없습니다>";

    MyItsAviManager = new TItsAviManager();
    MyItsAviManager->LoadFromDb();
    MyItsAviManager->LoadFromSectDb();
	FCodeIFD = ItsCodeManager->FLists.Find("IFD"); //제어기유형
}
//---------------------------------------------------------------------------

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

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

        try
        {
            MyItsAviManager->FLists.Lock();
            FOR_STL(TItsAvi*, pAvi, MyItsAviManager->FLists)
            {
                if (pAvi->DEL_YN == "Y") continue;

                FOR_STL(TItsAviSect*, pObj, pAvi->FSubLists)
                {
                    if (pObj->DEL_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]  = pAvi->AVI_LCTN_NM;     //제어기위치
                    m_pGDC->Values[nRow][Column02->Index]  = pObj->AVI_SECT_NMBR;   //AVI ID
                    m_pGDC->Values[nRow][Column03->Index]  = pObj->AVI_SECT_NM;     //위치
                    m_pGDC->Values[nRow][Column99->Index]  = (int)pObj;
                }
            }
        }
        __finally
        {
            MyItsAviManager->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();
        CMM_ExpandCollapseChk(TvList, true);
    }
}
//---------------------------------------------------------------------------

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

int  __fastcall TFRAMEAviSectList2::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][Column02->Index]);
    ALinkIds = sLinkId;
    return 1;
}
//---------------------------------------------------------------------------

int  __fastcall TFRAMEAviSectList2::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[Column02->Index]);
                nSelCnt++;
            }
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------

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

void __fastcall TFRAMEAviSectList2::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= "AVI구간정보";

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

void __fastcall TFRAMEAviSectList2::OnCloseQuery(bool &CanClose)
{
    try {
        if (MyItsAviManager)
        {
            delete MyItsAviManager;
            MyItsAviManager = NULL;
        }
    } catch(...) {}
}
//---------------------------------------------------------------------------

