<template>
	<view>
		<u-navbar
			title="群公告"
			placeholder
			:autoBack="true"
		>
			<view class="u-nav-slot" slot="right" v-if="isOwner || isAdmin">
				<u-button type="primary" size="mini" @click="save">保存</u-button>
			</view>
		</u-navbar>
		<u-parse v-if="!isOwner && !isAdmin" :content="announcement"></u-parse>
		<u--textarea v-else 
			count 
			confirmType="done" 
			focus 
			autoHeight 
			height="500"
			maxlength="-1" 
			border="none" 
			:adjustPosition="false"
			class="textarea"
			placeholder="暂无公告"
			v-model="announcement"
			>
		</u--textarea>
	</view>
</template>

<script>
	import {mapGetters} from "vuex";
	import IMSDK,{GroupMemberRole,} from "openim-uniapp-polyfill";
	export default {
		computed: {
			...mapGetters([
				"storeCurrentConversation",
				"storeCurrentMemberInGroup",
				"storeCurrentGroup",
			]),
			isOwner() {
				return this.storeCurrentMemberInGroup.roleLevel === GroupMemberRole.Owner;
			},
			isAdmin() {
				return this.storeCurrentMemberInGroup.roleLevel === GroupMemberRole.Admin;
			},
		},
		data() {
			return {
				announcement:"",
			}
		},
		onLoad() {
			this.announcement = this.storeCurrentGroup.notification;
		},
		methods: {
			back(){
				uni.navigateBack();
			},
			save(){
				if(!this.isOwner && !this.isAdmin){
					return ;
				}
				IMSDK.asyncApi(IMSDK.IMMethods.SetGroupInfo, IMSDK.uuid(), {
					groupID: this.storeCurrentGroup.groupID,
					notification: this.announcement
				}).then(res=>{
					console.log(res);
					uni.navigateBack();
				}).catch(e=>{
					console.log(e);
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.textarea{
	}
</style>