<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <user-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table ref="table" 
        :columns="columns" 
        :datasource="datasource" 
        :selection.sync="selection" 
        row-key="id" 
        :page-size="this.$store.state.tablePageSize"
        @columns-change="handleColumnChange"
        :cache-key="cacheKeyUrl"
        >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>

          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon"
            @click="openEdit(null)">新增</el-button>

          <el-button size="small" type="primary" icon="el-icon-refresh-left" class="ele-btn-icon" :loading="loading"
            @click="toRefresh()">
            刷新
          </el-button>
        </template>

        <!-- 状态列 -->
        <!--     <template v-slot:status="{ row }">
          {{ checkStatus(row) }}
        </template> -->

        <!-- 操作列 -->
        <template v-slot:action="{ row }">

          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)">
            修改
          </el-link>

          <el-link type="primary" :underline="false" @click="btnView(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>



    <!-- 编辑弹窗 -->
    <user-edit :visible.sync="showEdit" :data="current" @done="reload" ref="userEdit" />


  </div>
</template>

<script>
import tabMixins from '@/mixins/tableColumnsMixin';
import UserSearch from './components/user-search.vue';
import UserEdit from './components/user-edit.vue';
import { pageList, syncVersion,  removeItem } from '@/api/technology/version/version.js';
export default {
  name: 'technologyVersion',
  mixins: [tabMixins],
  components: {
    UserSearch,
    UserEdit
  },
  data() {
    return {
      // 表格列配置
      columns: [
      {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          align: 'center',
          prop: 'code',
          label: '工艺类型编码',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'name',
          label: '工艺类型名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          align: 'center',
          prop: 'version',
          label: '版本号',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          align: 'center',
          prop: 'createTime',
          label: '创建时间',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 220,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false,
      statusList: [
        { label: '草稿', value: -1 },
        { label: '失效', value: 0 },
        { label: '生效', value: 1 }
      ],
      loading: false,
      cacheKeyUrl:"a864b0cd-technology-version"
    };
  },
  methods: {
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      return pageList({ pageNum: page, size: limit, ...where });
    },
    // async datasource({ page, limit, where, order }) {
    //   const res = await pageList({
    //     ...where,
    //     ...order,
    //     pageNum: page,
    //     size: limit
    //   });
    //   return res;
    // },
    /* 点击刷新 */
    toRefresh() {
      this.loading = true;
      syncVersion().then(res => {
        if (res == '0') {
          this.loading = false;
          this.$message.success('数据刷新成功！')
          this.reload()
        }
      })
        .catch((e) => {
          this.loading = false;
        });
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where: where });
    },

    /* 打开编辑弹窗 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
      this.$refs.userEdit.$refs.form &&
        this.$refs.userEdit.$refs.form.clearValidate();
    },

    /* 查看详情 */
    btnView({ id }) {
      this.$router.push({
        path: '/technology/version/details',
        query: { id }
      })
    },

          /* 删除 */
      remove (row) {
        const loading = this.$loading({ lock: true });
        removeItem([row.id ]).then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        });
      },

  }
};
</script>
