<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        row-key="id"
        cache-key="systemUserTable"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            新建
          </el-button>
        </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)"
          >
            <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>
    <!-- 编辑弹窗 -->
    <list-edit
      :visible.sync="showEdit"
      :data="current"
      @done="reload"
      ref="listEdit"
    />
  </div>
</template>

<script>
  import { getGroupPage , removeItem } from '@/api/material/list';
  import ListEdit from './components/list-edit.vue';
  export default {
    name: 'MaterialList',
    components: {
      ListEdit
    },
    data () {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left',
            label: '序号'
          },
          {
            prop: 'name',
            label: '物料组名称',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'code',
            label: '物料组编码',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'categoryLevelRootName',
            label: '关联分类树',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter:(_row, _column, cellValue) => {
              return _row.categoryLevelRootName+'分类';
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 220,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true

          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false
      };
    },
    methods: {
      /* 表格数据源 */
      datasource ({ page, limit, where, order }) {
        return getGroupPage({ ...where, ...order, pageNum: page, size: limit });
      },
      /* 刷新表格 */
      reload (where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 打开编辑弹窗 */
      openEdit (row) {
        this.current = row;
        this.showEdit = true;
        this.$refs.listEdit.$refs.form &&this.$refs.listEdit.$refs.form.clearValidate();
      },
      /* 删除 */
      remove (row) {
        const loading = this.$loading({ lock: true });
        removeItem([row.id])
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            // this.$message.error(e.message);
          });
      }
    }
  };
</script>
