<template>
<div class="qcRules">
  <div v-show="$route.name === 'qcRules'">
    <Query
      class="qc-rules__query"
      :selectedIds="selectedIds"
      @getList="getList"
    ></Query>
    <List
      :list="qcRulesList"
      :total="total"
      :currentNo="currentNo"
      :pageSize="pageSize"
      @getList="getList"
      @selectionChange="cacheSelectedIds"
      @toCreator="toCreator"
    ></List>
  </div>
  <router-view></router-view>
</div>
</template>

<script>
import Query from './components/Query'
import List from './components/List'
import Creator from './components/Creator'
import routerMixins from '../routerMixins'

export default {
  name: 'QCRules',
  mixins: [routerMixins],
  components: { Query, List, Creator },
  created () {
  },
  mounted () {
    this.getList()
  },

  data () {
    return {
      query: {
        'page.currentPage': 1,
        'page.limitCount': 20,
      }, // 查询条件
      qcRulesList: [], // 质检规则列表
      total: 0, // 总数据量
      currentNo: 1,
      pageSize: 20,
      selectedIds: '', // 选中的id字符串序列 (','隔开)

      detailData: {}, // 详情数据
      mode: 'create', // 新增框模式
    }
  },
  methods: {
    // 获取质检规则列表
    getList (query = {}) {
      const url = this.$API.strategy.qcRules.getList
      const param = { ...this.query, ...query }
      param.ruleCode !== undefined && (param.ruleCode = `%${param.ruleCode}%`) // 避免传空值只能查到部分数据
      this.$https.post(url, param)
        .then(data => {
          this.qcRulesList = data.rows
          this.total = data.total
          this.currentNo = data.currentPage
          this.pageSize = data.limit
        })
    },
    // 勾选变化, 缓存id
    cacheSelectedIds (ids) {
      this.selectedIds = ids
    },
    // 编辑 或 详情
    toCreator (ruleCode, mode = 'detail') {
      const url = this.$API.strategy.qcRules.detail
      const param = { ruleCode }
      this.$https.post(url, param)
        .then(data => {
          this.$router.push({
            name: 'qcRulesCreator',
            params: {
              detailData: data,
              mode,
            }
          })
        })
    }
  },
}
</script>

<style lang="scss" scoped>
.qc-rules__query {
  margin-bottom: 20px;
}
</style>
