<template>
<div class="block">
  <div v-show="$route.name === 'block' || $route.name === 'viewBlock'">
    <Query
      class="block__query"
      :selectedIds="selectedIds"
      :queryParams="queryParams"
      @getList="getList"
      @toShowCreator="toShowCreator"
    ></Query>
    <List
      :currentNo="currentNo"
      :pageSize="pageSize"
      :list="blockList"
      :total="total"
      @getList="getList"
      @selectionChange="cacheSelectedIds"
      @toShowCreator="toShowCreator"
    ></List>
    <Creator
      :showCreator="showCreator"
      :mode="mode"
      :detailData="detailData"
      @close="close"
      @createDone="getList"
    ></Creator>
  </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: 'Block',
  mixins: [ routerMixins ],
  components: { Query, List, Creator },
  mounted () {
    !this.queryParams && this.getList()
  },
  computed: {
    queryParams () {
      return this.$route.params.queryParams
    },
  },
  watch: {
    queryParams: {
      immediate: true,
      handler (val) {
        (val && this.$route.name === 'block') && this.getList(val)
      }
    },
  },

  data () {
    return {
      query: {
        'page.currentPage': 1,
        'page.limitCount': 10,
      }, // 查询条件
      // queryParams: {}, // 路由查询参数
      blockList: [], // 盘点列表
      total: 0, // 总数据量
      selectedIds: '', // 选中的id字符串序列 (','隔开)
      showCreator: false, // 不显示
      mode: 'create', // 新增弹窗的模式
      detailData: {}, // 详情信息
      currentNo: 1,
      pageSize: 10
    }
  },
  methods: {
    // 获取盘点列表
    getList (query = {}) {
      const url = this.$API.inventory.block.getList
      const param = { ...this.query, ...query }
      param.holdId !== undefined && (param.holdId = `%${param.holdId}%`) // 避免传空值只能查到部分数据
      this.$https.post(url, param)
        .then(data => {
          this.blockList = data.rows
          this.total = data.total
          this.currentNo = data.currentPage
          this.pageSize = data.limit || this.query['page.limitCount']
        })
    },
    // 勾选变化, 缓存id
    cacheSelectedIds (ids) {
      this.selectedIds = ids
    },
    // 显示新增表单
    toShowCreator (mode = 'create', detailData = {}) {
      this.showCreator = true
      this.mode = mode
      this.detailData = detailData
    },
    close () {
      this.showCreator = false
    }
  },
}
</script>

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