<template>
	<view >
		<view class="course_status_list">
			<view class="course_status_item" :class="requestParam.status==0?'active':''" @click="setStatus(0)"> 全部 </view>
			<view class="course_status_item" :class="requestParam.status==1?'active':''" @click="setStatus(1)"> 待上课 </view>
			<view class="course_status_item" :class="requestParam.status==2?'active':''" @click="setStatus(2)"> 已取消 </view>
		</view>
		<view class="course_list">
			<view class="course_item"  v-for="(item,index) in productList" :key="index">
				<view class="course_title">
					<view class="course_prc">
						<image :src="item.teacher_pic" mode=""></image>
					</view>
					<view class="title">
						<view class="title_text">
							<view class="tea_name">
								{{item.teacher_name}}
								<!-- 王老师 -->
							</view>
							<view class="btn_1"  @click="cancelPopup(index)" v-if="item.status===1" >
								取消预约
							</view>
							<view class="btn_2" v-if="item.status===2">
								已取消
							</view>
 							<view class="btn_3" v-if="item.status===3">
								已完成
							</view>
						</view>
						<view class="subtilte">
							{{item.course_name}}
						</view>
						<view class="course_arrt">
							<view class="text">
								上课地址：
							</view>
							<view class="attr">
								{{item.course_address}}
								<!-- 深圳市龙华区民治街道和平路3号 -->
							</view>
						</view>
					</view>
				</view>
				<view class="classTime">
					<view class="text">
						上课时间
					</view>
					<view class="time">
							{{timeStamp(item.start_time *1000).date+' '+timeStamp(item.start_time *1000).hour}}
						<!-- 2024--10-24周三上午10:30 -->
					</view>
				</view>
			</view>
		</view>
		<uni-popup ref="cancelPopup">
			<view class="cancel_layout">
				<view class="cancel_title">
					取消预约
				</view>
				<view class="cancel_text">
					是否确认取消课程？取消后可重新更新预约
				</view>
				<view class="cancel_btn">
					<view class="btn_1" @click="closeCancel()">
						在想想
					</view>
					<view class="btn_2" @click="cancelCourse()">
						确认
					</view>
				</view>
			</view>
		</uni-popup>
		<view class="to_bottom" > -----到底啦-----</view>
	</view>
</template>

<script>
	export default {
		data(){
			
			return{
				//产品列表
				productList:[],
				// 请求参数
				requestParam:{
          page:1,
          status:0,
				},
				// 是否最后一页
				isLast:false,
				// 是否请求中
				isReqing:false,
				//当前取消的index
				curIndex:0,
			};
		},
		onLoad() {
			
		},
		onShow() {
			// 没有数据的话，或者请求中，不允许刷新
			if( this.isReqing )		return ;
			// 请求参数
			this.requestParam.name	= "";
			// 请求参数
			this.requestParam.page	= 1;
			// 是否是最后一页
			this.isLast			 	= false;
			// 设置请求中
			this.isReqing			= true;
			this.$http.request('api/course/reservation_list',this.requestParam.page).then((re)=>{
				// 设置非请求中
				this.isReqing		= false;
				// 成功结果
				if( re.code == 'success' ){
					if(re.data.last_page <= this.requestParam.page ) this.isLast = true;
					this.productList = re.data.data;
				}
			});
		},
		methods:{
      setStatus(status){
        // 登录提示
        if( !this.$checkAccess.alterLogin() )  return ;
        // 请求中，不再请求
        if( this.isReqing ) 		return;
        // 初始化页码为1
        this.requestParam.page		= 1;
        // 是否是最后一页
        this.isLast			 		= false;
        // 状态变更
        this.requestParam.status	= status;
        // 设置请求中
        this.isReqing				= true;
        // 请求列表
        this.$http.request('api/course/reservation_list',this.requestParam).then((re)=>{
          // 设置非请求中
          this.isReqing			= false;
          // 成功结果
          if( re.code == 'success' ){
            if(re.data.last_page >= this.requestParam.page ) this.isLast = true;
            this.productList = re.data.data;
          }
        });
      },
			//弹出层
			cancelPopup(index){
				this.curIndex = index;
				
				this.$refs.cancelPopup.open("center");
			},
			//关闭弹出层
			closeCancel(){
				this.$refs.cancelPopup.close();
			},
			//取消预约事件
			cancelCourse(){
			let {id , schedule_id, orders_product_id} = this.productList[this.curIndex]
				console.log(id , schedule_id, orders_product_id);
				this.$http.request("api/course/cancel_reservation/",{id:id,schedule_id:schedule_id,orders_product_id:orders_product_id},'post').then((res)=>{
					if(res.code == 'success'){
            this.$http.request('api/course/reservation_list',this.requestParam).then((re)=>{
              // 设置非请求中
              this.isReqing			= false;
              // 成功结果
              if( re.code == 'success' ){
                if(re.data.last_page >= this.requestParam.page ) this.isLast = true;
                this.productList = re.data.data;
              }
            });
					}else{
						uni.showToast({
							title:`${res.msg}`,
							icon:"error"
						})
					}
				})
				this.$refs.cancelPopup.close();
			},
			
			
			
			strFormat(str) {
				return str < 10 ? `0${str}` : str
			},
			//时间戳转日期
			 timeStamp(time) {
				const dates = new Date(time)
				const year = dates.getFullYear()
				const month = dates.getMonth() + 1
				const date = dates.getDate()
				const day = dates.getDay()
				const hour = dates.getHours()
				const min = dates.getMinutes()
				const days = [ '日', '一', '二', '三', '四', '五', '六']
				return {
					allDate: `${year}/${this.strFormat(month)}/${this.strFormat(date)}`,
					date: `${this.strFormat(year)}-${this.strFormat(month)}-${this.strFormat(date)}`, //返回的日期 07-01
					day: `周${days[day]}`, //返回的礼拜天数  星期一
					hour: this.strFormat(hour) + ':' + this.strFormat(min) //返回的时钟 08:00
				}
			},
		},
	}
	
</script>

<style lang="less">
	.course_status_list{
		display: block;
		height: 60rpx;
		color: #666666;
		line-height: 60rpx;
		background: #FFFFFF;
		padding: 10rpx 35rpx;
		.course_status_item{
			float: left;
			font-size: 28rpx;
			padding: 0rpx 20rpx;
		}
		.course_status_item.active{
			color: #000000;
			font-weight: bold;
		}
	}
	.course_list{
		.course_item{
			background-color: #FFFFFF;
			 margin: 20rpx 0rpx;
			 padding-top: 1rpx;
			.course_title{
				width: 690rpx;
				margin: 20rpx auto 0rpx;
				display: flex;
				.course_prc{
					width: 164rpx;
					 
					image{
						width: 164rpx;
						height: 164rpx;
						border-radius: 20rpx;
						border: 2rpx solid;
					}
				}
				.title{
					flex: 1;
					margin-left: 30rpx;
					.title_text{
						display: flex;
						justify-content: space-between;
						.tea_name{
							font-size: 30rpx;
							font-weight: 300;
						}
						.btn_1{
							display: inline-flex;
							justify-content: center;
							align-items: center;
							width: 200rpx;
							padding: 10rpx;
							color: #00bba5;
							border: 2rpx solid #00bba5;
							border-radius: 40rpx;
						}
						.btn_2{
							display: inline-flex;
							justify-content: center;
							align-items: center;
							width: 200rpx;
							padding: 10rpx;
							color: #FFFFFF;
							background-color: #d3d3d3;
							border-radius: 40rpx;
						}
					}
					.subtilte{
						padding: 5rpx 5rpx 5rpx 0rpx;
						font-size: 26rpx;
					}
					.course_arrt{
						display: flex;
						font-size: 24rpx;
						padding: 10rpx 10rpx 10rpx 0rpx;
						.text{
							display: inline-flex;
							justify-content: center;
							align-items: center;
							width: 120rpx;
							// border: 2rpx solid;
						}
						.attr{
							// border: 2rpx solid;
							flex: 1;
							flex-wrap: wrap;
						}
					}
				}
			}
			.classTime{
				width: 690rpx;
				// border: 2rpx solid;
				display: flex;
				margin: 0rpx auto;
				margin-top: 10rpx;
				padding: 10rpx;
				.text{
					font-size: 26rpx;
					display: inline-flex;
					justify-content: center;
					width: 164rpx;
				}
				.time{
					font-size: 26rpx;
					font-weight: bold;
					margin-left: 30rpx;
				}
			}
		}
	}
	.cancel_layout{
		background-color: #FFFFFF;
		width: 690rpx;
		height: 30vh;
		border-radius: 40rpx;
		display: flex;
		flex-direction: column;
		overflow: hidden;
		.cancel_title{
			display: inline-flex;
			justify-content: center;
			align-items: center;
			padding: 10rpx;
			 margin: 20rpx;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
		}
		.cancel_text{
			display: inline-flex;
			justify-content: center;
			align-items: center;
			margin: 30rpx auto;
			width: 576rpx;
			height: 50rpx;
			padding:30rpx;
		}
		.cancel_btn{
			border-top: 2rpx solid #d3d3d3;
			display: flex;
			justify-content: space-between;
			padding-top: 20rpx;
			height: 130rpx;
			.btn_1{
				display: inline-flex;
				justify-content: center;
				align-items: center;
				height: 100rpx;
				flex: 1;
				font-size: 30rpx;
				color: #51bf81;
			}
			.btn_2{
				display: inline-flex;
				justify-content: center;
				align-items: center;
				flex: 1;
				height: 100rpx;
				font-size: 30rpx;
			}
		}
	}
	
	
	
</style>
