
let _todList;
let ixrNm;
let strtDt;
let endDt;
let strtTm;
let endTm;
let searchBtn;
let closeBtn;
let strtTmVal = new Date();
let endTmVal  = new Date();
strtTmVal.setHours("00");
strtTmVal.setMinutes("00");
strtTmVal.setSeconds("00");
endTmVal.setHours("23");
endTmVal.setMinutes("59");
endTmVal.setSeconds("59");
const _apiUrl = '/api/scrs/tb_sc_sgnl_ixr_mngm/list';

const _sttsUrl = '/api/scrs/tb_sc_sgnl_ixr_stts/list';
const _pageName = '신호 운영 이력 정보';
$(()=>{

    _todList = $(".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",
            },
            export:{
                enabled : true,
                texts   : {
                    exportAll:"엑셀 파일",
                },
            },
            keyExpr: "sgnl_ixr_nmbr",
            scrolling: {
                mode: "virtual",
            },
            sorting:{
                mode: "none",
            },
            columns: [
                {
                    dataField: "sgnl_ixr_nmbr",
                    caption: "NO",
                    alignment: "center",
                },
                {
                    dataField : 'hh',
                    caption: "시간",
                    alignment: "center",
                },
                {
                    dataField : 'mm',
                    caption: "분",
                    alignment: "center",
                },
                {
                    dataField : 'cycl',
                    caption: "주기",
                    alignment: "center",
                },
                {
                    dataField : 'ofst',
                    caption: "옵셋",
                    alignment: "center",
                },
                {
                    dataField : 'cntl_stts',
                    caption: "제어상태",
                    alignment: "center",
                    lookup : {
                        dataSource: [],
                        displayExpr: 'cmmn_cd_kor_nm',
                        valueExpr  : 'cmmn_cd'
                    }
                },
                {
                    dataField : 'cntl_mode',
                    caption: "제어모드",
                    alignment: "center",
                    lookup : {
                        dataSource: [],
                        displayExpr: 'cmmn_cd_kor_nm',
                        valueExpr  : 'cmmn_cd'
                    }
                },
                {
                    caption: "1현시",
                    alignment: "center",
                    columns : [
                        {
                            dataField : 'aring_1phas',
                            caption: "A",
                            alignment: "center",
                        },
                        {
                            dataField : 'bring_1phas',
                            caption: "B",
                            alignment: "center",
                        },
                    ]
                },
                {
                    caption: "2현시",
                    alignment: "center",
                    columns : [
                        {
                            dataField : 'aring_2phas',
                            caption: "A",
                            alignment: "center",
                        },
                        {
                            dataField : 'bring_2phas',
                            caption: "B",
                            alignment: "center",
                        },
                    ]
                },
                {
                    caption: "3현시",
                    alignment: "center",
                    columns : [
                        {
                            dataField : 'aring_3phas',
                            caption: "A",
                            alignment: "center",
                        },
                        {
                            dataField : 'bring_3phas',
                            caption: "B",
                            alignment: "center",
                        },
                    ]
                },
                {
                    caption: "4현시",
                    alignment: "center",
                    columns : [
                        {
                            dataField : 'aring_4phas',
                            caption: "A",
                            alignment: "center",
                        },
                        {
                            dataField : 'bring_4phas',
                            caption: "B",
                            alignment: "center",
                        },
                    ]
                },
                {
                    caption: "5현시",
                    alignment: "center",
                    columns : [
                        {
                            dataField : 'aring_5phas',
                            caption: "A",
                            alignment: "center",
                        },
                        {
                            dataField : 'bring_5phas',
                            caption: "B",
                            alignment: "center",
                        },
                    ]
                },
                {
                    caption: "6현시",
                    alignment: "center",
                    columns : [
                        {
                            dataField : 'aring_6phas',
                            caption: "A",
                            alignment: "center",
                        },
                        {
                            dataField : 'bring_6phas',
                            caption: "B",
                            alignment: "center",
                        },
                    ]
                },
                {
                    caption: "7현시",
                    alignment: "center",
                    columns : [
                        {
                            dataField : 'aring_7phas',
                            caption: "A",
                            alignment: "center",
                        },
                        {
                            dataField : 'bring_7phas',
                            caption: "B",
                            alignment: "center",
                        },
                    ]
                },
                {
                    caption: "8현시",
                    alignment: "center",
                    columns : [
                        {
                            dataField : 'aring_8phas',
                            caption: "A",
                            alignment: "center",
                        },
                        {
                            dataField : 'bring_8phas',
                            caption: "B",
                            alignment: "center",
                        },
                    ]
                },
               
            ],
            onExporting(e){
                gridExcellExporting(e, _pageName);
            },
        })
        .dxDataGrid("instance");

    ixrNm     = new dxSelect($(".ixr_nm"), 'outlined', 200, 30, 'ixr_nm', 'sgnl_ixr_nmbr', false, 'sgnl_ixr_nmbr', '교차로명');
    strtDt    = new dxDate($(".strt-dt"), 'yyyy-MM-dd', '조회기간 시작일', 140, 30, 'date', null, '', null);
    strtTm    = new dxDate($(".strt-tm"), 'HH:mm', '조회기간 시작시각', 80, 30, 'time', null, '', null);
    endDt     = new dxDate($(".end-dt"),  'yyyy-MM-dd', '조회기간 종료일', 140, 30, 'date', null, '', null);
    endTm     = new dxDate($(".end-tm"),  'HH:mm', '조회기간 종료시각', 80, 30, 'time', null, '', null);
    
    strtDt.setValue(new Date());
    strtTm.setValue(strtTmVal);
    endDt.setValue(new Date());
    endTm.setValue(endTmVal);
    searchBtn = new dxBtn($(".search-btn"),'조회', 'refresh', 'outlined', 80, 30, false, true); 
    closeBtn  =  new dxBtn($(".close-btn"), '닫기', 'close', 'outlined', 80, 30, false, true);
    closeBtn.onClick(()=>window.close());
    fetchBaseData();
    searchBtn.onClick(searchBtnClick);
})

function fetchBaseData(){
    getDataAsync(_apiUrl, (jsonData)=>{
        ixrNm.setDataSource(jsonData);
        ixrNm.setValue(jsonData[0].sgnl_ixr_nmbr);
    })
    getDataAsync('/api/database/code/cmmn-cd/CTLSTTS', recvCdData, 5);
    getDataAsync('/api/database/code/cmmn-cd/CTLMODE', recvCdData, 6);
}
function recvCdData(jsonData, idx){
    if (jsonData && jsonData.length > 0) {
        jsonData.map((item)=>{
            item.cmmn_cd = Number(item.cmmn_cd);
        })
    }
    _todList.option('columns[' + idx + '].lookup.dataSource', jsonData);
}

function searchBtnClick(){
    let TO_DT        = null;
    let FROM_DT      = null;
    let id           = ixrNm.getValue();
    let updateData = {};
    if (nullChecker(id) === "") {
        return alertWarning("교차로 ID를 선택해주세요.", null, ixrNm);
    }
    if (!validationCheck(strtDt.text, endDt.text, strtTm.text, endTm.text)){
        return false;
    }
    let strtDtVal = strtDt.getSendDateValue().substring(0,8);
    let strtTmVal = strtTm.getSendTimeValue();
    let endDtVal  = endDt.getSendDateValue().substring(0,8);
    let endTmVal  = endTm.getSendTimeValue();
    FROM_DT = strtDtVal + strtTmVal;
    TO_DT   = endDtVal + endTmVal;
    if (FROM_DT > TO_DT) {
        return alertWarning("검색 시작일시가 종료일시 보다 큽니다.", null, endDt);
    }
    updateData[ 'FROM_DT' ] = FROM_DT;
    updateData[ 'TO_DT' ]   = TO_DT;
    updateData[ 'id' ]      = id;
    getDataAsync( _sttsUrl, (jsonData)=>{
        _todList.option( 'dataSource', jsonData );
        selResultMsg(jsonData);
    }, null, updateData);
    
}