<template>
    <view class="create">
        <u--textarea v-model="content" count height="200" maxlength="200" placeholder="请输入公告内容"></u--textarea>

        <u-button type="primary" text="发布" :disabled="disabled" @click="onCreate" color="#34CE98"></u-button>
    </view>
</template>

<script>
    import {
        createGroupAnnouncement
    } from '@/apis/interfaces/im.js'

    export default {
        data() {
            return {
                targetId: '',
                content: ''
            }
        },
        onLoad(e) {
            this.targetId = e.targetId
        },
        computed: {
            disabled() {
                return this.content.length == 0 || this.content.length > 200
            }
        },
        methods: {
            onCreate() {
                createGroupAnnouncement(this.targetId, this.content).then(res => {
                    uni.$emit('groupAnnouncementCreated')
                    uni.showToast({
                        title: '发布成功',
                        success: () => {
                            setTimeout(() => {
                                uni.navigateBack()
                            }, 1000)
                        }
                    })
                }).catch(err => {
                    uni.showToast({
                        icon: 'none',
                        title: err.message
                    })
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    .create {
        padding: $padding;

        .u-button {
            margin-top: $padding;
        }
    }
</style>
