<template>
  <!-- 数据表格 -->
  <ele-pro-table
    ref="table"
    :columns="columns"
    :datasource="datasource"
    height="calc(100vh - 265px)"
    full-height="calc(100vh - 116px)"
    tool-class="ele-toolbar-form"
    cache-key="systemOrgUserTable"
    :selection.sync="selection"
  >
    <template v-slot:name="{ row }">
      <span class="listName">
        <img src="../../../assets/code1.png" v-if="row.type == 1" />
        <img src="../../../assets/code2.png" v-if="row.type == 2" />
        {{ row.name }}
      </span>
    </template>
    <template v-slot:toolbar>
      <el-dropdown trigger="click">
        <el-button type="primary">
          新建<i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="openType('add')"
            >新建分类</el-dropdown-item
          >
          <el-dropdown-item
            @click.native="openEdit('add')"
            v-if="parentData.parentId != 0"
            >新建编码</el-dropdown-item
          >
        </el-dropdown-menu>
      </el-dropdown>
      <el-button
        type="primary"
        @click.native="openEdit('edit')"
        :disabled="selection.length != 1"
      >
        编辑
      </el-button>
      <el-popconfirm
        class="ele-action"
        title="确定要删除吗？"
        @confirm="remove()"
      >
        <template v-slot:reference>
          <el-button type="danger" :disabled="selection.length == 0">
            删除
          </el-button>
        </template>
      </el-popconfirm>
      <!-- <el-button type="primary"> 启用 </el-button>
      <el-button type="primary"> 停用 </el-button> -->
    </template>
    <TypeEdit
      ref="typeEditRef"
      :parentId="parentData.id"
      @done="done"
    ></TypeEdit>
    <codeEdit
      ref="codeEditRef"
      :parentId="parentData.id"
      @done="done"
    ></codeEdit>
  </ele-pro-table>
</template>
   
  <script>
import codeEdit from './components/codeEdit.vue';
import TypeEdit from './components/typeEdit.vue';

import { listParentId,removeCode } from '@/api/businessCode';
export default {
  components: {
    codeEdit,
    TypeEdit
  },
  props: {
    // 上级
    parentData: {
      type: Object,
      default: () => {}
    }
  },

  data() {
    return {
      selection: [],
      // 表格列配置
      columns: [
        {
          width: 45,
          type: 'selection',
          columnKey: 'selection',
          align: 'center'
          // selectable: (row, index) => {
          //   return !this.disabledTableList
          //     .map((item) => item.id)
          //     .includes(row.id);
          // }
        },
        {
          label: '名称',
          prop: 'name',
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left',
          slot: 'name'
        },
        {
          prop: 'remark',
          label: '描述',
          align: 'center',
          showOverflowTooltip: true
        },

        {
          prop: 'createUserName',
          label: '创建人',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'updateUserName',
          label: '修改人',
          align: 'center',
          showOverflowTooltip: true
        },
        {
          prop: 'updateTime',
          label: '修改时间',
          align: 'center',
          showOverflowTooltip: true
        },

        {
          prop: '',
          label: '状态',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          formatter: (_row, _column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        }
      ],
      // 是否显示编辑弹窗
      showEditFlag: false
    };
  },
  created() {},

  methods: {
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      return listParentId({
        ...where,
        pageNum: page,
        size: limit,
        parentId: this.parentData?.id
      });
    },
    /* 刷新表格 */
    reload(where) {
      this.$nextTick(() => {
        this.$refs.table.reload({ pageNum: 1, where: where });
      });
    },

    /* 显示编辑 */
    openEdit(type, row = '') {
      if (type == 'edit' && this.selection[0].type == 1) {
        this.openType(type, this.selection[0]);
        return;
      }
      this.$refs.codeEditRef.open(type, this.selection[0]);
    },
    openType(type, row = '') {
      this.$refs.typeEditRef.open(type, row);
    },

    // async success(current) {
    //   await this.reload();
    //   this.current = current;
    //   this.$nextTick(() => {
    //     this.$refs.PowerRef &&
    //       this.$refs.PowerRef.setTableList(this.current.userAuthority);
    //   });
    // },

    /* 删除 */
    remove(row) {
      removeCode(this.selection.map((item) => item.id))
        .then((msg) => {
          this.$message.success('操作成功');
          this.$emit('done', this.parentData);
        })
        .catch((e) => {
          // this.$message.error(e.message);
        });
    },
    done() {
      this.$emit('done', this.parentData);
    },
    getTableList() {
      return JSON.parse(JSON.stringify(this.selection));
    }
  }
};
</script>

<style lang="scss" scoped>
.listName {
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    margin-right: 3px;
  }
}
</style>