let updateData          = [];
let cmtrGradClsfTable   = null;
const _pageName         = '카메라상태 모니터링';
const _apiUri           = '/api/database/cmtr-grad-clsf';
let editCancleBtn       = null;
let editBtn             = null;
let searchBtn           = null;
let applyBtn            = null;

$(()=>{
    //상단 조회 버튼
    searchBtn = $(".search-button").dxButton({
        stylingMode:'outlined',
        text:'조회',
        icon:'refresh',
        height:'30px',
        width: '80px',
        onClick(){
            fetchListData();
        }
    }).dxButton('instance');

    //상단 닫기 버튼
    $(".cancle-button").dxButton({
        text:'닫기',
        icon:'close',
        height:'30px',
        width: '80px',
        stylingMode:'outlined',
        onClick(){
            window.close();
        }
    }).dxButton('instance');

    //소통 등급 테이블
    cmtrGradClsfTable = $(".cmtr-grad-clsf-table").width('100%').height('100%').dxDataGrid({
        dataSource              : null,
        allowColumnReordering   : true,
        showColumnLines         : true,
        allowColumnResizing     : true,
        showBorders             : true,
        rowAlternationEnabled   : true,
        columnAutoWidth         : true,
        noDataText              : '표출할 정보가 없습니다.',
        keyExpr                 : ['cmtr_grad_cd','sect_grad_desc'],
        focusedRowEnabled       : true,
        scrolling: {
            mode: "standard",
        },
        paging: {
            enabled: true,
            pageSize: 1000,
        },
        selection: {
            mode: 'single',
        },
        headerFilter: {
            visible: true,
        },
        grouping: {
            autoExpandAll: true,
            contextMenuEnabled : true,
        },
        groupPanel:{
            visible: true,
            allowColumnDragging : true,
            emptyPanelText:"그룹 지을 항목을 여기에 끌어다 놓으세요",
        },
        export:{
            enabled: true,
            texts: {
                exportAll:"엑셀 파일",
            },
        },
        // onToolbarPreparing: function(e){
        //     var toolbarItems = e.toolbarOptions.items; 
        //     console.log(toolbarItems);
        //     $.each(toolbarItems, function(index, item) {
        //         if(item.name === "groupPanel") {
        //               item.location = "brfore";
        //               // Change the item options here
        //         }else                              
        //             item.location = "after";              
        //     });
        //     //move searchPanel to the first position
        // },
        onExporting(e){
            gridExcellExporting(e, _pageName);
        },
        columns:[
            {
                caption   : "구분",
                alignment : 'center',
                width     : 80,
                visible   : false,
                columns:[
                    {
                    dataField    : "sect_grad_desc",
                    caption      : "교차로",
                    alignment    : "center",
                    allowEditing : false,
                    groupIndex   : 0,
                    sortIndex    : 0,
                    sortOrder    : "asc",
                    }
                ]
            },
            {
                caption:"교차로 CCTV상태",
                alignment:"center",
                columns:[
                    {
                        dataField    : "cmtr_grad_desc",
                        caption      : "카메라 ID",
                        alignment    : "center",
                        allowEditing : false,
                        cellTemplate(c,e){
                            let result = nullChecker(e.data.cmtr_grad_desc);
                            return  $('<div style="text-align: left; padding-left: 10px;">'+result+'</div>')
                        }
                    },
                    {
                        dataField     : "lwst_trvl_sped",
                        caption       : "카메라 명칭",
                        alignment     : "center",
                        editorOptions : {
                            dataType  : 'number',
                            min       : 0,
                            max       : 999,
                        }
                    },
                    {
                        dataField     : "hghs_trvl_sped",
                        caption       : "상태",
                        alignment     : "center",
                        editorOptions : {
                            dataType  : 'number',
                            min       : 0,
                            max       : 999,
                        }
                    },
                    {
                        dataField   : "cngs_grad_yn",
                        caption     : "업데이트 시각",
                        alignment   : "center",
                    },
                ],
            },
        ],
        onRowClick(e){
        },
        onRowDblClick(e){
        },
        onRowUpdated(e){
            updateData.push(e.data);
        }

    }).dxDataGrid("instance");



    fetchListData();
})

//소통등급 리스트 정보 수신
function fetchListData(){
    getDataAsync(_apiUri, (jsonData)=>{cmtrGradClsfTable.option('dataSource', jsonData)});
}
