<template>
    <div class="edit-view">
        <div class="title">{{ formData.title }}</div>
        <div class="time">发布时间：{{ formData.createdAt }}</div>
        <div class="content" v-html="formData.content"></div>
    </div>
</template>
<script>
export default {
    name: 'AnnouncementEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('announcement/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {}
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            let data = { ...this.formData };

            this.saving = true;
            this.$http
                .post('/announcement/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/announcement/del/${this.formData.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        console.log(e);
                        this.$message.error((e || {}).error || '删除失败');
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped>
.title {
    font-size: 24px;
    text-align: center;
    padding: 20px;
    font-weight: bold;
}
.time {
    font-size: 12px;
    text-align: right;
    margin-right: 50px;
    padding: 10px;
}

.content {
    padding: 20px 50px;
}
</style>
