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

      <center-list
        :tableData="tableList"
        :currentPage="currentPage"
        :pageSize="pageSize"
        :total="total"
        @getList="getList"
        @handleSelectChange="cacheSelectedIds"
        @handleOpenEditForm="handleOpenEditForm"
        @handleOpenDetail="handleOpenDetail">
      </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: 'PreReceiving',
  components: {
    HeadTodo,
    CenterList
  },
  created () {
    this.getList()
  },
  mounted () {

  },
  data () {
    return {
      tableList: [], // 预收货列表数据
      total: 0, // 查询列表数据总条数
      selectedIds: '', // checkbox选中的id字符串序列
      mode: 'create',
      query: {
        'page.currentPage': 1,
        'page.limitCount': 10
      },
      currentPage: 1,
      pageSize: 10,
    }
  },
  methods: {

    // 查询列表
    getList (query) {
      const url = this.$API.stockIn.preReceiving.getList
      const param = { ...this.query, ...query }
      this.$https.post(url, param)
        .then(res => {
          this.tableList = res.rows
          this.total = res.total
          this.currentPage = res.currentPage
          this.pageSize = res.limit
        })
    },

    //  新增
    handleOpenForm (mode) {
      this.mode = mode
      this.$router.push({
        name: 'preReceivingAdd',
        params: {
          mode
        }
      })
    },

    // 编辑
    handleOpenEditForm (mode, headData, carData, goodsData, page, limit, total) {
      this.mode = mode
      this.$router.push({
        name: 'preReceivingAdd',
        params: {
          headData,
          carData,
          goodsData,
          page,
          limit,
          total,
          mode
        }
      })
    },

    handleOpenDetail (mode, ...arg) {
      this.mode = mode
      this.$router.push({
        name: 'preReceivingAdd',
        params: {
          mode,
          ...arg
        }
      })
    },

    // checkbox勾选id储存
    cacheSelectedIds (ids) {
      this.selectedIds = ids
      console.log('勾选ids：', ids)
    },

  },
  // 路由跳回来前判断是否需要刷新列表
  beforeRouteUpdate (to, from, next) {
    if (to.meta.needRefresh) {
      this.getList()
      delete to.meta.needRefresh
    }
    next()
  },
}
</script>
<style lang="scss">
    .mrg-b{
        margin-bottom: 20px;
    }
    .mrg-l{
        margin-left:10px;
    }
</style>
