<template>
  <!-- 新增/编辑架构分类节点 -->
  <div class="classifyNode">
    <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="handleClose" width="600px"
    :append-to-body="true" :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <el-form-item :label="row.type==1?'所属类别名称':'所属产品名称'" prop="productName">
          <el-input v-model="form.productName" disabled placeholder="请输入所属产品名称"></el-input>
        </el-form-item>
        <el-form-item label="所属架构">
          <el-select v-model="form.parent_id" placeholder="请输入所属产品架构" :disabled="this.form.disabled" style="width: 100%;">
            <el-option v-for="item in options" :key="item.id" :label="item.pathName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="架构名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入架构名称"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="">
          <el-upload ref="upload" action="#" :auto-upload="false" :on-change="handleChange" list-type="picture"
            :show-file-list="false">
            <span v-if="form.systemFileList && form.systemFileList.length > 0" class="avatar">
              <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" style="width:100%;height: 100%;"  
                :src="form.systemFileList[0].guid ? $commonJS.checkViewer(form.systemFileList[0].guid) : form.systemFileList[0].url"
                :preview-src-list="form.systemFileList ? form.systemFileList.map(item => { return item.guid ? $commonJS.checkViewer(item.guid) : item.url }) : []"></el-image>
            </span>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="架构说明" prop="description">
          <el-input v-model="form.description" type="textarea" :rows="2" placeholder="请输入架构说明"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    treeDataPath: Array
  },
  data() {
    return {
      row:{},
      // 分类节点弹窗title
      title: '',
      // 控制弹窗显示与隐藏
      dialogVisible: false,
      // 分类节点表单数据
      form: {},
      // 待上传文件或图片的数组
      files: [],
      // 分类节点表单字段校验
      rules: {
        productName: [{ required: true, message: '请输入所属产品名称', trigger: 'blur' },],
        name: [{ required: true, message: '请输入架构名称', trigger: 'blur' },],
      },
      // 所属架构选择的数据源
      options: [{ id: 0, pathName: '/' }],
    }
  },
  mounted() {

  },
  methods: {
    // 打开弹窗,data有值是编辑，productName是产品名称，newData有值是树节点上的新增
    open(data, row, newData) {
      // console.log(data, newData,row);
      this.$set(this,'row',row)
      if (data) {
          this.options=this.treeDataPath
          this.form = JSON.parse(JSON.stringify(data))
          this.title = row.type==1?'编辑类别架构': '编辑产品架构'
          this.$set(this.form, 'parent_id', this.form.parentId)
      } else {
        this.title = row.type==1?'新增类别架构':'新增产品架构'
        if (newData && newData.pathName) {
          this.$set(this.form, 'parent_id', newData.id)
          this.options=this.treeDataPath
        } else {
          this.form.disabled=true
          this.$set(this.form, 'parent_id', 0)
        }
      }
      
      this.form.type=row.type//类型：1产品类别，2产品，3技术分类，4自定义树
      this.form.productName = row.name
      this.form.typeId=row.id
      this.dialogVisible = true
    },
    // 图片变化的change事件
    handleChange(file, fileList) {
      this.$set(this.form, 'systemFileList', [{ url: file.url }])
      this.files = [file.raw]
    },
    // 点击已上传的文件//showViewer是图片显示查看器
    handlePictureCardPreview(file) {
      this.$refs.image.showViewer = true
    },
    // 文件列表移除文件
    handleRemove(file) {
      this.files = []
      this.form.systemFileList = []//直接清空适用于单张图片
    },
    // 弹窗确定提交 
    submit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          let formData = new FormData()
          if (this.files && this.files.length > 0) {
            formData.append('files', this.files[0])
          }
          formData.append('treeNode', JSON.stringify(this.form))
          if (this.form.id) {// 编辑
            this.$api.updateTreeNode(formData).then(res => {
              if (res.code == 200) {
                this.$emit('isSuccess', '更新成功')
                this.$message.success('更新成功')
                this.handleClose()
              }
            }).catch(err => {
              this.$message.error(err.message)
            })
          } else {//新增
            this.$api.addTreeNode(formData).then(res => {
              if (res.code == 200) {
                this.$emit('isSuccess', '新增成功')
                this.$message.success('新增成功')
                this.handleClose()
              } 
            }).catch(err => {
              this.$message.error(err.message)
            })
          }
        }
      })
    },
    // 关闭
    handleClose() {
      this.$refs.ruleForm.resetFields()
      this.form = {}
      this.dialogVisible = false
    },
  },
}
</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;

  .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;
  }

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

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

.avatar:hover .deleteImg {
  display: block;
}
</style>