new Vue({
	el: '#main',
	data: {     
        alertData1:'',//总人数
        alertData2:'',//新增人数
        alertData3:'',//门诊人次
        alertData4:'',//住院人次
        alertChart1:'',//左边第一块表格
        alertChart2:'',//右上第一块
        alertChart3:'',//右上第二块
        alertChart4:'',//右上第三块
        alertChart5:'',//右下
        bigData:'',//总数据
        alertType:0,
        alertTime:0,
        firstDay: "", //上月第一天
        lastDay: "", //上月最后一天
        firstMonth:'',
        lastMonth:'',
        code:[
            'VIEW_15_0001',//总人数和新增人数
            'VIEW_15_0002',//门诊和住院人次
            'VIEW_15_0003',//区县分布
            'VIEW_15_0004',//性别分布
            'VIEW_15_0005',//疾病前五
            'VIEW_15_0006',//就诊月趋势
            'VIEW_15_0007',//就诊年趋势
        ],
        qushiMonth:'',
        qushiYear:'',
        firstDayYear: "", //本年第一天
		lastDayYear: "", //本年最后一天
		halfYearStartDate: "", //近半年开始时间
		halfYearEndDate: "", //近半年结束时间
        yesterdayDate:"",//昨天
        type:0
	},
	mounted: function() {
        var vm = this;
        this.formatDate();
        this.initData(this.type);
        var myDate = new Date();
        var maxMonth = myDate.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var maxYear = myDate.getFullYear();
        var max = maxYear.toString()+'-'+maxMonth.toString();
        console.log(vm.firstMonth,vm.lastMonth)
        var moren = vm.firstMonth+' 至 '+vm.lastMonth;
        layui.use('laydate', function(){
            var laydate = layui.laydate;
            //执行一个laydate实例
            laydate.render({
              elem: '#time', //指定元素,
              type:'month',
              range: '至', //或 range: '~' 来自定义分割字符
              theme: '#05273e',
              position: 'abolute',
              right:'200px',
              value: moren,
              max:max,
              btns: ['confirm'],
              done: function(value, date, endDate){
                console.log(value); //得到日期生成的值，如：2017-08-18
                console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
                console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
                console.log(vm.firstDay);
                if(date.month < 10){
                    vm.firstDay = date.year+'-0'+date.month+'-01';
                }else{
                    vm.firstDay = date.year+'-'+date.month+'-01';
                }
                if(endDate.month < 10){
                    vm.lastDay = endDate.year+'-0'+endDate.month+'-'+vm.getLastDay(endDate.year,endDate.month);
                }else{
                    vm.lastDay = endDate.year+'-'+endDate.month+'-'+vm.getLastDay(endDate.year,endDate.month);
                }
                vm.halfYearStartDate = getLastHalfMonthArray(endDate.year+'-'+endDate.month)
                vm.halfYearEndDate = vm.lastDay;
                vm.initData(vm.type);
                vm.alertTime = 0;
              }
            //   showBottom: false
            });
        });
        
    },
    methods: {
        initData:function(type){
            var vm = this;
            if(type == 0){
                event_type = '';//全部
            }else if(type == 1){
                event_type = 1;//住院
            }else if(type == 2){
                event_type = 0;//门诊
            }
            var linkageFilter1 = "quotaDate >= '" + vm.firstDay + "' and quotaDate <= '" + vm.lastDay + "';";
            var linkageFilter3 = "quotaDate >= '" + vm.halfYearStartDate + "' and quotaDate <= '" + vm.halfYearEndDate + "';";
            console.log(linkageFilter1)
            var code = this.code;
            var urlNumber = '/gov/report/getNoChartTemplateData';
            var urlChart = '/gov/report/getTemplateDataByViewCode';
            var reqUrl = [
                {url:urlNumber,reqType: 'get',data: {viewCodeStr:code[0]}},//总人数
                {url:urlNumber,reqType: 'get',data: {viewCodeStr:code[0],linkageFilter:linkageFilter1}},//新增人数
                {url:urlNumber,reqType: 'get',data: {viewCodeStr:code[1],linkageFilter:linkageFilter1+'eventType=0'}},//门诊,住院人次
                {url:urlChart,reqType: 'get',data: {viewCode:code[2],linkageFilter:linkageFilter1+'eventType='+event_type}},//区县分布
                {url:urlChart,reqType: 'get',data: {viewCode:code[3],linkageFilter:linkageFilter1+'eventType='+event_type}},//性别分布
                {url:urlChart,reqType: 'get',data: {viewCode:code[4],linkageFilter:linkageFilter1+'eventType='+event_type}},//疾病前五
                {url:urlChart,reqType: 'get',data: {viewCode:code[5],linkageFilter:linkageFilter3+'eventType='+event_type}},//老年人就诊趋势-月
                {url:urlChart,reqType: 'get',data: {viewCode:code[6],linkageFilter:'eventType='+event_type}},//老年人就诊趋势-年
            ]
            $(".div-mask-layer").show();
            httpRequest.getReqPromises(reqUrl).then(function(datas) {
                console.log(datas);
                debugger
                vm.alertData1 = datas[0].obj.VIEW_15_0001[0].HC_15_1006;
                vm.alertData2 = datas[1].obj.VIEW_15_0001[0].HC_15_1006;
                vm.alertData3 = datas[2].obj.VIEW_15_0002[0].HC_15_1003;
                vm.alertData4 = datas[2].obj.VIEW_15_0002[0].HC_15_1004;
                var chart1 = datas[3].obj.viewInfos[0].options[0].option;
                var chart2 = datas[4].obj.viewInfos[0].options[0].option;
                var chart3 = datas[5].obj.viewInfos[0].options[0].option;
                var chart4 = datas[6].obj.viewInfos[0].options[0].option;
                var chart5 = datas[7].obj.viewInfos[0].options[0].option;
                vm.qushiMonth = chart4;
                vm.qushiYear = chart5;
                vm.setAlertChart1(chart1);
                vm.setAlertChart3(chart2);
                vm.setAlertChart4(chart3);
                vm.setAlertChart5(chart4);
                $(".div-mask-layer").hide();
            })
        },
        setAlertChart1:function(data){
            var options = JSON.parse(data);
            options = quxianChart(options);
            // var options = this.bigData[t]['区县分布'];
            // options.series[0].label = {show: true,position: "right",color: '#b5e1fc'}
            this.alertChart1 = options;
        },
        setAlertChart2:function(data){
            var options = JSON.parse(data);
            options = nianlingChart(options);
            options.color = ["#6576e0","#03fa6c","#ff616f","#0fa5f2","#00e6f3"];
            options.legend.formatter = function(name) {
                var index = 0;
                var clientlabels = options.legend.data;
                var clientcounts = _.map(options.series[0].data,function(item,idx){
                	return item.value;
                });
                clientlabels.forEach(function(value,i){
                    if(value == name){
                        index = i;
                    }
                });
                return name + "  " + clientcounts[index];
            }
            options.legend.left = '48%';
            options.legend.top = 0.044 * window.screen.width;//85;
            options.legend.itemWidth = 0.005 * window.screen.width;//10;
            options.legend.itemHeight = 0.005 * window.screen.width;//10;
            options.legend.itemGap = 0.0078 * window.screen.width;//15;
            options.series[0].center = ['25%','55%'];
            this.alertChart2 = options
        },
        setAlertChart3:function(data){
            var options = JSON.parse(data);
            options = xingbieChart(options);
            options.color = ["#00E7F3","#ff616f"];
            options.legend.formatter = function(name) {
                var index = 0;
                var clientlabels = options.legend.data;
                var clientcounts = _.map(options.series[0].data,function(item,idx){
                	return item.value;
                });
                clientlabels.forEach(function(value,i){
                    if(value == name){
                        index = i;
                    }
                });
                return name + "  " + clientcounts[index];
            }
            options.legend.left = '48%';
            options.legend.top = 0.0625 * window.screen.width;//120
            options.legend.itemWidth = 0.005 * window.screen.width;//10;
            options.legend.itemHeight = 0.005 * window.screen.width;//10;
            options.legend.itemGap = 0.0078 * window.screen.width;//15;
            options.series[0].center = ['25%','55%'];
            this.alertChart3 = options
        },
        setAlertChart4:function(data){
            var options = JSON.parse(data);
            options = jibingTopChart(options);
            options.series[0].label = {show: true,position: "right",color: '#b5e1fc'};
            options.series[0].itemStyle.normal.color = '#FFF71A';
            this.alertChart4 = options
        },
        setAlertChart5:function(data){
            var options = JSON.parse(data);
            console.log(options)
            options = qushiChart(options);
            // var options = this.bigData[t]['区县分布'];
            // options.series[0].label = {show: true,position: "right",color: '#b5e1fc'}
            this.alertChart5 = options;
        },
        alertTitle:function(type){
            this.alertTime = 0;
            this.type = type;
            this.initData(type)
        },
        alertChart:function(time){
            var vm = this;
            this.alertTime = time;
            if(time == 0){
                vm.setAlertChart5(vm.qushiMonth);
            }else if(time == 1){
                vm.setAlertChart5(vm.qushiYear);
            }
        },
        closeAlertClick:function(){
        	parent.layer.closeAll();
        },
        GetQueryString :function (name){
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  unescape(r[2]); return null;
        },
        chartArea:function(param){
            console.log(param);
        },
        clickBarItem:function(item){
        	console.log(item)
        },
		formatDate: function() {
			//获取上个月第一天
			var firstdate = new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1);
			//获取上个月最后一天
			var date = new Date();
			var day = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
			var enddate = new Date(new Date().getFullYear(), new Date().getMonth() - 1, day);
			//获取本年第一天和最后一天
			var firstYearDate = new Date();
			firstYearDate.setDate(1);
			firstYearMonty = firstYearDate.setMonth(0);
			//近半年开始时间
			var date1 = new Date();
			date1.setMonth(date1.getMonth() - 6);
			var year1 = date1.getFullYear() - 1;
			var month1 = date1.getMonth() + 1;
			month1 = (month1 < 10 ? "0" + month1 : month1);
			//格式化结果
            this.firstDay = firstdate.format("yyyy-MM-dd");
            this.firstMonth = firstdate.format("yyyy-MM");
            this.lastDay = enddate.format("yyyy-MM-dd");
            this.lastMonth = enddate.format("yyyy-MM");
            this.firstDayYear = firstYearDate.format("yyyy-MM-dd");
			this.lastDayYear = (new Date().getFullYear()-1) + "-12-31";
			this.halfYearStartDate = year1.toString() + '-' + month1.toString() + "-01";
			this.halfYearEndDate = this.getCurrentMonthLast2017();
			this.yesterdayDate = this.getYesterday2017(-1, '-');
        },
		getCurrentMonthLast: function() {
			var date = new Date();
			var currentMonth = date.getMonth();
			var nextMonth = ++currentMonth;
			var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth-1, 1);
			var oneDay = 1000 * 60 * 60 * 24;
			return(new Date(nextMonthFirstDay - oneDay)).format("yyyy-MM-dd");
		},
		getYesterday:function(num, str){
			var today = new Date();
		    var nowTime = today.getTime();
		    var ms = 24*3600*1000*num;
		    today.setTime(parseInt(nowTime + ms));
		    var oYear = today.getFullYear();
		    var oMoth = (today.getMonth() + 1).toString();
		    if (oMoth.length <= 1) oMoth = '0' + oMoth;
		    var oDay = today.getDate().toString();
		    if (oDay.length <= 1) oDay = '0' + oDay;
		    return oYear + str + oMoth + str + oDay;
		},
        formatDate2017: function() {
			//获取上个月第一天
			var firstdate = new Date(new Date().getFullYear()-1, new Date().getMonth() - 1, 1);
			//获取上个月最后一天
			var date = new Date();
			var day = new Date(date.getFullYear()-1, date.getMonth(), 0).getDate();
			var enddate = new Date(new Date().getFullYear()-1, new Date().getMonth() - 1, day);
			//获取本年第一天和最后一天
			var firstYearDate = new Date();
			firstYearDate.setFullYear(new Date().getFullYear()-1)
			firstYearDate.setDate(1);
			firstYearMonty = firstYearDate.setMonth(0);
			//近半年开始时间
			var date1 = new Date();
			date1.setMonth(date1.getMonth() - 6);
			var year1 = date1.getFullYear() - 1;
			var month1 = date1.getMonth() + 1;
			month1 = (month1 < 10 ? "0" + month1 : month1);
			//格式化结果
            this.firstDay = firstdate.format("yyyy-MM-dd");
            this.firstMonth = firstdate.format("yyyy-MM");
            this.lastDay = enddate.format("yyyy-MM-dd");
            this.lastMonth = enddate.format("yyyy-MM");
			this.firstDayYear = firstYearDate.format("yyyy-MM-dd");
			this.lastDayYear = (new Date().getFullYear()-1) + "-12-31";
			this.halfYearStartDate = year1.toString() + '-' + month1.toString() + "-01";
			this.halfYearEndDate = this.getCurrentMonthLast2017();
			this.yesterdayDate = this.getYesterday2017(-1, '-');
        },
		getYesterday2017:function(num, str){
			var today = new Date();
		    var nowTime = today.getTime();
		    var ms = 24*3600*1000*num;
		    today.setTime(parseInt(nowTime + ms));
		    var oYear = today.getFullYear()-1;
		    var oMoth = (today.getMonth() + 1).toString();
		    if (oMoth.length <= 1) oMoth = '0' + oMoth;
		    var oDay = today.getDate().toString();
		    if (oDay.length <= 1) oDay = '0' + oDay;
		    return oYear + str + oMoth + str + oDay;
		},
		getCurrentMonthLast2017: function() {
			var date = new Date();
			var currentMonth = date.getMonth();
			var nextMonth = ++currentMonth;
			var nextMonthFirstDay = new Date(date.getFullYear() - 1, nextMonth-1, 1);
			var oneDay = 1000 * 60 * 60 * 24;
			return(new Date(nextMonthFirstDay - oneDay)).format("yyyy-MM-dd");
        },
        getLastDay(year, month) {
            var new_year = year; //取当前的年份
            var new_month = month++; //取下一个月的第一天，方便计算（最后一天不固定）
            if (month > 12) {
              //如果当前大于12月，则年份转到下一年
              new_month -= 12; //月份减
              new_year++; //年份增
            }
            var new_date = new Date(new_year, new_month, 1); //取当年当月中的第一天
            return new Date(new_date.getTime() - 1000 * 60 * 60 * 24).getDate(); //获取当月最后一天日期
        },
    
    }
});
//数字格式化
function toThousands(str) {
    if(!str)return
    str  =parseInt(str);
    if(typeof(str) == 'number')str = str.toString()

	var newStr = "";
	var count = 0;
	if(str.indexOf(".") == -1) {
		for(var i = str.length - 1; i >= 0; i--) {
			if(count % 3 == 0 && count != 0) {
				newStr = str.charAt(i) + "," + newStr;
			} else {
				newStr = str.charAt(i) + newStr;
			}
			count++;
		}
		str = newStr;
	} else {
		for(var i = str.indexOf(".") - 1; i >= 0; i--) {
			if(count % 3 == 0 && count != 0) {
				newStr = str.charAt(i) + "," + newStr;
			} else {
				newStr = str.charAt(i) + newStr; //逐个字符相接起来
			}
			count++;
		}
		str = newStr + (str + "00").substr((str + "00").indexOf("."), 3);
	}
	return str;
}