@extends('admin.master')

@section('style')
<link href="{{ asset('data/assets/css/bootstrap-datetimepicker.css') }}" rel="stylesheet">
@endsection

@section('top')
	<div class="top-breadcrumb">
        <h5>地域分布</h5>
    </div>
@endsection

@section('content')
	@include('admin.statistic.top')
	<h2 class="statistic-title">访问者国家/地址比重</h2>
	<div id="main" class="mt-20" style="height:600px;"></div>
    <table class="table table-hover">
        <thead>
            <tr>
                <th width="80" class="text-center">
                    序列
                </th>
                <th>国家/地址</th>
                <th>浏览量PV</th>
                <th>访客量UV</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for='(item,$index) in external'>
                <td align="center">@{{ $index + 1 }}</td>
                <td>@{{ item.key }}</td>
                <td>@{{ item.pv }}</td>
                <td>@{{ item.uv }}</td>
            </tr>
        </tbody>
    </table>
@endsection

@section('foot')
<script src="{{ asset('data/assets/js/bootstrap-datetimepicker.min.js') }}"></script>
<script src="{{ asset('data/assets/js/echarts.min.js') }}"></script>
<script type="text/javascript">
var token = "{{ inject('setting')->config('apptoken') }}";
var si    = "{{ inject('setting')->config('appid') }}";
var today = "{{ date('Y-m-d') }}";
var yesterday = "{{ date('Y-m-d', strtotime('-1 day')) }}";
var sevenday  = "{{ date('Y-m-d', strtotime('-6 day')) }}";
var month     = "{{ date('Y-m-d', strtotime('-29 day')) }}";
var urlLoad   = "{{ config('setting.api_url') }}" + "/country";

var visit = new Vue({
	el: '#main-view',
	data: {
		fromDate: month,
		toDate: today,
		dataSource: {},
		options: {},
		myChart: '',
        external: '',
		isActive: 3,
		timeText: ['今日', '昨天', '最近7天', '最近30天'],
	},
	mounted: function() {
		var that = this;
		this.myChart = echarts.init(document.getElementById('main'));
        this.myChart.showLoading();
        this.load(month, today);
        window.onresize = function sizes() {
            that.myChart.resize();
        };  
	},
	methods: {
		load: function(startTime, endTime) {
			var _this = this;
			$.ajax({
				type: "GET",
				url: urlLoad,
				data: {"si": si, "fromDate": startTime, "toDate": endTime},
				success: function(json) {
					_this.option(json);
                    _this.external = json;
				}
			});
		},
		change: function(index) {
			this.isActive = index;
			switch (index) {
				case 0:
					this.fromDate = today;
					this.toDate   = today;
					this.load(this.fromDate, this.toDate);
					break;
				case 1:
					this.fromDate = yesterday;
					this.toDate   = yesterday;
					this.load(this.fromDate, this.toDate);
					break;
				case 2:
					this.fromDate = sevenday;
					this.toDate   = today;
					this.load(this.fromDate, this.toDate);
					break;
				case 3:
					this.fromDate = month;
					this.toDate   = today;
					this.load(this.fromDate, this.toDate);
					break;		
			}
		},
		search: function() {
			this.fromDate = $('.datetimepicker-start').val();
			this.toDate   = $('.datetimepicker-end').val();
			this.load(this.fromDate, this.toDate);
		},
		contact: function(data, keys) {
			var arr = [];
			for ($i = 0; $i < data.length; $i++) {
				arr.push(data[$i][keys]);
			}
			return arr;
		},
        contactArr: function(data, keysA, keysB) {
			var arr = [];
			for ($i = 0; $i < data.length; $i++) {
                arr.push({value: data[$i][keysA], name: data[$i][keysB]});
			}
			return arr;
		},
		option: function(json) {
			this.options = {
                title : {
                    text: '访问者国家/地址比重',
                    subtext: '日期范围：' + this.fromDate + ' -- ' + this.toDate,
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    type: 'scroll',
                    orient: 'vertical',
                    right: 10,
                    top: 20,
                    bottom: 20,
                    data: this.contact(json, 'key')
                },
                series : [
                    {
                        name: '浏览量',
                        type: 'pie',
                        radius : '55%',
                        center: ['46%', '50%'],
                        data: this.contactArr(json, 'pv', 'key'),
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
			};
            this.myChart.hideLoading();
			this.myChart.setOption(this.options);
		},

	}
});

$('.datetimepicker-start').datetimepicker({
	language: 'zh-CN',
	autoclose: true,
	minView: 'month'
});
$('.datetimepicker-end').datetimepicker({
	language: 'zh-CN',
	autoclose: true,
	minView: 'month'
});


</script>
@endsection
