<template>
  <div>
    <en-table-layout :tableData="tableData.data" :loading="loading">
      <template slot="table-columns">
        <el-table-column prop="create_time" label="变动日期">
          <template slot-scope="{ row }">{{ row.create_time | unixToDate }}</template>
        </el-table-column>
        <el-table-column prop="warehouse_entry_sn" label="入库单号" />
        <el-table-column prop="source_type" label="变动类型" :formatter="filterSourceType" />
        <el-table-column prop="source_sn" label="相关单号" />
        <el-table-column prop="change_num" label="变动数量" />
        <el-table-column prop="batch_remain_num" label="库存数量" />
      </template>

      <el-pagination v-if="tableData" slot="pagination" @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange" :current-page="tableData.page_no" :page-sizes="[10, 20, 50, 100]"
        :page-size="tableData.page_size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.data_total">
      </el-pagination>
    </en-table-layout>
  </div>
</template>

<script>
import * as API_Statistics from '@/api/statistics'
export default {
  name: 'statisticsOfGoods',
  data() {
    return {
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 高级搜索数据
      advancedForm: {},
      // 列表数据
      tableData: '',
      dialogVisible: false
    }
  },
  watch: {
    $route: {
      immediate: true,
      handler(newVal) {
        if (newVal.name === 'statisticsOfGoods' && newVal.params.warehouse_entry_sn) {
          this.params.page_no = 1
          this.params.warehouse_entry_sn = newVal.params.warehouse_entry_sn
          this.params.product_id = newVal.params.product_id
          this.getStockBatchFlow()
        }
      }
    }
  },
  methods: {
    filterSourceType(row) {
      switch (row.source_type) {
        case 'WAREHOUSE_ENTRY':
          return '采购入库'
        case 'WAREHOUSE_OUT':
          return '出库'
        case 'SUPPLIER_RETURN':
          return '供应商退货'
        case 'ORDER_RETURN':
          return '订单退货'
        case 'STOCK_TRANSFER':
          return '库存调报'
        case 'STOCK_DAMAGE':
          return '库存报损'
        case 'CHANGE_FORM':
          return '换货'
      }
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.getStockBatchFlow()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.getStockBatchFlow()
    },

    /** 获取列表 */
    getStockBatchFlow() {
      this.loading = true
      API_Statistics.getStockBatchFlow(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
</style>
