<template>
  <a-modal
    centered
    class="bulkReturnGoodsDetail-modal"
    :footer="null"
    :maskClosable="false"
    title="详情"
    v-model="isShow"
    @cancle="isShow = false"
    :width="960">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-card size="small" :bordered="false" class="bulkReturnGoodsDetail-cont">
        <a-descriptions size="small">
          <a-descriptions-item label="散件退货单号">{{ (basicInfoData&&basicInfoData.sparePartsReturnNo) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="业务状态">{{ (basicInfoData&&basicInfoData.stateDictValue) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="财务状态">{{ (basicInfoData&&basicInfoData.settleStateDictValue) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="供应商">{{ (basicInfoData&&basicInfoData.supplierName) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="是否抓单">{{ (basicInfoData&&basicInfoData.isGrabDictValue) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="备注">{{ (basicInfoData&&basicInfoData.remark) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="创建时间">{{ (basicInfoData&&basicInfoData.createDate) || '--' }}</a-descriptions-item>
          <a-descriptions-item label="审核时间">{{ (basicInfoData&&basicInfoData.auditTime) || '--' }}</a-descriptions-item>
        </a-descriptions>
      </a-card>
      <a-card size="small" :bordered="false" class="bulkReturnGoodsDetail-cont">
        <!-- 总计 -->
        <a-alert type="info" style="margin-bottom:10px">
          <div slot="message">
            总款数<strong>{{ (productTotal&&(productTotal.purchaseSize || productTotal.purchaseSize==0)) ? productTotal.purchaseSize : '--' }}</strong> ，
            总数量<strong>{{ (productTotal&&(productTotal.purchaseQty || productTotal.purchaseQty==0)) ? productTotal.purchaseQty : '--' }}</strong> ，
            总金额<strong>{{ (productTotal&&(productTotal.purchaseCost || productTotal.purchaseCost==0)) ? '￥'+productTotal.purchaseCost : '--' }}</strong>
          </div>
        </a-alert>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
          :scroll="{ x: isGrab=='1'?890:910, y: 450 }"
          bordered>
        </s-table>
      </a-card>
    </a-spin>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import { sparePartsRetDetail, sparePartsRetDetailList, sparePartsRetDetailCount } from '@/api/sparePartsRet'
export default {
  name: 'BulkReturnGoodsDetailModal',
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: String || Number,
      default: ''
    },
    isGrab: {
      type: String || Number,
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        const params = Object.assign(parameter, { sparePartsReturnSn: this.itemSn })
        return sparePartsRetDetailList(params).then(res => {
          this.getDetailCount(params)
          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
          return data
        })
      },
      basicInfoData: null, //  基本信息
      productTotal: null
    }
  },
  computed: {
    columns () {
      let arr = []
      if (this.isGrab == '1') { // 抓单
        arr = [
          { title: '序号', dataIndex: 'no', width: 50, align: 'center' },
          { title: '散件入库单号', dataIndex: 'sparePartsPurchaseNo', width: 180, align: 'center', customRender: function (text) { return text || '--' } },
          { title: '产品编码', dataIndex: 'productCode', width: 180, align: 'center', customRender: function (text) { return text || '--' } },
          { title: '产品名称', dataIndex: 'productName', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '单位', dataIndex: 'productUnit', width: 60, align: 'center', customRender: function (text) { return text || '--' } },
          { title: '退货单价', dataIndex: 'purchaseCost', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '退货数量', dataIndex: 'purchaseQty', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '退货金额', dataIndex: 'purchaseAmount', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
        ]
      } else { // 不抓单
        arr = [
          { title: '序号', dataIndex: 'no', width: 50, align: 'center' },
          { title: '产品编码', dataIndex: 'productCode', width: 180, align: 'center', customRender: function (text) { return text || '--' } },
          { title: '产品名称', dataIndex: 'productName', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '仓库', dataIndex: 'warehouseName', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
          { title: '仓位', dataIndex: 'warehouseLocationName', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
          { title: '单位', dataIndex: 'productUnit', width: 60, align: 'center', customRender: function (text) { return text || '--' } },
          { title: '退货单价', dataIndex: 'purchaseCost', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '退货数量', dataIndex: 'purchaseQty', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '退货金额', dataIndex: 'purchaseAmount', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
        ]
      }
      return arr
    }
  },
  methods: {
    // 详情
    getDetail () {
      sparePartsRetDetail({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data || null
        } else {
          this.basicInfoData = null
        }
      })
    },
    // 合计
    getDetailCount () {
      sparePartsRetDetailCount({ sparePartsReturnSn: this.itemSn }).then(res => {
        if (res.status == 200) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.basicInfoData = null
        this.$refs.table.clearTable()
      } else {
        const _this = this
        setTimeout(() => (
          _this.$refs.table.refresh(true)
        ), 300)
      }
    },
    itemSn (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.getDetail()
      }
    }
  }
}
</script>

<style lang="less">
.bulkReturnGoods-basicInfo-modal {
  .ant-modal-body {
    padding: 40px 40px 24px;
  }
  .btn-cont {
    text-align: center;
    margin: 35px 0 10px;
  }
}
</style>
