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 state(c, e, e.data.cbox_door_stts_cd);
                        }
                    },
                    {
                        dataField    : "fan_stts_desc",
                        caption      : "팬",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            return state(c, e, e.data.fan_stts_cd);
                        }
                    },
                    {
                        dataField    : "hetr_stts_desc",
                        caption      : "히터",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            console.log('히터',e.data);
                            return state(c, e, e.data.hetr_stts_cd);
                        }
                    },
                    {
                        dataField    : "cbox_tmpr",
                        caption      : "함체온도",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            return state(c, e, 'temp');
                        }
                    },
                    {
                        dataField    : "video_input_desc",
                        caption      : "Video 인정",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            // if (e.data.cmnc_stts_cd === "CMS1") {
                            //     c.text("-");
                            // }
                            // else  {
                            //     text = nullChecker(e.displayValue) !== "" ? e.displayValue : '-';
                            //     c.text(text);
                            // }
                            return sttsTemplate(c, e, e.data.video_input);
                        }
                    },
                    {
                        dataField    : "pan",
                        caption      : "PAN",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            return sttsTemplate(c, e, e.data.pan);
                        }
                    },
                    {
                        dataField    : "tilt",
                        caption      : "TILT",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            return sttsTemplate(c, e, e.data.tilt);
                        }
                    },
                    {
                        dataField    : "zoom",
                        caption      : "ZOOM",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            return sttsTemplate(c, e, e.data.zoom);
                        }
                    },
                    {
                        dataField    : "focus",
                        caption      : "FOCUS",
                        alignment    : "center",
                        cssClass     : 'p-n',
                        cellTemplate(c, e){
                            return sttsTemplate(c, e, e.data.focus);
                        }
                    }
                ]
            },
        ],
      
        
    }).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){
    let text   = e.displayValue;
    const cmncCd = e.data.cmnc_stts_cd;
    const check = cmncCd === code ? true : false;
    const nullVal = ['정보없음', 'UNKNOWN', 255, null, "-", undefined, ''];
    if (!check && cmncCd === 'CMS1') {
        c.text('-');
        c.addClass('gray');
    }
    else if (nullVal.includes(text)) {
        c.text('-');
        c.addClass('gray');
    }
    else {
        c.addClass(code);
        c.text(text);
    }
    return c;
 }