<template>
  <div>
    <el-dialog  :title="title" :visible.sync="showDialog" width="1000px" :close-on-click-modal="false"  :before-close="handleClose" append-to-body>
        <el-form :model="form"  ref="form" label-width="80px" label-position="left">
            <el-form-item label="附图" prop="fileGuid">
                <el-upload ref="upload" action="#" :auto-upload="false" :on-change="change" list-type="picture"
                    :show-file-list="false">
                    <span style="display:flex;flex-direction:row;flex-wrap:wrap;">
                        <span class="avatar" v-if="form.fileGuid"
                            style="margin-left:10px">
                            <span class="deleteImg">
                            <span>
                                <i class="el-icon-zoom-in" @click.stop="handlePictureCardPreview()"></i>
                                <i class="el-icon-delete" @click.stop="handleRemove()"></i>
                            </span>
                            </span>
                            <el-image ref="image" class="img" :src="$commonJS.checkViewer(form.fileGuid)" :preview-src-list="[$commonJS.checkViewer(form.fileGuid)]"></el-image>
                        </span>
                        <i class="el-icon-plus avatar-uploader-icon" v-if="!form.fileGuid"></i>
                    </span>
                </el-upload>
            </el-form-item>
            <el-form-item label="附图标题" prop="figureTitle">
                <el-input v-model="form.figureTitle" placeholder="请输入附图标题"></el-input>
            </el-form-item>
            <el-form-item label="附图说明" prop="description">
                <el-input v-model="form.description" placeholder="请输入附图说明"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" :loading="btnLoading" @click="submit" >确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    projectId:{
        default:0
    }
  },
  data() {
    return {
        title:'',
        showDialog:false,
        form:{},
        btnLoading:false,
    };
  },
  watch: {},
  computed: {},
  created() {},
  mounted() {},
  methods: {
    open(form){
        this.form = JSON.parse(JSON.stringify(form))
        if(this.form.id){
            this.title = '编辑附图'
        }else{
            this.title = '新增附图'
        }
        this.showDialog = true
    },
    handleClose(){
        this.showDialog = false
    },
    submit(){
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.btnLoading = true
            var a = ''
            if(this.form.id){
                a = '编辑'
            }else{
                a = '新增'
            }
            this.form.projectId=this.projectId
            this.$api.updateProjectFigure(this.form).then(response=>{
                if(response.code == 200){
                    this.$message.success(`${a}附图成功`)
                    this.handleClose()
                    this.btnLoading = false
                    this.$emit('save',true)
                }
            }).catch(error=>{
                this.$message.error(`${a}附图失败`)
                this.btnLoading = false
            })
          }
        })
    },
    //文件上传
    change(file,fileList){
        let formData = new FormData()
        formData.append('sourceId',this.$constants.sourceId)
        formData.append('files',file.raw)
        this.$api.uploadFile(formData).then(response=>{
            if(response.code == 200){
                this.$set(this.form,'fileGuid',response.data[0])
            }
        })
    },
    handleRemove() {
        this.$set(this.form,'fileGuid','')
    },
    handlePictureCardPreview() {
        this.$refs.image.showViewer = true
    },
  },
};
</script>
<style lang="scss" scoped>
.avatar-uploader-icon {
  background-color: #fbfdff;
  border: 1px dashed #c0ccda;
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}

.avatar {
  position: relative;
  width: 148px;
  height: 148px;
  display: block;
}

.avatar:hover .deleteImg {
  display: block;
}

.deleteImg {
  display: none;
  font-size: 30px;
  width: 148px;
  height: 148px;
  background-color: black;
  opacity: 0.6;
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  margin: auto;
  z-index: 999;
}

.avatar .img {
  width: 148px;
  height: 148px;
}

.deleteImg span i {
  margin-left: 10px;
  color: #fff;
}

.deleteImg span {
  display: flex;
  align-items: center;
  /*垂直居中*/
  justify-content: center;
  /*水平居中*/
  width: 100%;
  height: 100%;
}
</style>