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

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

#include "FrmParkMsgF.h"
#include "ITS_OPLibF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#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 "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "cxContainer"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"
TFrmParkMsg *FrmParkMsg = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmParkMsg::TFrmParkMsg(TComponent* Owner, TPrltCtlr *AObj)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

    FObj = AObj;

    pADO->Connection = ITSDb_GetConnection();
    TvList->OptionsView->NoDataToDisplayInfoText = "<주차정보>";
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmParkMsg::FormShow(TObject *Sender)
{
    if (FObj)
    {
        Caption = FObj->PRLT_CTLR_ID + ": " + FObj->PRLT_NM;
        //PnlMenu->Visible = FObj->REAL_DATA_YN == "Y" ? true : false;
    }
    TmrShow->Enabled = true;
    //EdNm->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TFrmParkMsg::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    if (FObj)
    {
        //EdNmbr->Text = FObj->PRLT_CTLR_NMBR;
        //EdId->Text = FObj->PRLT_CTLR_ID;
        EdNm->Text = FObj->PRLT_NM;
        EdTel->Text = FObj->PRLT_TEL;
        EdAddr->Text = FObj->ISTL_LCTN_ADDR;

        //PnlMenu->Visible = FObj->REAL_DATA_YN == "Y" ? true : false;
    }

    TmrQry->Interval = 100;
    TmrQry->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmParkMsg::TmrQryTimer(TObject *Sender)
{
    TmrQry->Enabled = false;

    if (!FObj) return;

    //if (FObj->REAL_DATA_YN == "Y")
    {
        SelectParkMsg();
    }

    TmrQry->Interval = 60 * 1000;
    TmrQry->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmParkMsg::SelectParkMsg()
{
    String sQry;

    if (!FObj)
    {
        return;
    }

    CMM_ClearGridTableView(TvList);

    sQry = "SELECT 0 as data_seq, A.prk_plce_nmbr, A.prk_plce_manage_no, A.prk_plce_nm, A.prk_plce_adres,                   \r\n"
           "       B.cmnc_stts_cd, DECODE(C.stts_cd, '0', '정상', '이상') AS cmnc_stts_desc,                                \r\n"
           "       C.data_colct_time,                                                                                       \r\n"
           "       DECODE(sign(to_date(C.data_colct_time, 'YYYYMMDDHH24MISS') - SYSDATE-10/1440), -1, 1, 0) as colct_stts,  \r\n"
           "       fnParkingCgsSttus(C.prk_cmprt_co, c.prk_parking_co) AS parking_cgs_sttus,                                \r\n"
           "       DECODE(fnParkingCgsSttus(C.prk_cmprt_co, C.prk_parking_co), 0, '여유',                                   \r\n"
           "                                   1, '보통',                                                                   \r\n"
           "                                   2, '혼잡',                                                                   \r\n"
           "                                   3, '만차',                                                                   \r\n"
           "                                   '알수없음') AS parking_cgs_desc,                                             \r\n"
           "       C.prk_cmprt_co AS prk_cmprt_co,                                                                          \r\n"
           "       C.remndr_prk_cmprt_co AS remndr_prk_cmprt_co,                                                            \r\n"
           "       C.prk_parking_co AS prk_parking_co                                                                       \r\n"
           "  FROM TB_PRK_PLCE A, TB_PRK_PLCE_STTS B,                                                                       \r\n"
           "       TB_PRK_PLCE_RT C                                                                                         \r\n"
           " WHERE A.prk_plce_nmbr = :p01                                                                                   \r\n"
           "   AND A.prk_plce_nmbr = B.prk_plce_nmbr                                                                        \r\n"
           "   AND A.prk_plce_nmbr = C.prk_plce_nmbr                                                                        \r\n"
           "   AND A.pis_type     != 'N'                                                                                    \r\n"
           "   AND A.del_yn        = 'N'                                                                                    \r\n";

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

        try
        {
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", FObj->CTLR_NMBR);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                String colctStts = "";
                int COLCT_STTS = pADO->FieldByName("colct_stts")->AsInteger;
                if (COLCT_STTS == 1) colctStts = " [수집 이상]";
                nRow = TvList->DataController->AppendRecord();
                EdNm->Text = pADO->FieldByName("prk_plce_nm")->AsString;
                EdAddr->Text = pADO->FieldByName("prk_plce_adres")->AsString;
                EdClctDt->Text = ITSUtil_StrToDateTime(pADO->FieldByName("data_colct_time")->AsString).FormatString(STR_DATETIME) + colctStts;
                TvList->DataController->Values[nRow][Col03->Index] = pADO->FieldByName("cmnc_stts_desc")->AsString;
                TvList->DataController->Values[nRow][Col04->Index] = pADO->FieldByName("prk_cmprt_co")->AsString;
                TvList->DataController->Values[nRow][Col05->Index] = pADO->FieldByName("remndr_prk_cmprt_co")->AsString;
                TvList->DataController->Values[nRow][Col06->Index] = pADO->FieldByName("prk_parking_co")->AsString;
                TvList->DataController->Values[nRow][Col07->Index] = pADO->FieldByName("parking_cgs_desc")->AsString;
            }
            ITSDb_SQLClose(pADO);
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(...)
        {
            throw Exception("알수없는 오류가 발생하였습니다.");
        }
    }
    __finally
    {
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmParkMsg::FormDestroy(TObject *Sender)
{
    TmrQry->Enabled = false;
    try {
        if (FObj) FObj->FData1 = NULL;
    } catch(...) {}
}
//---------------------------------------------------------------------------

