<template>
  <a-modal
    centered
    class="outIndetail-modal"
    :footer="null"
    :maskClosable="false"
    title="出库明细"
    v-model="isShow"
    @cancel="isShow = false"
    :zIndex="1010"
    :width="1024">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="baseInfo" v-if="baseData">
        <div>
          <span>产品编码：{{ baseData.dealerProductEntity ? baseData.dealerProductEntity.code : baseData.productCode }}</span>
        </div>
        <div>
          <span>产品名称：{{ baseData.dealerProductEntity ? baseData.dealerProductEntity.name : baseData.productName }}</span>
        </div>
      </div>
      <div class="outIndetail-modal-table">
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: 450 }"
          :defaultLoadData="false"
          :showPagination="false"
          bordered>
        </s-table>
      </div>
      <div class="btn-cont">
        <a-button id="storeTransferOut-basicInfo-modal-back" @click="isShow = false" style="margin-left: 15px;">关闭</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { stockOutDetailList } from '@/api/stockOut'
export default {
  name: 'OutinDetailModal',
  components: { STable },
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    },
    outBizType: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      baseData: null,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        // outBizType = SHOP_CALL_OUT（店内调出）   /  SALES （销售）
        const params = this.baseData
        console.log(params)
        const queryParams = { outBizType: this.outBizType, productSn: params.productSn }
        if (this.outBizType == 'SALES') {
          queryParams.outBizSn = params.salesBillSn
          queryParams.outBizNo = params.salesBillNo
        }
        if (this.outBizType == 'SHOP_CALL_OUT') {
          queryParams.outBizSn = params.storeCallOutSn
          queryParams.outBizNo = params.storeCallOutNo
        }
        // 仓库仓位
        queryParams.warehouseSn = params.warehouseSn
        queryParams.warehouseLocationSn = params.warehouseLocationSn
        return stockOutDetailList(queryParams).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i].no = no + i + 1
              data[i].totalCost = Number(data[i].putCost * data[i].outQty).toFixed(2)
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '库存批次', dataIndex: 'stockBatchNo', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '入库类型', dataIndex: 'putBizTypeDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        // { title: '成本价', dataIndex: 'putCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '本次出库数量', dataIndex: 'outQty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
        // { title: '成本小计', dataIndex: 'totalCost', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]
      if (this.$hasPermissions('M_ShowAllCost')) {
        arr.splice(5, 0, { title: '成本价', dataIndex: 'putCost', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ?  _this.toThousands(text) : '--') } })
        arr.splice(7, 0, { title: '成本小计', dataIndex: 'totalCost', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ?  _this.toThousands(text) : '--') } })
      }
      return arr
    }
  },
  methods: {
    cansel () {
      this.isShow = false
      this.baseData = null
    },
    getData (row) {
      this.baseData = row
      this.$nextTick(() => {
        this.$refs.table.refresh(true)
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>

<style lang="less">
.outIndetail-modal {
	.ant-modal-body {
		padding: 20px;
    .outIndetail-modal-table{
      padding: 20px 0 0;
    }
    .baseInfo{
      line-height: 24px;
      font-size: 14px;
    }
	}
	.btn-cont {
		text-align: center;
		margin: 35px 0 10px;
	}
}
</style>
