<template>
  <a-modal
    centered
    class="inventoryQueryDetail-modal"
    :footer="null"
    :maskClosable="false"
    title="库存详情"
    v-model="isShow"
    @cancle="isShow=false"
    :width="960">
    <!-- 合计 -->
    <a-alert type="info" style="margin-bottom:10px">
      <div class="ftext" slot="message">
        现有库存总数量（个）：<strong>{{ stockQty }}</strong>；
        <span v-if="$hasPermissions('B_isShowCost')">现有库存总成本(￥)：<strong>{{ stockCost }}</strong>。</span>
      </div>
    </a-alert>
    <!-- 库存详情 -->
    <s-table
      v-if="openModal"
      class="sTable"
      ref="table"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ x: 1580 }"
      bordered>
      <!-- 库存数量 -->
      <template slot="currentQty" slot-scope="text, record">
        {{ (record.currentQty + record.freezeQty) || 0 }}
        <span v-if="record.freezeQty">（冻结{{ record.freezeQty }}）</span>
      </template>
    </s-table>
    <div class="btn-cont">
      <a-button id="inventoryQueryDetail-detail-modal-back" @click="isShow = false">返回列表</a-button>
    </div>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import { stockDetailList, stockDetailCount } from '@/api/stock'
export default {
  name: 'InventoryQueryDetailModal',
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemId: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return stockDetailList(Object.assign(parameter, { stockSn: this.itemId })).then(res => {
          const data = res.data
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.disabled = false
          // 总计
          this.getTotal(Object.assign(parameter, { stockSn: this.itemId }))
          return data
        })
      },
      currentStock: { // 合计信息
        currentQty: '',
        putCost: ''
      }
    }
  },
  computed: {
    stockQty () { //  库存总数量 合计
      const currentStockQty = Number(this.currentStock.currentStockQty) || 0
      const freezeStockQty = Number(this.currentStock.freezeStockQty) || 0
      return currentStockQty + freezeStockQty
    },
    stockCost () { //  库存总成本 合计
      const currentStockCost = Number(this.currentStock.currentStockCost) || 0
      const freezeStockCost = Number(this.currentStock.freezeStockCost) || 0
      return ((currentStockCost * 1000 + freezeStockCost * 1000) / 1000).toFixed(2)
    },
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'productCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库时间', dataIndex: 'putTime', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: 140, align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: 140, align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '入库类型', dataIndex: 'putBizTypeDictValue', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '库存数量', scopedSlots: { customRender: 'currentQty' }, width: 180, align: 'center' }
      ]
      if (this.$hasPermissions('B_isShowCost')) {
        arr.splice(9, 0, { title: '成本单价', dataIndex: 'putCost', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } })
      }
      return arr
    }
  },
  methods: {
    // 合计
    getTotal (param) {
      stockDetailCount(param).then(res => {
        if (res.status == 200 && res.data) {
          this.currentStock = res.data
        } else {
          this.currentStock = { // 合计信息
            currentQty: '',
            putCost: ''
          }
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>

<style lang="less">
  .inventoryQueryDetail-modal{
    .btn-cont {
      text-align: center;
      margin: 5px 0 10px;
    }
  }
</style>
