let _list;
let _totList;
let ixrNm;
let istlLctn;
let lane;
let strtDt;
let endDt;
let searchBtn;
let closeBtn;
let strtDtVal = new Date();
let endDtVal  = new Date();
strtDtVal.setHours("00");
strtDtVal.setMinutes("00");
strtDtVal.setSeconds("00");
endDtVal.setHours("23");
endDtVal.setMinutes("59");
endDtVal.setSeconds("59");
$(()=>{
    _list = $('.list').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 :'multiple',
            showCheckBoxesMode  : "onClick"
        },
        keyExpr : ['ixr_nm', 'clct_dt'],
        paging: {
            enabled: false,
            pageSize: 1000,
        },
        scrolling: {
            mode: 'standard',
        },
        columns:  [
            {
                caption   : "기본 정보",
                alignment : "center",
                columns : [
                    {
                        dataField : "ixr_nm",
                        caption   : '교차로명',
                        alignment : 'center',
                    },
                    {
                        dataField : "istl_lctn",
                        caption   : '설치위치',
                        alignment : 'center',
                    },
                    {
                        dataField : "clct_dt",
                        caption   : '수집일시',
                        alignment : 'center',
                    },
                    {
                        dataField : "day",
                        caption   : '요일',
                        alignment : 'center',
                    },
                    {
                        dataField : "lane",
                        caption   : '차로',
                        alignment : 'center',
                    },
                    {
                        dataField : "intl_atmp_heat_lngt",
                        caption   : '대기행렬길이(m)',
                        alignment : 'center',
                    },
                    {
                        dataField : "ocpy_rate",
                        caption   : '점유율',
                        alignment : 'center',
                    },
                    {
                        dataField : "walk_cnt",
                        caption   : '보행자수',
                        alignment : 'center',
                    },
                ]
            },
            {
                caption   : '직진 교통량(대)',
                alignment : "center",
                columns : [
                    {
                        dataField : "lrg_go_tfvl",
                        caption   : '대형차',
                        alignment : 'center',
                    },
                    // {
                    //     dataField : "mddl_go_tfvl",
                    //     caption   : '중형차',
                    //     alignment : 'center',
                    // },
                    {
                        dataField : "smal_go_tfvl",
                        caption   : '소형차',
                        alignment : 'center',
                    },
                    {
                        dataField : "bus_dvrs_lane_go_tfvl",
                        caption   : '버스',
                        alignment : 'center',
                    },
                ]
            },
            {
                caption   : '좌회전 교통량(대)',
                alignment : "center",
                columns : [
                    {
                        dataField : "lrg_left_tfvl",
                        caption   : '대형차',
                        alignment : 'center',
                    },
                    // {
                    //     dataField : "mddl_left_tfvl",
                    //     caption   : '중형차',
                    //     alignment : 'center',
                    // },
                    {
                        dataField : "smal_left_tfvl",
                        caption   : '소형차',
                        alignment : 'center',
                    },
                    {
                        dataField : "bus_dvrs_lane_left_tfvl",
                        caption   : '버스',
                        alignment : 'center',
                    },
                ]
            },
            {
                caption   : '우회전 교통량(대)',
                alignment : "center",
                columns : [
                    {
                        dataField : "lrg_rght_tfvl",
                        caption   : '대형차',
                        alignment : 'center',
                    },
                    // {
                    //     dataField : "mddl_rght_tfvl",
                    //     caption   : '중형차',
                    //     alignment : 'center',
                    // },
                    {
                        dataField : "smal_rght_tfvl",
                        caption   : '소형차',
                        alignment : 'center',
                    },
                    {
                        dataField : "bus_dvrs_lane_rght_tfvl",
                        caption   : '버스',
                        alignment : 'center',
                    },
                ]
            },
            {
                caption   : 'U턴 교통량(대)',
                alignment : "center",
                columns : [
                    {
                        dataField : "uturn_tfvl",
                        caption   : '교통량',
                        alignment : 'center',
                    },
                ]
            },
        ],
    });
    _totList = $('.tot-list').width('100%').height('100%').dxDataGrid({
        dataSource : [],
        allowColumnReordering   : true,
        showColumnLines         : true,
        showBorders             : true,
        allowColumnResizing     : true,
        rowAlternationEnabled   : true,
        columnAutoWidth         : true,
        noDataText              : '표출할 정보가 없습니다.',
        scrolling: {
            mode: 'standard',
        },
        columns:  [
            {
                dataField : "intl_atmp_heat_lngt",
                caption   : '대기행렬길이(m)',
                alignment : 'center',
            },
            {
                dataField : "ocpy_rate",
                caption   : '점유율',
                alignment : 'center',
            },
            {
                dataField : "walk_cnt",
                caption   : '보행자수',
                alignment : 'center',
            },
            {
                caption   : '직진 교통량(대)',
                alignment : "center",
                columns : [
                    {
                        dataField : "lrg_go_tfvl",
                        caption   : '대형차',
                        alignment : 'center',
                    },
                    // {
                    //     dataField : "mddl_go_tfvl",
                    //     caption   : '중형차',
                    //     alignment : 'center',
                    // },
                    {
                        dataField : "smal_go_tfvl",
                        caption   : '소형차',
                        alignment : 'center',
                    },
                    {
                        dataField : "bus_dvrs_lane_go_tfvl",
                        caption   : '버스',
                        alignment : 'center',
                    },
                ]
            },
            {
                caption   : '좌회전 교통량(대)',
                alignment : "center",
                columns : [
                    {
                        dataField : "lrg_left_tfvl",
                        caption   : '대형차',
                        alignment : 'center',
                    },
                    // {
                    //     dataField : "mddl_left_tfvl",
                    //     caption   : '중형차',
                    //     alignment : 'center',
                    // },
                    {
                        dataField : "smal_left_tfvl",
                        caption   : '소형차',
                        alignment : 'center',
                    },
                    {
                        dataField : "bus_dvrs_lane_left_tfvl",
                        caption   : '버스',
                        alignment : 'center',
                    },
                ]
            },
            {
                caption   : '우회전 교통량(대)',
                alignment : "center",
                columns : [
                    {
                        dataField : "lrg_rght_tfvl",
                        caption   : '대형차',
                        alignment : 'center',
                    },
                    // {
                    //     dataField : "mddl_rght_tfvl",
                    //     caption   : '중형차',
                    //     alignment : 'center',
                    // },
                    {
                        dataField : "smal_rght_tfvl",
                        caption   : '소형차',
                        alignment : 'center',
                    },
                    {
                        dataField : "bus_dvrs_lane_rght_tfvl",
                        caption   : '버스',
                        alignment : 'center',
                    },
                ]
            },
            {
                dataField : "uturn_tfvl",
                caption   : 'U턴교통량(대)',
                alignment : 'center',
            },
        ],
    });

    ixrNm     = new dxSelect($(".ixr_nm"), 'outlined', 200, 30, 'ixr_nm', 'ixr_id', false, 'ixr_id', '교차로명');
    istlLctn  = new dxSelect($(".istl_lctn"), 'outlined', 280, 30, 'istl_lctn', 'cmra_id', false, 'cmra_id', '설치위치');
    lane      = new dxSelect($(".lane"), 'outlined', 120, 30, 'desc', 'lane', false, 'lane', '차선');
    strtDt    = new dxDate($(".strt-dt"), 'yyyy-MM-dd HH:mm', '조회기간 시작일시', 140, 30, 'datetime', null, '', null);
    endDt     = new dxDate($(".end-dt"),  'yyyy-MM-dd HH:mm', '조회기간 종료일시', 140, 30, 'datetime', null, '', null);
    
    strtDt.setValue(strtDtVal);
    endDt.setValue(endDtVal);
    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());
    ixrNm.onItemClick((e)=>ixrNmClick(e.itemData.ixr_id));
    fetchBaseData();
})

function fetchBaseData(){
    let listData = [];
    getData('/api/scrs/tb_sc_ixr_mngm/list', listData);
    ixrNm.setDataSource(listData[0]);
    if (listData[0] && listData[0].length > 0) {
        dataSorting(listData[0], 'ixr_id');
        ixrNm.setValue(listData[0][0].ixr_id);
        ixrNmClick(listData[0][0].ixr_id);
    }
}
function ixrNmClick(ixrIdVal){
    let istlData = [];
    getData('/api/scrs/tb_sc_ixr_cmra_mngm/' + encodeURIComponent(ixrIdVal), istlData);
    istlLctn.setDataSource(istlData[0]);
    console.log(istlData);
    if (istlData[0] && istlData[0].length > 0) {
        istlLctn.setValue(istlData[0][0].cmra_id);
    }
}

/**
 * 
CLCT_DT
IXR_ID
CMRA_ID
DRCT_DVSN_CD
LANE
INTL_ATMP_HEAT_LNGT
OCPY_RATE
WALK_CNT
LRG_GO_TFVL
MDDL_GO_TFVL
SMAL_GO_TFVL
LRG_LEFT_TFVL
MDDL_LEFT_TFVL
SMAL_LEFT_TFVL
LRG_RGHT_TFVL
MDDL_RGHT_TFVL
SMAL_RGHT_TFVL
UTURN_TFVL
SPED
BUS_DVRS_LANE_GO_TFVL
BUS_DVRS_LANE_LEFT_TFVL
BIKE_GO_TFVL
BIKE_LEFT_TFVL
BIKE_RGHT_TFVL
 */