var template = ''
$.ajax('../html/dyjdRzsfjd.html',{
	data: {},
	dataType: 'html',
	cache: false,
	timeout: 60000, 
	async: false,
	error: function(res) {
	},
	success: function(res) {
		template = res
	}
})
Vue.component('mzf-dyjd-rzsfjd', {
	props: ['type'],
	template: template,
	data: function() {
		return {
			mzfSfjdData:[],
			list:[],
			startTime:new Date('2023','05','26'),
			endTime:new Date(),
			regionList:[],
			regionData:[
				{label:'全市',id:'1',type:'1'}
			],
			regionListList:[],
			tertiaryHospitalsData:[
				{label:'全部',id:'1',type:'2'}
			],
			communityHospitalsList:[],
			communityHospitalsData:[
				{label:'全部',id:'1',type:'3'}
			],
			groupingList:[],
			groupingData:[
				{label:'全部',id:'1',type:'4'}
			],
			administrationStatusList:[],
			administrationStatusData:[
				{label:'全部',id:'1',type:'5'}
			],
			show:false,
			showTwo:false,
			showThree:false,
			currentPage:1,
			currentSize:10, 
			datatotal:0, //总数
			loadingTwo:false,
			setDisabled:{
				disabledDate:function(time) {
					return time.getTime() < new Date('2023','05','26') || time.getTime() > Date.now();
				}
			},
			setDisabled1:{
				disabledDate:function(time) {
					return  time.getTime() > Date.now();
				}
			},
			s:'',
			e:'',
			mergeObj: {},
            mergeArr: ['areaName', 'gradeHospitalName','communityHospitalName','targetEntryCont','v1AllCount'],
		}
	},
	created: function(){
		if(this.type=='dyjdrzsfjd'){
			this.startTime=new Date('2021','11','1')
		}
		this.s = this.startTime.format('yyyy-MM-dd')
		this.e = this.endTime.format('yyyy-MM-dd')
		this.list.unshift({type:'0',id:'time',label:this.s +"~"+ this.e})
		this.getCopdFollowData()
	},
	watch:{
		'type':{
			handler:function(o){
				  console.log('判断是否是第一阶段随访入组表单',o)
					this.getCopdFollowData()
			},
			deep: true,
		},
		'startTime':{
			 handler:function(o) {
				if(!o) {
					this.list.splice(0,1)
				}
			 },
			 deep: true,
			 immediate: true
		},
		'endTime':{
			 handler:function(o) {
				if(!o) {
					this.list.splice(0,1)
				}
			 },
			 deep: true,
			 immediate: true
		},
	},
	methods: {
		getCopdFollowData:function() {
			var vm = this
			vm.loadingTwo = true
			var data = {
				startTime:this.startTime.format('yyyy-MM-dd'),
				endTime:this.endTime.format('yyyy-MM-dd')
			}
		     if(vm.type==="dyjdrzsfjd"){
				 statisticAPI.getFirstStageEntryData({
				 	jsonStr:JSON.stringify(data)
				 }).then(function(res){
				 	if(res.status == 200) {
				 		vm.loadingTwo = false
				 		// console.log( '旧:我是第一阶段随访入组记录',res.data);
				 		vm.mzfSfjdData = vm.mergeArrays(res.data.data01,res.data.data02)
					    console.log( '新:我是第一阶段随访入组记录',vm.mzfSfjdData);
				 		vm.getSpanArr(vm.mzfSfjdData)
				 	}
				 }).catch(function(err){
				 	vm.loadingTwo = false
				 })
			 }else{
				 statisticAPI.getCommunityHospitalFollowData({
				 	jsonStr:JSON.stringify(data)
				 }).then(function(res){
				 	if(res.status == 200) {
				 		vm.loadingTwo = false
				 		// console.log( '旧:我是第二阶段社区医院随访记录',res.data);
				 		vm.mzfSfjdData = vm.mergeArrays(res.data.data01,res.data.data02)
						console.log( '新:我是第二阶社区医院随访进度',res.data);
				 		vm.getSpanArr(vm.mzfSfjdData)
				 	}
				 }).catch(function(err){
				 	vm.loadingTwo = false
				 })
			 }
			 
			
			
			
		},
		//合并新数据
		mergeArrays:function(array1, array2) {
		    var mergedArray = [];
		array1.forEach(function(item1,index){
			array2.slice(index*3,index*3+3).forEach(function(item2){
				 var mergedObj = Object.assign({}, item1, item2);
				  mergedArray.push(mergedObj);
			})
		})
		    return mergedArray;
		},
		startTimeChange:function(o) {
			if(!this.list[0] || this.list[0].type!=0) {
			 this.list.unshift({type:'0',id:'time',label:o.format('yyyy-MM-dd') +"~"+ this.endTime.format('yyyy-MM-dd')})
			}else{
				this.list[0].label = o.format('yyyy-MM-dd') +"~"+ this.endTime.format('yyyy-MM-dd')
			}
		},
		endTimeChange:function(o) {
			if(!this.list[0] || this.list[0].type!=0) {
				this.list.unshift({type:'0',id:'time',label:this.startTime.format('yyyy-MM-dd') +"~"+ o.format('yyyy-MM-dd')})
			}else{
				this.list[0].label = this.startTime.format('yyyy-MM-dd') +"~"+ o.format('yyyy-MM-dd')
			}
		},
		deleteClick:function(item) {
			if(item.id == "time") {
				this.startTime = ''
				this.endTime = ''
				this.list.splice(0,1)
			}
			var index = this.list.findIndex(function(v){
				return item.id == v.id
			})
			this.list.splice(index,1)
		},
		// 下一页  上一页
		handleCurrentChange:function(val) {
			var vm = this
			vm.currentPage = val
		},
		change:function(val) {
			this.list = this.list.concat(val)
		},
		// 确定
		confirm:function() {
			this.getCopdFollowData()
		},
		// 取消
		cancel:function() {
			this.startTime = new Date('2023','05','26')
			this.endTime = new Date()
			if(!this.list[0] || this.list[0].type!=0) {
			 this.list.unshift({type:'0',id:'time',label:this.startTime.format('yyyy-MM-dd') +"~"+ this.endTime.format('yyyy-MM-dd')})
			}else{
				this.list[0].label = this.startTime.format('yyyy-MM-dd') +"~"+ this.endTime.format('yyyy-MM-dd')
			}
		},
		// 导出 慢阻肺随访进度
		exportHandle:function() {
			var vm = this
			vm.loadingTwo = true
			var data = {
				startTime:this.startTime.format('yyyy-MM-dd'),
				endTime:this.endTime.format('yyyy-MM-dd')
			}
			var jsonStr = JSON.stringify(data)
			if(vm.type=="dyjdrzsfjd"){
				statisticAPI.exportFirstStageEntryData({
					jsonStr: encodeURIComponent(jsonStr)
				},'慢阻肺第一阶段入组随访进度.xls').then(function(res){
					vm.loadingTwo = false
				})
			}else{
				statisticAPI.exportCommunityHospitalFollowData({
					jsonStr: encodeURIComponent(jsonStr)
				},'慢阻肺第二阶段社区医院随访进度.xls').then(function(res){
					vm.loadingTwo = false
				})
			}
			
		},
		
		// getSpanArr(data) {
		// 	this.mergeArr.forEach((key, index1) => {
		// 		var count = 0; // 用来记录需要合并行的起始位置
		// 		this.mergeObj[key] = []; // 记录每一列的合并信息
		// 		data.forEach((item, index) => {
		// 			// index == 0表示数据为第一行,直接 push 一个 1
		// 			if(index === 0) {
		// 				this.mergeObj[key].push(1); 
		// 			} else {
		// 				// 判断当前行是否与上一行其值相等 如果相等 在 count 记录的位置其值 +1 表示当前行需要合并 并push 一个 0 作为占位
		// 				if(item[key] === data[index - 1][key]) { 
		// 					this.mergeObj[key][count] += 1;
		// 					this.mergeObj[key].push(0);
		// 				} else {
		// 					// 如果当前行和上一行其值不相等 
		// 					count = index; // 记录当前位置 
		// 					this.mergeObj[key].push(1); // 重新push 一个 1
		// 				}
		// 			}
		// 		})
		// 	})
		// },
		getSpanArr(data) {
			this.mergeArr.forEach((key, index1) => {
				var count = 0; // 用来记录需要合并行的起始位置
				this.mergeObj[key] = []; // 记录每一列的合并信息
				data.forEach((item, index) => {
					// index == 0表示数据为第一行,直接 push 一个 1
					if(index === 0) {
						this.mergeObj[key].push(1); 
					} else {
						// 判断当前行是否与上一行其值相等 如果相等 在 count 记录的位置其值 +1 表示当前行需要合并 并push 一个 0 作为占位
						if(key==='targetEntryCont'||key==='v1AllCount'){
							if(item[key] === data[index - 1][key] && this.mergeObj[key][count]%3!=0) {
									this.mergeObj[key][count] += 1;
									this.mergeObj[key].push(0);
							} else {
								// 如果当前行和上一行其值不相等 
								count = index; // 记录当前位置 
								this.mergeObj[key].push(1); // 重新push 一个 1
							}
						}else{
							if(item[key] === data[index - 1][key]  ) {
									this.mergeObj[key][count] += 1;
									this.mergeObj[key].push(0);
							} else {
								// 如果当前行和上一行其值不相等 
								count = index; // 记录当前位置 
								this.mergeObj[key].push(1); // 重新push 一个 1
							}
						}
						
					}
				})
			})
		},
		//得到行、列的合并值
		objectSpanMethod: function({row, column, rowIndex, columnIndex}) {
            if(this.mergeArr.indexOf(column.property) !== -1) { 
                // 判断其值是不是为0 
                if(this.mergeObj[column.property][rowIndex]) { 
                    return [this.mergeObj[column.property][rowIndex], 1]
                } else {
                    // 如果为0则为需要合并的行
                    return [0, 0]; 
                }
            }
		},	
		renderHeadeRexpert: function(h, item) {
					var index = item.$index
				  return [
					index=='2'?'目标v2入组例数':index=='3'?'首例V1入筛时间(YYYY/MM/DD)':(index=='4'||index=='5')?'首例V2入组时间(YYYY/MM/DD)':item.column.index=='11'?'正常访视':item.column.index=='12'
			?'超窗':item.column.index=='13'?'正常访视':item.column.index=='14'?'超窗':item.column.index=='15'?'V3':item.column.index=='16'?'V4':item.column.index=='17'?'V5':item.column.index=='18'?'V6':item.column.index=='19'?'合计':item.column.index=='20'?'SEA':item.column.index=='21'?'病情恶化':item.column.index=='22'?'并发其他疾病':item.column.index=='23'?'自愿退出':item.column.index=='24'?'研究者终止':item.column.index=='25'?'不符合纳排':item.column.index=='26'?'失访':item.column.index=='27'?'妊娠':
			item.column.index=='28'?'死亡':item.column.index=='29'?'其他':'',
					h(
					  'el-tooltip',
					  {
						props: {
						  content: (function() {
							return index=='2'?'即目标入组例数(与管理目标数一致)':index=='3'?'完成V1的提交时间,即“第一例完成V1并成功纳入筛选期的访视日期':(index=='4'||index=='5')?'查询第一个入组的时间(访视2触发入组,统计第一个做访视2的患者,且是完成访视2提交的时间。)':item.column.index=='11'?'在访视窗口内完成的访视,即访视计划时间±7天内完成的访视。':item.column.index=='12'?'超过窗口期做的访视。':item.column.index=='13'?'在访视窗口内完成的访视,即访视计划时间±14天内完成的访视。':item.column.index=='14'?'超过窗口期做的访视。':
					item.column.index=='15'?'V3提前终止研究的人数':item.column.index=='16'?'V4提前终止研究的人数':item.column.index=='17'?'V5提前终止研究的人数':item.column.index=='18'?'V6提前终止研究的人数':item.column.index=='19'?'提前终止研究的人数':item.column.index=='20'?'提前终止原因为受试者出现不可耐受的过敏反应或严重不良事件的人数。':item.column.index=='21'?'提前终止原因为“在研究中出现病情的急剧恶化,无法继续参与研究”的人数。':item.column.index=='22'?'提前终止原因为“并发可能会影响研究结果的疾病”的人数。':item.column.index=='23'?'提前终止原因为“受试者不愿意或不能继续研究,或要求撤回知情同意”的人数。':
					item.column.index=='24'?'提前终止原因为“受试者不愿意或不能继续研究,或要求撤回知情同意”的人数。':item.column.index=='25'?'提前终止原因为“发现受试者不符合任一项纳入标准,或符合任一项排除标准”的人数。':item.column.index=='26'?'提前终止原因为失访的人数。':item.column.index=='27'?'提前终止原因为妊娠的人数。':
					item.column.index=='28'?'提前终止原因为死亡的人数。':item.column.index=='29'?'提前终止原因为其他的人数':''
						  })(),
						  placement: 'top'
						}
					  },
					  [
						h('span', {
						  class: {
							'el-icon-question': true
						  }
						})
					  ]
					)
				  ]
				},
	}
})