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

btnArr = [
    cancleBtn = {
        class  : 'close-btn',
        box    : null,
        text   : '닫기',
        icon   : 'close',
        method : function(){
            window.close();
        }
    },
    searchBtn = {
        class  : 'search-btn',
        box    : null,
        text   : '조회',
        icon   : 'refresh',
        method : function(){
            fetchListData();
        }
    },
]
$(()=>{
    //CCTV 상태 전체조회 테이블
    sttsTable = $(".stts-table").width('100%').height('100%').dxDataGrid({
        dataSource              : null,
        allowColumnReordering   : true,
        showColumnLines         : true,
        showBorders             : true,
        allowColumnResizing     : true,
        rowAlternationEnabled   : true,
        columnAutoWidth         : true,
        focusedRowEnabled       : true,
        noDataText              : '표출할 정보가 없습니다.',
        headerFilter: {
            visible: true,
        },
        selection  : {
            mode :'single'
        },
        keyExpr : 'cctv_ctlr_nmbr',
        paging: {
            enabled: false,
            pageSize: 1000,
        },
        scrolling: {
            mode: 'standard',
        },
        columns:[
            {
                caption   : 'CCTV 정보',
                alignment : 'center',
                columns   : [
                    {
                        dataField    : "cctv_ctlr_nmbr",
                        caption      : "번호",
                        alignment    : "center",
                        sortIndex    : 0, 
                        sortOrder    : "asc",
                        cssClass     : 'p-n',
                    },
                    {
                        dataField    : "cctv_ctlr_id",
                        caption      : "CCTV ID",
                        alignment    : "center",
                        cssClass     : 'p-n',
                    },
                    {
                        dataField    : "istl_lctn_nm",
                        caption      : "CCTV 명칭",
                        alignment    : "center",
                        cellTemplate(c,e){
                            c.addClass('p-nl')
                            c.text(e.displayValue)
                        }
                    },
                ]
            },
            {
                caption   : '통신',
                alignment : 'center',
                columns   : [
                    {
                        dataField    : "updt_dt",
                        caption      : "업데이트 시각",
                        alignment    : "center",
                        cssClass     : 'p-n',
                    },
                    {
                        dataField    : "cmnc_stts_desc",
                        caption      : "통신 상태",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c,e){
                            return sttsTemplate(c, e, e.data.cmnc_stts_cd);
                        }
                    },
                ]
            },
            {
                caption   : '제어기상태',
                alignment : 'center',
                columns   : [
                    {
                        dataField    : "cbox_door_stts_desc",
                        caption      : "문열림",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            return sttsTemplate(c, e, e.data.cbox_door_stts_cd);
                        }
                    },
                    {
                        dataField    : "fan_stts_desc",
                        caption      : "팬",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            return sttsTemplate(c, e, e.data.fan_stts_cd);
                        }
                    },
                    {
                        dataField    : "hetr_stts_desc",
                        caption      : "히터",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            return sttsTemplate(c, e, e.data.hetr_stts_cd);
                        }
                    },
                    {
                        dataField    : "cbox_tmpr",
                        caption      : "함체온도",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            return sttsTemplate(c, e, 'temp');
                        }
                    },
                    {
                        dataField    : "video_input_desc",
                        caption      : "Video 인정",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            return sttsTemplate(c, e, e.data.video_input);
                        }
                    },
                    {
                        dataField    : "pan",
                        caption      : "PAN",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            text = nullChecker(e.displayValue) !== "" ? e.displayValue : '-';
                            c.text(text);
                        }
                    },
                    {
                        dataField    : "tilt",
                        caption      : "TILT",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            text = nullChecker(e.displayValue) !== "" ? e.displayValue : '-';
                            c.text(text);
                        }
                    },
                    {
                        dataField    : "zoom",
                        caption      : "ZOOM",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            text = nullChecker(e.displayValue) !== "" ? e.displayValue : '-';
                            c.text(text);
                        }
                    },
                    {
                        dataField    : "focus",
                        caption      : "FOCUS",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            text = nullChecker(e.displayValue) !== "" ? e.displayValue : '-';
                            c.text(text);
                        }
                    }
                ]
            },
        ],
      
        
    }).dxDataGrid("instance");

    btnArr.map((obj)=>{
        creatBtn($('.' + obj.class), obj.icon, obj.text, obj.text, 'outlined', obj.method);
    });

    fetchListData();
})

function fetchListData(){
    getDataAsync(commonUri, (jsonData)=>{
        const listData = jsonData;
        if (listData.length > 0) {
            listData.map((item)=>{
                console.log(item.updt_dt)
                if (item.updt_dt) item.updt_dt = getParseDateTime(item.updt_dt);
            })
        }
        console.log(listData);
        sttsTable.option('dataSource', listData);
    });
}

function sttsTemplate(c, e, code){
    const text = e.displayValue;
    if (nullChecker(code) === "" && code !== "temp" ) {
        c.text('-');
        c.addClass('gray');
    }
    else if (code === "temp" && (nullChecker(text) === "" || text === 255)){
        c.text('-');
        c.addClass('gray');
    }
    else {
        c.addClass(code);
        c.text(text);
    }
    return c;
 }