//우측 섹션 관련
let _managerData = [];
let _managerTable;
let _infoList;
let _dayCtlrList;
let _specCtlrList;
let _editBtn;
let _cancelBtn;
let _saveBtn;
//공통
/**
 * @TODO 교차로 그룹 정보로 변경해야함
 */
let _commonUri = "/api/scrs/tb_sc_ixr_mngm/list";
let _groupUri  = '/api/scrs/tb_sc_ixr_grop_mngm/list';
const _pageName = '신호교차로그룹 정보';
const pageMsg = "신호교차로그룹";

$(() => {
    //상단 닫기 버튼
    const closeBtn = new dxBtn($('.close-btn'), '닫기', 'close', 'outlined', 80, 30, false, true); 
    _editBtn = new dxBtn($('.edit-btn'), '수정', 'edit', 'outlined', 80, 30, false, true); 
    _cancelBtn = new dxBtn($('.cancel-btn'), '취소', 'close', 'outlined', 80, 30, false, false); 
    _saveBtn = new dxBtn($('.save-btn'), '저장', 'save', 'outlined', 80, 30, false, false); 
    closeBtn.onClick(()=>window.close());

    //소통정보 이력분석 테이블
    _managerTable = $(".manager-table")
        .width("100%")
        .height("100%")
        .dxDataGrid({
            dataSource: [],
            allowColumnReordering: true,
            showColumnLines: true,
            showBorders: true,
            allowColumnResizing: true,
            rowAlternationEnabled: true,
            focusedRowEnabled: true,
            columnAutoWidth: true,
            noDataText: "표출할 정보가 없습니다.",
            selection: {
                mode: "single",
            },
            keyExpr: "ixr_grop",

            headerFilter: {
                visible: true,
            },
            scrolling: {
                mode: "virtual",
            },
            columns: [
                {
                    dataField: "ixr_grop",
                    caption: "번호",
                    width: 120,
                    alignment: "center",
                },
                {
                    dataField: "ixr_grop_nm",
                    caption: "교차로 그룹 명칭",
                    alignment: "center",
                    cellTemplate(c, e) {
                        return alignTemplate(c, e, 'left');
                    },
                },
            ],
            onRowDblClick(e) {
                listTableDblClick(e.data);
            },
        })
        .dxDataGrid("instance");
 
    _infoList = $(".info-list")
        .width("100%")
        .height("100%")
        .dxDataGrid({
            dataSource: [],
            allowColumnReordering: true,
            showColumnLines: true,
            showBorders: true,
            allowColumnResizing: true,
            rowAlternationEnabled: true,
            focusedRowEnabled: true,
            columnAutoWidth: true,
            noDataText: "표출할 정보가 없습니다.",
            selection: {
                mode: "single",
            },
            sorting:{
                mode: "none",
            },
            keyExpr: "seq",
            scrolling: {
                mode: "virtual",
            },
            columns: [
                {
                    dataField: "seq",
                    caption: "NO",
                    alignment: "center",
                },
                {
                    dataField: "pstn_ixr_nmbr",
                    caption: "교차로번호",
                    alignment: "center",
                },
                {
                    dataField: "ixr_nm",
                    caption: "교차로명",
                    alignment: "center",
                },
            ],
            onRowDblClick(e) {
            },
        })
        .dxDataGrid("instance");
       
    _dayCtlrList = $(".day-ctlr-list")
        .width("100%")
        .height("100%")
        .dxDataGrid({
            dataSource: [],
            allowColumnReordering: true,
            showColumnLines: true,
            showBorders: true,
            allowColumnResizing: true,
            rowAlternationEnabled: true,
            focusedRowEnabled: true,
            columnAutoWidth: true,
            noDataText: "표출할 정보가 없습니다.",
            selection: {
                mode: "single",
            },
            keyExpr: "ixr_grop",
            scrolling: {
                mode: "virtual",
            },
            sorting:{
                mode: "none",
            },
            columns: [
                {
                    caption: "",
                    alignment: "center",
                    cellTemplate(c, e){
                        c.text('시각계획');
                    }
                },
                {
                    dataField : 'sun_hms_plan',
                    caption: "일",
                    alignment: "center",
                },
                {
                    dataField : 'mon_hms_plan',
                    caption: "월",
                    alignment: "center",
                },
                {
                    dataField : 'tue_hms_plan',
                    caption: "화",
                    alignment: "center",
                },
                {
                    dataField : 'wed_hms_plan',
                    caption: "수",
                    alignment: "center",
                },
                {
                    dataField : 'thu_hms_plan',
                    caption: "목",
                    alignment: "center",
                },
                {
                    dataField : 'fri_hms_plan',
                    caption: "금",
                    alignment: "center",
                },
                {
                    dataField : 'sat_hms_plan',
                    caption: "토",
                    alignment: "center",
                },
            ],
        })
        .dxDataGrid("instance");

    _specCtlrList = $('.spec-ctlr-list')
        .width("100%")
        .height("100%")
        .dxDataGrid({
            dataSource: [],
            allowColumnReordering: true,
            showColumnLines: true,
            showBorders: true,
            allowColumnResizing: true,
            rowAlternationEnabled: true,
            focusedRowEnabled: true,
            columnAutoWidth: true,
            noDataText: "표출할 정보가 없습니다.",
            selection: {
                mode: "single",
            },
            keyExpr: "no",
            scrolling: {
                mode: "virtual",
            },
            sorting:{
                mode: "none",
            },
            columns: [
                {
                    dataField: "spcl_dd_seq",
                    caption: "NO",
                    alignment: "center",
                },
                {
                    dataField : "spcl_dd_mn",
                    caption: "월",
                    alignment: "center",
                },
                {
                    dataField : "spcl_dd_dd",
                    caption: "일",
                    alignment: "center",
                },
                {
                    dataField : "hms_plan_nmbr",
                    caption: "게획번호",
                    alignment: "center",
                },
            ],
        })
        .dxDataGrid("instance");
        
    fetchBaseData();
});

function fetchBaseData(){
    let data = [];
    getData(_groupUri, data);
    recvListData(data[0], _managerData);
    if (data[0] && data[0].length > 0) {
        listTableDblClick(data[0][0]);
    }
}
function listTableDblClick(data){
    if (data) {
        const groupNmbr = encodeURIComponent(data.ixr_grop);
        getDataAsync('/api/scrs/tb_sc_week_hms_cntl/' + groupNmbr, recvListData, _infoList);
        getDataAsync('/api/scrs/tb_sc_spcl_dd_hms_cntl/' + groupNmbr, recvListData, _dayCtlrList);
        getDataAsync('/api/scrs/tb_sc_pstn_ixr_mngm/' + groupNmbr, recvListData, _specCtlrList);
    }
}
function recvListData(jsonData, table) {
    table.option('dataSource', jsonData);
}
