<template>
  <el-dialog title="移动到" :visible.sync="dialogTreeVisible" width="500px">
    <div class="border">
      <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick" :load="loadNode" lazy highlight-current accordion>
        <div :class="data.class" slot-scope="{ node, data }">
          <div class="flex-align-center">
            <img src="../../../assets/file_icons/folder.png" style="margin-right:10px" width="15" alt="">
            <p>{{ node.label }}</p>
          </div>              
        </div>
      </el-tree>
    </div>
    <el-alert
      v-if="errormsg"
      style="margin-top:10px"
      title="不能将文件移动到自身或其子文件夹下"
      type="error"
      show-icon>
    </el-alert>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogTreeVisible = false" size="small">取 消</el-button>
      <el-button type="primary" @click="changeAttachment" :disabled="errormsg" size="small">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props:['checklist'],
  data () {
    return {
      dialogTreeVisible:false,
      errormsg:false,
      activeid:0,
      data:[],
      children:[],
      defaultProps: {
        children: 'children',
        label: 'document'
      },
      params:{
        "classname": "system.attachment.MediaCenter",
        "method": "queryAttachment",
        "content": {
          "where": {
            "condition":"",//模糊搜索，文件或文件夹名称
            "parentid":0,
            "isonlyfolder":1//传1表示只查询文件夹
          }
        }
      }
    }
  },
  methods:{
     queryAttachment (id) {
      return new Promise(async (reslove,reject)=>{
        this.params.content.where.parentid = id
        const res = await this.$api.requested(this.params)
        if (id === 0) {
          this.data = res.data
          this.data.forEach(e => {
            e.children = []
          });
        } else {
          this.children = res.data
          res.data.forEach(e=>{
            e.isLeaf = true
          })
        }
        reslove()
      })
    },
    async handleNodeClick (val) {
      if (this.checklist[0].attachmentid !== val.attachmentid) {        
        this.activeid = val.attachmentid
        this.errormsg = false
      } else {
        this.errormsg = true
      }
      await this.queryAttachment(val.attachmentid)
    },
    loadNode(node, resolve) {
      if (node.level === 0) {
        return resolve(this.data);
      }
      setTimeout(() => {
        resolve(this.children);
      }, 1000);
    },
    async changeAttachment () {
      let folder = [{
        "attachmentid": this.checklist[0].attachmentid,
        "document": this.checklist[0].document,
        "parentid":this.activeid//文件夹iD
      }]
      const res = await this.$api.requested({
        "classname": "system.attachment.MediaCenter",
        "method": "changeAttachment",
        "content": {
          "files":folder
        }
      })
      if (res.code === 1) {
        this.$emit('onChangeFolderModify')
        this.$message({
          message:'移动成功',
          type:'success'
        })
      }
    }
  },
  mounted () {
  }
}

</script>
<style>
</style>
<style scoped>
.border{
  border:1px solid #ccc
}
</style>