<template>
  <a-modal
    centered
    class="inventoryQueryDetail-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow=false"
    :width="960">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 合计 -->
      <a-alert type="info" style="margin-bottom:10px">
        <div class="ftext" slot="message">
          可用库存总数量：<strong>{{ currentStock&&(currentStock.currentStockQty || currentStock.currentStockQty==0) ? currentStock.currentStockQty : '--' }}个</strong>；
          <div style="display: inline-block;" v-if="$hasPermissions('M_ShowAllCost')">
            可用库存总成本：<strong>{{ currentStock&&(currentStock.currentStockCost || currentStock.currentStockCost==0) ? toThousands(currentStock.currentStockCost) : '--' }}</strong>；
          </div>
          冻结库存总数量：<strong>{{ currentStock&&(currentStock.freezeQty || currentStock.freezeQty==0) ? currentStock.freezeQty : '--' }}个</strong>；
          <div style="display: inline-block;" v-if="$hasPermissions('M_ShowAllCost')">
            冻结库存总成本：<strong>{{ currentStock&&(currentStock.freezeCost || currentStock.freezeCost==0) ? toThousands(currentStock.freezeCost) : '--' }}</strong>。
          </div>
        </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-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { stockDetailList, stockByProductSn } from '@/api/stock'
export default {
  name: 'InventoryQueryDetailModal',
  components: { STable },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  computed: {
    modalTit () {
      return '库存详情'
    },
    columns () {
      const _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '17%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '17%', align: 'left', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'productOrigCode', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库时间', dataIndex: 'putTime', width: '8%', 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: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '可用库存数量', dataIndex: 'currentQty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '冻结库存数量', dataIndex: 'freezeQty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
        // { title: '成本单价', dataIndex: 'putCost', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
      if (this.$hasPermissions('M_ShowAllCost')) {
        arr.splice(10, 0, { title: '成本单价', dataIndex: 'putCost', width: '7%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      return arr
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return stockDetailList(Object.assign(parameter, { stockSn: this.nowData && this.nowData.stockSn || '', existStockFlag: '1' })).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.nowData && this.nowData.stockSn || '', existStockFlag: '1' }))
          }
          this.spinning = false
          return data
        })
      },
      currentStock: null
    }
  },
  methods: {
    // 合计
    getTotal (param) {
      stockByProductSn({ productSn: this.nowData && this.nowData.productSn || '' }).then(res => {
        if (res.status == 200 && res.data) {
          this.currentStock = res.data
        } else {
          this.currentStock = null
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.currentStock = null
        this.$refs.table.clearTable()
      }
    }
  }
}
</script>

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