<template>
	<view>
		<view class="from flex justify-center">
			<view class="from-box flex justify-center">
				<view class="from-box-c">
					<u-form :model="form" label-position="top" ref="uForm">
						<u-form-item label="优惠券名称">
							<u-input v-model="form.couponName" placeholder="请输入优惠券名称" />
						</u-form-item>
						<u-form-item label="有效期天数">
							<u-input v-model="form.endDate" type="number" placeholder="请选择有效期天数" />
						</u-form-item>
						<u-form-item label="可使用订单最低金额">
							<u-input v-model="form.minMoney" type="number" placeholder="请输入可使用订单最低金额" />
						</u-form-item>
						<!-- <u-form-item label="所需积分数">
							<u-input v-model="form.needIntegral" type="number" placeholder="请输入所需积分数" />
						</u-form-item> -->
						<u-form-item label="优惠券金额">
							<u-input v-model="form.money" type="number" placeholder="请输入优惠券金额" />
						</u-form-item>
						<u-form-item label="优惠券图片">
							<u-upload ref="uUpload" :file-list="fileList" :multiple="false" max-count="1" :action="action" @on-remove="onRemove" @on-change="onChange"></u-upload>
						</u-form-item>

					</u-form>
				</view>

			</view>
		</view>
		<!-- 保存 -->
		<view class="button flex justify-center align-center">
			<view class="button-box flex justify-center align-center" @click="sumbit()">
				保存
			</view>
		</view>

	</view>
</template>

<script>
	import configdata from '@/common/config.js';
	export default {
		data() {
			return {
				action: '',
				// action: config.APIHOST+'/alioss/upload',
				fileList: [],
				form: {
					couponId:'',
					couponName: '',
					endDate: '',
					minMoney: '',
					// needIntegral: '',
					money: '',
					shopId: uni.getStorageSync('shopId'),
					couponPicture: '',
				},
			};
		},
		onShow() {
			let that = this
			that.action=that.config("APIHOST1") + '/admin/new-file/upload'
		},
		onLoad(option) {
			let info = JSON.parse(option.info)
			this.fileList = [
				{url:info.couponPicture}
			]
			this.form = {
				couponId:info.couponId,
				couponName: info.couponName,
				endDate: info.endDate,
				minMoney: info.minMoney,
				// needIntegral: info.needIntegral,
				money: info.money,
				shopId: uni.getStorageSync('shopId'),
				couponPicture: info.couponPicture,
			},
			console.log(this.form,this.form.couponName)
		},
		methods: {
			onRemove(e){
				this.form.couponPicture = ''
			},
			onChange(e){
				
				if(e.data){
					let obj = JSON.parse(e.data)
					if(obj.code!=0){
						uni.showToast({
							title:'上传失败，请重新上传!',
							icon:'none'
						})
						
						this.$refs.uUpload.clear()
					}else{
						this.form.couponPicture = obj.data
					}
					
				}else{
					uni.showToast({
						title:'上传失败，请重新上传!',
						icon:'none'
					})
					this.$refs.uUpload.clear()
				}
			},
			//提交
			sumbit(){
				if(!this.form.couponName){
					uni.showToast({
						title:'请输入优惠券名称',
						icon:'none'
					})
					return
				}
				if(!this.form.endDate){
					uni.showToast({
						title:'请输入有效期天数',
						icon:'none'
					})
					return
				}
				if(!this.form.minMoney){
					uni.showToast({
						title:'请输入可使用订单最低金额',
						icon:'none'
					})
					return
				}
				// if(!this.form.needIntegral){
				// 	uni.showToast({
				// 		title:'请输入所需积分数',
				// 		icon:'none'
				// 	})
				// 	return
				// }
				if(!this.form.money){
					uni.showToast({
						title:'请输入优惠券金额',
						icon:'none'
					})
					return
				}
				if(!this.form.couponPicture){
					uni.showToast({
						title:'请上传优惠券图片',
						icon:'none'
					})
					return
				}
				uni.showLoading({
					title:'提交中...',
					mask: true, // 是否显示透明蒙层，防止触摸穿透
				})
				this.$Request.postJson("/admin/coupon/updateCoupon", this.form).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title:'修改成功'
						})
						setTimeout(()=>{
							uni.navigateBack()
						},1000)
					}else{
						uni.showModal({
						  title: '提示',
						  content: res.msg,
						  success: function (res) {
						    if (res.confirm) {
						    } else if (res.cancel) {
						    }
						  }
						});
					}
					uni.hideLoading()
				});
				
			},
			config: function(name) {
				var info = null;
				if (name) {
					var name2 = name.split("."); //字符分割
					if (name2.length > 1) {
						info = configdata[name2[0]][name2[1]] || null;
					} else {
						info = configdata[name] || null;
					}
					if (info == null) {
						let web_config = cache.get("web_config");
						if (web_config) {
							if (name2.length > 1) {
								info = web_config[name2[0]][name2[1]] || null;
							} else {
								info = web_config[name] || null;
							}
						}
					}
				}
				return info;
			},
		},
	}
</script>

<style lang="scss">
	.from {
		width: 100%;
		height: auto;
		margin-top: 30rpx;

		.from-box {
			width: 686rpx;
			height: 100%;
			border-radius: 16rpx;
			background-color: #ffffff;

			.from-box-c {
				width: 646rpx;
			}
		}
	}

	.button {
		width: 100%;
		height: 120rpx;
		background-color: rgb(241, 241, 241);
		position: fixed;
		bottom: 0;
		z-index: 999;

		.button-box {
			width: 686rpx;
			height: 80rpx;
			border-radius: 40rpx;
			background-color: #FCD202;
		}
	}
</style>
