<template>
<div class="transaction">
  <div v-show="$route.name === 'transaction'">
    <Query
      class="transaction__query"
      :selectedIds="selectedIds"
      :queryParams="queryParams"
      :queryType="queryType"
      @getList="getList"
    ></Query>
    <List
      :currentNo="currentNo"
      :pageSize="pageSize"
      :list="transactionList"
      :total="total"
      @selectionChange="cacheSelectedIds"
      @getList="getList"
    ></List>
  </div>
  <router-view></router-view>
</div>
</template>

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

export default {
  name: 'Transaction',
  components: { Query, List },
  mounted () {
    !this.queryParams && this.getList()
  },
  computed: {
    queryParams () {
      return this.$route.params.queryParams
    },
    queryType () {
      return this.$route.params.type
    }
  },
  watch: {
    queryParams: {
      immediate: true,
      handler (val) {
        (val && this.$route.name === 'transaction') && this.getList(val)
      }
    },
  },

  data () {
    return {
      query: {
        'page.currentPage': 1,
        'page.limitCount': 10,
      }, // 查询条件
      // queryParams: {}, // 路由传递的查询条件
      // queryType: '', // 路由传递的查询类型
      transactionList: [], // 盘点列表
      total: 0, // 总数据量
      selectedIds: '', // 选中的id字符串序列 (','隔开)
      currentNo: 1,
      pageSize: 10
    }
  },
  methods: {
    // 获取盘点列表
    getList (query = {}) {
      const url = this.$API.inventoryQuery.transaction.getList
      const param = { ...this.query, ...query }
      param.quickCondition !== undefined && (param.quickCondition = `%${param.quickCondition}%`) // 避免传空值只能查到部分数据
      this.$https.post(url, param)
        .then(data => {
          this.transactionList = data.rows
          this.total = data.total
          this.currentNo = data.currentPage
          this.pageSize = data.limit
        })
    },
    // 勾选变化, 缓存id
    cacheSelectedIds (ids) {
      this.selectedIds = ids
    }
  },
}
</script>

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