<template>
	<view class="content">

		<uni-forms :modelValue="formData">
			<view class="content1">
				<uni-forms-item label="圈子名称" name="name">
					<uni-easyinput type="text" v-model="formData.circleName" placeholder="输入圈子名称" />
				</uni-forms-item>
			</view>
			<view class="content2">
				<view class="title">
					圈子标签
				</view>
				<view class="">
					<uni-forms-item :label="'标签'+(index+1)" name="labelName" v-for="(item,index) in labelList"
						:key="index">
						<view class="flex">
							<uni-easyinput type="text" v-model="item.labelName" placeholder="输入圈子标签,2-4个字" />
							<uni-icons type="plus-filled" @click="add(item)" size="40"></uni-icons>
							<uni-icons type="minus-filled" @click="subtract(index)" size="40"></uni-icons>
						</view>
					</uni-forms-item>
				</view>
			</view>
		</uni-forms>

		<view class="">
			<button type="primary" @click="submit">提交</button>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	var that;
	export default {
		data() {
			return {
				labelList: [{
						labelName: ''
					},
					{
						labelName: ''
					}
				],
				formData: {
					circleName: '',
					circleLabel: [],
					commonId: ''

				}
			};
		},
		onLoad() {
			that = this
			this.userInfo = uni.getStorageSync("userInfo")
			this.formData.commonId = this.userInfo.id
		},
		methods: {
			submit() {
				this.formData.circleLabel = []
				if (!this.formData.circleName) {
					this.$refs.uToast.show({
						type: 'error',
						message: '圈子名称不能为空！',
					})
					return
				}
				for (let i = 0; i < this.labelList.length; i++) {
					if (!this.labelList[i].labelName) {
						this.$refs.uToast.show({
							type: 'error',
							message: '标签内容不能为空！',
						})
						return
					}
					if (this.labelList[i].labelName.length > 4 || this.labelList[i].labelName.length < 2) {
						this.$refs.uToast.show({
							type: 'error',
							message: '标签内容2-4个字！',
						})
						return
					}
					this.formData.circleLabel.push(this.labelList[i].labelName)
				}
				this.formData.circleLabel = this.formData.circleLabel.toString()

				this.$request.baseRequest('admin.unimall.circleManagementInfo', 'add', {
					circleManagementInfo: JSON.stringify(this.formData)
				}, failres => {
					this.$refs.uToast.show({
						type: 'error',
						message: failres.errmsg,
					})
					uni.hideLoading()
				}).then(res => {
					this.$refs.uToast.show({
						type: 'success',
						message: '提交成功！',
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 500)
				})
			},
			add() {
				if (this.labelList.length > 3) {
					this.$refs.uToast.show({
						type: 'error',
						message: '“标签数量2-4个！',
					})
					return
				}
				this.labelList.push({
					labelName: ''
				})
			},
			subtract(index) {
				if (this.labelList.length < 3) {
					this.$refs.uToast.show({
						type: 'error',
						message: '“标签数量2-4个！',
					})
					return
				}
				this.labelList.splice(index, 1)
			},
		}
	}
</script>

<style lang="scss">

</style>