new Vue({
	el: '#main',
	data: {
		roles: [],
		selectedRole: "",
		isopen: true,
		signAnalysis: [{
			photo: '../../../images/qianyujingzhan_icon.png',
			label: '签约进展',
			url: 'sign-progress.html'
		}, {
			photo: '../../../images/xuqianjingzhan_icon.png',
			label: '续签进展',
			url: 'renew-progress.html'
		}, {
			photo: '../../../images/jumingfenxi_icon.png',
			label: '居民分析',
			url: 'resident-analysis.html'
		}, {
			photo: '../../../images/tuigaiqian_icon.png',
			label: '退改签',
			url: 'change-back.html'
		}, {
			photo: '../../../images/lianglvfenxi_icon.png',
			label: '两率分析',
			url: 'two-rate-analysis.html'
		}],
		serviceAnalysis: [{
			photo: '../../../images/zongtifenxi_icon.png',
			label: '总体分析',
			url: 'comprehensive-analysis.html'
		}, {
			photo: '../../../images/zixunfenxi_icon.png',
			label: '咨询分析',
			url: 'consulting-analysis.html'
		}, {
			photo: '../../../images/pijianfenxi_icon.png',
			label: '评价分析',
			url: 'estimate-analysis.html'
		}, {
			photo: '../../../images/changcufangfenxi_icon.png',
			label: '长处方分析',
			url: 'prescription-analysis.html'
		}],
		signRateData: {
		    signTask: {
		        text: '',
		        rate: '0.00',
		        sign: 0,
		        people: 0
		    },
		    sign: {
		        text: '',
		        rate: '0.00',
		        sign: 0,
		        people: 0
		    },
		    renew: {
		        text: '',
		        rate: '0.00',
		        sign: 0,
		        people: 0
		    }
		}
	},
	methods: {
		open: function() {
			EventBus.$emit('open-click', {});
		},
		showDetail: function(url) {
			window.location.href = url;
		},
		goToPage: function(val){
		    var url = "sign-progress.html";
		    if(val == 2){
		        url = "renew-progress.html";
		    }
		    window.location.href = url;
		}
	},
	mounted: function() {
	    //获取缓存里面的userRole
	    var roles = window.localStorage.getItem("userRole");
	    this.roles = JSON.parse(roles);
		var selected = window.localStorage.getItem("selectedRole");
		if(selected && selected!= "undefined") {
			this.selectedRole = JSON.parse(selected).code;
		} else {
			this.selectedRole = this.roles[0].code;
			var selected = this.roles[0];
			window.localStorage.setItem("selectedRole", JSON.stringify(selected));
		}
		$("#main").removeClass("c-hide")
	},
	watch: {
		selectedRole: function(val) {
			var selected = _.findWhere(this.roles, {
				code: val
			});
			window.localStorage.setItem("selectedRole", JSON.stringify(selected));
		    getSignData(this);
		}
	}
})

function getSignData(vm){
    var code = vm.selectedRole,
        now = new Date(),
        params = {
            area: code,
            level: code == '350200' ? 4 : code.length == 6 ? 3 : 2,
            endDate: now.format("yyyy-MM-dd")
        };
    statisticAPI.getSignInfo(params).then(function(res){
        if(res.status == 200){
            var data = res.data;
            vm.signRateData = {
                signTask: {
                    text: formatRate(data.signTaskRate.rate),
                    rate: '0.00',
                    sign: data.signTaskRate.sign,
                    people: data.signTaskRate.people
                },
                sign: {
                    text: formatRate(data.signRate.rate),
                    rate: '0.00',
                    sign: data.signRate.sign,
                    people: data.signRate.people
                },
                renew: {
                    text: data.renewRange,
                    rate: '0.00',
                    sign: data.renew.split("/")[0],
                    people: data.renew.split("/")[1]
                }
            }
            var signArr = [
                    {name: "签约数", value: data.signRate.sign},
                    {name: "未签约数", value: data.signRate.people - data.signRate.sign},
                ],
                signTaskArr = [
                    {name: "签约数", value: data.signTaskRate.sign},
                    {name: "未达标数", value: data.signTaskRate.people - data.signTaskRate.sign}
                ],
                val = data.renew.split("/")[1] - data.renew.split("/")[0],
                renewArr = [
                    {name: "续签数", value: data.renew.split("/")[0]},
                    {name: "未达标数", value: val<0 ? 0 : val}
                ],
                color = ['#12b7f5', '#EBEBF5'];
            var signChart = drawPieChart("signMain", signArr, color, true);
            var completeChart = drawPieChart("completeMain", signTaskArr, color, true);
            var renewChart = drawPieChart("renewMain", renewArr, color, true);
            window.onresize = function() {
                signChart.resize();
                completeChart.resize();
                renewChart.resize();
            }
        }else{
            console.log(res.msg);
        }
    })
}

function formatRate(str){
    var val = parseFloat(str);
    return val.toFixed(2)+"%";
}
