/*소통통계*/

var _StatsLinkVrtx1Arr = [];
var _StatsIfscVrtx2Arr = [];
var _StatsIfscVrtx3Arr = [];
var _StatsRoadVrtx4Arr = [];
var _StatsRoadVrtx5Arr = [];
var _StatsTrafficInfoArr = [];

/*교통량통계*/

function showVdsStatsInfo(linkId) {
    if (linkId == '') {
        alert('검지기를 선택해 주세요.');
        return;
    }

    $('#vdsListSub').val(linkId).prop('selected', true);
    $('#vdsName').empty().html('[' + $('#vdsListSub option:selected').text() + ']');

    var statsStrtYear = $('#statsStrtYear').val();
    var statsStrtMonth = $('#statsStrtMonth').val();
    var statsStrtDay = $('#statsStrtDay').val();
    var statsStrtDate = statsStrtYear + statsStrtMonth + statsStrtDay + '000000';

    var statsEndYear = $('#statsEndYear').val();
    var statsEndMonth = $('#statsEndMonth').val();
    var statsEndDay = $('#statsEndDay').val();
    var statsEndDate = statsEndYear + statsEndMonth + statsEndDay + '235959';
    $.ajax({
        type: 'POST',
        url: '/common/getVdsStatsInfo.do',
        data: 'linkId=' + linkId + '&statsStrtDate=' + statsStrtDate + '&statsEndDate=' +
                statsEndDate,
        success: function (data) {
            var str = '';
            if (data.length > 0) {
                var statsHour = [
                    '00',
                    '01',
                    '02',
                    '03',
                    '04',
                    '05',
                    '06',
                    '07',
                    '08',
                    '09',
                    '10',
                    '11',
                    '12',
                    '13',
                    '14',
                    '15',
                    '16',
                    '17',
                    '18',
                    '19',
                    '20',
                    '21',
                    '22',
                    '23'
                ];
                var series = [];
                for (var i = 0; i < data.length; i++) {
                    str += '<ul>';
                    str += '<li><a href="javascript:void(0)" title="통계날짜:' + data[i].stat_dd + '" tabIndex' +
                            '="26">' + data[i].stat_dd + '</a></li>';

                    data[i].tfvlArr.forEach(function (el, index) {
                        str += '<li><a href="javascript:void(0)" title="' + index + '시의 통행량:'
                            + addComma(el) + '" tabIndex="26">' + addComma(el) + '</a></li>';
                    });
                    str += '<li><a href="javascript:void(0)" title="통행량합계:' + addComma(
                        data[i].tfvlSum
                    ) + '" tabIndex="26">' + addComma(data[i].tfvlSum) + '</a></li>';
                    str += '</ul>';

                    series.push({name: data[i].stat_dd, data: data[i].tfvlArr});
                }

                $('#highcharts').show();
                drawCharts(
                    $('#vdsListSub option:selected').text(),
                    statsHour,
                    '시간',
                    '교통량(차량댓수)',
                    series
                );
            } else {
                $('#highcharts').hide();
                str += '<ul><li style="flex:1 !important"><a href="javascript:void(0)" title="데이터가 없습니' +
                        '다" tabIndex="26">데이터가 없습니다.</a></li></ul>';
            }
            $('#vdsStatsBody')
                .empty()
                .html(str);
        },
        beforeSend: function () {
            $('#loading').show();
        },
        complete: function () {
            $('#loading').hide();
        },
        error: function (e) {
            alert('교통량통계정보를 가저오는데 실패했습니다.');
        }
    });
}

function drawCharts(title, xAxis, xTitle, yTitle, series) {
    Highcharts.chart('highcharts', {
        chart: {
            type: 'line'
        },
        title: {
            text: title
        },
        xAxis: {
            categories: xAxis,
            title: {
                text: xTitle
            }
        },
        yAxis: {
            title: {
                text: yTitle
            }
        },
        plotOptions: {
            line: {
                dataLabels: {
                    enabled: true
                },
                enableMouseTracking: false
            }
        },
        series: series
    });
}

function getLastDay(month) {
    var lastDate = new Date($('#statsYear').val(), month, '');
    var str = '';

    for (var ii = 1; ii <= lastDate.getDate(); ii++) {
        str += '<option value="' + ii + '">' + ii + '</option>';
    }
    $('#statsDay').empty().html(str);
}

function addComma(num) {
    var regexp = /\B(?=(\d{3})+(?!\d))/g;
    return num.toString().replace(regexp, ',');
}