<style lang="less">

</style>

<template>
    <div>
        <Row>
            <Col span="18">
                <Card>
                    <Form :label-width="80">
                        <FormItem label="标题">
                            <Input v-model="form.title" icon="android-list"/>
                        </FormItem>
                        <FormItem label="状态">
                            <Select v-model="form.status" style="width: 150px">
                                <Option :value="0">进行中</Option>
                                <Option :value="1">结束</Option>
                            </Select>
                        </FormItem>
                    </Form>
                    <div class="margin-top-20">
                        <editor :editor-content="form.content"></editor>
                    </div>
                    <div class="submit">
                        <Button @click="handlePublish" :loading="publishLoading" icon="ios-checkmark" size="large" type="primary">发布</Button>
                    </div>
                </Card>
            </Col>
        </Row>
    </div>
</template>

<script>
import tinymce from 'tinymce';
import editor from '../main-components/editor'
export default {
    name: 'edit',
    components: {
        editor
    },
    props: {
        formContent: Object
    },
    watch: {
        formContent() {
            if (!this.formContent.status) {
                this.formContent.status = 0;
            }
            this.form = this.formContent;
        }
    },
    data () {
        return {
            form: this.formContent || {},
            publishLoading: false
        };
    },
    methods: {
        handlePublish() {
            let content = tinymce.activeEditor.getContent();
            if (this.form.title && content) {
                this.publishLoading = true;
                let data = {
                    title: this.form.title,
                    status: this.form.status,
                    content: content
                };
                if (this.form.id) {
                    data.id = this.form.id;
                }
                this.axios.post("/campaign/store", data).then((res) => {
                    if (res.data.status == 0) {
                        this.$Message.success('操作成功！');
                        this.publishLoading = false;
                        setTimeout(() => {
                            this.$emit("pub-success");
                        }, 1000);
                    }
                });
            } else {
                this.$Message.error('请完善所有信息！');
                this.publishLoading = false;
            }
        }
    }
};
</script>
