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

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

#include "FRAME_DsrcOfferSectListF.h"
#include "CDSRoadF.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 "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"
//TFRAMEDsrcOfferSectList *FRAMEDsrcOfferSectList;
//---------------------------------------------------------------------------
__fastcall TFRAMEDsrcOfferSectList::TFRAMEDsrcOfferSectList(TComponent* Owner)
    : TFrame(Owner)
{
    ColumnSel->Options->Filtering = false;
    m_pGDC = TvList->DataController;

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

	//FCodeIFD = ItsCodeManager->FLists.Find("IFD"); // 방향코드
}
//---------------------------------------------------------------------------

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

    int nRow = 0;
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.OFFR_SECT_ID, A.OFFR_DRCT_NM, A.ID, B.PRE_ID \r\n"
           "  FROM TB_RSE_OFFR_SECT_MNGM A,                       \r\n"
           "       TB_RSE_OFFR_DRCT_INFR B                        \r\n"
           " WHERE A.ID = B.ID                                    \r\n"
           "   AND A.OBU_ENTR_DRCT_NMBR = B.OBU_ENTR_DRCT_NMBR    \r\n"
           " ORDER BY OFFR_SECT_ID                                \r\n";
           //" ORDER BY TO_NUMBER(OFFR_SECT_ID)                     \r\n";
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
            pADO->Close();
            pADO->Connection = ITSDb_GetConnection();
            pADO->SQL->Clear();
            pADO->SQL->Text = sQry;

            pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
                nRow = m_pGDC->AppendRecord();

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

                m_pGDC->Values[nRow][Column01->Index] = pADO->FieldByName("OFFR_SECT_ID")->AsString;
                m_pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("OFFR_DRCT_NM")->AsString;
                m_pGDC->Values[nRow][Column03->Index] = pADO->FieldByName("ID")->AsString;
                m_pGDC->Values[nRow][Column04->Index] = pADO->FieldByName("PRE_ID")->AsString;

                String sId = pADO->FieldByName("OFFR_SECT_ID")->AsString;
                m_pGDC->Values[nRow][Column11->Index] = sId.ToIntDef(0);              //ID
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
        catch(...)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            throw Exception(FrmLang->lblDbErr->Caption);//"알수없는 DB 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        Column11->SortIndex = Column11->Index;
        Column11->SortOrder = soAscending;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

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

int  __fastcall TFRAMEDsrcOfferSectList::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 TFRAMEDsrcOfferSectList::GetSelIds(TStringList *AStringList)
{
    int nSelCnt = 0;

    try
    {
        TvList->BeginUpdate();

#if 0
        int nRowCnt = m_pGDC->RecordCount;
        for (int ii = 0; ii < nRowCnt; ii++)
        {
            if (((bool)m_pGDC->Values[ii][ColumnSel->Index]))
            {
                AStringList->Add(m_pGDC->Values[ii][Column00->Index]);
                nSelIdx++;
            }
        }
#else
        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++;
            }
        }
#endif
    }
    __finally
    {
        TvList->EndUpdate();
    }
    return nSelCnt;
}
//---------------------------------------------------------------------------

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

void __fastcall TFRAMEDsrcOfferSectList::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= "DSRC-RSE정보제공구간정보";

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

void __fastcall TFRAMEDsrcOfferSectList::OnCloseQuery(bool &CanClose)
{
    try {
    } catch(...) {}
}
//---------------------------------------------------------------------------

