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

      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        cache-key="systemRoleTable20"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit('add')"
          >
            新建
          </el-button>
          <!-- <el-button
            size="small"
            type="primary"
            class="ele-btn-icon"
            @click="openclass"
          >
            设置班次
          </el-button> -->
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit('edit', 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>
    <edit ref="edit" @done="done"></edit>
    <setclasses ref="setclasses" @done="done"></setclasses>
  </div>
</template>
<script>
  import edit from './components/edit.vue';
  import setclasses from './components/setclasses.vue';
  import { getteampage, deleteteam } from '@/api/workforceManagement/team';
  import search from './components/search.vue';
  export default {
    components: {
      search,
      edit,
      setclasses
    },
    data() {
      return {
        selection: [],
        columns: [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center',
            fixed: 'left'
          },
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '编码'
          },
          {
            label: '名称',
            prop: 'name'
          },

          {
            label: '所属工厂-厂房',
            prop: 'workshopPlanName'
          },

          {
            label: '所属产线',
            prop: 'productionLineName'
          },

          {
            label: '生产版本',
            prop: 'produceVersionName'
          },
          {
            label: '人数',
            prop: 'userNumber'
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 220,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true,
            fixed: 'right'
          }
        ],
        dict: {
          enabled: {
            1: '生效',
            0: '未生效'
          },
          groupId: []
        }
      };
    },
    methods: {
      datasource({ page, where, limit }) {
        let data = getteampage({
          ...where,
          pageNum: page,
          size: limit
        });
        return data;
      },
      openEdit(type, row) {
        this.$refs.edit.open(type, row);
      },
      remove(row) {
        let par = [row.id];
        deleteteam(par)
          .then((message) => {
            this.$message.success(message);
            this.done();
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },

      done() {
        this.$refs.table.reload({
          page: 1
        });
      },
      openclass() {
        if (this.selection.length <= 0) {
          this.$message.error('请选择班组');
          return;
        }
        let list = this.selection.map((n) => n.id);
        this.$refs.setclasses.open(list);
      }
    }
  };
</script>
