<template>
    <div id="root">
        <el-form ref="form" :model="form" label-width="80px" class="form">
            <el-form-item label="所属项目">
                <el-select v-model="form.buildingId" placeholder="请选择">
                    <el-option
                            v-for="item in buildingList"
                            :key="item.buildingId"
                            :label="item.buildingName"
                            :value="item.buildingId">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="资讯图片">
                <el-upload
                        class="avatar-uploader"
                        :headers="uploadHeaders"
                        :action="upFileUrl"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="海报图片">
                <el-upload
                        class="avatar-uploader"
                        :headers="uploadHeaders"
                        :action="upFileUrl"
                        :show-file-list="false"
                        :on-success="shareImageHandleAvatarSuccess">
                    <img v-if="shareImageUrl" :src="shareImageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="资讯标题">
                <el-input v-model="form.newsName"></el-input>
            </el-form-item>
            <el-form-item label="资讯类型">
                <el-select v-model="form.newsTypeId" placeholder="请选择">
                    <el-option
                            v-for="item in typeList"
                            :key="item.newsTypeId"
                            :label="item.newsTypeName"
                            :value="item.newsTypeId">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="资讯详情">
                <rich-editor v-model="form.newsDetail" style="height: 400px; width: 1000px;" />
            </el-form-item>
            <el-form-item label="状态">
                <el-select v-model="form.newsStatus" placeholder="请选择">
                    <el-option
                            v-for="item in optionNewsStatus"
                            :key="item.newsStatus"
                            :label="item.name"
                            :value="item.newsStatus">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitNewsType">保存</el-button>
                <el-button @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import myWangeditor from '../../../components/wangeditor'
    import apis from '../../../config/api'
    export default {
        name: "newsEdiIndex",
        data() {
            return {
                upFileUrl: apis.file.upload.url,
                imageUrl: '', // 资讯图预览
                shareImageUrl: '', // 海报图预览
                optionNewsStatus: [
                    {
                        name: '已发布',
                        newsStatus: 0
                    },
                    {
                        name: '未发布',
                        newsStatus: 1
                    }
                ],
                form: {
                    newsId: '',
                    newsTypeId: '',
                    newsImg: '',
                    shareImg: '',
                    newsName: '',
                    buildingId: '',
                    newsStatus: '',
                    newsDetail: ''
                },
                buildingList: [],
                buildingForm: {
                    pageNum: 1,
                    pageSize: 100
                },
                typeList: [],
                typeForm: {
                    buildingId: '',
                    pageNum: 1,
                    pageSize: 100
                },

            }
        },
        components: {
            // eslint-disable-next-line vue/no-unused-components
            'my-wangeditor': myWangeditor
        },
        created() {
            this.getBuildList()
            this.getTypeList()
            this.form.newsId = this.$route.query.id
            if (this.form.newsId !== undefined) {
                this.getById()
            }
        },
        mounted () {

        },
        computed: {
            uploadHeaders () {
                const token = localStorage.getItem('x-token') || ''
                return {
                    Authorization: `Bearer ${token}`
                }
            }
        },
        methods: {
            getEdiCentent(value) {
                console.log('富文本值：', value)
                this.form.newsDetail = value
            },
            handleAvatarSuccess(res, file) { // 资讯图
                this.imageUrl = URL.createObjectURL(file.raw);
                // console.log(res)
                this.form.newsImg = res.data
            },
            shareImageHandleAvatarSuccess(res, file) { // 海报图
                this.shareImageUrl = URL.createObjectURL(file.raw);
                // console.log(res)
                this.form.shareImg = res.data
            },
            getTypeList() {
                this.$store.dispatch('news/getTypeList', this.typeForm).then((res) => {
                    this.typeList = res.records
                }).catch((err) => {
                    this.$notify.error(err.msg || err.message);
                    console.log('news/getTypeList err')
                })
            },
            getBuildList() {
                this.$store.dispatch('building/getBuildings', this.buildingForm).then((res) => {
                    this.buildingList = res.records
                }).catch((err) => {
                    this.$notify.error(err.msg || err.message);
                    console.log('building/getBuildings err')
                })
            },
            submitNewsType() {
                if (this.form.newsId === '' || this.form.newsId === undefined) {
                    this.$store.dispatch('news/add', this.form).then((res) => {
                        this.$message.success('操作成功！')
                        this.$router.go(-1)
                        // console.log(res)
                    }).catch((err) => {
                        this.$notify.error(err.msg || err.message);
                        console.log('news/add err')
                    })

                    return;
                }

                // 修改
                this.$store.dispatch('news/update', this.form).then((res) => {
                    this.$message.success('操作成功！')
                    this.$router.go(-1)
                    // console.log(res)
                }).catch((err) => {
                    this.$notify.error(err.msg || err.message);
                    console.log('news/update err')
                })

            },
            cancel() {
                this.$router.go(-1)
            },
            getById() {
                this.$store.dispatch('news/getById', this.form).then((res) => {
                    console.log('news/getById: ', res)
                    this.form.newsName = res.newsName
                    this.form.newsTypeId = res.newsTypeId
                    this.form.newsStatus = res.newsStatus
                    this.form.newsImg = res.newsImg
                    this.form.shareImg = res.shareImg
                    this.shareImageUrl = res.shareImg
                    this.imageUrl = res.newsImg
                    this.form.buildingId = res.buildingId
                    this.form.newsDetail = res.newsDetail
                }).catch((err) => {
                    this.$notify.error(err.msg || err.message);
                    console.log('news/getById err')
                })
            }
        }
    }
</script>

<style lang="scss">
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>