<template>
  <a-modal
    centered
    class="chainStockReportDetail-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancle="isShow=false"
    :width="960">
    <!-- 合计 -->
    <a-alert type="info" showIcon style="margin-bottom:15px">
      <div class="ftext" slot="message">现有库存总数量（个）：
        <strong>
          <!-- {{ currentStock.currentQty || 0 }} -->207
        </strong>；现有库存总成本(￥)：
        <strong>
          <!-- {{ currentStock.putCost || 0 }} -->1550.5
        </strong>。</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="chainStockReportDetail-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: ''
    }
  },
  computed: {
    modalTit () {
      return '详情'
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      // 表头
      columns: [
        { 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' },
        { title: '成本单价', dataIndex: 'putCost', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 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
        // })
        const _this = this
        return new Promise(function (resolve, reject) {
          const data = {
            pageNo: 1,
            pageSize: 10,
            list: [
              { id: '1', productCode: 'GA-11078N', productName: '冠牌空气滤清器 丰田新款佳美2.4 12款逸致1.8', productOrigCode: '17807-20040', putTime: '2021-01-28 14:56:11', warehouseName: '默认仓库', warehouseLocationName: '默认仓位', putBizTypeDictValue: '散件入库', currentQty: '207', freezeQty: '', putCost: '1550.50' }
            ],
            count: 10
          }
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = i + 1
          }
          _this.disabled = false
          resolve(data)
        })
      },
      currentStock: { // 合计信息
        currentQty: '',
        putCost: ''
      }
    }
  },
  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">
  .chainStockReportDetail-modal{
    .btn-cont {
      text-align: center;
      margin: 5px 0 10px;
    }
  }
</style>
