<template>
	<view>

		<view class="username">
			<view class="namecont">活动类型</view>
			<picker v-if="type !=3" mode="selector" style="width: 70%;" :range="activityList" range-key="codeName"
				@change="changefeilvalue">
				<view class="text1" v-if="activityName">{{activityName}}</view>
				<view class="text1 fcor999" v-else>请选择活动类型</view>
			</picker>
			<image v-if="type !=3" src="../../../static/img/jtg.png" mode="widthFix" class="iconw"></image>
			<view v-if="type == 3">{{activityName}}</view>
		</view>

		<view class="username">
			<view class="namecont">优惠类型</view>
			<picker v-if="type !=3" mode="selector" style="width: 70%;" :range="preferentialList" range-key="codeName"
				@change="changefeilvalue1">
				<view class="text1" v-if="preferentialName">{{preferentialName}}</view>
				<view class="text1 fcor999" v-else>请选择优惠类型</view>
			</picker>
			<image v-if="type !=3" src="../../../static/img/jtg.png" mode="widthFix" class="iconw"></image>
			<view v-if="type ==3">{{preferentialName}}</view>
		</view>

		<view class="width100 backcorfff">
			<view class="username">
				<view class="namecont" >优惠金额</view>
				<input v-if="type !=3" :placeholder="placeholderTip" v-model="price" type="text"
					placeholder-style="color: #bfbfbf;" style="width: 72%;" />
				<view v-if="type ==3">{{price}}</view>
			</view>
		</view>

		<view class="username">
			<view class="namecont">结束时间</view>
			<picker mode="date" class="width70" :value="end_date" :start="startDate" :end="endDate"
				@change="bindDateChange">
				<view v-if="end_date">{{end_date}}</view>
				<view class="fcor999" v-else>请选择结束时间</view>
			</picker>
			<image src="../../../static/img/jtg.png" mode="widthFix" class="iconw"></image>
		</view>

		<view class="width100 backcorfff" v-if="preferentialType == 1">
			<view class="username">
				<view class="namecont">优惠条件</view>
				<input v-if="type !=3" placeholder="请输入优惠条件" v-model="condition" type="number"
					placeholder-style="color: #bfbfbf;" />
				<view v-if="type == 3">{{condition}}</view>
			</view>
		</view>

		<view class="username">
			<view class="namecont">限制类型</view>
			<picker v-if="type !=3" mode="selector" style="width: 70%;" :range="partakeList" range-key="codeName"
				@change="changefeilvalue2">
				<view class="text1" v-if="partakeName">{{partakeName}}</view>
				<view class="text1 fcor999" v-else>请选择限制类型</view>
			</picker>
			<image v-if="type !=3" src="../../../static/img/jtg.png" mode="widthFix" class="iconw"></image>
			<view v-if="type == 3">{{partakeName}}</view>
		</view>

		<view class="width100 backcorfff">
			<view class="username">
				<view class="namecont">限制数量</view>
				<input v-if="type !=3" placeholder="请输入限制数量" v-model="partakeNum" type="text"
					placeholder-style="color: #bfbfbf;" />
				<view v-if="type == 3">{{partakeNum}}</view>
			</view>
		</view>

		<view class="btn" @tap="addActivity()" v-if="type == 1">提交活动</view>
		<view class="btn" @tap="addActivity()" v-if="type == 2">修改活动</view>
		<view class="btn" @tap="addActivity()" v-if="type == 3">修改时间</view>
	</view>
</template>

<script>
	import {
		addActivity,
		updateActivity,
		getActivityDetail,
		updateActivityEndTime
	} from '../../../Utils/Api.js';
	let app = getApp();
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				type: '', //区分
				activityNo: '', //活动编号
				userInfo: app.globalData.userInfo, //用户信息
				accountNumber: '', // 商户号
				activityType: '', //活动类型
				activityName: '', //活动类型名称
				activityList: app.globalData.Dictionaries.REBATE_ACTIVITY_TYPE,
				preferentialType: '', //优惠类型
				preferentialName: '', //优惠类型名称
				preferentialList: app.globalData.Dictionaries.REBATE_PREFERENTIAL_TYPE,
				price: '', //优惠金额
				end_date: '', //到期时间
				condition: '', //优惠条件
				partakeType: '', //限制类型
				partakeName: '', //限制类型名称
				partakeList: app.globalData.Dictionaries.REBATE_PARTAKE_TYPE, //限制类型数据
				partakeNum: '', //限制数量
			}
		},
		onLoad(options) {
			this.type = options.type;
			this.accountNumber = options.merNo;
			if (this.type == 2 || this.type == 3) {
				this.activityNo = options.activityno;
				this.getActivityDetail();
			}
		},
		computed: {
			//开始时间转化
			startDate() {
				return this.getDate('start');
			},
			//结束时间转化
			endDate() {
				return this.getDate('end');
			},
			placeholderTip(){
				if(this.preferentialType==3){
					return "折扣比例(10~99),例98折:98"
				}
				return '请输入优惠金额'
			}
		},
		methods: {
			//查询活动详情
			getActivityDetail() {
				let data = {
					activityNo: this.activityNo
				}
				getActivityDetail(data).then(res => {
					if (res.return_code == '000000') {
						this.activityType = res.return_data.activityType;
						for (var i = 0; i < this.activityList.length; i++) {
							if (this.activityType == this.activityList[i].codeValue) {
								this.activityName = this.activityList[i].codeName;
							}
						}
						this.preferentialType = res.return_data.preferentialType;
						for (var j = 0; j < this.preferentialList.length; j++) {
							if (this.preferentialType == this.preferentialList[j].codeValue) {
								this.preferentialName = this.preferentialList[j].codeName;
							}
						}
						this.price = res.return_data.price;
						this.end_date = res.return_data.endTimeStr;
						if (res.return_data.preferentialType == 1) {
							this.condition = res.return_data.condition;
						}
						this.partakeType = res.return_data.restrictPartakeType;
						for (var k = 0; k < this.partakeList.length; k++) {
							if (this.partakeType == this.partakeList[k].codeValue) {
								this.partakeName = this.partakeList[k].codeName;
							}
						}
						this.partakeNum = res.return_data.restrictPartakeNum;
					} else {
						uni.showToast({
							title: res.return_msg,
							icon: 'none',
							duration: 2000
						})
					}
				});
			},
			// 获取当前时间
			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 + 50;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			//选择活动类型
			changefeilvalue(e) {
				this.activityType = this.activityList[e.target.value].codeValue;
				this.activityName = this.activityList[e.target.value].codeName;
			},
			//选择优惠类型
			changefeilvalue1(e) {
				this.preferentialType = this.preferentialList[e.target.value].codeValue;
				this.preferentialName = this.preferentialList[e.target.value].codeName;
				this.condition = '';
			},
			//选择限制类型
			changefeilvalue2(e) {
				this.partakeType = this.partakeList[e.target.value].codeValue;
				this.partakeName = this.partakeList[e.target.value].codeName;
			},
			//结束时间
			bindDateChange: function(e) {
				this.end_date = e.target.value;
			},

			//添加活动
			addActivity() {
				if (!this.activityType) {
					uni.showToast({
						title: '请选择活动类型',
						icon: "none",
						duration: 2000
					});
					return;
				}
				if (!this.preferentialType) {
					uni.showToast({
						title: '请选择优惠类型',
						icon: "none",
						duration: 2000
					});
					return;
				}
				if (!this.price){
					uni.showToast({
						title: '请输入优惠金额',
						icon: "none",
						duration: 2000
					});
					return;
				}
				if(this.preferentialType==3){
					let reg =/^[1-9][0-9]$/; //10-99最低打一折
					if(!reg.test(this.price)){
						this.price="";
						uni.showToast({
							title: '请输入正确的折扣比例,例如99折输入:99,打七折输入:70',
							icon: "none",
							duration: 2000
						});
						return
					}
				}
				if (!this.end_date) {
					uni.showToast({
						title: '请选择结束时间',
						icon: "none",
						duration: 2000
					});
					return;
				}
				if (this.preferentialType == 1 && !this.condition) {
					uni.showToast({
						title: '请输入优惠条件',
						icon: "none",
						duration: 2000
					});
					return;
				}
				if (this.type == 1) {
					uni.showModal({
						title: '提交活动',
						content: '确认信息是否正确。',
						success: (res) => {
							if (res.confirm) {
								uni.showLoading({
									title: '提交中...'
								})
								let b = new Date(this.end_date).getTime();
								let endTime = b + 57599000;
								let datas = {
									"merNo": this.userInfo.mer.merNo,
									"activityType": this.activityType,
									"preferentialType": this.preferentialType,
									"price": this.price,
									"endTime": endTime,
									"condition": this.condition,
									"partakeType": this.partakeType,
									"partakeNum": this.partakeNum
								}
								addActivity(datas).then(res => {
									if (res.return_code == '000000') {
										uni.showToast({
											title: '提交成功',
											icon: 'none',
											duration: 2000
										})
										uni.navigateBack();
									} else {
										uni.showToast({
											title: res.return_msg,
											icon: 'none',
											duration: 2000
										})
									}
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}

				if (this.type == 2) {
					uni.showModal({
						title: '修改活动',
						content: '确认信息是否正确。',
						success: (res) => {
							if (res.confirm) {
								uni.showLoading({
									title: '提交中...'
								})
								let b = new Date(this.end_date).getTime();
								let endTime = b + 57599000;
								let datas = {
									"activityNo": this.activityNo,
									"activityType": this.activityType,
									"preferentialType": this.preferentialType,
									"price": this.price,
									"endTime": endTime,
									"condition": this.condition,
									"partakeType": this.partakeType,
									"partakeNum": this.partakeNum
								}
								updateActivity(datas).then(res => {
									if (res.return_code == '000000') {
										uni.showToast({
											title: '提交成功',
											icon: 'none',
											duration: 2000
										})
										uni.navigateBack();
									} else {
										uni.showToast({
											title: res.return_msg,
											icon: 'none',
											duration: 2000
										})
									}
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
				
				if (this.type == 3) {
					uni.showModal({
						title: '修改活动时间',
						content: '确认信息是否正确。',
						success: (res) => {
							if (res.confirm) {
								uni.showLoading({
									title: '提交中...'
								})
								let b = new Date(this.end_date).getTime();
								let endTime = b + 57599000;
								let datas = {
									"activityNo": this.activityNo,
									"endTime": endTime,
								}
								updateActivityEndTime(datas).then(res => {
									if (res.return_code == '000000') {
										uni.showToast({
											title: '修改成功',
											icon: 'none',
											duration: 2000
										})
										uni.navigateBack();
									} else {
										uni.showToast({
											title: res.return_msg,
											icon: 'none',
											duration: 2000
										})
									}
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.username {
		width: calc(100% - 90upx);
		height: 100upx;
		display: flex;
		align-items: center;
		background-color: rgba($color: #ffffff, $alpha: 0.1);
		border-bottom: 1px solid #f6f6f6;
		padding: 8upx 45upx;

		input {
			width: 50%;
			height: 50upx;
			font-size: 16px;
			color: #333333;
			font-weight: blod;
		}

		.get-code {
			position: absolute;
			display: flex;
			align-items: center;
			justify-content: center;
			right: 7px;
			z-index: 3;
			border: 1px solid #bfbfbf;
			width: 25%;
			padding: 3px 5px;
			border-radius: 22px;

			&:after {
				content: " ";
				width: 1upx;
				height: 50upx;
				position: absolute;
				z-index: 3;
				margin-right: 100%;
				left: 0;
				top: 20upx;
			}
		}

		.namecont {
			color: #666666;
			width: 28%;
		}
	}

	.btn {
		color: #FFFFFF;
		background-color: #0083f5;
		width: 90%;
		margin-left: 5%;
		margin-top: 100rpx;
		margin-bottom: 50rpx;
		height: 90rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 10rpx;
		font-size: 40rpx;
	}
</style>