<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="图片">
<!--                class="avatar-uploader"-->
                <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-input v-model="form.newsTypeName"></el-input>
            </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 apis from '../../../../config/api'
    export default {
        name: "newsTypeEdiIndex",
        data() {
            return {
                upFileUrl: apis.file.upload.url,
                buildingList: [],
                imageUrl: '',
                form: {
                    newsTypeId: '',
                    newsTypeImg: '',
                    newsTypeName: '',
                    buildingId: ''
                },
                buildingForm: {
                    pageNum: 1,
                    pageSize: 100
                },
            }
        },
        created() {
            this.getBuildList()
            this.form.newsTypeId = this.$route.query.id
            if (this.form.newsTypeId !== undefined) {
                this.getById()
            }
        },
        computed: {
            uploadHeaders () {
                const token = localStorage.getItem('x-token') || ''
                return {
                    Authorization: `Bearer ${token}`
                }
            }
        },
        methods: {
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
                // console.log(res)
                this.form.newsTypeImg = res.data
            },
            beforeImgUpload(file) {
                if (file.type !== "image/jpeg" && file.type !== "image/png") {
                    this.$message.error("上传图片只能是 JPG 或 PNG 格式!");
                    return false;
                }

                this.showLoadding("上传中...");
                return true;
            },
            getBuildList() {
                this.$store.dispatch('building/getBuildings', this.buildingForm).then((res) => {
                    this.buildingList = res.records
                }).catch(() => {
                    console.log('building/getBuildings err')
                })
            },
            submitNewsType() {
                if (this.form.newsTypeId === '' || this.form.newsTypeId === undefined) {
                    this.$store.dispatch('news/addType', 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/addType err')
                    })

                    return;
                }

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

            },
            cancel() {
                this.$router.go(-1)
            },
            getById() {
                this.$store.dispatch('news/getTypeById', this.form).then((res) => {
                    this.form.newsTypeName = res.newsTypeName
                    this.form.newsTypeImg = res.newsTypeImg
                    this.imageUrl = res.newsTypeImg
                    this.form.buildingId = res.buildingId
                }).catch(() => {
                    console.log('news/getTypeById err')
                })
            },
        }
    }
</script>
<style lang="scss" scoped>
.form {
    width: 300px;
    margin-left: auto;
    margin-right: auto;
}
</style>
<style scoped>
    .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>