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

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

#include "PRK0020MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#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 "cxGroupBox"
#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 "dxSkinMcSkin"
#pragma resource "*.dfm"

TPRK0020M *PRK0020M = NULL;
//---------------------------------------------------------------------------
__fastcall TPRK0020M::TPRK0020M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);
}
//---------------------------------------------------------------------------

void __fastcall TPRK0020M::CommClose()
{
    try
    {
        TmrQry->Enabled = false;
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TPRK0020M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TPRK0020M::FormInit()
{
}
//---------------------------------------------------------------------------

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

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

void __fastcall TPRK0020M::RefreshData()
{
    // 검색조건에 의한 링크 데이터 조회
    String sQry;
    TADOQuery *pADO = NULL;

    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 = 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";

    CMM_ClearGridTableView(TvList);

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

            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 = TvList->DataController->AppendRecord();

                TvList->DataController->Values[nRow][Col01->Index] = pADO->FieldByName("prk_plce_nmbr")->AsString;
                TvList->DataController->Values[nRow][Col02->Index] = pADO->FieldByName("prk_plce_manage_no")->AsString;
                TvList->DataController->Values[nRow][Col03->Index] = pADO->FieldByName("prk_plce_nm")->AsString;

                String colctStts = "";
                int COLCT_STTS = pADO->FieldByName("colct_stts")->AsInteger;
                //if (COLCT_STTS == 1) colctStts = " [수집 이상]";

#if 0
                int COLCT_STTS = pADO->FieldByName("colct_stts")->AsInteger;
                if (COLCT_STTS == 1) {
                    TvList->DataController->Values[nRow][Col04->Index] = "[수집 이상]";
                }
                else {
                    TvList->DataController->Values[nRow][Col04->Index] = pADO->FieldByName("cmnc_stts_desc")->AsString;
                }
#endif

                String STTS_CD = pADO->FieldByName("cmnc_stts_desc")->AsString;
                TvList->DataController->Values[nRow][Col04->Index] = STTS_CD;

                String data_colct_time = pADO->FieldByName("data_colct_time")->AsString;
                if (STTS_CD == "정상") {
                    if (data_colct_time != "")
                        TvList->DataController->Values[nRow][Col05->Index] = ITSUtil_StrToDateTime(pADO->FieldByName("data_colct_time")->AsString).FormatString(STR_DATETIME) + colctStts;
                    else
                        TvList->DataController->Values[nRow][Col05->Index] = data_colct_time + colctStts;
                }
                else {
                    if (data_colct_time != "")
                        TvList->DataController->Values[nRow][Col05->Index] = ITSUtil_StrToDateTime(pADO->FieldByName("data_colct_time")->AsString).FormatString(STR_DATETIME);
                    else
                        TvList->DataController->Values[nRow][Col05->Index] = data_colct_time;
                }

                TvList->DataController->Values[nRow][Col08->Index] = pADO->FieldByName("prk_cmprt_co")->AsString;
                TvList->DataController->Values[nRow][Col09->Index] = pADO->FieldByName("remndr_prk_cmprt_co")->AsString;
                TvList->DataController->Values[nRow][Col07->Index] = pADO->FieldByName("prk_parking_co")->AsString;
                TvList->DataController->Values[nRow][Col06->Index] = pADO->FieldByName("parking_cgs_desc")->AsString;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("주차장 실시간정보", 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("주차장 실시간정보", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        TvList->EndUpdate();
        CMM_ExpandCollapseChk(TvList, true);
        //cxGroupBox3->Caption = "▶ 주차정보";// (최종가공시각: " + FRAMETrafficLinkList1->FLastAnsTime + ")";
    }
}
//---------------------------------------------------------------------------

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

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

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

    RefreshData();

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

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

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



void __fastcall TPRK0020M::TvListCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
	if (AViewInfo)
	{
        if (AViewInfo->GridRecord->RecordIndex < 0) return;
        if (VarIsNull(AViewInfo->GridRecord->DisplayTexts[Col04->Index])) return;

        String sConn = AViewInfo->GridRecord->DisplayTexts[Col04->Index];

        if (sConn.Pos("이상")) {
            ACanvas->Canvas->Brush->Color = clRed;
            ACanvas->Canvas->Font->Color = clWhite;
        }
    }
}
//---------------------------------------------------------------------------

