<template>
	<view class="content">
		<view class="card">
			<view class="item">
				<view class="lable">商品兑换券名称</view>
				<view class="value"> 
					<up-input v-model="formData.title" placeholder="填写名称" border="none" clearable ></up-input>	
				</view>
			</view>
			<view class="item">
				<view class="lable">使用门槛</view>
				<view class="value"> 
					<view>满</view><input v-model="formData.fullAmount"  @change="formData.fullAmount = $utils.isMoney(formData.fullAmount)" type="digit" placeholder="填写金额" border="none"></input><view>元，可用</view>
				</view>
			</view>
			<view class="item">
				<view class="lable">总发放数量</view>
				<view class="value"> 
					<up-input v-model="formData.number" @change="formData.number = $utils.isNumber(formData.number)" type="number" placeholder="填写数量" border="none" clearable ></up-input>	
				</view>
			</view>
		</view>
		<view class="card">
		
			<view class="item">
				<view class="lable">指定抵扣商品</view>
				<view class="value"> 
					<view class="selectGoods" @tap="selectGoodsOpen">
						<view>
							<view class="title">选择商品</view>
							<view class="goodsName" v-if='!formData.proId'>选择指定商品</view>
							<view class="goodsName" v-else>
								<view class="goodsItem" >
									<view class="productName">{{formData.proName}}</view>
									<!-- <up-input @tap.stop="stop" style="border-bottom: 1rpx solid #666;" v-model="item.num" placeholder="填写数量" border="none"  ></up-input>	 -->
								</view>
							</view>
						</view>
						<up-icon name="arrow-right" color="#9F9F9F" size="22"></up-icon>
					</view>
				</view>
			</view>
		
			
		</view>
	
		<view class="bottomPop">
			<button @click="save">保存</button>
		</view>
		
		<select-goods ref="goods" @affirm="affirm"></select-goods>
	</view>
</template>
<script setup>
	import { reactive, ref } from 'vue';
	import { onLoad, onShow  } from '@dcloudio/uni-app';
	import go from '@/commons/utils/go.js'
	import selectGoods from './components/select-goods';
	import { addCoupon, updateCoupon } from '@/http/api/coupon.js'
	const pageData = reactive({
		id: null,
		title: "",
		show: false,
	})
	let formData = reactive({
		title: "",
		type: '2',
		fullAmount: null,
		number: null,
		proId: null,
		proName: null,
		products: []
	})
	
	onLoad((options) => {
		if ( options.type == 'info' ) {
			let itemData = JSON.parse(options.item);
			for (let item in itemData) {
			    formData[item] = itemData[item]
			}
			formData.userDays = formData.userDays.split(",");
		}
	})
	
	let goods = ref(null)
	let selectGoodsOpen = () => {
		goods.value.open();
	}

	let stop = () => { }
	
	let affirm = (item) => {
		formData.proId = item.id
		formData.proName = item.name
	}
	
	/**
	 * 保存
	 */
	let save = () => {
		if( !formData.title ){
			uni.$utils.showToast("请输入商品名称")
			return false;
		}
		if( !formData.fullAmount ){
			uni.$utils.showToast("请输入使用门槛")
			return false;
		}
		if( !formData.number ){
			uni.$utils.showToast("请输入发放数量")
			return false;
		}
		let params = {
			...formData
		}
		if(formData.id){
			updateCoupon(params).then((res) => {
				uni.$utils.showToast("操作成功",1000)
				setTimeout(()=>{
					go.back(1)
				},1000)
			})
		} else{
			addCoupon(params).then((res) => {
				uni.$utils.showToast("操作成功",1000)
				setTimeout(()=>{
					go.back(1)
				},1000)
			})
		}
	}
	
</script>

<style lang="scss">
	
	.content {
		background: #F9F9F9;
		padding: 32rpx 28rpx 150rpx 28rpx;

		.card{
			padding: 32rpx 24rpx;
			background-color: #fff;
			margin-bottom: 32rpx;
			.item{
				padding-bottom: 24rpx;
				border-bottom: 2rpx solid #E5E5E5;
				margin-bottom: 24rpx;
				.lable{
					font-weight: bold;
					font-size: 28rpx;
					color: #333333;
					margin-bottom: 16rpx;
					display: flex;
					align-items: center;
				}
				.value{
					display: flex;
					justify-content: flex-start;
					align-items: center;
					input{
						width: 150rpx;
						text-align: center;
					}
					.selectGoods{
						width: 100%;
						display: flex;
						justify-content: space-between;
						align-items: center;
						padding: 32rpx 16rpx;
						background: #F9F9F9;
						border-radius: 12rpx 12rpx 12rpx 12rpx;
						.title{
							font-weight: bold;
							font-size: 28rpx;
							color: #333333;
							margin-bottom: 16rpx;
						}
						.goodsName{
							font-weight: 400;
							font-size: 24rpx;
							color: #999999;
						}
						.goodsItem{
							display: flex;
							align-items: center;
							.productName{
								margin-right: 20rpx;
							}
						}
					}
				}
			}
			.item:last-child{
				border-bottom: none;
				margin-bottom: 0;
			}
		}
		.bottomPop{
			position: fixed;
			bottom: 0;
			left: 0;
			height: 150rpx;
			width: 100%;
			background-color: #fff;
			>button {
				width: 530rpx;
				margin: 30rpx 0;
				margin-left: 50%;
				transform: translateX(-50%);
				height: 80rpx;
				line-height: 80rpx;
				color: #fff;
				background: #318AFE;
				border-radius: 56rpx 56rpx 56rpx 56rpx;
			}
		}
	}
</style>