<template>
  <a-modal
    centered
    class="inventoryQueryDetail-modal"
    :footer="null"
    :maskClosable="false"
    title="库存详情"
    v-model="isShow"
    @cancel="isShow=false"
    width="80%">
    <!-- 合计 -->
    <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_inventoryQuery_detail_costPrice')">可用库存总成本：<strong>{{ (currentStock&&(currentStock.currentStockCost || currentStock.currentStockCost==0)) ? toThousands(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>
      <template slot="currentQty" slot-scope="text, record">
        <span style="vertical-align: middle;margin-right:5px;"> {{ (record.currentQty ||record.currentQty==0)?record.currentQty:'--' }}</span>
        <a-tooltip placement="bottom" v-if="record.lockFlag==1" >
          <template slot="title">
            采购入库的库存为锁定库存
          </template>
          <a-badge count="锁" v-if="record.lockFlag==1" :number-style="{ zoom:'80%' }"></a-badge>
        </a-tooltip>

      </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 { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { stockDetailList, stockDetailCount } from '@/api/stock'
export default {
  name: 'InventoryQueryDetailModal',
  mixins: [commonMixin],
  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
              data.list[i].productOrigCode = data.list[i].productOrigCode.trim()
            }
            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 _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '10%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', align: 'left', width: '22%', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: '10%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '入库时间', dataIndex: 'putTime', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: '10%', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: '10%', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '入库类型', dataIndex: 'putBizTypeDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '库存数量', scopedSlots: { customRender: 'currentQty' }, dataIndex: 'currentQty', width: '6%', align: 'center' }
      ]
      if (this.$hasPermissions('B_inventoryQuery_detail_costPrice')) { // 成本价权限
        arr.splice(9, 0, { title: '成本单价', dataIndex: 'putCost', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(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
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.resetPage()
        this.$emit('close')
        this.$store.state.app.curActionPermission = ''
      }
    }
  }
}
</script>

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