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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "CDSNodeF.h"
#include "CDSLinkF.h"
#include "CDSIfscF.h"
#include "CDSRoadF.h"
#include "ITSLangTransF.h"
#include "ITSMapF.h"
#pragma hdrstop

#include "IAN0040MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCalendar"
#pragma link "cxClasses"
#pragma link "cxContainer"
#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 "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxSpinEdit"
#pragma link "cxSplitter"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "cxTimeEdit"
#pragma link "cxTrackBar"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "FRAME_OpenMapF"
#pragma resource "*.dfm"

TIAN0040M *IAN0040M = NULL;
//---------------------------------------------------------------------------
void TIAN0040M_MapCallback(TObject *ASender, const String AFunc, const String AArgs, const bool AFirst)
{
    try
    {
        if (!IAN0040M) return;
        IAN0040M->OpenMapCallback(ASender, AFunc, AArgs, AFirst);
    }
    catch(Exception &exception)
    {
    }
}
//---------------------------------------------------------------------------

__fastcall TIAN0040M::TIAN0040M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FTitle = Caption;//"소통정보 이력분석";

    FLinkMngr = new TTrafficValManager();
    FIfscMngr = new TTrafficValManager();
    FRoadMngr = new TTrafficValManager();

    CbLinkLevel->ItemIndex = 1; //IFSC
    FLinkLevel = CbLinkLevel->ItemIndex+1;
    FLayerIdx = LyrTp_Node;
    FRAMEOpenMap1->LayerIdx = FLayerIdx;
    FRAMEOpenMap1->ChkShowNmbr->Visible = false;
    FRAMEOpenMap1->BtnMoveMapToFit->Visible = false;
    FRAMEOpenMap1->MapCallbackFunc = TIAN0040M_MapCallback;
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::CommClose()
{
    try
    {
        if (BtnStop->Enabled)
            BtnStopClick((TObject*)BtnStop);

        delete FLinkMngr;
        delete FIfscMngr;
        delete FRoadMngr;

        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::FormShow(TObject *Sender)
{
    Refresh();

    FRAMEOpenMap1->OpenMap();

    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::FormInit()
{
    ADOQry->Connection = ITSDb_GetConnection();
    ADOSta->Connection = ITSDb_GetConnection();

    m_pGDC = TvList->DataController;

    TDateTime dtNow = Now() - ((double)3 / (double)24);

    DtStDate->Date = dtNow;
    DtEdDate->Date = Now();

    //DtStTime->EditValue = dtNow.FormatString("hh") + ":00";
    dtNow = Now() - ((double)2 / (double)24);
    //DtEdTime->EditValue = dtNow.FormatString("hh") + ":59";

    DtStTime->Time = StrToDateTime(dtNow.FormatString("hh") + ":00");
    DtEdTime->Time = StrToDateTime(dtNow.FormatString("hh") + ":59");
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;

    if (TmrShow->Tag == 1)
    {
        int nIndex = m_pGDC->FocusedRecordIndex;
        if (nIndex < 0)
        {
            cxTrackBar1->Position = 0;
            FStatTime = "";

            BtnPlay->Enabled = true;
            BtnStop->Enabled = false;
            BtnSearch->Enabled = true;
            return;
        }

        if (m_pGDC->FocusedRecordIndex + 1 == m_pGDC->RecordCount)
        {
            cxTrackBar1->Position = 0;
            FStatTime = "";

            BtnPlay->Enabled = true;
            BtnStop->Enabled = false;
            BtnSearch->Enabled = true;
            return;
        }

        m_pGDC->FocusedRecordIndex++;
        nIndex = m_pGDC->FocusedRecordIndex;

        FSimIndex = nIndex;
        cxTrackBar1->Position = nIndex;
        FStatTime = VarToStr(m_pGDC->Values[nIndex][Column04->Index]);
        //cxGroupBox1->Caption = "▶ 소통정보 표출 - " + VarToStr(m_pGDC->Values[nIndex][Column02->Index]);
        cxGroupBox1->Caption = lblText1->Caption + " - " + VarToStr(m_pGDC->Values[nIndex][Column02->Index]);
        SelStatisticsHistory(FStatTime);


        BtnPlay->Enabled = false;
        BtnStop->Enabled = true;
        TmrShow->Interval = SeRefreshSec->Value * 1000;
        TmrShow->Enabled  = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::InitGisLink()
{
    if (!FRAMEOpenMap1->MapStart) return;

    TItsNode* pNode;
    String    STRT_NM;
    String    END_NM;

    try
    {
        if (FLinkLevel == 1)
        {
            ItsLinkManager->FLists.Lock();
            try
            {
                FOR_STL(TItsLink*, pLink, ItsLinkManager->FLists)
                {
                    if (pLink->DEL_YN == "Y")   continue;

                    if (pLink->IsVertex)
                    {
                        TTrafficVal* pTraf = new TTrafficVal();
                        pTraf->ID = pLink->LINK_ID;
                        pTraf->IsUpdate = true;
                        pTraf->GRADE_VAL = 0;
                        FLinkMngr->FLists.Push(pTraf->ID, pTraf);

                        pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
                        if (pNode) STRT_NM = pNode->NODE_NAME; else STRT_NM = " ";
                        pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
                        if (pNode) END_NM = pNode->NODE_NAME; else END_NM = " ";

                        sJs.printf(L"addEditLink('%s', '%s', '%s', '%s', '%s', '%s')",
                            pLink->LINK_ID.c_str(),
                            pLink->ROAD_NAME.c_str(),
                            STRT_NM.c_str(),
                            END_NM.c_str(),
                            pLink->X_CRDN_ARR.c_str(),
                            pLink->Y_CRDN_ARR.c_str());

                        FRAMEOpenMap1->ExecScript(sJs);
                    }
                }
            }
            __finally
            {
                ItsLinkManager->FLists.UnLock();
            }
            FRAMEOpenMap1->ExecScript("makeEditLinkLayer()");
        }
        else
        if (FLinkLevel == 2)
        {
            ItsIfscManager->FLists.Lock();
            try
            {
                FOR_STL(TItsIfsc*, pIfsc, ItsIfscManager->FLists)
                {
                    if (pIfsc->DEL_YN == "Y")   continue;

                    if (pIfsc->IsVertex)
                    {
                        TTrafficVal* pTraf = new TTrafficVal();
                        pTraf->ID = pIfsc->IFSC_ID;
                        pTraf->IsUpdate = true;
                        pTraf->GRADE_VAL = 0;
                        FIfscMngr->FLists.Push(pTraf->ID, pTraf);

                        sJs.printf(L"addEditLink('%s', '%s', '%s', '%s', '%s', '%s')",
                            pIfsc->IFSC_ID.c_str(),
                            pIfsc->IFSC_NM.c_str(),
                            pIfsc->STRT_NM.c_str(),
                            pIfsc->END_NM.c_str(),
                            pIfsc->X_CRDN_ARR.c_str(),
                            pIfsc->Y_CRDN_ARR.c_str());

                        FRAMEOpenMap1->ExecScript(sJs);
                    }
                }
            }
            __finally
            {
                ItsIfscManager->FLists.UnLock();
            }
            FRAMEOpenMap1->ExecScript("makeEditLinkLayer()");
        }
        else
        if (FLinkLevel == 3)
        {
            ItsRoadManager->FLists.Lock();
            try
            {
                FOR_STL(TItsRoad*, pRoad, ItsRoadManager->FLists)
                {
                    if (pRoad->DEL_YN == "Y")   continue;

                    if (pRoad->IsVertex)
                    {
                        TTrafficVal* pTraf = new TTrafficVal();
                        pTraf->ID = pRoad->ROAD_ID;
                        pTraf->IsUpdate = true;
                        pTraf->GRADE_VAL = 0;
                        FRoadMngr->FLists.Push(pTraf->ID, pTraf);

                        sJs.printf(L"addEditLink('%s', '%s', '%s', '%s', '%s', '%s')",
                            pRoad->ROAD_ID.c_str(),
                            pRoad->ROAD_NM.c_str(),
                            pRoad->STRT_NM.c_str(),
                            pRoad->END_NM.c_str(),
                            pRoad->X_CRDN_ARR.c_str(),
                            pRoad->Y_CRDN_ARR.c_str());

                        FRAMEOpenMap1->ExecScript(sJs);
                    }
                }
            }
            __finally
            {
                ItsRoadManager->FLists.UnLock();
            }
            FRAMEOpenMap1->ExecScript("makeEditLinkLayer()");
        }
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

//    SelStatisticsHistory(""); return;
#if 0
	FStDateTime = DtStDate->Date.FormatString("yyyymmdd") + DtStTime->Time.FormatString("hh");
	FEdDateTime = DtEdDate->Date.FormatString("yyyymmdd") + DtEdTime->Time.FormatString("hh");
#else
	FStDateTime = DtStDate->Date.FormatString("yyyymmdd") + DtStTime->Time.FormatString("hhnn");
	FEdDateTime = DtEdDate->Date.FormatString("yyyymmdd") + DtEdTime->Time.FormatString("hhnn");
#endif
    if (FStDateTime > FEdDateTime)
    {
        Application->MessageBox(FrmLang->lblQryDtErr->Caption.c_str(),//L"검색 시작시각이 검색 종료시각 보다 큽니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStDate;
        return;
    }
	if ( StrToInt(DtEdTime->Time.FormatString("hh")) - StrToInt(DtStTime->Time.FormatString("hh")) >= 3)
	{
        Application->MessageBox(lblText2->Caption.c_str(),//L"3시간 이상의 정보를 조회할 수 없습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONWARNING|MB_APPLMODAL);
        ActiveControl = DtStDate;
		return;
	}
#if 0
    FStDateTime = FStDateTime + "0000";
    FEdDateTime = FEdDateTime + "5959";
#else
    FStDateTime = FStDateTime + "00";
    FEdDateTime = FEdDateTime + "59";
#endif

    if (FLinkLevel != CbLinkLevel->ItemIndex+1)
    {
        //링크레벨을 고정하지 않고 변경할 경우 여기에서 링크 버텍스를 다시 로딩해 줘야 한다.
    }
    FLinkLevel = CbLinkLevel->ItemIndex+1;

    SelHistory();

    ClearGis();
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::BtnCloseClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    IAN0040M = NULL;
    Action = caFree;    
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::DtStDatePropertiesValidate(TObject *Sender, Variant &DisplayValue, TCaption &ErrorText, bool &Error)
{
    DtEdDate->Date = DtStDate->Date;
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::SelHistory()
{
    TSqlCursor sqlCrs;

    String sQry;
    TADOQuery *pADO = ADOQry;

    int nLevel = StrToInt(FLinkLevel);
    switch(nLevel)
    {
      case 1:
      sQry = "SELECT PRCN_DT, COUNT(LINK_ID) CNT            \r\n"
             "     FROM TB_LINK_TRAF_HS A                   \r\n"
             "    WHERE PRCN_DT BETWEEN :p01 AND :p02       \r\n"
             "    GROUP BY PRCN_DT                          \r\n"
             "    ORDER BY PRCN_DT                          \r\n";
        break;
      case 2:
      sQry = "SELECT STAT_DT AS PRCN_DT, COUNT(IFSC_ID) CNT \r\n"
             "     FROM TB_IFSC_15M_STAT A                  \r\n"
             "    WHERE STAT_DT BETWEEN :p01 AND :p02       \r\n"
             "    GROUP BY STAT_DT                          \r\n"
             "    ORDER BY STAT_DT                          \r\n";
        break;
      case 3:
      sQry = "SELECT PRCN_DT, COUNT(ROAD_ID) CNT            \r\n"
             "     FROM TB_ROAD_TRAF_HS A                   \r\n"
             "    WHERE PRCN_DT BETWEEN :p01 AND :p02       \r\n"
             "    GROUP BY PRCN_DT                          \r\n"
             "    ORDER BY PRCN_DT                          \r\n";
        break;
    default: return;
    }

    try
    {
        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", FStDateTime);
        ITSDb_SQLBind(pADO, "p02", FEdDateTime);
        ITSDb_SQLOpen(pADO);

        DspHistory();
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &exception)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(exception.ClassName()), exception.Message, sQry);
        throw Exception(String(exception.ClassName()) + exception.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::DspHistory()
{
    TADOQuery *pADO = ADOQry;

    try
    {
        CMM_ClearGridTableView(TvList);

        int nRow = 0;
        try
        {
            TvList->BeginUpdate();
            m_pGDC->RecordCount = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next(), nRow++)
            {
                m_pGDC->Values[nRow][Column01->Index] = nRow+1;

                //m_pGDC->Values[nRow][Column02->Index] = pADO->FieldByName("PRCN_DT2")->AsDateTime.DateTimeString();  //가공시각
                m_pGDC->Values[nRow][Column02->Index] = ITSUtil_StrToTime(pADO->FieldByName("PRCN_DT")->AsString);  //가공시각
                m_pGDC->Values[nRow][Column03->Index] = pADO->FieldByName("CNT")->AsInteger;
                m_pGDC->Values[nRow][Column04->Index] = pADO->FieldByName("PRCN_DT")->AsString;
            }
        }
        __finally
        {
            if (pADO)
            {
                pADO->Close();
            }

            TvList->EndUpdate();
            CxList->SetFocus();

            cxTrackBar1->Position = 0;
	        cxTrackBar1->Properties->Max = nRow;
	        cxTrackBar1->Properties->Frequency = cxTrackBar1->Properties->Max * 0.1;

            LblRecords->Caption = FormatFloat("##,##0", m_pGDC->RecordCount) + FrmLang->lblEA->Caption;//" 건";

            String sEnd = FrmLang->lblQrySel->Caption + " [" + LblRecords->Caption + "]";//"데이터 " + LblRecords->Caption + " 이 조회 되었습니다.";
            Application->MessageBox(sEnd.c_str(),
                                    FrmLang->lblQryEnd->Caption.c_str(),//L"데이터 조회 완료",
                                    MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);
        }
    }
    catch(EDatabaseError &E)
    {
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(...)
    {
        throw Exception(FrmLang->lblDbErr->Caption);//"알수없는 오류가 발생하였습니다.");
    }
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::BtnPlayClick(TObject *Sender)
{
    if (m_pGDC->RecordCount <= 0)
    {
        Application->MessageBox(lblText3->Caption.c_str(),//L"표출할 정보가 없습니다. 이력정보를 다시 조회하신 후 작업을 진행 하십시요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

	m_pGDC->FocusedRecordIndex = 0;
	int nIndex = m_pGDC->FocusedRecordIndex;
	if (nIndex < 0) return;

    FSimIndex = nIndex;

    cxTrackBar1->Position = nIndex;
    FStatTime = VarToStr(m_pGDC->Values[nIndex][Column04->Index]);

    CxList->SetFocus();
    BtnPlay->Enabled = false;
    BtnStop->Enabled = true;
    BtnSearch->Enabled = false;

    cxGroupBox1->Caption = lblText1->Caption + " - " + VarToStr(m_pGDC->Values[nIndex][Column02->Index]);
    SelStatisticsHistory(FStatTime);

    TmrShow->Tag = 1;
    TmrShow->Interval = SeRefreshSec->Value * 1000;
    TmrShow->Enabled  = true;
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::BtnStopClick(TObject *Sender)
{
    TmrShow->Enabled = false;
    BtnPlay->Enabled = true;
    BtnStop->Enabled = false;
    BtnSearch->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::SelStatisticsHistory(String AStatTime)
{
    int        ii;
    TSqlCursor sqlCrs;

    String     sQry;
    TADOQuery *pADO = ADOSta;

#if 1
    sQry = "SELECT A.IFSC_ID, NVL(B.CMTR_GRAD_CD, 'LTC0') TRFGRD, C.SPED, C.STAT_DT REGDATE \r\n"
           "  FROM TB_IFSC           A,                                                     \r\n"
           "       TB_SECT_GRAD_STUP B,                                                     \r\n"
           "       TB_IFSC_15M_STAT  C                                                      \r\n"
           " WHERE A.IFSC_ID      = C.IFSC_ID                                               \r\n"
           "   AND A.SECT_GRAD_CD = B.SECT_GRAD_CD                                          \r\n"
           "   AND (C.SPED >= B.LWST_TRVL_SPED AND C.SPED <= B.HGHS_TRVL_SPED)              \r\n"
           "   AND STAT_DT = :p01                                                           \r\n";

        ITSDb_SQLText(pADO, sQry);
        ITSDb_SQLBind(pADO, "p01", AStatTime);
#else
    sQry = "SELECT A.IFSC_ID, 'LTC' || mod(rownum, 4) as TRFGRD, 1 as SPED \r\n"
           "  FROM TB_IFSC A \r\n";

        ITSDb_SQLText(pADO, sQry);
#endif

    int nGradeCd;
    try
    {
        ITSDb_SQLOpen(pADO);

        for( ; !pADO->Eof; pADO->Next())
        {
            String sID = pADO->FieldByName("IFSC_ID")->AsString;
            TTrafficVal *pTraf = FIfscMngr->FLists.Find(sID);
            if (pTraf)
            {
                String TRFGRD = pADO->FieldByName("TRFGRD")->AsString;

                if      (TRFGRD == "LTC1") nGradeCd = 1;
                else if (TRFGRD == "LTC2") nGradeCd = 2;
                else if (TRFGRD == "LTC3") nGradeCd = 3;
                else                       nGradeCd = 0;

                if (pTraf->GRADE_VAL != nGradeCd)
                {
                    pTraf->GRADE_VAL = nGradeCd;

                    sJs.printf(L"updateHistLink('%s', %d)", sID.c_str(), nGradeCd);
                    FRAMEOpenMap1->ExecScript(sJs);
                }
            }
        }
    }
    catch(EDatabaseError &E)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQry);
        throw Exception(String(E.ClassName()) + E.Message);
    }
    catch(Exception &exception)
    {
        ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
        DBERRORMSG(Caption, String(exception.ClassName()), exception.Message, sQry);
        throw Exception(String(exception.ClassName()) + exception.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::ClearGis()
{
    cxGroupBox1->Caption = lblText1->Caption;//"▶ 소통정보 표출";
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    if (BtnPlay->Enabled == false) return;

	int nRow = m_pGDC->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    FStatTime = VarToStr(m_pGDC->Values[nRow][Column04->Index]);
    cxGroupBox1->Caption = lblText1->Caption + " - " + VarToStr(m_pGDC->Values[nRow][Column02->Index]);
    SelStatisticsHistory(FStatTime);
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::BtnSearch2Click(TObject *Sender)
{
    Application->ProcessMessages();

    if (!FRAMEOpenMap1->MapStart) return;

    String sLinkId = EdLinkId->Text.Trim();
    sJs.printf(L"extentLayerObject(%d, '%s')", LyrTp_Traffic, sLinkId.c_str());
    FRAMEOpenMap1->ExecScript(sJs);
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::OpenMapCallback(TObject *ASender, const String AFunc, const String AArgs, const bool AFirst)
{
    try
    {
        if (AFirst)
        {
            //지도를 편집모드로 변경
            sJs.printf(L"setEditMode(%d, 1)", FLayerIdx);
            FRAMEOpenMap1->ExecScript(sJs);

            //편집레이어 생성
            FRAMEOpenMap1->ExecScript("initEditLayer()");

            FRAMEOpenMap1->ExecScript("setEditArrow(0)");
            FRAMEOpenMap1->ExecScript("setEditColor()");

            //배경링크레이어 생성
            InitGisLink();

            return;
        }
    }
    catch(Exception &exception)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIAN0040M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    FRAMEOpenMap1->OnCloseQuery(CanClose);
}
//---------------------------------------------------------------------------

