<template>
  <a-modal
    centered
    class="inventoryQueryDetail-modal"
    :footer="null"
    :maskClosable="false"
    title="库存详情"
    v-model="isShow"
    @cancel="isShow=false"
    :width="960">
    <!-- 合计 -->
    <a-alert type="info" style="margin-bottom:10px">
      <div class="ftext" slot="message">
        可用库存总数量（个）：<strong>{{ (currentStock&&(currentStock.currentStockQty || currentStock.currentStockQty==0)) ? currentStock.currentStockQty : '--' }}</strong>；
        <span v-if="$hasPermissions('B_isShowCost')">可用库存总成本(￥)：<strong>{{ (currentStock&&(currentStock.currentStockCost || currentStock.currentStockCost==0)) ? currentStock.currentStockCost : '--' }}</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="{ y: 500 }"
      bordered>
    </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 => {
          let data
          if (res.status == 200) {
            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: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', align: 'center', width: '31%', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库时间', dataIndex: 'putTime', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: '6%', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: '6%', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '入库类型', dataIndex: 'putBizTypeDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '库存数量', dataIndex: 'currentQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
      if (this.$hasPermissions('B_isShowCost')) { // 成本价权限
        arr.splice(9, 0, { title: '成本单价', dataIndex: 'putCost', width: '6%', 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: ''
          }
        }
      })
    },
    resetPage () {
      this.currentStock = { // 合计信息
        currentQty: '',
        putCost: ''
      }
      this.$refs.table.clearTable()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
      this.resetPage()
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>

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