<template>
	<view style="width: 100%;height: 100%;">
		<!-- 历史周选择 -->
		<u-select v-model="oldshow" :list="oldlist" @confirm="choiceNew"></u-select>
		<!-- 历史 -->
		<view>
			<view style="padding: 30rpx 35rpx;" class="flex justify-between">
				<view style="color: #666666;" @click="oldshow = true">
					<text style="margin-right: 20rpx;">选择排班周数</text>
					<u-icon name="arrow-down" color="#666666" size="28" v-if="!show"></u-icon>
					<u-icon name="arrow-up" color="#666666" size="28" v-else></u-icon>
				</view>

			</view>
			<view class="flex" style="background-color: #FFFFFF;">
				<!-- 时间 -->
				<view class="title" style="width: 20%;">时间</view>
				<u-line color="#cecece" direction="col" />
				<!-- 上午 -->
				<view class="title" style="width: 26%;">上午</view>
				<u-line color="#cecece" direction="col" />
				<!-- 下午 -->
				<view class="title" style="width: 26%;">下午</view>
				<u-line color="#cecece" direction="col" />
				<!-- 晚上 -->
				<view class="title" style="width: 26%;">晚上</view>
			</view>

			<!-- 表格一条内容  循环七遍-->
			<view v-for="(item,index) in oldWeek" :key='index'>
				<u-line color="#cecece" />
				<!-- 星期一 -->
				<view class="flex" style="background-color: #FFFFFF;">

					<view class="content" style="width: 20%;font-weight: bold;">
						<view>
							<view style="text-align: center;">{{item.week}}</view>
							<view style="text-align: center;">{{item.schedule_date}}</view>
						</view>
					</view>
					<u-line color="#cecece" direction="col" />
					<!-- 上午 -->
					<view class="content" style="width: 26%;">
						<view style="width: 80%;text-align: center;word-break:break-all">{{item.type.zao==false?'-':item.type.zao}}</view>
					</view>
					<u-line color="#cecece" direction="col" />
					<!-- 下午 -->
					<view class="content" style="width: 26%;">
						<view style="width: 80%;text-align: center;word-break:break-all">{{item.type.xia==false?'-':item.type.xia}}</view>
					</view>
					<u-line color="#cecece" direction="col" />
					<!-- 晚上 -->
					<view class="content" style="width: 26%;">
						<view style="width: 80%;text-align: center;word-break:break-all">{{item.type.wan==false?'-':item.type.wan}}</view>
					</view>
				</view>
			</view>
		</view>
		<view style="height: 200rpx;"></view>
		<!-- 保存 -->
		<view class="bottomButton" @click="paiban" v-if="!old">
			排班设置
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//历史的排班选择
				oldshow: false,

				oldlist: [],

				// title: '周一',
				// value: '10-26',
				// hospital: [{
				// 	name: '早上',
				// 	checked: true,
				// 	disabled:true
				// }, {
				// 	name: '下午',
				// 	checked: false
				// }, {
				// 	name: '晚上',
				// 	checked: false
				// }],
				hospitalWeek: [],

				//历史记录的数组
				oldWeek: [ 
				
				],

			}
		},
		methods: {
			paiban() {
				uni.navigateTo({
					url: 'Scheduling'
				})
			},
			
			cliold: async function(e) {
			
				let times = e[0].label;
				times = times.split(" 至 ");
				let startime = times[0];
				let endtime = times[1];
				let weekArr = this.getDateStr(startime, endtime, 0);
			
				
				console.log(res);
				
				
				if (res.status == 0) {
					/* if (typeof res.data.list[0] != undefined && typeof res.data[0] != 'undefined') {
						let data = res.data.list;
						let arr = [];
						for (let i = 0; i < this.oldWeek.length; i++) {
							for (let p = 0; p < data.length; p++) {
								if (this.oldWeek[i].value == data[p].schedule_date) {
									this.oldWeek[i].morning = data[p].type.zao ? data[p].type.zao : '暂无';
									this.oldWeek[i].afternoon = data[p].type.xia ? data[p].type.xia : '暂无';
									this.oldWeek[i].night = data[p].type.wan ? data[p].type.wan : '暂无';
								}
							}
						}
			
					} else {
						for (let i = 0; i < this.oldWeek.length; i++) {
							this.oldWeek[i].morning = '暂无';
							this.oldWeek[i].afternoon = '暂无';
							this.oldWeek[i].night = '暂无';
						}
					} */
				}
			
			},
			
			
			getTimeList:async function(){
				let res = await this.$request.post("doctor/getLinsList");
				if(res.status==0){
					if(res.data.service!=0){
						let times = res.data.service.created_at.substring(0,10);
						times = times.split('-');
	
						this.getweek(times[0],times[1],times[2]);
					}
					
					if(res.data.list.length!=0){
						let leng = this.oldlist.length;
						let lists = res.data.list;
						for(let i=0;i<lists.length;i++){
							console.log('开始时间L',(lists[i].start_time.schedule_date).replace(/-/g,"/"))
							this.oldlist.push({
								label:(lists[i].start_time.schedule_date).replace(/-/g,"/") + "-" + (lists[i].end_time.schedule_date).replace(/-/g,"/"),
								value: leng+1
							})
						}
					}
				}
				console.log(res);
			},
			
			
			getTemplate: async function(startTime,endTime) {
				let that = this;
				// let startTime = '2020-' + that.hospitalWeek[0].value;
				// let startTime = '2020-' + '11-30';
				// let endTime = '2020-' + that.hospitalWeek[6].value;
				// let endTime = '2020-' + '12-6';
			 
				let res = await that.$request.post("doctor/getLinsDetail", {
					'start_date': startTime,
					'end_date': endTime
				});
				console.log("res", res)
				console.log('oldeList',this.oldlist)
				// that.oldWeek = res.data.list
				if (res.status == 0) {
		
				if (typeof res.data.list[0] != undefined && typeof res.data.list[0] != 'undefined') {
					let data = res.data.list;
					let arr = [];
 
					for (let i = 0; i < this.oldWeek.length; i++) {
						for (let p = 0; p < data.length; p++) {
							if (this.oldWeek[i].schedule_date == data[p].schedule_date) {
								this.oldWeek[i].type.zao = data[p].type.zao ? data[p].type.zao : '-';
								this.oldWeek[i].type.xia = data[p].type.xia ? data[p].type.xia : '-';
								this.oldWeek[i].type.wan = data[p].type.wan ? data[p].type.wan : '-';
							}
						}
					}
				} else {
					for (let i = 0; i < this.oldWeek.length; i++) {
						this.oldWeek[i].type.zao = '-';
						this.oldWeek[i].type.xia = '-';
						this.oldWeek[i].type.wan = '-';
					}
				}
				}
			},
			
			
			
			/**
			 * 拿到开始日到今天的每周周一和周天数据
			 * 传参：year开始年（num），month开始月（num），day开始日（num）
			 * 
			 * 
			 * */
			 getweek(startYear,startMonth,startDay){
				 /**
				  * startYear开始年份 例2020 
				  * startMonth开始月份 例12
				  * startDay开始日  例5
				  * d  开始的时间对象
				  * monday  开始时间的星期一
				  * sunday  开始时间的星期天
				  * nowday  今天的时间
				  * nowMonday 今天的周一
				  * weeknum 开始到今天有几周
				  * */
				// let startYear = 2020;//开始年
				// let startMonth = 11;//开始月
				// let startDay = 12;//开始的日
				let startweek = 0; //开始时间是星期几
				let oneDayTime = 24 * 60 * 60 * 1000;//固定数据 一天的时长
				var d = new Date()
				//把时间调到对应的日期
				d.setFullYear(startYear, startMonth - 1, startDay);
				console.log('今天', d.toLocaleDateString())
				//本周第几天 因系统会把周日作为第0天
				startweek = d.getDay() == 0 ? 7 : d.getDay();
				console.log('这天星期', startweek)
				//拿到当天的周一和周日
				var monday = new Date(d.getTime() - (startweek - 1) * oneDayTime)
				var sunday = new Date(monday.getTime() + 6 * oneDayTime)
				console.log('这天星期一是', monday.toLocaleDateString())
				console.log('这天星期日是', sunday.toLocaleDateString())
				//拿到今天距离那天多少周
				var nowday = new Date() //今天时间
				var nowMonday = new Date(nowday.getTime() - ((nowday.getDay() == 0 ? 7 : nowday.getDay()) - 1) * oneDayTime) //这周星期一时间
				console.log('今天的周一是', nowMonday.toLocaleDateString())
				var weeknum = Math.ceil(this.getCountOfWeekX(d,nowMonday)+1); //计算从 ****----****有多少个周
				console.log("有几个周", weeknum)
				//开始循环，用开始的星期一到星期天，
			
				for(let i = 0; i < weeknum ; i++){
					let mon = new Date(monday.getTime() + oneDayTime* (i*7))
					// mon.setTime()
					let	sun = new Date(mon.getTime() + oneDayTime* 6)
					this.oldlist.push({
						label: mon.getFullYear()+'/'+(mon.getMonth()+1)+'/'+mon.getDate() + "-" + sun.getFullYear()+'/'+(sun.getMonth()+1)+'/'+sun.getDate(),
						value: i
					})
					mon.setTime(mon.getTime() + oneDayTime)
				}
				console.log("oldlist",this.oldlist)
			 },
			 getCountOfWeekX(startDate, endDate) {
			 	var start = startDate.getTime();
			 	var end = endDate.getTime()
			 	let w=(endDate - startDate)/(7*24*60*60*1000);
			 	return  w;
			 },
			
			
			Appendzero(obj) {
				if (obj < 10) return "0" + "" + obj;
				else return obj;
			},
			
			getTime() {
				var week = [{
					value: 1,
					label: ''
				}, {
					value: 2,
					label: ''
				}, {
					value: 3,
					label: ''
				}, {
					value: 4,
					label: ''
				}]

				//直接获取这周五的时间，然后遍历 给选择器
				// var aData = new Date();
				// aData.setTime(aData.getTime() + 24 * 60 * 60 * 1000 * (7 - aData.getDay()))
				// for (let i = 0; i < 4; i++) {
				// 	aData.setTime(aData.getTime() + 24 * 60 * 60 * 1000 * 1)
				// 	var week1 = aData.getFullYear() + "." + (aData.getMonth() + 1) + "." + this.Appendzero(aData.getDate());
				// 	console.log('第', i + 1, '个周一的时间', week1) //周一的时间
				// 	//周日
				// 	aData.setTime(aData.getTime() + 24 * 60 * 60 * 1000 * (aData.getDay() + 5))
				// 	var week2 = aData.getFullYear() + "." + (aData.getMonth() + 1) + "." + this.Appendzero(aData.getDate());
				// 	console.log('第', i + 1, '个周日的时间', week2) //周日的时间
				// 	this.oldlist[i].label = week1 + ' - ' + week2 //给选择器数组
				// }
				//直接获取下周一时间，给表格
				
				
				var bData = new Date();
				bData.setTime(bData.getTime() + 24 * 60 * 60 * 1000 * (1 - bData.getDay()))
				for (let i = 0; i < 7; i++) {
					var week = (bData.getMonth() + 1) + "-" + this.Appendzero(bData.getDate());
					
					bData.setTime(bData.getTime() + 24 * 60 * 60 * 1000 * 1);
					this.oldWeek.push({
						type: {
							"zao": false,
							"xia": false,
							"wan": false,
						},
						week: this.getWeek(bData.getFullYear()+'-'+week),
						schedule_date: week,
					})
					 
				}
				this.getTemplate(bData.getFullYear()+'-'+this.oldWeek[0].schedule_date,bData.getFullYear()+'-'+this.oldWeek[6].schedule_date)
			},
			
			 getWeek(dateString) {
			        var dateArray = dateString.split("-");
			        let date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
			        return "周" + "日一二三四五六".charAt(date.getDay());
			    },
			
			//点击新增周
			choiceNew(e) {
				this.hospitalWeek.map(val => {
					val.hospital[0].disabled = false;
					val.hospital[1].disabled = false;
					val.hospital[2].disabled = false;
				})
				console.log(e)
				this.time = e[0].label
				let times = e[0].label;
				   times = times.split("-");
				   let startime = times[0].replace(/\//g, '-');
				   let endtime = times[1].replace(/\//g, '-');
				   this.getDateStr(startime,endtime,0)
				
				// var bData = new Date();
				// bData.setTime(bData.getTime() + 24 * 60 * 60 * 1000 * (8 - bData.getDay() + (7 * e[0].value)))
				// for (let i = 0; i < 7; i++) {
				// 	var week = (bData.getMonth() + 1) + "-" + this.Appendzero(bData.getDate());
				// 	bData.setTime(bData.getTime() + 24 * 60 * 60 * 1000 * 1);
				// 	this.oldWeek[i].schedule_date = week
				 
				// }
				this.getTemplate(startime,endtime)
				
			},
			getDateStr(startDate, endDate, dayLength) {
				var str = startDate;
				for (var i = 0;; i++) {
					var getDate = this.getTargetDate(startDate, dayLength);
					startDate = getDate;
					if (getDate <= endDate) {
						str += ',' + getDate;
					} else {
						break;
					}
				}
				let dates = str.split(',');
				 
				let arr = [];
				let obj;
				var bData = new Date();
				for (let i = 0; i < 7; i++) {
					obj = {
						week: this.getweeks(dates[i]),
						schedule_date: dates[i].substring(5),
						type: {
							"zao": '-',
							"xia": '-',
							"wan": '-',
						},
					}
					arr.push(obj)
				}
				this.oldWeek = arr
			},
			// startDate: 开始时间；dayLength：每隔几天，0-代表获取每天，1-代表日期间隔一天
			getTargetDate(date, dayLength) {
				dayLength = dayLength + 1;
				var tempDate = new Date(date);
				tempDate.setDate(tempDate.getDate() + dayLength);
				var year = tempDate.getFullYear();
				var month = tempDate.getMonth() + 1 < 10 ? "0" + (tempDate.getMonth() + 1) : tempDate.getMonth() + 1;
				var day = tempDate.getDate() < 10 ? "0" + tempDate.getDate() : tempDate.getDate();
			
				return year + "-" + month + "-" + day;
			},
			getweeks(date) {
				var weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
				var dateStr = date;
				var myDate = new Date(Date.parse(dateStr.replace(/-/g, "/")));
				return weekDay[myDate.getDay()]
			
			},
			
			
		},
		
		
		onLoad() {
			// let res = this.getCurrentWeek();
			// console.log(res);
			// let startTime = '2020-' + '11-30';
			// let endTime = '2020-' + that.hospitalWeek[6].value;
			// let endTime = '2020-' + '12-07';
			// this.getweek(2020,10,15);
			this.getTime();
			this.getTimeList();
			// this.getTemplate(startTime,endTime)
		},
	}
</script>

<style>
	page {
		background-color: #f0f0f0
	}

	.title {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 30rpx 0;
		font-weight: bold;
	}

	.content {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 30rpx 0;

	}

	/* 底部按钮 */
	.bottomButton {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #0000ff;
		color: #FFFFFF;
		z-index: 1;

	}
</style>
