new Vue({
	el: '#main',
	data: {
		roles: [],
		selectedRole: "",
		isopen: true,
		signAnalysis: [{
			photo: '../../../images/qianyujingzhan_icon.png',
			label: '签约进展',
			url: 'sign-progress.html',
            allowArea:"",
		}, {
			photo: '../../../images/xuqianjingzhan_icon.png',
			label: '续签进展',
			url: 'renew-progress.html',
            allowArea:"",
		}, {
			photo: '../../../images/jumingfenxi_icon.png',
			label: '居民分析',
			url: 'resident-analysis.html',
            allowArea:"",
		}, {
			photo: '../../../images/tuigaiqian_icon.png',
			label: '退改签',
			url: 'change-back.html',
            allowArea:"",
		}, {
			photo: '../../../images/lianglvfenxi_icon.png',
			label: '两率分析',
			url: 'two-rate-analysis.html',
            allowArea:"",
		}],
		serviceAnalysis: [{
			photo: '../../../images/zongtifenxi_icon.png',
			label: '总体分析',
			url: 'comprehensive-analysis.html',
            allowArea:"",
		}, {
			photo: '../../../images/zixunfenxi_icon.png',
			label: '咨询分析',
			url: 'consulting-analysis.html',
            allowArea:"",
		}, {
			photo: '../../../images/pijianfenxi_icon.png',
			label: '评价分析',
			url: 'estimate-analysis.html',
            allowArea:"",
		}, {
			photo: '../../../images/changcufangfenxi_icon.png',
			label: '长处方分析',
			url: 'prescription-analysis.html',
			allowArea:"",
		}, {
			photo: '../../../images/zigetongji_icon.png',
			label: '资质分析',
			url: 'qualifications-analysis.html',
			allowArea:"350203",
		}, {
			photo: '../../../images/buzhufeiyong_icon.png',
			label: '补助费用统计',
			url: 'subsidy-cost.html',
            allowArea:"350203",
		}, {
			photo: '../../../images/gongdanfenxi_icon.png',
			label: '服务工单分析',
			url: 'service-sheet-analysis.html',
            allowArea:"350203",
		}, {
			photo: '../../../images/xiangyinfenxi_icon.png',
			label: '响应速度分析',
			url: 'response-speed-analysis.html',
            allowArea:"350203",
		}],
		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.sessionStorage.getItem("userRole");
	    //如果没有缓存userRole，则说明是第一次进入统计页面，需要重新请求患者信息
	    if(!roles){
	        var vm = this;
	        httpRequest.getDoctorInfo().then(function(res){
	            var docInfo = res.data,
                userRole = docInfo.userRole;
                if(userRole.length>0){
                    window.sessionStorage.setItem("userRole", JSON.stringify(userRole));
                    window.sessionStorage.setItem("docInfo", JSON.stringify(docInfo));
                    vm.roles = userRole;
                    vm.selectedRole = userRole[0].code;
                    var selected = userRole[0];
                    window.sessionStorage.setItem("selectedRole", JSON.stringify(selected));
                    $("#main").removeClass("c-hide")
                }else{
                     top.toastr.warning("您没有权限查看该页面");
                }	            
	        })
	    }else{
	        this.roles = JSON.parse(roles);
	        if(this.roles.length>0){
	            var selected = window.sessionStorage.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.sessionStorage.setItem("selectedRole", JSON.stringify(selected));
                }
                $("#main").removeClass("c-hide")
	        }else{
	            top.toastr.warning("您没有权限查看该页面");
	        }	        
	    }
	    console.log(this.selectedRole)
		
	},
	watch: {
		selectedRole: function(val) {
			var selected = _.findWhere(this.roles, {
				code: val
			});
			window.sessionStorage.setItem("selectedRole", JSON.stringify(selected));
		    getSignData(this);
		    console.log(this.selectedRole)
		}
	}
})

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 val1 = data.signRate.people - data.signRate.sign,
                val2 = data.signTaskRate.people - data.signTaskRate.sign;
            var signArr = [
                    {name: "签约数", value: data.signRate.sign},
                    {name: "未签约数", value: val1 >0 ? val1 : 0},
                ],
                signTaskArr = [
                    {name: "签约数", value: data.signTaskRate.sign},
                    {name: "未达标数", value: val2 >0 ? val2 : 0 }
                ],
                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)+"%";
}
