var reqList = [];
var util = {};
util.treeTableXcode = function(data, xcode) {
	xcode = xcode || "";
	for(var i = 0; i < data.length; i++) {
		var item = data[i];
		item.xcode = xcode + i;
		if(item.children && item.children.length > 0) {
			util.treeTableXcode(item.children, item.xcode + "-");
		}
	}
};
Vue.use(Vuedals.default);
new Vue({
	el: "#main",
	data: {
		tableData: [],
        valueArea: "350203",
        optionsArea: [{code:"350203",name:"思明区"}],
		roleType:"1",  //1、调度员，2、服务医生
		dateValue: "",
		defaultValue: ['00:00:00', '23:59:59'],
		pickerOptions: {
			shortcuts: [{
				text: '最近一周',
				onClick: function(picker) {
					var end = new Date();
					var start = new Date();
					start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
					picker.$emit('pick', [start, end]);
				}
			}, {
				text: '最近一个月',
				onClick: function(picker) {
					var end = new Date();
					var start = new Date();
					start.setMonth(start.getMonth() - 1);
					picker.$emit('pick', [start, end]);
				}
			}, {
				text: '最近三个月',
				onClick: function(picker) {
					var end = new Date();
					var start = new Date();
					start.setMonth(start.getMonth() - 3);
					picker.$emit('pick', [start, end]);
				},
			}, {
				text: '最近半年',
				onClick: function(picker) {
					var end = new Date();
					var start = new Date();
					start.setMonth(start.getMonth() - 6);
					picker.$emit('pick', [start, end]);
				},
			}, {
				text: '最近一年',
				onClick: function(picker) {
					var end = new Date();
					var start = new Date();
					start.setFullYear(start.getFullYear() - 1);
					picker.$emit('pick', [start, end]);
				},
			}],
			disabledDate: function(d) {
				return d > new Date();
			}
		}
	},
	components: {
		vuedals: Vuedals.Component
	},
	mounted: function() {
		var vm = this
		//初始化时间
		var end = new Date();
		var start = new Date();
		start.setFullYear(start.getFullYear() - 1);
		vm.dateValue = [start, end]
		//初始化数据
        vm.initData()
//      vm.getDistrict()
		$("#main").removeClass("c-hide");
		//监听后退按钮的操作
		EventBus.$on("back-click", function(arg) {
			history.go(-1);
		});
		//监听页面刷新
		EventBus.$on("refresh-click", function(arg) {
//			alert("刷新")
			vm.initData()
		});
		window.onresize = function() {
			
		}
	},
	methods: {		
        initData: function() {
            var vm=this
            var startDate,endDate
            if(typeof(vm.dateValue[0])!="string"){
                startDate=vm.dateValue[0].format("yyyy-MM-dd")
                endDate=vm.dateValue[1].format("yyyy-MM-dd")
            }else{
                startDate=vm.dateValue[0]
                endDate=vm.dateValue[1]
            }          
            var params={
                userAgent:localStorage.getItem('wlyyAgentForDoc'),
                area:vm.valueArea,startDate:startDate,endDate:endDate,roleType:vm.roleType
            }
            statisticAPI.doorResponseAnalyze(params).then(function(res) {
                if(res.status == 200) {
                    vm.tableData=res.data
                    util.treeTableXcode(vm.tableData);
                } else {
                    vm.$message.error(res.msg)
                }
            })          
        },
        changeDateValue: function() {
            alert(JSON.stringify(this.dateValue))
        },
        getDistrict: function() {
            var vm=this
            var params={
                type:3,code:"350200"
            }
            homeAPI.getDistrict(params).then(function(res) {
                if(res.status == 200) {
                    vm.optionsArea=res.list
                    vm.valueArea=vm.optionsArea[0].code
                    vm.initData()
                } else {
                    vm.$message.error(res.msg)
                }
            })
        },
        exportData: function() {
            var vm=this
            var startDate,endDate
            if(typeof(vm.dateValue[0])!="string"){
                startDate=vm.dateValue[0].format("yyyy-MM-dd")
                endDate=vm.dateValue[1].format("yyyy-MM-dd")
            }else{
                startDate=vm.dateValue[0]
                endDate=vm.dateValue[1]
            }          
            var params={
                userAgent:localStorage.getItem('wlyyAgentForDoc'),
                area:vm.valueArea,startDate:startDate,endDate:endDate,
            }
            var str = statisticAPI.doorResponseAnalyzeExport+"?area="+vm.valueArea+"&startDate="+startDate+"&endDate="+endDate+"&roleType="+vm.roleType;
            window.open(str, "接收包数据导出")//导出数据
        },
		treeClick: function(item, index) {
			if(item.open) {
				this.collapse(item, index);
			} else {
				this.expand(item, index);
			}
		},
		expand: function(item, index) {
			if(!item.children) {
				return index;
			}
			//展开
			for(var i = 0; item.children && i < item.children.length; i++) {
				var child = item.children[i];
				this.tableData.splice(++index, 0, child);
				if(child.children && child.children.length > 0 && child.open) {
					index = this.expand(child, index);
				}
			}
			item.open = true;
			return index;
		},
		collapse: function(item, index) {
			if(!item.children) {
				return index;
			}
			//收缩
			item.open = false;
			var len = 1;
			for(var i = index + 1; i < this.tableData.length - 1; i++) {
				var xcode = this.tableData[i].xcode;
				if(xcode.startsWith(item.xcode + "-")) {
					len++;
				} else {
					break;
				}
			}
			this.tableData.splice(index + 1, len);
		}
	}
});