<template>
  <div v-loading="isLoading" style="max-height: 400px; overflow: auto;">
    <el-table :data="list" ref="goodsTable" style="width: 100%" @selection-change="chooseGoods">
      <el-table-column type="selection"></el-table-column>
      <el-table-column type="index" label="序号" show-overflow-tooltip/>
      <el-table-column prop="acceptanceNumber" label="验收单编号" show-overflow-tooltip/>
      <el-table-column prop="acceptanceGoodsNumber" label="验收单货物编号" show-overflow-tooltip/>
      <el-table-column prop="goodsTotalNum" label="货物数量" show-overflow-tooltip/>
      <el-table-column prop="expectedSettlementDate" label="预估结算日期" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.expectedSettlementDate ? scope.row.expectedSettlementDate.split(' ')[0] : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="acceptanceStatusDict.name" label="验收状态" show-overflow-tooltip/>
    </el-table>
    <div class="choose-good-page">
      <el-pagination
        :page-sizes="[10, 20, 30, 50]"
        :page-size="size"
        :total="total"
        :current-page="page"
        layout="slot, total, sizes, prev, pager, next, jumper"
        background
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'chooseGoods',
  data() {
    return {
      list: [],
      page: 1,
      size: 10,
      total: 0,
      isLoading: false,
      selectedInfos: []
    }
  },
  props: ["info", "originList"],
  mounted() {
    this.fetchData()
  },
  methods: {
    // 获取货物信息列表
    fetchData() {
      this.isLoading = true
      request({
        url: '/bill/getAcceptanceGoods',
        method: 'post',
        data: { 
          page: this.page,
          rows: this.size,
          condition: this.info.businessNumber, 
          conditionLong: this.info.supplierId 
        }
      }).then(({ data: { rows, records } }) => {
        this.isLoading = false
        this.list = rows ? rows.map(item => {
          item.goodsTotalNum = item.acceptanceGoodInfos.reduce((t, i) => {
            t = parseFloat(Number(t) + Number(i.goodsQuantity)).toFixed(4)
            return t
          }, 0)
          return item
        }) : []
        this.total = records
        this.$nextTick(() => {
          if(this.info.source == 'jiesuan') {
            this.list.forEach(item => {
              if(this.originList.some(i => i.id == item.acceptanceGoodsNumber) || this.selectedInfos.some(i => i.acceptanceGoodsNumber == item.acceptanceGoodsNumber)) {
                this.$refs.goodsTable.toggleRowSelection(item)
              }
            })
          } else {
            this.list.forEach(item => {
              if(this.originList.some(i => i.id == item.id) || this.selectedInfos.some(i => i.id == item.id)) {
                this.$refs.goodsTable.toggleRowSelection(item)
              }
            })
          }
        })
      })
    },
    handleCurrentChange(val) {
      this.page = val
      this.fetchData()
    },
    handleSizeChange(val) {
      this.size = val
      this.page = 1
      this.fetchData()
    },
    chooseGoods(val) {
      this.selectedInfos = [ ...val ]
    }
  }
}
</script>

<style lang="scss">
.choose-good-page {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
  padding-bottom: 10px;
}
</style>