<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :default-expand-all="false"
        :need-page="false"
        cache-key="systemMenuTable"
        @done=""
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            添加
          </el-button>
          <el-button class="ele-btn-icon" size="small" @click="expandAll">
            展开全部
          </el-button>
          <el-button class="ele-btn-icon" size="small" @click="foldAll">
            折叠全部
          </el-button>
          <el-button type="primary" size="mini" icon="el-icon-upload2" plain @click="uploadFile">导入</el-button>
        </template>
        <!-- 标题列 -->
        <template v-slot:name="{ row }">
          <i :class="row.icon"></i> {{ row.name }}
        </template>
        <!-- 类型列 -->
        <template v-slot:type="{ row }">
          <el-tag
            type="primary"
            size="mini"
            :disable-transitions="true"
            v-if="row.type == 1"
          >
            菜单
          </el-tag>
          <el-tag
            type="warning"
            size="mini"
            :disable-transitions="true"
            v-if="row.type == 2"
          >
            按钮
          </el-tag>
        </template>
        <template v-slot:status="{ row }">
          <el-tag
            type="danger"
            :disable-transitions="true"
            v-if="row.status == 0"
          >
            关闭
          </el-tag>
          <el-tag
            type="primary"
            :disable-transitions="true"
            v-if="row.status == 1"
          >
            开启
          </el-tag>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            @confirm="remove(row)"
            v-if="row.id != 99999"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <org-edit
      :visible.sync="showEdit"
      :data="editData"
      :parent-id="parentId"
      :organization-list="data"
      @done="reload"
    />

    <importDialog :defModule="moudleName" ref="importDialogRef" @success="reload" />
  </div>
</template>

<script>

import OrgEdit from './components/org-edit.vue';
import importDialog from "@/components/upload/import-dialog.vue";

import {
  listOrganizations,
  removeOrganization
} from '@/api/system/organization';

export default {
  name: 'SystemOrganization',
  components: {OrgEdit, importDialog},
  data() {
    return {
      //模块名称
      moudleName : "mainGroup",
      // 加载状态
      loading: false,
      // 列表数据
      data: [],
      // 选中数据
      current: null,
      // 是否显示表单弹窗
      showEdit: false,
      // 编辑回显数据
      editData: null,
      // 上级id
      parentId: null
    };
  },
  computed: {
    // 表格列配置
    columns() {
      return [
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'name',
          label: '部门名称',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'name'
        },
        {
          prop: '',
          label: '负责人',
          showOverflowTooltip: true,
          align: 'center',
          formatter: (_row) => {
            return _row.manager.map(item => item.name).join(',')
          },
          minWidth: 110,
        },

        {
          prop: 'sort',
          label: '排序',
          showOverflowTooltip: true,
          align: 'center',
          width: 60
        },
        {
          prop: 'status',
          label: '状态',
          showOverflowTooltip: true,
          align: 'center',
          width: 80,
          slot: 'status'
        },
        {
          prop: 'createTime',
          label: '创建时间',
          showOverflowTooltip: true,
          align: 'center',
          minWidth: 110,
          formatter: (_row, _column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 190,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true
        }
      ]
    },
  },
  created() {
    //this.query();
  },
  methods: {
    /* 查询 */
    async datasource() {

      const data = await listOrganizations()
      this.data = this.$util.toTreeData({
        data: data,
        idField: 'id',
        parentIdField: 'parentId'
      });
      return this.data

    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload();
    },
    /* 选择数据 */
    onNodeClick(row) {
      if (row) {
        this.current = row;
        this.parentId = row.id;
        this.$refs.tree.setCurrentKey(row.id);
      } else {
        this.current = null;
        this.parentId = null;
      }
    },
    /* 展开全部 */
    expandAll() {
      this.$refs.table.toggleRowExpansionAll(true);
    },
    /* 折叠全部 */
    foldAll() {
      this.$refs.table.toggleRowExpansionAll(false);
    },
    /* 显示编辑 */
    openEdit(item) {
      this.editData = item;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      this.$confirm('确定要删除选中的机构吗?', '提示', {
        type: 'warning'
      }).then(() => {
          const loading = this.$loading({lock: true});
          removeOrganization([row.id])
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              // this.$message.error(e.message);
            });
        })
        .catch(() => {
        });
    },
    uploadFile () {
      this.$refs.importDialogRef.open();
    }
  }
};
</script>

<style lang="scss" scoped>
.sys-organization-list {
  height: calc(100vh - 264px);
  box-sizing: border-box;
  border-width: 1px;
  border-style: solid;
  overflow: auto;
}

.sys-organization-list :deep(.el-tree-node__content) {
  height: 30px;

  & > .el-tree-node__expand-icon {
    margin-left: 10px;
  }
}
</style>
