<template>
  <app-container>
    <!--筛选条件-->
    <search-area size="small" @search="search">
      <search-item>
        <el-input v-model.trim="listQuery.deptName" size="small" placeholder="组织名称" clearable />
      </search-item>
    </search-area>
    <!--查询结果Table显示-->
    <tree-table v-loading="listLoading" :data="list" :show-index="true" :columns="columns" rowkey="simplename" class="treetable" border stripe>
      <el-table-column v-if="showOperate" label="操作" width="130">
        <template slot-scope="scope">
          <el-button type="warning" size="small" @click="edit(scope.row)">
            设置联系人
          </el-button>
        </template>
      </el-table-column>
    </tree-table>

    <!--编辑责任人的对话框-->
    <edit-responsible-user v-show="dialogFormVisible" ref="editdept" @watchChild="fetchData" />
  </app-container>
</template>

<script>

import treeTable from '@/components/TreeTable'
import { getDeptResponseList } from '@/api/base/configResponse'
import { toTreeList } from '@/utils/structure'
import EditResponsibleUser from './editResponsibleUser'

export default {
  name: 'ListResponsibleUser',
  components: {
    EditResponsibleUser,
    treeTable
  },
  data() {
    return {
      listQuery: {
        deptName: ''
      },
      columns: [
        {
          text: '组织名称',
          value: 'simplename'
        },
        {
          text: '联系人',
          value: 'userName'
        },
        {
          text: '联系电话',
          value: 'phone'
        }
      ],
      list: [],
      listLoading: true,
      dialogFormVisible: false,
      dialogStatus: ''
    }
  },
  computed: {
    'showOperate': function() { // 判断是否显示操作列，如果编辑和删除的权限都没有则不显示操作列
      return this.hasPerm('/sys/dept/update') || this.hasPerm('/sys/dept/delete')
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    add() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$refs.editdept.initDialog(this.dialogStatus, this.dialogFormVisible)
    },
    edit(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      console.log('row:' + row)
      this.$refs.editdept.initDialog(this.dialogFormVisible, row)
    },
    // del(row) {
    //   this.$confirm(
    //     '确定要删除' + row.simplename + '吗？',
    //     '确认删除',
    //     {
    //       confirmButtonText: '确定',
    //       cancelButtonText: '取消',
    //       type: 'warning'
    //     }
    //   ).then(() => {
    //     delDept(row.id).then(response => {
    //       if (response.code === 200) {
    //         this.$message.success('删除成功')
    //         this.fetchData()
    //         // deleteItem(this.list, row)// 前端删除指定项
    //       }
    //     })
    //   })
    // },
    search() {
      this.fetchData()
    },
    fetchData() {
      console.log('fetchData')
      this.listLoading = true
      getDeptResponseList(this.listQuery).then(response => {
        this.list = toTreeList(response.data, '0', false)
        if (this.list[0].id === '0') { // 如果有顶级，去掉
          this.list = this.list[0].children
        }
        this.listLoading = false
      })
    },
    // 在嵌套列表list中删除指定元素
    deleteItem(des) {
      // deleteIftem(this.list, des)
      const del = (list, item) => {
        for (const i in list) {
          if (list[i].id === des.id) {
            list.splice(i, 1)
            return
          } else {
            if (list[i].children && list[i].children.length > 0) {
              del(list[i].children, des)
            }
          }
        }
      }
      del(this.list, des)
    }
    // indexMethod(index) {
    //   return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    // }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .treetable{
    margin-bottom: 50px;
  }
</style>
