<template>
	<view>
		<view class="container">
			<view class="banner-box"><image src="/static/imgs/yiliao.png" mode=""></image></view>
			<view class="main">
				<view class="tab-box">
					<view class="tab-item" style="width: 100%;">就诊预约修改</view>
				</view>
				<!-- 理发预约表单 -->
				<view class="form-box one" >
					<!-- 选择日期 -->
					<scroll-view scroll-x="true" class="scroll-view">
						<view class="time-item-box" :class="applyDate == item.tiems ? 'time-active' : ''" v-for="(item, index) in date" :key="index" @click="switchTab(index)">
							<text class="time">{{ item.tiems.substr(5, 10) }}</text>
							<text class="week">{{ item.week }}</text>
						</view>
					</scroll-view>

					<view class="content" v-if="this.date[this.chooseDateIndex].isholiday==2||(this.date[this.chooseDateIndex].isholiday!=1&&this.date[this.chooseDateIndex].week!='星期六'&&this.date[this.chooseDateIndex].week!='星期日')">
						<!-- 预约项目 -->
						<view v-for="(item1, index) in parentProjectList" :key="index">
							<!-- <view style="color: #666;">{{item1.project_name}}</view>
							<scroll-view scroll-x="true" class="scroll-view">
								<view class="barber">
									<view class="barber-item" v-for="(item, index) in item1.childrenList" :key="index" @click="chooseProject(item)" v-if="(!item.project_day)||item.project_day.indexOf(date[chooseDateIndex].weekDay)!=-1">
										<text class="round" :class="chooseProjectId == item.id ? 'barberactive' : ''"></text>
										<text class="input_left">{{ item.project_name }}</text>
									</view>
								</view>
							</scroll-view> -->
							
							<view style="color: #333;font-weight: 600;">
								<view >{{item1.project_name}}</view>
							</view>
							<view class="barber-doctor"  >
								<view :class="chooseProjectId == item.id ? 'barber-doctor-item-choose' : 'barber-doctor-item'" class=""  v-for="(item, index) in item1.childrenList" :key="index" @click="chooseProject(item)" v-if="(!item.project_day)||item.project_day.indexOf(date[chooseDateIndex].weekDay)!=-1">
									<text >{{ item.project_name }}</text>
								</view>
							</view>
						</view>
						<view class="time-box">
							<view
								class="time-item"
								:class="[chooseProjectInfoId == item.id ? 'active' : '']"
								@click="chooseProjectInfo(item)"
								v-for="(item, index) in projectInfoList"
								:key="index"
							>
								<view class="time-text">{{ item.project_time }}</view>
								<!-- <view class="time-text">
									已约
									<text :class="item.applyNum?item.applyNum:0 <= 0 ? 'color-666' : ''">{{item.applyNum?item.applyNum:0}}</text>
									位，余
									<text :class="item.project_num-(item.applyNum?item.applyNum:0) <= 0 ? 'color-666' : ''">{{ item.project_num-(item.applyNum?item.applyNum:0 )}}</text>
									位
								</view> -->
								<view class="time-text" v-if="item.project_num!=-1">
									已约
									<text :class="item.applyNum?item.applyNum:0 <= 0 ? 'color-666' : ''">{{item.applyNum?item.applyNum:0}}</text>
									位，余
									<text :class="item.project_num-(item.applyNum?item.applyNum:0) <= 0 ? 'color-666' : ''">{{ item.project_num-(item.applyNum?item.applyNum:0 )}}</text>
									位
								</view>
								<view class="time-text" v-if="item.project_num==-1">
									已约
									<text >{{item.applyNum?item.applyNum:0}}</text>
									位
								</view>
								
								<image class="icon" src="/static/imgs/handel.png" mode=""></image>
							</view>
						</view>

						<!-- <view class="form-item">
							<text class="label">部门</text>
							<view class="ipt-box select">
								<image class="arrow" src="/static/icons/right-arrow.png" mode=""></image>
								<picker class="picker" v-if="deptList.length > 0" :range="deptList" @change="changeDepartment" range-key="name">
									<view>{{ deptList[departmentIndex].name }}</view>
								</picker>
							</view>
						</view>
						<view class="form-item">
							<text class="label">联系人</text>
							<view class="ipt-box"><input type="text" placeholder="请填写预约人姓名" v-model="userName" /></view>
						</view>
						<view class="form-item">
							<text class="label">联系电话</text>
							<view class="ipt-box"><input type="number" placeholder="请填写联系电话" v-model="phone" /></view>
						</view> -->
						<view class="form-item">
							<text class="label">备注</text>
							<view class="ipt-box"><input type="number" placeholder="选填" v-model="desc" /></view>
						</view>
						<!-- 评论 -->
						<!-- <view class="evaluate">
							<view class="title">用户评价</view>
							<view class="evaluate_item" v-for="(item,index) in commentlist" :key="index">
								<view class="user">
									<text class="username input_left">{{item.applicant_name}}</text>
									<text>
										<image v-for="(star,index) in item.satisfied_level" :key="index" src="../../static/imgs/showstar.png" alt="">
									</text>
								</view>
								<view class="text">{{item.comment_content}}</view>
							</view>
						</view> -->
					</view>
					<view v-if="this.date[this.chooseDateIndex].isholiday==0&&(this.date[this.chooseDateIndex].week=='星期六'||this.date[this.chooseDateIndex].week=='星期日')" style="height: 100%;line-height: 400upx;text-align: center;color: #666;">
						今日周末不可预约
					</view>
					<view v-if="this.date[this.chooseDateIndex].isholiday==1" style="height: 100%;line-height: 400upx;text-align: center;color: #666;">
						今日节假日不可预约
					</view>
				</view>
				<!-- 预约记录列表 -->
			</view>
		</view>
		<view >
			<view class="occupied"></view>
			<view class="footer-btn" v-if="this.date[this.chooseDateIndex].isholiday==2||(this.date[this.chooseDateIndex].isholiday!=1&&this.date[this.chooseDateIndex].week!='星期六'&&this.date[this.chooseDateIndex].week!='星期日')"><view class="btn" @click="submit">提交</view></view>
		</view>
	</view>
</template>
<script>
import { getAllDept, getSeekmedicalProject,getSeekmedicalProjectInfo,saveSeekmedicalProjectRecord ,getMySeekmedicalProjectRecord,cancelSeekmedicalRecord} from '@/common/api.js';
import { callbackRequest, getStorage, getDate, alertTip, regPhone, formatDate } from '@/common/util.js';
import loadMore from '@/components/loadMore.vue';
import contentNone from '@/components/contentNone.vue';
export default {
	components: {
		loadMore,
		contentNone
	},
	data() {
		return {
			user: getStorage('userInfo'),
			parentProjectList:[],
			projectList:[],//项目列表
			chooseProjectId:0,
			choseProject:{},
			projectInfoList:[],//项目详细列表
			chooseProjectInfoId:0,
			choseProjectInfo:{},
			deptList: [], //部门列表
			departmentIndex: 0, //选择的部门下标
			
			appointlist: [], //预约记录列表
			
			chooseDateIndex: -1, //当前选择的理发日期
			userName: '', //申请人姓名
			phone: '', //申请人手机号
			desc: '', // 备注
			
			date: getDate(), //获取今天起 最近30天时间
			commentlist: [], //评论列表
			formatDate: formatDate,
			id:'',
			type:1,
			record:{},
			deptId:'',
			applyDate:'',
			
		};
	},
	onLoad(option) {
		this.getCalendar();
		this.id = option.id;
		this.type = option.type ||1;
		// 部门
		
		
		this.initHaircut();
		this.getDepartment();
	},
	methods: {
		compareTime(time1){
			var endTime = time1.split("-")[1];
			var endDate = this.date[this.chooseDateIndex].tiems+" "+endTime;
			endDate = endDate.replace(/-/g,'/'); 
			var endDateTime = new Date(endDate).getTime();
			var nowDateTime = new Date().getTime();
			if(endDateTime>nowDateTime){
				return true;
			}else{
				return false;
			}
		},
		//提交预约
		submit() {
			var regPhone = /^1[3|4|5|6|7|8|9]\d{9}$/;
			if (this.chooseProjectInfoId==0) {
				alertTip('请选择时间段');
				return false;
			}  
			if (this.userName.length == 0) {
				alertTip('请输入联系人姓名');
				return false;
			}  
			if (!regPhone.test(this.phone)) {
				alertTip('请输入正确的手机号');
				return false;
			}
			uni.showModal({
				title: '确认',
				content: '提交后不可再次修改，请确认是否提交？',
				showCancel: true,
				cancelText: '取消',
				confirmText: '确定',
				success: res => {
					if (res.confirm) {
						let params = {
							"method": saveSeekmedicalProjectRecord,
							data: {
								id:this.id,
								editType:this.type,
								projectId:this.chooseProjectId,
								projectName:this.choseProject.project_name,
								projectInfoId:this.chooseProjectInfoId,
								projectInfoName:this.choseProjectInfo.project_time,
								//applyId:this.user.userId,
								//applyName:this.userName,
								//applyDeptId:this.deptList[this.departmentIndex].id,
								//applyDeptName:this.deptList[this.departmentIndex].name,
								//applyPhone:this.phone,
								remark:this.desc,
								modifyBy:this.user.userId,
								applyDate:this.date[this.chooseDateIndex].tiems,
							}
						}
						callbackRequest(params)
							.then(res => {
								let result = res.data;
								if (result.returnCode == 1) {
									alertTip(result.returnMsg);
									if(this.type==1){
										uni.redirectTo({
											url: `/pages/medical/see-doctor?currentTabIndex=1`
										});
									}else if(this.type==2){
										uni.redirectTo({
											url: `/pages/medical/see-doctor-sl`
										});
									}
									
									// setTimeout(_ => {
									// 	this.currentTabIndex = 1;
									// }, 2000)
								} else {
									alertTip(result.returnMsg)
								}
							})
					} 
				}
			});
		
			
		},
		chooseProject(item){
			this.chooseProjectId = item.id;
			this.choseProject = item;
			this.getSeekmedicalProjectInfo();
		},
		chooseProjectInfo(item){
			// if(item.project_num-(item.applyNum?item.applyNum:0) <= 0 || !item.compareTime){
			// 	return;
			// }
			this.choseProjectInfo = item;
			this.chooseProjectInfoId = item.id;
		},
		//获取部门列表
		getDepartment() {
			let data = {
				method: getAllDept,
				data: {}
			};
			callbackRequest(data).then(res => {
				console.log(res);
				if (res.data.returnCode == 1) {
					this.deptList = res.data.returnData;
					for(var i=0;i<this.deptList.length;i++){
						if(this.deptList[i].id==this.eptId){
							this.departmentIndex = i;
						}
					}
				}
			});
		},
		//初始化数据
		initHaircut() {
			let params = {
				"method": getSeekmedicalProject,
				"data": {
					userId: this.user.id
				}
			}
			callbackRequest(params)
				.then(res => {
					res = res.data;
					if (res.returnCode === "1") {
						this.parentProjectList = res.returnData;
						this.projectList = this.parentProjectList[0].childrenList
						//this.projectList = res.returnData;
						this.getRecord();
						
					}
				})
		},
		getSeekmedicalProjectInfo(){
			let params = {
				"method": getSeekmedicalProjectInfo,
				"data": {
					userId: this.user.userId,
					projectId:this.chooseProjectId,
					applyDate:this.applyDate,
				}
			}
			callbackRequest(params)
				.then(res => {
					res = res.data;
					if (res.returnCode === "1") {
						this.projectInfoList = res.returnData;
						for(var i=0;i<this.projectInfoList.length;i++){
							if(this.chooseProjectInfoId==this.projectInfoList[i].id){
								this.choseProjectInfo = this.projectInfoList[i];
							}
						}
					}
				})
		},
		//选择日期
		switchTab(index) {
			if (this.chooseDateIndex != index) {
				this.chooseDateIndex = index;
				this.applyDate = this.date[this.chooseDateIndex].tiems
				
				this.getSeekmedicalProjectInfo();
			}
		},
		//选择部门
		changeDepartment(e) {
			this.departmentIndex = e.detail.value;
		},
		getRecord() {
			let params = {
				"method": getMySeekmedicalProjectRecord,
				data: {
					applyId: this.user.id,
					id:this.id,
				},
			}
			callbackRequest(params)
				.then(res => {
					if (res.data.returnCode == 1) {
						if(res.data.returnData.length > 0){
							this.record = res.data.returnData[0];
							this.userName = this.record.apply_name;
							this.phone = this.record.apply_phone;
							this.desc = this.record.remark;
							this.deptId = this.record.apply_dept_id;
							this.chooseProjectId = this.record.project_id;
							this.applyDate = this.record.apply_date;
							this.chooseProjectInfoId = this.record.project_info_id;
							for(var i=0;i<this.date.length;i++){
								if(this.date[i].tiems==this.applyDate){
									this.chooseDateIndex = i;
								}
							}
							for(var i=0;i<this.projectList.length;i++){
								if(this.chooseProjectId == this.projectList[i].id){
									this.choseProject = this.projectList[i];
								}
							}
							this.getSeekmedicalProjectInfo();
						} else {
						}
					}
				})
		},
		getCalendar(params){
			let params2 = {
				method: "greenCalendar/getCalendarList",
				data: {
					date_name:this.date[0].tiems,
				}
			};
			callbackRequest(params2).then(res => {
				if (res.data.returnCode == 1) {
					var list = res.data.returnData;
					
					for(var j=0;j<this.date.length;j++){
						var fl = true;
						for(var i=0;i<list.length;i++){
							if(list[i].date_name==this.date[j].tiems){
								this.$set(this.date[j], 'isholiday',list[i].isholiday );
								fl = false;
							}
						}
						if(fl){
							this.$set(this.date[j], 'isholiday','0' );
						}
					}
				}
			});
		},
	}
};
</script>

<style lang="scss">
@import '@/static/css/common.scss';
@import url('../../static/css/haircut/haircut.css');
.barber-doctor{
	display: flex;flex-wrap:wrap;border-bottom: 1px solid #e5e5e5; padding-bottom: 20upx;
	.barber-doctor-item{
		width: auto;border: 1px solid #E3E3E3;border-radius: 50px; padding: 8upx 14upx;color: #575757;margin-right: 20upx;margin-top: 20upx;font-size: 26upx;
	}
	.barber-doctor-item-choose{
		width: auto;border: 1px solid #00c277;border-radius: 50px; padding: 8upx 14upx;color: white;margin-right: 20upx;margin-top: 20upx;font-size: 26upx;
		background: #00c277;font-weight: 600;
	}
}
page {
	background: #f8f8f8;
	font-size: 30upx;
}
.bg-gray {
					background-color: #c5c5c5;
				}
.scroll-view {
	padding-top: 30upx;
	white-space: nowrap;
	width: 100%;
	height: 120upx;

	.time-item-box {
		padding: 0 10upx;
		margin: 0 16upx;
		display: inline-block;

		.time {
			display: block;
			width: 100%;
			font-size: 28upx;
			color: #666;
		}

		.week {
			display: block;
			width: 100%;
			font-size: 28upx;
			color: #666;
		}
	}

	.time-active {
		.time {
			color: #00c277;
		}

		.week {
			color: #00c277;
		}
	}
}

.detail {
	// margin-top: 10upx;
	margin-bottom: 14upx;
	background: #ffffff;
	border-radius: 4upx;
}

.detail_header {
	padding: 15upx 30upx;
	border-bottom: 1px solid #eee;
}

.detail_header .blueSign {
	height: 30upx;
	width: 10upx;
	background: #00c277;
	display: inline-block;
	margin: 10upx 14upx 0 0;
}

.detail_header .font-tilte {
	display: inline-block;
	font-size: 40upx;
	color: #333;
	line-height: 50upx;
	vertical-align: top;
}

.detail_header .font-result {
	display: inline-block;
	float: right;
	font-size: 26upx;
	color: #00c277;
	line-height: 50upx;
	vertical-align: top;
}

.detail_msg {
	padding: 20upx 30upx;
	overflow: hidden;
}

.detail_msg .info-item {
	margin-bottom: 10upx;
	font-size: 28upx;

	text {
		display: inline-block;
		width: 140upx;
	}

	.infoSpan {
		width: auto;
		font-size: 28upx;
		color: #666;
		float: right;
	}
}

.comment-btn {
	float: right;
	font-size: 24upx;
	border-radius: 10upx;
	color: #fff;
	line-height: 50upx;
	text-align: center;
	min-width: 140upx;
	background-color: #00c277;
	margin-left:20upx;
}	

.container {
	position: relative;
	overflow: hidden;

	.banner-box {
		width: 100%;
		height: 240upx;
		position: absolute;
		top: 0;
		left: 0;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.main {
		margin: 172upx 40upx 0;
		position: relative;
		z-index: 10;

		.tab-box {
			overflow: hidden;
			border-radius: 10upx 10upx 0 0;
			background: rgba(132, 149, 167, 0.8);
			height: 70upx;

			.tab-item {
				float: left;
				height: 70upx;
				width: 50%;
				font-size: 30upx;
				color: #e5f6ff;
				text-align: center;
				line-height: 70upx;
			}

			.active_tab {
				background: #fff;
				color: #333;
				border-radius: 10upx 10upx 0 0;
				font-weight: bold;
			}
		}

		.form-box {
			// padding: 20upx 30upx 50upx;
			background: #fff;

			.form-item {
				overflow: hidden;
				border-bottom: 1px solid #eee;
				padding: 30upx 0;

				.label {
					float: left;
					width: 150upx;
					font-size: 28upx;
					color: #333;
					font-weight: bold;
					line-height: 50upx;
				}

				.ipt-box {
					margin-left: 160upx;

					input {
						width: 100%;
						height: 50upx;
						line-height: 50upx;
						font-size: 26upx;
						color: #666;
					}
				}

				.select {
					.arrow {
						float: right;
						width: 16upx;
						height: 29upx;
						margin-top: 10upx;
					}

					.picker {
						height: 50upx;
						line-height: 50upx;
						font-size: 26upx;
						color: #666;
					}
				}
			}

			.sure-btn {
				width: 100%;
				height: 80upx;
				font-size: 28upx;
				color: #fff;
				text-align: center;
				line-height: 80upx;
				border-radius: 8upx;
				background: #0066cc;
				background: linear-gradient(to right, #ffc200, #ff9000);
				/*设置按钮为渐变颜色*/
				margin-top: 50upx;
			}
		}

		.list-box {
			.list-item {
				background: #fff;
				margin-bottom: 10upx;

				.title {
					border-bottom: 1px solid #eee;
					padding: 20upx 10upx;
					overflow: hidden;
					line-height: 40upx;

					.line {
						display: inline-block;
						width: 10upx;
						height: 32upx;
						background: $assistColor;
						margin-top: 4upx;
					}

					.text {
						display: inline-block;
						font-size: 28upx;
						color: #444;
						font-weight: bold;
						margin-left: 10upx;
						vertical-align: top;
					}

					.status {
						font-size: 26upx;
						color: $assistColor;
						float: right;
					}
				}

				.content {
					// padding: 5upx 5upx;

					.info-item {
						overflow: hidden;
						padding-bottom: 10upx;

						.fl-text {
							float: left;
							font-size: 26upx;
							color: #444;
							font-weight: bold;
							line-height: 40upx;
						}

						.fr-text {
							float: right;
							font-size: 26upx;
							color: #666;
							line-height: 40upx;
						}
					}
				}
			}
		}
	}
}

.content {
	background: #fff;

	.time-box {
		overflow: hidden;
		padding-top: 20rpx;

		.time-item {
			// float: left;
			// width: 180rpx;
			margin: 0 0rpx 20rpx 20rpx;
			font-size: 26rpx;
			text-align: center;
			line-height: 32rpx;
			padding: 10rpx 0;
			position: relative;
			border: 1px solid #ccc;
			box-sizing: border-box;

			.time-text {
				color: #666;

				text {
					padding: 0 6rpx;
					color: #00c277;
				}

				.color-666 {
					color: #666;
				}
			}

			.icon {
				position: absolute;
				right: 0;
				bottom: 0;
				width: 36rpx;
				height: 36rpx;
				display: none;
			}
		}

		.active {
			border: 1px solid #00c277;

			.icon {
				display: block;
			}
		}
	}

	/* 评论 */
	.evaluate {
		padding-top: 30upx;
		border-top: 1px solid #eee;
	}

	.evaluate .title {
		font-size: 28upx;
		color: #333;
		font-weight: bold;
		line-height: 40upx;
		padding: 0 30rpx 30rpx;
	}

	.evaluate .evaluate_item {
		border-bottom: 1px solid #eeeeee;
		padding: 0 0 30upx 50upx;
	}

	.evaluate .evaluate_item:last-child {
		border: none;
	}

	.evaluate .evaluate_item .user {
		padding: 20upx 0;
	}

	.evaluate .evaluate_item .user .username {
		font-size: 28rpx;
		color: #666;
		display: inline-block;
		vertical-align: top;
		padding-right: 20upx;
	}

	.evaluate .evaluate_item .user span image {
		width: 40upx;
		height: 40upx;
		margin-right: 5upx;
	}

	.evaluate .evaluate_item .text {
		font-size: 28upx;
		color: #666;
		line-height: 40upx;
	}
}
</style>
