<template>
  <a-modal
    centered
    class="chainStockReportDetail-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancle="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.totalQty) || 0 }} </strong>；
          可用库存总成本(￥)：<strong> {{ (currentStock && currentStock.totalCost) || 0 }} </strong>。
        </div>
      </a-alert>
      <!-- 详情 -->
      <s-table
        v-if="openModal"
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        bordered>
      </s-table>
      <div class="btn-cont">
        <a-button id="chainStockReportDetail-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 { reportStockDetailList, reportStockDetailCount } from '@/api/reportStock'
export default {
  name: 'InventoryQueryDetailModal',
  mixins: [commonMixin],
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    productSn: {
      type: [Number, String],
      default: ''
    },
    stockSn: {
      type: [Number, String],
      default: ''
    }
  },
  computed: {
    modalTit () {
      return '详情'
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      // 表头
      columns: [
        { title: '产品编码', dataIndex: 'productEntity.code', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productEntity.name', width: '15%', align: 'left', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'productEntity.origCode', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库时间', dataIndex: 'putTime', width: '14%', 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: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '成本单价', dataIndex: 'putCost', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        const params = Object.assign(parameter, { productSn: this.productSn, stockSn: this.stockSn })
        return reportStockDetailList(params).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.getCount(params)
          }
          this.spinning = false
          return data
        })
      },
      currentStock: null
    }
  },
  methods: {
    // 合计
    getCount (param) {
      reportStockDetailCount(param).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')
      }
    }
  }
}
</script>

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