<template>
	<view class="teamJoin white_style">
		<Nav className="nav_F6" :backVisible="true" :scroll="scroll">
			<block slot="content" class="omit">{{ $t('me.teamJoin.title') }}</block>
		</Nav>
		<view class="body_scroll clearfix" :style="getHeight">
			<view class="teamJoin_form">
				<van-cell-group>
				  <van-field
				    :value="form.applyMessage"
						:show-confirm-bar="false"
				    type="textarea"
						@input="bindInput"
				    placeholder="请输入"
				    :autosize=" { minHeight: 90 }"/>
				</van-cell-group>
			</view>
			<view class="btn" @click="joinGroup">发送</view>
		</view>
	</view>
</template>

<script>
	import mixins from '@/utils/mixins'
	import sdkService from '@/api/sdk_service'

	export default {
		mixins: [mixins],
		data() {
			return {
				form: {
					applyUser: uni.getStorageSync('uid'),
					applyMessage: ''
				}
			}
		},
		computed: {
			getHeight() {
				return `padding-top: ${this.CustomBar}px;`;
			}
		},
		onLoad (e) {
			if (e) {
				this.form.teamId = e.id
				this.form.groupID = e.groupID
				this.form.applyMessage =  `我是${e.name}，希望加入小队`
			}
		},
		methods: {
			// 跳转
			link (path) {
				uni.navigateTo({
					url: path
				})
			},
			// 输入
			bindInput (e) {
				this.form.applyMessage = e.detail
			},
			// 加入群组
			joinGroup () {
				sdkService.joinGroup({
					groupID: this.form.groupID,
					type: uni.$TUIKitTypes.GRP_PUBLIC,
					applyMessage: this.form.applyMessage
				}, res => {
					switch (res.status) {
						case uni.$TUIKitTypes.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
							this.$base.Tips('等待管理员同意')
							this.save()
						break;
						
						case uni.$TUIKitTypes.JOIN_STATUS_SUCCESS: // 加群成功
							this.$base.Tips('加群成功')
							this.save()
						break;
						
						case uni.$TUIKitTypes.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
							this.$base.Tips('已经在群中')
						break;
					}
				})
			},
			// 保存
			async save () {
				this.loading = true
				const result = await this.$axios.post(this.API.teamApply, this.form)
				this.loading = false
				if (result) {
					uni.navigateBack({
						delta: 1
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.teamJoin {
		min-height: calc(100vh);
		.body_scroll {
			.teamJoin_form {
				border-radius: 18rpx;
				background: $white;
				overflow: hidden;
				margin: 26rpx;
			}
			.btn {
				background-color: $white;
				margin: 118% auto 0 auto;
				width: 368rpx;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 12rpx;
				font-size: 32rpx;
				font-weight: 500;
				color: $YCB760;
			}
		}
	}
</style>
