<template>
<div class="list">
  <el-table
    ref="table"
    stripe
    border
    :max-height="tableMaxHeight"
    :data="list"
    @selection-change="selectionChange"
    @row-click="toCheck"
  >
    <table-column type="selection" fixed align="center"></table-column>
    <table-column label="规则编码" prop="ruleCode">
      <template slot-scope="scope">
        <span class="detail-no" @click.stop="toDetail(scope.row.ruleCode)">{{scope.row.ruleCode}}</span>
      </template>
    </table-column>
    <table-column label="规则名称" prop="ruleName" ></table-column>
    <table-column label="质检类型" prop="qcType">
      <template slot-scope="scope">
        {{ $utils.keyMatcher(scope.row.qcType, $keys.strategy.qcRules.qcType).name }}
      </template>
    </table-column>
    <table-column label="备注" prop="remark" ></table-column>
    <table-column label="操作" width="150" fixed="right">
      <template slot-scope="scope">
        <el-button
          v-auth="50410090107"
          title="编辑"
          size="mini"
          type="primary"
          icon="el-icon-edit"
          plain
          circle
          @click.stop="toEdit(scope.row)"
        ></el-button>
        <el-button
          v-auth="50410090108"
          title="删除"
          size="mini"
          type="danger"
          icon="el-icon-delete"
          plain
          circle
          @click.stop="toDelete(scope.row)"
        ></el-button>
      </template>
    </table-column>
  </el-table>
  <el-pagination
    class="pagination"
    @size-change="sizeChange"
    @current-change="currentChange"
    :current-page="currentNo"
    :page-sizes="[10, 20, 30, 40, 50]"
    :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
  ></el-pagination>
</div>
</template>

<script>
export default {
  props: ['list', 'total', 'currentNo', 'pageSize'],
  methods: {
    // 点击表格行
    toCheck (row) {
      this.$refs.table.toggleRowSelection(row)
    },
    // 页码变化
    currentChange (currentNo) {
      const query = { 'page.currentPage': currentNo, 'page.limitCount': this.pageSize }
      this.$emit('getList', query)
    },
    // 每页条数变化
    sizeChange (pageSize) {
      const query = { 'page.limitCount': pageSize, 'page.currentPage': this.currentNo }
      this.$emit('getList', query)
    },
    // 编辑
    toEdit (row) {
      this.$emit('toCreator', row.ruleCode, 'edit')
    },
    // 删除
    toDelete (row) {
      this.$confirm(`确定要删除吗 ?`)
        .then(() => {
          const url = this.$API.strategy.qcRules.delete
          const param = {
            ruleCodes: row.ruleCode,
          }
          this.$https.post(url, param)
            .then(data => {
              this.$emit('getList')
            })
        })
        .catch(e => console.log(e))
    },
    // 勾选变化
    selectionChange (selection) {
      console.log(selection)
      const selectedIds = selection.map(item => item.ruleCode).join(',')
      this.$emit('selectionChange', selectedIds)
    },
    // 前往详情
    toDetail (ruleCode) {
      this.$emit('toCreator', ruleCode)
    },
  },
}
</script>

<style lang="scss" scoped>
.pagination {
  margin-top: 20px;
}
.detail-no {
  color: #0682ea;
  &:hover {
    cursor: pointer;
  }
}
</style>
