<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" size="small" class="form-container">
        <el-form-item class="selectForm-container-item" prop="roleName">
          <el-input v-model.trim="listQuery.keywords" placeholder="类别名称" clearable/>
        </el-form-item>
        <el-button class="filter-item" type="primary" size="small" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button v-if="hasPerm('/ktype/add')" class="filter-item" style="margin-left: 10px;" size="small" type="primary" icon="el-icon-edit" @click="add">新增</el-button>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <tree-table v-loading="listLoading" :data="list" :columns="columns" size="small" rowkey="id" class="treetable" border stripe>
      <el-table-column v-if="showOperate" label="操作" width="250">
        <template slot-scope="scope">
          <el-button v-if="hasPerm('/ktype/edit')&&scope.row.pid!='-1'" type="warning" size="mini" @click="edit(scope.row)">修改</el-button>
          <el-button v-if="hasPerm('/ktype/delete')&&scope.row.pid!='-1'" type="danger" size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </tree-table>

    <!--编辑资源的对话框-->
    <edit-k-type v-show="editShow" ref="editktype" @watchChild="fetchData"/>
    <!--<edit-role v-show="editShow" ref="editrole" @watchChild="fetchData"/>-->

  </div>
</template>

<script>

import treeTable from '@/components/TreeTable'
import { getTypeList, delType } from '@/api/knowledge'
import { toTreeList } from '@/utils/structure'
import EditKType from './editKType'

export default {
  name: 'KTypeManage',
  components: {
    EditKType,
    treeTable
  },
  data() {
    return {
      listQuery: {
        keywords: ''
      },
      columns: [
        {
          text: '类别名称',
          value: 'typeName'
        },
        {
          text: '所属组织机构',
          value: 'deptName',
          width: 150
        }
      ],
      list: [],
      listLoading: true,
      dialogFormVisible: false,
      dialogStatus: '',
      functionShow: false, // 功能权限配置是否启用
      dataShow: false, // 数据权限配置是否启用
      editShow: false // 编辑角色权限是否启用
    }
  },
  computed: {
    'showOperate': function() { // 判断是否显示操作列，如果编辑和删除的权限都没有则不显示操作列
      return (this.hasPerm('/kype/edit') || this.hasPerm('/ktype/delete'))
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    add() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.editShow = true
      this.$refs.editktype.initDialog(this.dialogStatus, true)
    },
    edit(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.editShow = true
      this.$refs.editktype.initDialog(this.dialogStatus, true, row)
    },
    del(row) {
      this.$confirm(
        '确定要删除' + row.typeName + '吗？',
        '确认删除',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delType(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    search() {
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getTypeList(this.listQuery).then(response => {
        // response.data = [
        //   { typeName: '大类', id: '1', pid: '', deptName: '顶级', num: '1' },
        //   { typeName: '小类1', id: '2', pid: '1', deptName: '顶级', num: '1' },
        //   { typeName: '小类2', id: '3', pid: '1', deptName: '顶级', num: '1' }
        // ]
        this.list = toTreeList(response.data)
        this.listLoading = false
      })
    }
  }
}
</script>

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