let cancleBtn = null;
let searchBtn = null;
let sttsData  = [];
let sttsTable = null;
const commonUri = '/api/vms/monitoring/stts';

$(()=>{
    creatBtn($(".close-btn"), "close", "닫기", "닫기", "outlined", () => window.close());
    creatBtn($(".search-btn"), "refresh", "조회", "조회", "outlined", () => fetchListData());

    //CCTV 상태 전체조회 테이블
    sttsTable = $(".stts-table").width('100%').height('100%').dxDataGrid({
        dataSource              : sttsData,
        allowColumnReordering   : true,
        showColumnLines         : true,
        showBorders             : true,
        allowColumnResizing     : true,
        rowAlternationEnabled   : true,
        columnAutoWidth         : true,
        focusedRowEnabled       : true,
        noDataText              : '표출할 정보가 없습니다.',
        headerFilter: {
            visible: true,
        },
        selection  : {
            mode :'single'
        },
        keyExpr : 'vms_ctlr_nmbr',
        paging: {
            enabled: false,
            pageSize: 1000,
        },
        scrolling: {
            mode: 'standard',
        },
        columns:[
            {
                caption   : 'VMS 정보',
                alignment : 'center',
                cssClass  : 'padding-ctrl',
                columns   : [
                    {
                        dataField    : "vms_ctlr_nmbr",
                        caption      : "VMS 번호",
                        alignment    : "center",
                        cssClass     : 'padding-ctrl',
                        sortIndex    : 0, 
                        sortOrder    : "asc",
                        width        : 130,
                    },
                    {
                        dataField    : "vms_ctlr_id",
                        caption      : "VMS ID",
                        alignment    : "center",
                        cssClass     : 'padding-ctrl',
                    },
                    {
                        dataField    : "vms_nm",
                        caption      : "VMS 명칭",
                        alignment    : "center",
                        cssClass     : 'padding-ctrl',
                        cellTemplate(c,e){
                            c.css('text-align','left');
                            c.text(e.displayValue)
                        }
                    },
                    {
                        dataField    : "vms_type_desc",
                        caption      : "크기",
                        alignment    : "center",
                        cssClass     : 'padding-ctrl',
                    },
                ]
            },
            {
                caption   : '통신',
                alignment : 'center',
                cssClass  : 'padding-ctrl',
                columns   : [
                    {
                        dataField    : "cmnc_stts_desc",
                        caption      : "통신",
                        alignment    : "center",
                        width        : 100,
                        cellTemplate(c,e){
                            return state(c, e, 'cmnc_stts_cd');
                        }
                    },
                ]
            },
            {
                caption   : '모듈',
                alignment : 'center',
                cssClass  : 'padding-ctrl',
                visible   : false,
                columns   : [
                    {
                        dataField    : "pwer_stts_desc",
                        caption      : "전원상태",
                        alignment    : "center",
                        width        : 110,
                        cellTemplate(c,e){
                            return state(c, e, 'pwer_stts_cd');
                        }
                    },
                    
                ]
            },
            {
                caption   : '함체',
                alignment : 'center',
                columns   : [
                    {
                        dataField    : "cbox_door_stts_desc",
                        caption      : "도어",
                        alignment    : "center",
                        width        : 100,
                        cellTemplate(c,e){
                            return state(c, e, 'cbox_door_stts_cd');
                        }
                    },
                    {
                        dataField    : "fan_stts_desc",
                        caption      : "팬상태",
                        alignment    : "center",
                        width        : 100,
                        cellTemplate(c,e){
                            return state(c, e, 'fan_stts_cd');
                        }
                    },
                    {
                        dataField    : "hetr_stts_desc",
                        caption      : "히터상태",
                        alignment    : "center",
                        width        : 110,
                        cellTemplate(c,e){
                            return  state(c, e, 'hetr_stts_cd');
                        }
                    },
                    {
                        dataField    : "cbox_tmpr",
                        caption      : "온도",
                        alignment    : "center",
                        width        : 100,
                        cellTemplate(c,e){
                            return state(c, e, 'cbox_tmpr');
                        }
                    },
                    {
                        dataField    : "brgh_val",
                        caption      : "화면휘도",
                        alignment    : "center",
                        width        : 110,
                        cellTemplate(c,e){
                            return  state(c, e, 'brgh_val');
                        }
                    },
                ]
            },
            {
                caption   : '메세지 표출',
                cssClass  : 'padding-ctrl',
                alignment : 'center',
                columns   : [
                    {
                        dataField    : "oper_mode_desc",
                        caption      : "운영모드",
                        alignment    : "center",
                        width        : '120',
                        cellTemplate(c,e){
                            return operMode(c, e);
                        }
                    },
                    {
                        dataField    : "updt_dt",
                        caption      : "제공시각",
                        alignment    : "center",
                        dataType     : "date",
                        format       : "yyyy-MM-dd HH:mm:ss",
                    },
                    {
                        dataField    : "provide_cnt",
                        caption      : "제공",
                        alignment    : "center",
                    },
                ]
            },
          
        ],
    }).dxDataGrid("instance");
    fetchListData();
})

function fetchListData(){
    getDataAsync(commonUri, (jsonData)=>{
        const listData = jsonData;
        if (listData.length > 0){

            listData.map((item)=>{
                if (item.updt_dt) item.updt_dt = getParseDateTime(item.updt_dt);
                item.auto = item.auto_used + '/' + item.auto_total;
                item.basic = item.basic_used + '/' + item.basic_total;
                item.fixed = item.fixed_used + '/' + item.fixed_total;
            })
        }
        sttsTable.option('dataSource', listData);
    });
}

