<template>
	<view class="page">
		<pyh-nv ref="nv" :config="nvConfig"></pyh-nv>
		<view class="listBox" :style="{'padding-top':(pageTop+'px')}">
		
		<view class="content">
			
			<view class="message-list">
				
			<view class="titleall-box">
				<view class="titleall-left"><view class="titleall-left-line"></view>预约日期</view>
				<view class="" style="font-size: 32rpx;font-weight: 600;display: flex;" @click="bspersonshow = true">{{bspersoninfo.name}} <u-icon name="arrow-down-fill" color="#2979ff" size="20"></u-icon></view>
			</view>
			<u-picker :show="bspersonshow"  @confirm="bspersonxz" @close="bspersongb" :columns="bspersonselector" defaultIndex="2"></u-picker>
				<view>
					<!-- 插入模式 -->
					<uni-calendar class="uni-calendar--hook" :selected="info.selected" :showMonth="false" @change="change" @monthSwitch="monthSwitch" />
				</view>
			</view>
			<view class="message-list">
			<view class="titleall-box">
				<view class="titleall-left"><view class="titleall-left-line"></view>预约时间</view>
			</view>
			<view class="recharge-list">
				<view class="list" @click="xztime(index)" v-for="(item,index) in sjlist" :key="index" :class="{ 'action': item.status }" v-if="sjlist.length > 0">
					<view class="price">
						<text>{{item.time}}</text>
					</view>
				</view>
				<view class="list" v-for="(item,index) in sjlist" :key="index" :class="{ 'action': item.status }" v-if="sjlist.length == 0 || sjlist == '' || sjlist == ''">
					<span>没有空闲时间</span>
				</view>
			</view>
			</view>
			
			<view class="message-list">
				<view class="info-box">
					<view class="info-box-list">
						<view class="serve">
							<view class="serve-title">火化炉</view>
							<view class="serve-right" @click="hhlopen">
								<text>*</text>{{from.cremationFurnaceInfo}}
								<image src="../../static/down3.png"></image>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 火化炉弹窗 -->
			<u-picker :show="hhlshow"  @confirm="hhlqr" @close="hhlclose" :columns="columns"></u-picker>
			<view class="tips">
				<view class="tips-top">温馨提示：</view>
				<view>
				请于预约时间段提前30分钟到达殡仪馆，办理相关手续，高温炉火化时间为50秒左右，普通炉火化时间约为40分钟左右，具遗体情况可能会提前或延长火化时间的情况：请于谅解！	
				</view>
			</view>
			
			<view class="page-footer">
			  <view class="footer-buy">
			    <view class="cart-add" @click="open">
					<view><image src="../../static/btn3.png"></image></view>
					<view class="cart-add-text"><image src="../../static/phone.png"></image><text>联系电话</text></view>
			    </view>
			    <view class="buy-at" @click="funeralresult">
					<image src="../../static/btn4.png"></image>
			      <text>立即预约</text>
			    </view>
			  </view>
			</view>
		</view>
		</view>
	</view>
</template>

<script>
	/**
	 * 获取任意时间
	 */
	function getDate(date, AddDayCount = 0) {
		if (!date) {
			date = new Date();
		}
		if (typeof date !== 'object') {
			date = date.replace(/-/g, '/');
		}
		const dd = new Date(date);
	
		dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期
	
		const y = dd.getFullYear();
		const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1; // 获取当前月份的日期，不足10补0
		const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
		return {
			fullDate: y + '-' + m + '-' + d,
			year: y,
			month: m,
			date: d,
			day: dd.getDay()
		};
	}
	export default {
		name: "index",
		data() {
			return {
				OrderType: 0,
				nvConfig:{
					title:"遗体火化",
					bgColor:"#ffffff",
					color:"#000000",
					fixedAssist:{
						hide:true,
					},
				},
				bzpersonlist:[],
				bspersoninfo:{
					name:"",
					obituaryOrderNumber:0,
				},
				bspersonshow:false,
				bspersonselector: [
					// ['中国', '美国', '日本']
				],
				info: {
					lunar: true,
					range: true,
					insert: false,
					selected: []
				},
				
				sjlist:[],
				
				from:{
					// obituaryOrderNumber:24681216,
					scheduledCremationDate:"",
					scheduledCremationTime:"",
					cremationFurnaceInfo:"火化炉选择",
					contactPhone: "联系电话",
					cremationDate: "2023-09-06",
					cremationTime: "2023-09-20",
					// 经办日期
					handledTime: "2023-09-19",
					handlerName: "姓名",
					rating: "未评价",
					status:"等通知"
				},
				hhlshow:false,
				// 火化炉完整列表
				hhlwzlist:[],
				hhlid:"",
				columns: [
				    ['拣灰炉', '平板炉']
				],
				// 当前天数
				nowdate:"",
				
				// 报丧人的数据
				bsform: {

					name: '',

					intro: '',

					sex: '难受'

				},
			};
		},
		onPageScroll(e) {this.$refs.nv.pageScroll(e)},
		computed:{
			pageTop(){return parseInt(88*uni.getSystemInfoSync().windowWidth/750) + uni.getSystemInfoSync().statusBarHeight}
		},
		onLoad(params) {
			this.OrderType = params.type;
			var now = new Date();
			this.nowdate = this.timestampToTime(now)
			this.from.scheduledCremationDate = this.timestampToTime(now)
			console.log("时间",this.from.scheduledCremationDate);
			// this.gethhsjsl(this.parseTime(now));
			this.getbsinfo();
			this.zxhhllist(this.timestampToTime(now));
		},
		onReady() {
			this.$nextTick(() => {
				this.showCalendar = true;
			});
			// TODO 模拟请求异步同步数据
			setTimeout(() => {
				this.info.date = getDate(new Date(), -30).fullDate;
				this.info.startDate = getDate(new Date(), -60).fullDate;
				this.info.endDate = getDate(new Date(), 30).fullDate;
				this.info.selected = [
					{
						date: getDate(new Date(), -3).fullDate,
						info: '打卡'
					},
					{
						date: getDate(new Date(), -2).fullDate,
						info: '签到',
						data: {
							custom: '自定义信息',
							name: '自定义消息头'
						}
					},
					{
						date: getDate(new Date(), -1).fullDate,
						info: '已打卡'
					}
				];
			}, 2000);
		},
		methods:{
			timestampToTime(timestamp) {
			    timestamp = timestamp ? timestamp : null;
			    let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
			    let Y = date.getFullYear() + '-';
			    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
			    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
			    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
			    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
			    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
			    return Y + M + D;
			},
			// 火化炉列表获取
			zxhhllist(date){
				console.log("初始化时间",date);
				this.API.zxhhllist().then(res=>{
					console.log("火化炉",res);
					this.columns = [];
					this.hhlwzlis = []
					var info = []
					for(let i = 0;i < res.data.length;i++){
						for(let s = 0;s < res.data[i].funeralCrematoriumFurnaceInfoPos.length;s++){
							info.push(res.data[i].funeralCrematoriumFurnaceInfoPos[s].furnaceName)
							this.hhlwzlist.push(res.data[i].funeralCrematoriumFurnaceInfoPos[s])
							if(i == 0 && s == 0){
								console.log("时间");
								this.hhlid = res.data[i].funeralCrematoriumFurnaceInfoPos[s].id
								this.from.cremationFurnaceInfo = res.data[i].funeralCrematoriumFurnaceInfoPos[s].furnaceName
								this.zxhqhhltimelist(date);
							}
						}
					}
					this.columns.push(info)
					console.log("整理火化炉信息",this.hhlwzlist);
				})
			},
			open(){
				console.log("123",this.contentshow);
				var that = this
				uni.showModal({
					title: '联系电话',
					content: '如有问题请拨打电话:' + "02836241643",
					success: function (res) {
						if (res.confirm) {
							console.log('用户点击确定');
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 获取用户的报丧记录
			getbsinfo(){
				console.log("userinfo",uni.getStorageSync("USERS_KEY"));
				var info = {
					userId:uni.getStorageSync("USERS_KEY").userId
				}
				this.API.getbsinfo(info).then(res=>{
					console.log("获取用户报丧信息",res);
					this.bzpersonlist = res.rows;
					this.bspersoninfo.name = res.rows[0].deceasedName
					this.bspersoninfo.obituaryOrderNumber = res.rows[0].orderNumber
					var info = []
					for(let i = 0;i < res.rows.length;i++){
						info.push(res.rows[i].deceasedName)
					}
					this.bspersonselector.push(info)
				})
			},
			// 选择预约时间
			xztime(index){
				console.log(index);
				for(let i = 0;i < this.sjlist.length;i++){
					if(index == i){
						this.sjlist[i].status = true;
						this.from.scheduledCremationTime = this.sjlist[i].time
					}else{
						this.sjlist[i].status = false;
					}
				}
			},
			// 获取预约时间和数量
			gethhsjsl(times){
				this.API.gethhsjsl().then(res=>{
					console.log("获取预约时间和数量",res);
					console.log("时间111",times);
					this.sjlist = []
					for(let i = 0;i < res.data.length;i++){
						if(res.data[i].DateStr == times){
							for(let s = 0;s < res.data[i].timeAndTotalList.length;s++){
								var info = {
									time:"",
									total:0,
									residue:0,
									status:false
								}
								info.time = res.data[i].timeAndTotalList[s].timePeriod;
								info.total = res.data[i].timeAndTotalList[s].totalNumber;
								info.residue = res.data[i].timeAndTotalList[s].restNumber;
								if(s == 0){
									info.status = true
									this.from.scheduledCremationTime = res.data[i].timeAndTotalList[s].timePeriod;
								}
								this.sjlist.push(info)
							}
						}
					}
					console.log("this.sjlist",this.sjlist);
				})
			},
			// 报丧人确认
			bspersonxz(e){
				console.log(e);
				this.bspersoninfo.name = e.value[0]
				this.bspersoninfo.obituaryOrderNumber = this.bzpersonlist[e.indexs[0]].orderNumber
				console.log(this.bspersoninfo);
				this.bspersonshow = false;
			},
			bspersongb(){
				this.bspersonshow = false;
			},
			// 火化炉点击事件
			hhlopen(){
				this.hhlshow = true
			},
			// 火化炉确认事件
			hhlqr(e){
				console.log(e,this.hhlwzlist[e.indexs[0]]);
				this.hhlid = this.hhlwzlist[e.indexs[0]].id;
				console.log("火化炉id",this.hhlid);
				this.zxhqhhltimelist(this.nowdate);
				this.hhlshow = false;
				this.sjlist = []
				this.from.cremationFurnaceInfo = e.value[0];
			},
			// 最新获取时间火化炉时间
			zxhqhhltimelist(time){
				// time = time + ''
				var reg = /[\t\r\f\n\s]*/g;
				time = time.replace(reg,'');
				var infos = {
					furnaceId:this.hhlid,
					useStatus:"2",
					date:time
				}
				this.API.gethhsjsl(infos).then(res=>{
					console.log("最新时间列表",res);
					this.sjlist = []
					console.log("选择的时间",time);
					
					for(let i = 0;i < res.data.length;i++){
						console.log("时间是否正确",res.data[i].dateStr == time,res.data[i].dateStr,typeof(time),typeof(res.data[i].dateStr));
						// if(res.data[i].dateStr === time){
						// 	for(let s = 0;s < res.data[i].timeAndTotalList.length;s++){
						// 		var info = {
						// 			time:"",
						// 			status:false
						// 		}
						// 		info.time = res.data[i].timeAndTotalList[s].timePeriod;
						// 		if(s == 0){
						// 			info.status = true
						// 			this.from.scheduledCremationTime = res.data[i].timeAndTotalList[s].timePeriod;
						// 		}
						// 		this.sjlist.push(info)
						// 	}
						// }
						for(let s = 0;s < res.data[i].timeAndTotalList.length;s++){
							var info = {
								time:"",
								status:false
							}
							info.time = res.data[i].timeAndTotalList[s].timePeriod;
							this.sjlist.push(info)
						}
					}
				})
			},
			// 火化炉取消事件
			hhlclose(){
				this.hhlshow = false;
			},
			funeralresult(){
				console.log("user",uni.getStorageSync('USERS_KEY'));
				var user = uni.getStorageSync('USERS_KEY')
				this.from.handlerName = user.nickName
				this.from.handledTime = user.loginDate;
				this.from.userId = uni.getStorageSync('USERS_KEY').userId
				this.from.contactPhone = uni.getStorageSync('USERS_KEY').phonenumber
				this.from.cremationDate = this.from.scheduledCremationDate
				this.from.cremationTime = this.from.scheduledCremationDate
				// this.from.scheduledCremationTime = this.from.scheduledCremationTime
				if(this.from.scheduledCremationTime == ''){
					uni.showToast({
						title: '请选择预约时间',
						//将值设置为 success 或者直接不用写icon这个参数
						icon: 'error',
						//显示持续时间为 2秒
						duration: 2000
					}) 
					return
				}
				this.from.obituaryOrderNumber = this.bspersoninfo.obituaryOrderNumber
				this.from.furnaceId = this.hhlid
				console.log("from",this.from);
				// return
				this.API.hhlyy(this.from).then(res=>{
					console.log("遗体火化预约",res);
				})
				uni.navigateTo({
					url: '/pages/funeralresult/funeralresult',
				})
			},
			// 选择日期返回火化日期
			change(e) {
				console.log('change 返回:', e);
				this.zxhqhhltimelist(e.fulldate);
				this.this.from.scheduledCremationTime = ''
				// this.gethhsjsl(e.fulldate)
				this.from.scheduledCremationDate = e.fulldate;
				// 模拟动态打卡
				if (this.info.selected.length > 5) return;
				this.info.selected.push({
					date: e.fulldate,
					info: '打卡'
				});
			},
			confirm(e) {
				console.log('confirm 返回:', e);
			},
			monthSwitch(e) {
				console.log('monthSwitchs 返回:', e);
			},
			// 时间搓数据处理
			parseTime(timestamp){
				var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
				var Y = date.getFullYear() + '-';
				var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
				var D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
				var h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
				var m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
				var s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());

				let strDate = Y+M+D;
				console.log(strDate) //2020-05-08 17:44:56　
				return strDate;
			}

		}
	}
</script>

<style scoped lang="scss">
	@import 'funeralcremation.scss';
</style>
