<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-form-item class="selectForm-container-item" prop="deptName">
          <el-input v-model.trim="listQuery.deptName" placeholder="Dept Name" clearable/>
        </el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">Search</el-button>
        <el-button v-if="hasPerm('/dept/add')" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="add">Add</el-button>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <tree-table v-loading="listLoading" :data="list" :columns="columns" rowkey="simplename" class="treetable" border stripe>
      <el-table-column v-if="showOperate" label="Operation" width="120">
        <template slot-scope="scope">
          <el-button v-if="hasPerm('/dept/update')" type="warning" size="mini" @click="edit(scope.row)">Edit</el-button>
          <el-button v-if="hasPerm('/dept/delete')" type="danger" size="mini" @click="del(scope.row)">Delete</el-button>
        </template>
      </el-table-column>
    </tree-table>

    <!--编辑资源的对话框-->
    <edit-dept v-show="dialogFormVisible" ref="editdept" @watchChild="fetchData"/>
  </div>
</template>

<script>

import treeTable from '@/components/TreeTable'
import editDept from '@/views/system/dept/editdept'
import { getDeptList, delDept } from '@/api/dept'
import { toTreeList } from '@/utils/structure'

export default {
  name: 'ListDept',
  components: {
    treeTable,
    editDept
  },
  data() {
    return {
      listQuery: {
        deptName: ''
      },
      columns: [
        {
          text: 'Short name',
          value: 'simplename'
        },
        {
          text: 'Full name',
          value: 'fullname'
        },
        {
          text: 'Note',
          value: 'tips'
        },
        {
          text: 'Order',
          value: 'num',
          width: 80
        }
      ],
      list: [],
      listLoading: true,
      dialogFormVisible: false,
      dialogStatus: ''
    }
  },
  computed: {
    'showOperate': function() { // 判断是否显示操作列，如果编辑和删除的权限都没有则不显示操作列
      return this.hasPerm('/dept/update') || this.hasPerm('/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.dialogStatus, this.dialogFormVisible, row)
    },
    del(row) {
      this.$confirm(
        'Are you sure to delete department ' + row.simplename + '？',
        'Confirm to Delete',
        {
          confirmButtonText: 'OK',
          cancelButtonText: 'Cancel',
          type: 'warning'
        }
      ).then(() => {
        delDept(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('Delete Success')
            this.fetchData()
            // deleteItem(this.list, row)// 前端删除指定项
          }
        })
      })
    },
    search() {
      this.fetchData()
    },
    fetchData() {
      console.log('fetchData')
      this.listLoading = true
      getDeptList(this.listQuery).then(response => {
        this.list = toTreeList(response.data.list, '0', false)
        this.listLoading = false
      })
    },
    // 在嵌套列表list中删除指定元素
    deleteItem(des) {
      // deleteItem(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)
    }
  }
}
</script>

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