<template>
	<s-layout title="确认订单">
		<view class="bgc"></view>
		<view style="padding: 0 30rpx;">
			<!-- <view class="head tab-right-active">
				<view class="tab-box" v-if="state.tabIndex>0">
					<view class="item left" :class="{'active':state.tabIndex == 1}" @click="state.tabIndex = 1">
						<view class="tit-box">
							<text>配送</text>
						</view>
					</view>
					<view class="item right" :class="{'active':state.tabIndex == 2}" @click="state.tabIndex=2">
						<view class="tit-box">
							<text>到店自取</text>
						</view>
					</view>
				</view>
			</view> -->
			<!-- TODO：这个判断先删除 v-if="state.orderInfo.need_address === 1" -->
			<view class="bg-white address-box ss-m-b-14 ss-r-b-10" @tap="onSelectAddress">
				<s-address-item :item="state.addressInfo" :hasBorderBottom="false">
					<view class="ss-rest-button">
						<text class="_icon-forward" />
					</view>
				</s-address-item>
			</view>
			<view class="bg-white address-box ss-m-b-14 ss-r-b-10 tab2-box" v-if="state.tabIndex == 2">
				<view class="top-address">
					我是商家的预留地址
				</view>
				<view class="under-box">
					<view class="time font">
						<view>自取时间</view>
						<view class="chooseTime" @click="chooseTime">
							<picker mode="time" :value="state.time" :end="'23:59'" @change="bindTimeChange" >
								<view>{{state.time}}</view>
							</picker>

							<text class="_icon-forward right-forwrad-icon"></text>
						</view>
					</view>
					<view class="line">

					</view>
					<view class="tel font">
						<view>预留电话</view>
						<view class="edit-icon">
							<view class="ss-flex ss-col-center width">
								<input  maxlength="20" v-model="state.tel"
									:inputBorder="false" :clearable="false" :focus="state.focus" @blur="onBlur"/>
							</view>
							<image :src="sheep.$url.static('/static/img/shop/user/address/edit.png')" @click="editTel"/>
						</view>
					</view>
				</view>

			</view>

			<!-- 商品信息 -->
			<view class="order-card-box ss-m-b-14">
				<s-goods-item v-for="item in state.orderInfo.items" :key="item.skuId" :img="item.picUrl"
					:title="item.spuName" :skuText="item.properties.map((property) => property.valueName).join(' ')"
					:price="item.price" :num="item.count" marginBottom="10" />
				<view class="order-item ss-flex ss-col-center ss-row-between ss-p-x-20 bg-white ss-r-10">
					<view class="item-title">订单备注</view>
					<view class="ss-flex ss-col-center">
						<uni-easyinput maxlength="20" placeholder="建议留言前先与商家沟通" v-model="state.orderPayload.remark"
							:inputBorder="false" :clearable="false" />
					</view>
				</view>
			</view>

			<!-- 价格信息 -->
			<view class="bg-white total-card-box ss-p-20 ss-m-b-14 ss-r-10" v-if="false">
				<view class="total-box-content border-bottom">
					<view class="order-item ss-flex ss-col-center ss-row-between">
						<view class="item-title">商品金额</view>
						<view class="ss-flex ss-col-center">
							<text class="item-value ss-m-r-24">
								￥{{ fen2yuan(state.orderInfo.price.totalPrice) }}
							</text>
						</view>
					</view>
					<!-- TODO 芋艿：接入积分 -->
					<view class="order-item ss-flex ss-col-center ss-row-between"
						v-if="state.orderPayload.order_type === 'score'">
						<view class="item-title">扣除积分</view>
						<view class="ss-flex ss-col-center">
							<image :src="sheep.$url.static('/static/img/shop/goods/score1.svg')" class="score-img" />
							<text class="item-value ss-m-r-24">{{ state.orderInfo.score_amount }}</text>
						</view>
					</view>
					<view class="order-item ss-flex ss-col-center ss-row-between">
						<view class="item-title">运费</view>
						<view class="ss-flex ss-col-center">
							<text class="item-value ss-m-r-24">
								+￥{{ fen2yuan(state.orderInfo.price.deliveryPrice) }}
							</text>
						</view>
					</view>
					<!-- 优惠劵：只有 type = 0 普通订单（非拼团、秒杀、砍价），才可以使用优惠劵 -->
					<view class="order-item ss-flex ss-col-center ss-row-between" v-if="state.orderInfo.type === 0">
						<view class="item-title">优惠券</view>
						<view class="ss-flex ss-col-center" @tap="state.showCoupon = true">
							<text class="item-value text-red" v-if="state.orderPayload.couponId > 0">
								-￥{{ fen2yuan(state.orderInfo.price.couponPrice) }}
							</text>
							<text class="item-value" :class="state.couponInfo.length > 0 ? 'text-red' : 'text-disabled'"
								v-else>
								{{
			            state.couponInfo.length > 0 ? state.couponInfo.length + ' 张可用' : '暂无可用优惠券'
			          }}
							</text>
							<text class="_icon-forward item-icon" />
						</view>
					</view>
					<view class="order-item ss-flex ss-col-center ss-row-between"
						v-if="state.orderInfo.price.discountPrice > 0">
						<view class="item-title">活动优惠</view>
						<view class="ss-flex ss-col-center">
							<!--                @tap="state.showDiscount = true" TODO 芋艿：后续要把优惠信息打进去 -->
							<text class="item-value text-red">
								-￥{{ fen2yuan(state.orderInfo.price.discountPrice) }}
							</text>
							<text class="_icon-forward item-icon" />
						</view>
					</view>
					<view class="order-item ss-flex ss-col-center ss-row-between"
						v-if="state.orderInfo.price.vipPrice > 0">
						<view class="item-title">会员优惠</view>
						<view class="ss-flex ss-col-center">
							<text class="item-value text-red">
								-￥{{ fen2yuan(state.orderInfo.price.vipPrice) }}
							</text>
						</view>
					</view>
				</view>
				<view class="total-box-footer ss-font-28 ss-flex ss-row-right ss-col-center ss-m-r-28">
					<view class="total-num ss-m-r-20">
						共{{ state.orderInfo.items.reduce((acc, item) => acc + item.count, 0) }}件
					</view>
					<view>合计：</view>
					<view class="total-num text-red"> ￥{{ fen2yuan(state.orderInfo.price.payPrice) }} </view>
				</view>
			</view>
		</view>


		<!-- 选择优惠券弹框 -->
		<s-coupon-select v-model="state.couponInfo" :show="state.showCoupon" @confirm="onSelectCoupon"
			@close="state.showCoupon = false" />

		<!-- 满额折扣弹框 TODO 芋艿：后续要把优惠信息打进去 -->
		<s-discount-list v-model="state.orderInfo" :show="state.showDiscount" @close="state.showDiscount = false" />

		<!-- 底部 -->
		<su-fixed bottom :opacity="false" bg="bg-white" placeholder :noFixed="false" :index="200">
			<view class="footer-box border-top ss-flex ss-row-between ss-p-x-20 ss-col-center">
				<view class="total-box-footer ss-flex ss-col-center">
					<view class="total-num ss-font-30 text-red">
						￥{{ fen2yuan(state.orderInfo.price.payPrice) }}
					</view>
				</view>
				<button class="ss-reset-button ui-BG-Main-Gradient ss-r-40 submit-btn ui-Shadow-Main" @tap="onConfirm">
					提交订单
				</button>
			</view>
		</su-fixed>
	</s-layout>
</template>

<script setup>
	import {
		reactive,
		computed,
		getCurrentInstance,
		ref,
		onMounted
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import sheep from '@/sheep';
	import {
		isEmpty
	} from 'lodash';
	import OrderApi from '@/sheep/api/trade/order';
	import CouponApi from '@/sheep/api/promotion/coupon';
	import {
		fen2yuan
	} from '@/sheep/hooks/useGoods';

	const state = reactive({
		orderPayload: {},
		orderInfo: {
			items: [], // 商品项列表
			price: {}, // 价格信息
		},
		addressInfo: {}, // 选择的收货地址
		showCoupon: false, // 是否展示优惠劵
		couponInfo: [], // 优惠劵列表
		showDiscount: false, // 是否展示营销活动
		tabIndex: 1, //头部选项卡 参数为 0不显示选项卡,1,2
		// time:
		hours: computed(() => new Date().getHours()),
		time: computed(() => {
			const hours = new Date().getHours();
			const minutes = new Date().getMinutes();
			return `${hours < 10 ? '0' + hours : hours}:${minutes < 10 ? '0' + minutes : minutes}`;
		}),
		tel:'18827761619',
		focus:false
	});
	function bindTimeChange(e) {
		console.log(e,'eeeeeeeee')
	        const selectedTime = e.target.value;
	        const currentTime = state.time
			console.log(selectedTime,currentTime,'currentTime')
	        if (selectedTime < currentTime) {
				console.log('进来了小于当前时间')
	            // 用户选择的时间早于当前时间，您可以进行相应的处理，比如提示用户或重置为当前时间
	            state.time = currentTime;
				console.log(state.time,'小于小于')
	        } else {
				console.log('进来了大于大于')
	            state.time = selectedTime;
				console.log(state.time,'大于大于')
	        }
	    }
	function editTel(){
		console.log('点击了')
		// 获取输入框元素
		state.focus=true
		
	}
	function onBlur() {
	    console.log('我失焦了');
	    validatePhoneNumber(state.tel);
	}
	
	function validatePhoneNumber(phoneNumber) {
	    const phonePattern = /^1[0-9]{10}$/; // 手机号正则表达式
	    if (phonePattern.test(phoneNumber)) {
	        console.log('手机号合法');
			// uni.showToast({
			//   title: '手机号合法',
			// });
	    } else {
			uni.showToast({
				icon: 'none',
				title: '手机号不合法',
			});
			state.tel=''
	        console.log('手机号不合法');
	    }
	}

	// 选择地址
	function onSelectAddress() {
		uni.$once('SELECT_ADDRESS', (e) => {
			changeConsignee(e.addressInfo);
		});
		sheep.$router.go('/pages/user/address/list');
	}

	// 更改收货人地址&计算订单信息
	async function changeConsignee(addressInfo = {}) {
		if (!isEmpty(addressInfo)) {
			state.addressInfo = addressInfo;
		}
		await getOrderInfo();
	}

	// 选择优惠券
	async function onSelectCoupon(couponId) {
		state.orderPayload.couponId = couponId || 0;
		await getOrderInfo();
		state.showCoupon = false;
	}

	// 提交订单
	function onConfirm() {
		if (!state.addressInfo.id) {
			sheep.$helper.toast('请选择收货地址');
			return;
		}
		submitOrder();
	}

	// 创建订单&跳转
	async function submitOrder() {
		const {
			code,
			data
		} = await OrderApi.createOrder({
			items: state.orderPayload.items,
			couponId: state.orderPayload.couponId,
			addressId: state.addressInfo.id,
			deliveryType: state.tabIndex, // TODO 芋艿：需要支持【门店自提】
			pointStatus: false, // TODO 芋艿：需要支持【积分选择】
			combinationActivityId: state.orderPayload.combinationActivityId,
			combinationHeadId: state.orderPayload.combinationHeadId,
			seckillActivityId: state.orderPayload.seckillActivityId
		});
		if (code !== 0) {
			return;
		}
		// 更新购物车列表，如果来自购物车
		if (state.orderPayload.items[0].cartId > 0) {
			sheep.$store('cart').getList();
		}
		// 跳转到支付页面
		sheep.$router.redirect('/pages/pay/index', {
			id: data.payOrderId,
		});
	}

	// 检查库存 & 计算订单价格
	async function getOrderInfo() {
		// 计算价格
		const {
			data,
			code
		} = await OrderApi.settlementOrder({
			items: state.orderPayload.items,
			couponId: state.orderPayload.couponId,
			addressId: state.addressInfo.id,
			deliveryType: state.tabIndex, // TODO 芋艿：需要支持【门店自提】
			pointStatus: false, // TODO 芋艿：需要支持【积分选择】
			combinationActivityId: state.orderPayload.combinationActivityId,
			combinationHeadId: state.orderPayload.combinationHeadId,
			seckillActivityId: state.orderPayload.seckillActivityId
		});
		if (code !== 0) {
			return;
		}
		state.orderInfo = data;
		// 设置收货地址
		if (state.orderInfo.address) {
			state.addressInfo = state.orderInfo.address;
		}
	}

	// 获取可用优惠券
	async function getCoupons() {
		const {
			code,
			data
		} = await CouponApi.getMatchCouponList(
			state.orderInfo.price.payPrice,
			state.orderInfo.items.map((item) => item.spuId),
			state.orderPayload.items.map((item) => item.skuId),
			state.orderPayload.items.map((item) => item.categoryId),
		);
		if (code === 0) {
			state.couponInfo = data;
		}
	}

	onLoad(async (options) => {
		if (!options.data) {
			sheep.$helper.toast('参数不正确，请检查！');
			return;
		}
		state.orderPayload = JSON.parse(options.data);
		await getOrderInfo();
		await getCoupons();
	});
	
</script>

<style lang="scss" scoped>
	:deep() {
		.uni-input-wrapper {
			width: 320rpx;
		}

		.uni-easyinput__content-input {
			font-size: 28rpx;
			height: 72rpx;
			text-align: right !important;
			padding-right: 0 !important;

			.uni-input-input {
				font-weight: 500;
				color: #333333;
				font-size: 26rpx;
				height: 32rpx;
				margin-top: 4rpx;
			}
		}

		.uni-easyinput__content {
			display: flex !important;
			align-items: center !important;
			justify-content: right !important;
		}
	}

	.score-img {
		width: 36rpx;
		height: 36rpx;
		margin: 0 4rpx;
	}

	.order-item {
		height: 80rpx;

		.item-title {
			font-size: 28rpx;
			font-weight: 400;
		}

		.item-value {
			font-size: 28rpx;
			font-weight: 500;
			font-family: OPPOSANS;
		}

		.text-disabled {
			color: #bbbbbb;
		}

		.item-icon {
			color: $dark-9;
		}

		.remark-input {
			text-align: right;
		}

		.item-placeholder {
			color: $dark-9;
			font-size: 26rpx;
			text-align: right;
		}
	}

	.total-box-footer {
		height: 90rpx;

		.total-num {
			color: #333333;
			font-family: OPPOSANS;
		}
	}

	.footer-box {
		height: 100rpx;

		.submit-btn {
			width: 240rpx;
			height: 70rpx;
			font-size: 28rpx;
			font-weight: 500;

			.goto-pay-text {
				line-height: 28rpx;
			}
		}

		.cancel-btn {
			width: 240rpx;
			height: 80rpx;
			font-size: 26rpx;
			background-color: #e5e5e5;
			color: $dark-9;
		}
	}

	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}

	.subtitle {
		font-size: 28rpx;
		color: #999999;
	}

	.cicon-checkbox {
		font-size: 36rpx;
		color: var(--ui-BG-Main);
	}

	.cicon-box {
		font-size: 36rpx;
		color: #999999;
	}

	.bg-red {
		background-color: #EC3534;
	}

	.bgc {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 600rpx;
		z-index: -1;
		background: linear-gradient(180deg, #EC3534 0%, rgba(236, 53, 52, 0) 100%);
	}

	.head {
		// background: linear-gradient(to bottom, #ffc107, #f1f1f1);

		padding-top: 20px;

		.tab-box {
			position: relative;
			height: 40px;
			// margin: 0 12px;

			.item {
				position: absolute;
				top: 0;
				bottom: 0;
				height: 40px;
				text-align: center;
				font-size: 14px;

				width: 50%;

				.tit-box {
					position: relative;
					height: 36px;
					line-height: 36px;
					border-top-left-radius: 6px;
					border-top-right-radius: 6px;
					background-color: rgba(#ffffff, .6);
					width: 100%;
				}
			}

			.item:first-child {
				display: flex;
				flex-direction: row;
				justify-content: flex-start;
				align-items: flex-end;
				left: 0;
			}

			.item:last-child {
				display: flex;
				flex-direction: row;
				justify-content: flex-end;
				align-items: flex-end;
				right: 0;
			}

			.active {
				height: 40px;
				width: 51%;
				font-weight: bold;

				.tit-box {
					height: 40px;
					line-height: 40px;
					background-color: rgba(#ffffff, 1);
				}

			}

			.left .tit-box {
				// margin-left: 12px;
				margin-right: -12px;
				padding-right: 12px;
			}

			.right .tit-box {
				// margin-right: 12px;
				margin-left: -12px;
				padding-left: 12px;
			}

			.left.active .tit-box {
				margin: 0;
			}

			.right.active .tit-box {
				margin: 0;
			}

			.left.active .tit-box:after {
				content: '';
				position: absolute;
				right: -12px;
				bottom: 0;
				border-top: 12px solid transparent;
				border-right: 12px solid transparent;
				border-bottom: 36px solid #ffffff;
			}

			.right.active .tit-box:after {
				content: '';
				position: absolute;
				left: -12px;
				bottom: 0;
				border-top: 12px solid transparent;
				border-left: 12px solid transparent;
				border-bottom: 36px solid #ffffff;
			}
		}

		.head-bottom {
			overflow: hidden;
			background: #ffffff;
			border-top-left-radius: 6px;
			border-top-right-radius: 6px;
			border-bottom-left-radius: 6px;
			border-bottom-right-radius: 6px;

			.tab {

				.t-top {

					&-box {
						margin: 0;
						padding: 12px;

						.txt1 {
							font-size: 18px;
							color: #888;
						}

						.icon1 {
							margin-left: 8px;
							color: #bbb;
						}
					}

					&-box:active {
						background-color: #f5f5f5;
					}
				}

				.t-bb {
					margin: 0 12px;
					height: 0.5px;
					background-color: #E4E7ED;
				}

				.t-bottom {
					padding: 12px;

					.row1 {
						display: flex;
						flex-direction: row;
						justify-content: space-between;

						//padding: 12px 0 0;
						.left {
							font-size: 14px;
							font-weight: bold;
						}

						.right {
							font-size: 14px;
							color: #ff9800;

							i {
								margin-left: 8px;
								color: #bbb;
							}
						}
					}

					.row2 {
						margin-top: 6px;
						font-size: 12px;
						color: #ff9800;
					}
				}
			}
		}

		.head-bottom.selectOne {
			border-top-left-radius: 0;
			border-top-right-radius: px;
		}

		.head-bottom.selectTow {
			border-top-left-radius: 6px;
			border-top-right-radius: 0;
		}
	}

	.tab2-box {
		padding: 30rpx 20rpx;

		.top-address {
			font-weight: 600;
			font-size: 40rpx;
			color: #313131;
			margin-bottom: 20rpx;
		}

		.under-box {
			display: flex;

			.font {
				font-weight: 400;
				font-size: 30rpx;
				color: #7C7C7C;
			}

			.line {
				width: 2rpx;
				background-color: #D8D8D8;
				margin: 0 14rpx;
			}

			.time {
				.chooseTime {
					display: flex;
					align-items: center;
				}
			}

			.tel {
				.edit-icon {
					display: flex;
					align-items: center;
					image {
						width: 24rpx;
						height: 24rpx;
						margin-left: 18rpx;
					}
					.width{
						width:200rpx;
					}
				}
			}
		}
	}
</style>