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

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

//---------------------------------------------------------------------------
#include "CDSItsCrossInfoF.h"
//---------------------------------------------------------------------------
#include "FRAME_ItsCrossF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxInplaceContainer"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "cxTL"
#pragma link "cxTLdxBarBuiltInMenu"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma resource "*.dfm"
//TFRAMEItsCross *FRAMEItsCross;
//---------------------------------------------------------------------------
__fastcall TFRAMEItsCross::TFRAMEItsCross(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    FCompleted = false;
    tlItsCross->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsCross::InitItsCross()
{
    FCompleted = false;
    try
    {
        tlItsCross->BeginUpdate();
        tlItsCross->Clear();
        TcxTreeListNode *pRoot = tlItsCross->Root;

        FOR_STL(TRCrossRoad*, pObjSido, RCrossManager->FLists)
        {
            if (pObjSido->FCrosss.Size() == 0) continue;
            if (!pObjSido->FShow) continue;

            TcxTreeListNode *pCenter = pRoot->AddChild();
            pCenter->CheckGroupType = ncgCheckGroup;

            pCenter->Values[colItsCross->ItemIndex] = pObjSido->ROAD_NAME;
            pCenter->Values[colMem->ItemIndex] = 0;

            FOR_STL(TRCross*, pObjCross, pObjSido->FCrosss)
            {
                if (!pObjCross->FShow) continue;

                TcxTreeListNode *pCross = pCenter->AddChild();
                pCross->CheckGroupType = ncgCheckGroup;

                pCross->Values[colItsCross->ItemIndex] = pObjCross->CROSS_NAME;
                pCross->Values[colMem->ItemIndex] = 0;//(int)pObjCross;

                FOR_STL(TRCrossDir*, pObjLink, pObjCross->FLinks)
                {
                    if (!pObjLink->FShow) continue;

                    TcxTreeListNode *pLink = pCross->AddChild();
                    pLink->CheckGroupType = ncgCheckGroup;

                    pLink->Values[colItsCross->ItemIndex] = pObjLink->DIR_NAME;
                    pLink->Values[colMem->ItemIndex] = (int)pObjLink;
                }
            }
        }
    }
    __finally
    {
        tlItsCross->EndUpdate();
        FCompleted = true;
    }
    //ShowMessage("Node count = " + IntToStr(tlItsCross->AbsoluteCount));
    //tlItsCross->FullExpand;
}
//---------------------------------------------------------------------------

class TcxTreeListAccess : public TcxTreeList
{
};
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsCross::InitFilter(bool AShow)
{
#if 0
    FOR_STL(TRSido*, pObjSido, RLinkManager->FLists)
    {
        pObjSido->FShow = AShow;

        FOR_STL(TRCross*, pObjCross, pObjSido->FCrosss)
        {
            pObjCross->FShow = AShow;

            FOR_STL(TRLink*, pObjLink, pObjCross->FLinks)
            {
                pObjLink->FShow = AShow;
            }
        }
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsCross::FilterItsCross(String AFilter)
{
#if 0
    if (AFilter.IsEmpty())
    {
        InitFilter(true);
        InitItsCross();
        return;
    }

    InitFilter(false);
    FOR_STL(TRSido*, pObjSido, RLinkManager->FLists)
    {
        if (pObjSido->NAME.Pos(AFilter) > 0)
        {
            pObjSido->FShow = true;
            FOR_STL(TRCross*, pObjCross, pObjSido->FCrosss)
            {
                pObjCross->FShow = true;

                FOR_STL(TRLink*, pObjLink, pObjCross->FLinks)
                {
                    pObjLink->FShow = true;
                }
            }
        }
        else
        {
            FOR_STL(TRCross*, pObjCross, pObjSido->FCrosss)
            {
                if (pObjCross->NAME.Pos(AFilter) > 0)
                {
                    pObjSido->FShow = true;
                    pObjCross->FShow = true;

                    FOR_STL(TRLink*, pObjLink, pObjCross->FLinks)
                    {
                        pObjLink->FShow = true;
                    }
                }
                else
                {
                    FOR_STL(TRLink*, pObjLink, pObjCross->FLinks)
                    {
                        if (pObjLink->LINKID.Pos(AFilter)     > 0 ||
                            pObjLink->STNODENAME.Pos(AFilter) > 0 ||
                            pObjLink->EDNODENAME.Pos(AFilter) > 0 )
                        {
                            pObjSido->FShow = true;
                            pObjCross->FShow = true;
                            pObjLink->FShow = true;
                        }
                    }
                }
            }
        }
    }
    InitItsCross();
#endif
    tlItsCross->FullExpand();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsCross::FilterItsCrossA(String AFilter)
{
    if (AFilter.IsEmpty())
    {
        UnFilterItsCross();
        return;
    }

    //TcxTreeListAccess *pTlList = (TcxTreeListAccess*)tlItsCross;
    //pTlList->ChangesLocked = true;

    int ii;
    tlItsCross->BeginUpdate();
    try
    {
        //그룹을 모두 숨긴다.
        for (ii = 0; ii < tlItsCross->AbsoluteCount; ii++)
        {
            if (tlItsCross->AbsoluteItems[ii]->IsGroupNode)
            {
                tlItsCross->AbsoluteItems[ii]->Visible = false;
            }
        }

        for (ii = 0; ii < tlItsCross->AbsoluteCount; ii++)
        {
            if (tlItsCross->AbsoluteItems[ii]->IsGroupNode) continue;

            tlItsCross->AbsoluteItems[ii]->CheckState = cbsUnchecked;

            String sItsCrossName = tlItsCross->AbsoluteItems[ii]->Texts[colItsCross->ItemIndex];
            if (sItsCrossName.Pos(AFilter) == 0)
            {
                tlItsCross->AbsoluteItems[ii]->Visible = false;
            }
            else
            {
                TcxTreeListNode *pCenter = tlItsCross->AbsoluteItems[ii]->Parent;
                if (pCenter)
                {
                    pCenter->Visible = true;
                }
                tlItsCross->AbsoluteItems[ii]->Visible = true;
            }
        }
    }
    __finally
    {
        tlItsCross->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsCross::UnFilterItsCross()
{
    int ii;
    tlItsCross->BeginUpdate();
    try
    {
        for (ii = 0; ii < tlItsCross->AbsoluteCount; ii++)
        {
            if (!tlItsCross->AbsoluteItems[ii]->IsGroupNode)
            {
                tlItsCross->AbsoluteItems[ii]->CheckState = cbsUnchecked;
            }
            tlItsCross->AbsoluteItems[ii]->Visible = true;
        }
    }
    __finally
    {
        tlItsCross->EndUpdate();
    }
}
//---------------------------------------------------------------------------

AnsiString __fastcall TFRAMEItsCross::GetHTMLString()
{
    AnsiString sHtml = "";
    AnsiString sTemp;
    if (!FCompleted) return lblAll->Caption;//"전체";

    int ii, jj;
    tlItsCross->BeginUpdate();
    try
    {
        TcxTreeListNode *pRoot = tlItsCross->Root->GetNext();
        while(pRoot)
        {
            if (pRoot->CheckState == cbsUnchecked ||
                pRoot->Visible == false)
            {
                pRoot = pRoot->GetNext();
                continue;
            }

            for (ii = 0; ii < pRoot->Count; ii++)
            {
                TcxTreeListNode *pCross = pRoot->Items[ii];
                if (!pCross->Visible ||
                    pCross->CheckState == cbsUnchecked)
                {
                    continue;
                }

                int nChecked = 0;
                for (jj = 0; jj < pCross->Count; jj++)
                {
                    if (!pCross->Items[jj]->Visible)
                        continue;
                    if (pCross->Items[jj]->Checked) nChecked++;
                }
                if (nChecked > 0)
                {
                    sTemp = pCross->Texts[colItsCross->ItemIndex] + "(" + String(nChecked) + ")";
                    if (sHtml.IsEmpty() == false)
                    {
                        sHtml += ", ";
                    }
                    sHtml += sTemp;
                }
            }
            pRoot = pRoot->GetNext();
        }
    }
    __finally
    {
        tlItsCross->EndUpdate();
    }

    if (sHtml.IsEmpty()) sHtml = lblAll->Caption;//"전체";
    return sHtml;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsCross::GetCondition(STCondition& ACondition)
{
	ACondition.bUse = false;
	ACondition.Codes.clear();

    FSelNm = lblAll->Caption;//"전체";

    int ii, jj;
    tlItsCross->BeginUpdate();
    try
    {
        int nMemIdx;

        TcxTreeListNode *pRoot = tlItsCross->Root->GetNext();
        while(pRoot)
        {
            if (pRoot->CheckState == cbsUnchecked ||
                pRoot->Visible == false)
            {
                pRoot = pRoot->GetNext();
                continue;
            }
            for (ii = 0; ii < pRoot->Count; ii++)
            {
                TcxTreeListNode *pCross = pRoot->Items[ii];
                if (!pCross->Visible ||
                    pCross->CheckState == cbsUnchecked)
                {
                    continue;
                }

                for (jj = 0; jj < pCross->Count; jj++)
                {
                    if (!pCross->Items[jj]->Visible) continue;
                    if (!pCross->Items[jj]->Checked) continue;

                    String sMemIdx = pCross->Items[jj]->Texts[colMem->ItemIndex];
                    nMemIdx = StrToIntDef(sMemIdx, 0);
                    if (nMemIdx != 0)
                    {
                        try {
                            TRCrossDir* pObj = (TRCrossDir*)nMemIdx;
                            ACondition.Codes.push_back(pObj->VDS_CTLR_NMBR+","+pObj->VDS_CMRA_NO);
                            if (FSelNm == lblAll->Caption)//"전체")
                            {
                                FSelNm = pObj->DIR_NAME;
                            }
                        } catch(...) {}
                    }
                }
            }
            pRoot = pRoot->GetNext();
        }
    }
    __finally
    {
        tlItsCross->EndUpdate();
    }

	ACondition.bUse = !ACondition.Codes.empty();
}
//------------------------------------------------------------------------------

void __fastcall TFRAMEItsCross::EdFilterKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if (VK_RETURN == Key)
	{
        btnFilterClick(NULL);
	}
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsCross::btnFilterClick(TObject *Sender)
{
    String sFilter = EdFilter->Text.Trim();
    FilterItsCross(sFilter);
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsCross::btnNoFilterClick(TObject *Sender)
{
    EdFilter->Text = "";
	UnFilterItsCross();
}
//---------------------------------------------------------------------------

