<template>
	<view class="container">
		<u-cell-group class="form-info" :border="false">
			<u-field label="优惠券名称" placeholder="请输入优惠券名称" label-width="180" v-model="couponInfo.couponName"></u-field>
			<u-field label="条件金额" placeholder="请输入条件金额" label-width="180" v-model="couponInfo.fullAmount"></u-field>
			<u-field label="减免金额" placeholder="请输入减免金额" label-width="180" v-model="couponInfo.discountAmount"></u-field>
			<u-field label="有效期（天）" placeholder="请输入有效期（天）" label-width="180" v-model="couponInfo.term"></u-field>
		</u-cell-group>
		<view class="form-handle">
			<u-button type="success" shape="circle" :ripple="true" @click="submitData" :disabled="getPermit()" class="handle-custom">发放</u-button>
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		data() {
			return {
				couponInfo: {
					couponName: '',
					fullAmount: '',
					discountAmount: '',
					term: '',
				},
			}
		},
		onLoad() {},
		methods: {
			//  检查必填项
			getPermit() {
				if (!this.couponInfo.couponName || !this.couponInfo.fullAmount || !this.couponInfo.discountAmount || !this.couponInfo
					.term) {
					return true
				}
				return false
			},
			//  提交
			submitData() {
				NET.request(API.addCoupon, {
					...this.couponInfo,
				}, 'POST').then(res => {
					this.$refs.uTips.show({
						title: '发放优惠券成功',
						type: 'success',
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				}).catch(error => {
					this.$refs.uTips.show({
						title: error.data.msg,
						type: 'warning',
					})
				})
			},
		},
	}
</script>

<style lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;
		float: left;
		box-sizing: border-box;
		padding-bottom: 70px;
		overflow-y: auto;
		position: absolute;

		.form-info {
			width: 100%;
			float: left;

			/deep/.u-label-text {
				color: #333333;
			}

			/deep/.u-cell_title {
				color: #333333;
			}
		}

		.form-handle {
			width: calc(100% - 30px);
			height: 40px;
			position: fixed;
			bottom: 20px;
			left: 15px;
			background-color: #FFFFFF;

			.handle-custom {
				background-color: #51A539;

				/deep/button {
					background-color: #56a83a;
				}

				/deep/.u-btn--success--disabled {
					background-color: #74bd60 !important;
				}
			}
		}
	}
</style>
