<template>
    <div>
      <div v-show="$route.name === 'libraryGoods'">
        <head-todo
        :selectedIds="selectedIds"
        :skuCodes="skuCodes"
        :rows="rows"
        @getList="getList"
        @handleOpenForm="handleOpenForm">
        </head-todo>

        <center-list
            :tableData="tableList"
            :currentPage="currentPage"
            :pageSize="pageSize"
            :total="total"
            @handleSelectChange="cacheSelectedIds"
            @getList="getList"
            @handleOpenForm="handleOpenForm">
        </center-list>
      </div>
      <router-view></router-view>
    </div>
</template>
<script>
import HeadTodo from './components/head-todo'
import CenterList from './components/center-list'
export default {
  name: 'LibraryGoods',
  components: {
    HeadTodo,
    CenterList
  },
  data () {
    return {
      tableList: [],
      total: 0,
      query: {
        'page.currentPage': 1,
        'page.limitCount': 10,
      },
      selectedIds: '',
      mode: 'create',
      skuCodes: [],
      rows: [],
      pageSize: 10,
      currentPage: 1,
    }
  },
  created () {
    this.getList()
  },
  mounted () {

  },
  methods: {
    getList (query) {
      const url = this.$API.warehouseBasic.libraryGoods.getList
      const param = { ...this.query, ...query }
      this.$https.post(url, param).then(res => {
        this.tableList = res.rows
        this.total = res.total
        this.pageSize = res.limit
        this.currentPage = res.currentPage
      })
    },

    handleOpenForm (mode, ...args) {
      this.mode = mode
      this.$router.push({
        name: 'libraryGoodsAdd',
        params: {
          mode,
          ...args
        }
      })
    },

    // checkbox勾选储存id
    cacheSelectedIds (ids, skuCodes, rows) {
      this.selectedIds = ids
      this.skuCodes = skuCodes
      this.rows = rows
    }
  },
  // 路由跳回来前判断是否需要刷新列表
  beforeRouteUpdate (to, from, next) {
    if (to.meta.needRefresh) {
      this.getList()
      delete to.meta.needRefresh
    }
    next()
  },
}
</script>
