<template>
	<view>
		<cpn-navbar title="护理日历"></cpn-navbar>

		<view>
			<!-- 选择器 -->
			<view class="selecter">
				<view class="select-year" @click="isShowCell = true">
					<view class="text">
						{{select.year}}
					</view>
					<view class="arrow" :class="{arrowAni:isShowCell,arrowAni1:!isShowCell}">
						<u-icon name="arrow-down" size="20" color="#36596A"></u-icon>
					</view>
				</view>
				<view class="select-month">
					<view class="arrow" @click="monthMin">
						<u-icon name="arrow-left" size="20" color="#36596A"></u-icon>
					</view>
					<view class="text">
						{{select.month}}月
					</view>
					<view class="arrow" @click="monthPlus">
						<u-icon name="arrow-right" size="20" color="#36596A"></u-icon>
					</view>
				</view>
			</view>
			<!-- 选择年列表 -->
			<view class="year-options" v-show="isShowCell">
				<view class="mask" @click="isShowCell = false"> </view>
				<view class="options">
					<scroll-view scroll-y="true" style="height: 300rpx;">
						<view class="item" v-for="(item,index) in opsionts" :key="index"
							@click="select.year = item,isShowCell = false">
							{{item}}
						</view>
					</scroll-view>
				</view>
			</view>

			<!-- 日历表 -->
			<view class="canlendar">
				<view v-for="(row,index) in calendar" :key="index" class="row">
					<view v-for="(date,index1) in row" :key="index1" @click="dateClick(date,index1)" class="item"
						:class="{itemOver:date.type === -1||date.type === 1||date.type === 2,itemActive:date.date === select.date&&date.type === 0}">
						{{date.date}}
						<view class="icon icon1" v-if="dateIn(date.date) === 0&&date.type === 0"></view>
						<view class="icon icon2" v-if="dateIn(date.date) === 2&&date.type === 0"></view>
						<view class="icon icon3" v-if="dateIn(date.date) === 1&&date.type === 0"></view>
					</view>
				</view>
			</view>
			<!-- 日期状态提示 -->
			<view class="status">
				<view class="status-item">
					<view class="icon3"></view>
					<view>进行中</view>
				</view>
				<view class="status-item">
					<view class="icon1"></view>
					<view>待护理</view>
				</view>
				<view class="status-item">
					<view class="icon2"></view>
					<view>已完成</view>
				</view>
			</view>

			<!-- 选中护理当日信息 -->
			<view class="select-info">
				<view class="selet-date">
					{{select.year}}年{{select.month}}月{{select.date}}日
				</view>
				<view class="select-day">
					{{getDay(select.year+"/"+select.month+"/"+select.date)}}
				</view>
			</view>

			<!-- 选中当日护理人员 -->
			<view class="select-user-list">
				<scroll-view :scroll-y="true" style="height: 520rpx;position: relative;">
					<view v-if="selectUserList && selectUserList.length > 0">
						<view v-for="(item,index) in selectUserList" :key="index" class="user-item">
							<view class="left">
								<u-image src="/static/logo.png" height="104" width="104" shape="circle"></u-image>
							</view>
							<view class="center">
								<view class="top">{{item.customer.name}}</view>
								<view class="bottom">
									<view class="time">{{$u.timeFormat(new Date(item.start_time),'hh:MM')}}</view>
									<view class="list-status-icon"
										:class="{icon1:item.status === 0,icon2:item.status === 2,icon3:item.status === 1}">
									</view>
									<view class="list-status-text">{{formatStatus(item.status)}}</view>
								</view>
							</view>
							<view class="right" @click="toDetail(item)">
								<u-icon name="arrow-right" size="22"></u-icon>
							</view>
						</view>
					</view>
					<view v-else style="position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);">
						<u-empty></u-empty>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		formatDate
	} from 'common/util.js'
	export default {
		data() {
			return {
				testDay: [{
						date: 1,
						status: 2
					},
					{
						date: 6,
						status: 1
					}, {
						date: 9,
						status: 0
					}, {
						date: 14,
						status: 0
					}, {
						date: 16,
						status: 2
					}
				],
				opsionts: (() => {
					let res = []
					for (let i = 0; i < 20; i++) {
						res.push(new Date().getFullYear() - i)
					}
					return res
				})(),
				isShowCell: false,
				//时间选择
				select: {
					year: new Date().getFullYear(),
					month: new Date().getMonth() + 1,
					date: new Date().getDate()
				},
				//当前选择日期
				//dateIndex: new Date().getDate(),
				calendar: '',
				params: {
					year: true,
					month: true,
					day: false,
					hour: false,
					minute: false,
					second: false
				},
				selectUserList: [],

				//搜索条件
				selector: {
					page_size: 999,
					page: 1,
					date: '',
					lat: '',
					lng: ''
				}
			}
		},
		methods: {
			//判断日期是否有护理
			dateIn(date) {
				let res = this.testDay.filter(item => {
					return item.date === date
				})
				return res[0] ? res[0].status : false
			},
			monthPlus() {
				this.$u.throttle(() => {
					if (this.select.month < 12) {
						this.select.month++
					}
				})
			},
			monthMin() {
				this.$u.throttle(() => {
					if (this.select.month > 1) {
						this.select.month--
					}
				})
			},
			dateClick(date, index1) {
				if (date.type === 0) {
					this.select.date = date.date
				}
			},
			getCalendar(year, month) {
				let res = []

				//判断是否闰年
				function isLeap(year) {
					return (year % 100 == 0 ? (year % 400 == 0 ? 29 : 28) : (year % 4 == 0 ? 29 : 28))
				}

				//获取年的每个月天数
				let mDays = new Map()
				mDays.set(1, 31)
				mDays.set(2, isLeap(year))
				mDays.set(3, 31)
				mDays.set(4, 30)
				mDays.set(5, 31)
				mDays.set(6, 30)
				mDays.set(7, 31)
				mDays.set(8, 31)
				mDays.set(9, 30)
				mDays.set(10, 31)
				mDays.set(11, 30)
				mDays.set(12, 31)

				//获取月份的开始星期
				let startDay =
					new Date(`${year}-${month}-1`).getDay() != 0 ? new Date(`${year}-${month}-1`).getDay() : 7

				//let arrayLength = Math.ceil((mDays.get(month) + startDay) / 7)
				let arrayLength = 6

				for (let i = 1; i <= arrayLength; i++) {
					res.push([])
					for (let j = 0; j < 7; j++) {
						let index = (i - 1) * 7 + j
						let date = index - startDay + 2
						if (date < 1) {
							res[i - 1].push({
								date: (mDays.get(month - 1) ? mDays.get(month - 1) : 31) + date,
								type: 1
							})
						} else if (date > mDays.get(month)) {
							res[i - 1].push({
								date: date - mDays.get(month),
								type: 2
							})
						} else {
							let nowDate = `${new Date().getFullYear()}-${new Date().getMonth()+1}-${new Date().getDate()}`
							nowDate == `${this.year}-${this.month}-${date}` ?
								res[i - 1].push({
									date,
									type: 0,
									today: true
								}) :
								res[i - 1].push({
									date,
									type: 0
								})
						}
					}
				}
				res.unshift([{
					date: "一",
					type: -1
				}, {
					date: "二",
					type: -1
				}, {
					date: "三",
					type: -1
				}, {
					date: "四",
					type: -1
				}, {
					date: "五",
					type: -1
				}, {
					date: "六",
					type: -1
				}, {
					date: "日",
					type: -1
				}])
				this.calendar = res
			},


			async getList() {
				let res = await this.$u.api.nurseList(this.selector)

				this.selectUserList = res.data
			},
			toDetail(item) {
				uni.$u.throttle(() => {
					uni.navigateTo({
						url: `/pages/detailNursing/detailNursing?id=${item.id}`
					})
				})
			},
		},
		computed: {
			getDay() {
				return function(date) {
					if (!date) {
						return " "
					}
					let dayList = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
					return dayList[new Date(date).getDay()] ?? " "
				}
			},
			formatStatus() {
				return function(status) {
					switch (status) {
						case 0:
							return "待护理"
							break;
						case 1:
							return "进行中"
							break;
						case 2:
							return "已完成"
							break;
						default:
							return "未知"
					}
				}
			},
		},
		watch: {
			select: {
				handler(newVal, oldVal) {
					this.getCalendar(this.select.year, this.select.month)

					this.selector.date = `${this.select.year}-${this.select.month}-${this.select.date}`
					this.getList()
				},
				immediate: true,
				deep: true
			}
		},
		onShow() {
			uni.getLocation().then(res => {
				this.selector.lat = res[1]?.latitude
				this.selector.lng = res[1]?.longitude
			})
		}
	}
</script>

<style scoped lang="scss">
	.selecter {
		display: flex;
		justify-content: space-between;

		margin-top: 44rpx;

		.select-year {
			width: 224rpx;
			height: 70rpx;
			background: #FFFFFF;
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			margin-left: 34rpx;

			.text {
				flex: 1;
				height: 40rpx;
				font-size: 32rpx;
				font-family: PingFang-SC-Medium, PingFang-SC;
				font-weight: 500;
				color: #000000;
				line-height: 40rpx;
				text-align: center;
			}

			.arrow {
				width: 70rpx;
				height: 70rpx;
				background: rgba(20, 121, 255, 0.1);
				border-radius: 5rpx;
				border: 2rpx solid #FFFFFF;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.arrowAni {
				animation: rotate 500ms forwards;
			}

			.arrowAni1 {
				animation: rotate2 500ms forwards;
			}

			@keyframes rotate {
				0% {
					transform: rotate(0deg);
				}

				100% {
					transform: rotate(180deg);
				}
			}

			@keyframes rotate2 {
				0% {
					transform: rotate(180deg);
				}

				100% {
					transform: rotate(0deg);
				}
			}
		}

		.select-month {
			width: 224rpx;
			height: 70rpx;
			background: #FFFFFF;
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			margin-right: 40rpx;

			.arrow {
				width: 70rpx;
				height: 70rpx;
				background: rgba(20, 121, 255, 0.1);
				border-radius: 5rpx;
				border: 2rpx solid #FFFFFF;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.text {

				height: 40rpx;
				font-size: 32rpx;
				font-family: PingFang-SC-Medium, PingFang-SC;
				font-weight: 500;
				color: #000000;
				line-height: 40rpx;
			}
		}
	}

	.year-options {

		.mask {
			width: 100vw;
			height: 100vh;
			opacity: 1;
			z-index: 9;
			position: fixed;
			top: 0;
			left: 0;
		}

		.options {
			background: #fff;
			width: 224rpx;
			height: 300rpx;
			border-radius: 20rpx;
			box-shadow: 2rpx 6rpx 10rpx 10rpx rgba(206, 204, 204, 0.5);
			animation: enter 500ms forwards;

			z-index: 10;
			position: absolute;
			top: 350rpx;
			left: 34rpx;

			.item {
				font-size: 32rpx;
				font-family: PingFang-SC-Medium, PingFang-SC;
				font-weight: 500;
				color: #333333;
				text-align: center;

				border-bottom: 2rpx solid #EEEFF5;
				padding: 30rpx 0;
				margin: 0 20rpx;
			}
		}

		@keyframes enter {
			0% {
				opacity: 0;
				transform: translateY(-50%);
			}

			40% {
				opacity: 0;
				transform: translateY(-35%);
			}

			100% {
				opacity: 1;
				transform: translateY(0);
			}
		}
	}

	.canlendar {
		width: 710rpx;
		background: #FFFFFF;
		border-radius: 40rpx;

		margin: 36rpx auto 0 auto;

		.row {
			display: flex;
			justify-content: space-evenly;
			align-items: center;

			.item {
				height: 64rpx;
				width: 64rpx;
				text-align: center;
				line-height: 64rpx;
				border-radius: 10rpx;

				position: relative;
				margin: 10rpx;

				.icon {
					transform: translateX(-50%);
					position: absolute;
					top: calc(50% + 22rpx);
					left: 50%;
				}
			}

			//非本月的日
			.itemOver {
				color: #B7B7B7;
			}

			//选择的日
			.itemActive {
				color: #fff;
				background-color: #0F0F0F;
			}
		}
	}

	.status {
		height: 40rpx;
		font-size: 28rpx;
		font-family: PingFang-SC-Medium, PingFang-SC;
		font-weight: 500;
		color: #36596A;
		line-height: 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		margin: 36rpx 132rpx 0 132rpx;

		.status-item {
			display: flex;
			align-items: center;


		}
	}

	.select-info {
		height: 46rpx;
		font-size: 32rpx;
		font-family: PingFang-SC-Medium, PingFang-SC;
		font-weight: 500;
		color: #36596A;
		line-height: 46rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		margin-top: 36rpx;
		padding: 0 48rpx 0 44rpx;

	}

	.select-user-list {
		width: 710rpx;
		height: 520rpx;
		background: #FFFFFF;
		border-radius: 10rpx;

		margin: 18rpx auto;
		padding-bottom: 18rpx;

		.user-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 2rpx solid #EEEFF5;

			padding: 36rpx 0;
			margin: 0 20rpx;

			.left {}

			.center {
				flex: 1;

				margin-left: 28rpx;

				.top {
					height: 46rpx;
					font-size: 32rpx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 500;
					color: #333333;
					line-height: 46rpx;
				}

				.bottom {
					display: flex;
					align-items: center;

					padding-top: 18rpx;

					.time {
						width: 100rpx;
						background: #F9F9F9;
						height: 40rpx;
						font-size: 28rpx;
						font-family: PingFang-SC-Medium, PingFang-SC;
						font-weight: 500;
						color: #36596A;
						line-height: 40rpx;
						text-align: center;
						border-radius: 10rpx;
					}

					.list-status-icon {

						margin-left: 16rpx;
					}

					.list-status-text {
						height: 40rpx;
						font-size: 28rpx;
						font-family: PingFang-SC-Medium, PingFang-SC;
						font-weight: 500;
						color: #585858;
						line-height: 40rpx;

						margin-left: 16rpx;
					}
				}
			}

			.right {
				width: 70rpx;
				height: 70rpx;
				background: #F9F9F9;
				border-radius: 20rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>
