<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" :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 config from '../../common/config.js'
	export default {
		data() {
			return {
				action: 'https://tcwm.xianmaxiong.com/sqx_fast/alioss/upload',
				// action: config.APIHOST+'/alioss/upload',
				fileList: [],
				form: {
					couponName: '',
					endDate: '',
					minMoney: '',
					// needIntegral: '',
					money: '',
					shopId: uni.getStorageSync('shopId'),
					couponPicture: '',
				},
			};
		},
		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:'提交中...'
				})
				this.$Request.postJson("/admin/coupon/issueCoupon", this.form).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title:'发布成功'
						})
						setTimeout(()=>{
							uni.navigateBack()
						},1000)
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
					uni.hideLoading()
				});
				
			},
		},
	}
</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>
