<template>
	<view>
		<form @submit="addCoupons">

			<view class="couponsTitle  flex-y-center flex-between">
				<view class="couponsTitleL">优惠券名称</view>
				<input type="text" name="couponsName" class="couponsTitleLInput" placeholder-class="couponsTitleLPleahold"
				 placeholder="请输入优惠券名称" :value="info.title" maxlength="32" />
			</view>

			<view class="couponsTitle  flex-y-center flex-between">
				<view class="couponsTitleL">满额</view>
				<input type="digit" class="couponsTitleLInput" name="couponsFull" placeholder-class="couponsTitleLPleahold"
				 placeholder="请输入满额" :value="info.full_price" maxlength="6" />
			</view>

			<view class="couponsTitle  flex-y-center flex-between">
				<view class="couponsTitleL">优惠金额</view>
				<input type="digit" class="couponsTitleLInput" name="couponsDiscounts" placeholder-class="couponsTitleLPleahold"
				 placeholder="请输入优惠金额" :value="info.cut_price" maxlength="5" />
			</view>

			<view class="couponsTitle  flex-y-center flex-between">
				<view class="couponsTitleL">开始时间</view>
				<view class="timeSelete">
					<picker mode="date" :value="info.start_time" :start="startDate" :end="endDate" @change="bindDateChangeStart">
						<view class="uni-input">{{startDates}}</view>
					</picker>

				</view>
			</view>

			<view class="couponsTitle  flex-y-center flex-between">
				<view class="couponsTitleL">结束时间</view>
				<view class="timeSelete">

					<picker mode="date" :value="info.end_time" :start="startDate" :end="endDate" @change="bindDateChangeEnd">
						<view class="uni-input">{{endDates}}</view>
					</picker>
				</view>
			</view>

			<view class="couponsTitle  flex-y-center flex-between">
				<view class="couponsTitleL">优惠券数量</view>
				<input type="number" name="couponsNum" class="couponsTitleLInput" placeholder-class="couponsTitleLPleahold"
				 placeholder="请输入优惠券数量" :value="info.num" maxlength="4" />
			</view>

			<!-- 底部按钮 -->

			<button class="bottomButton" form-type="submit">添加优惠券</button>

		</form>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				token: '',
				info: "",
				startDates:"请选择优惠券开始时间",
				endDates:"请选择优惠券结束时间",
				id:""
			}

		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},

		onLoad(e) {
			this.token = uni.getStorageSync('token'); //读取缓存手机号码
		
			if (e.id) {
					this.id = e.id;
				this.Coupons();
			
			}
		},

		onShow() {

		},

		methods: {
			//请求详情
			Coupons() {
				this.request({
					url: "coupon/detail",
					data: {
						token: this.token,
						coupon_id: this.id,
					},
				}).then(res => {
					if (res.code == "200") {
						this.info = res.data.detail;
						this.startDates=res.data.detail.start_time;
						this.endDates=res.data.detail.end_time;
						
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});
			},

			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},

			// 开始时间
			bindDateChangeStart(e) {
				this.startDates = e.target.value;
			},
			//结束时间
			bindDateChangeEnd(e) {
				this.endDates = e.target.value;
			},

			//添加优惠券
			addCoupons(e) {

            console.log(e)
		    let {couponsName,couponsFull,couponsDiscounts,couponsNum}=	e.detail.value;
			
				let token = this.token;
				let startDates = this.startDates;
				let endDates = this.endDates;
		        let a = ((Date.parse(new Date(startDates))) / 1000); //开始的时间戳
				let b = ((Date.parse(new Date(endDates))) / 1000); //结束的时间戳

				if (couponsName == "") {
					uni.showToast({
						title: '请输入优惠券名称',
						icon: 'none',
						duration: 2000
					});
					return false
				} else if (couponsFull == "") {
					uni.showToast({
						title: '请输入优惠券满额',
						icon: 'none',
						duration: 2000
					});
					return false
				} else if (couponsDiscounts == "") {
					uni.showToast({
						title: '请输入优惠券金额',
						icon: 'none',
						duration: 2000
					});
					return false
				} else if (Number(couponsDiscounts)>Number(couponsFull) ) {
					uni.showToast({
						title: '优惠金额不可以大于满额哦！',
						icon: 'none',
						duration: 2000
					});
					return false	
				} else if (!startDates||startDates=="请选择优惠券开始时间") {
					uni.showToast({
						title: '请选择开始时间',
						icon: 'none',
						duration: 2000
					});
					return false
				} else if (!endDates||endDates=="请选择优惠券结束时间") {
					uni.showToast({
						title: '请选择结束时间',
						icon: 'none',
						duration: 2000
					});
					return false
				} else if (a > b) {
					uni.showToast({
						title: '开始时间不能大于结束时间哦',
						icon: 'none',
						duration: 2000
					});
					return false
				} else if (couponsNum == "") {
					uni.showToast({
						title: '请输入优惠券数量 ',
						icon: 'none',
						duration: 2000
					});
					return false
				}
				// coupon/edit
				let url=this.id?"coupon/edit":"coupon/create";
				this.request({
					url:url,
					data: {
						token: token,
						title: couponsName,
						cut_price: Number(couponsDiscounts),
						full_price: Number(couponsFull),
						start_time: startDates,
						end_time: endDates,
						num: Number(couponsNum),
						url:"https://",
						coupon_id:this.id,
					},
				}).then(res => {
					console.log(res, "res")
					if (res.code == "200") {
						uni.showModal({
							title: '提示',
							showCancel: false,
							content: '添加成功',
							success: function(res) {
								
								if (res.confirm) {
									uni.navigateBack({
										delta:1
									})
									// uni.reLaunch({
									// 	url: "../index/index"
									// })
								}
								
							}
						});
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				});




			},
		},

	}
</script>

<style>
	page {
		background: #f4f4f4;
	}

	.couponsTitle {
		height: 100rpx;
		width: 750rpx;
		background: #Fff;
	}

	.couponsTitleL {
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: 400;
		color: #333333;
		padding-left: 30rpx;

	}

	.couponsTitleLInput {
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: 400;
		color: #333;
		text-align: right;
		padding: 0 30rpx 0 200rpx;
		flex: 1;
	}

	.couponsTitleLPleahold {
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: 400;
		color: #cbcbcb;
	}

	/* 选择时间 */
	.timeSelete {
		text-align: right;
		flex: 1;
		font-size: 24rpx;
		padding-right: 30rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: 400;
		color: #7d7d7d;
	}

	/* 添加优惠券 */
	.bottomButton {
		position: absolute;
		left: 60rpx;
		bottom: 100rpx;
		width: 630rpx;
		height: 98rpx;
		line-height: 98rpx;
		background: linear-gradient(180deg, #6fa5ff, #438aff);
		border-radius: 8rpx;
		box-shadow: 0px 0px 8rpx 0px rgba(51, 135, 255, 0.2);
		font-size: 32rpx;
		font-family: Source Han Sans CN, Source Han Sans CN-Medium;
		font-weight: 500;
		text-align: center;
		color: #ffffff;
	}
</style>
