<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" v-if="$hasPermissions('B_sparePartsRetPrint')" :bordered="false" class="bulkReturnGoodsDetail-cont">
        <div style="float:right"><Print :disabled="dataSource.length==0" @handlePrint="handlePrint"></Print></div>
      </a-card>
      <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="{ y: 450 }"
          bordered>
        </s-table>
      </a-card>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import Print from '@/views/common/print.vue'
import { hdPrint } from '@/libs/JGPrint'
import { sparePartsRetDetail, sparePartsRetDetailList, sparePartsRetDetailCount, sparePartsRetDetailPrint, sparePartsRetDetailExport } from '@/api/sparePartsRet'
export default {
  name: 'BulkReturnGoodsDetailModal',
  components: { STable, Print },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: String || Number,
      default: ''
    },
    isGrab: {
      type: String || Number,
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      dataSource: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, { sparePartsReturnSn: this.itemSn })
        return sparePartsRetDetailList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.getDetailCount(params)
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.dataSource = data.list
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      basicInfoData: null, //  基本信息
      productTotal: null
    }
  },
  computed: {
    columns () {
      let arr = []
      if (this.isGrab == '1') { // 抓单
        arr = [
          { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
          { title: '散件入库单号', dataIndex: 'sparePartsPurchaseNo', width: '22%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '产品编码', dataIndex: 'productCode', width: '22%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '产品名称', dataIndex: 'productName', width: '22%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '单位', dataIndex: 'productUnit', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '退货单价', dataIndex: 'purchaseCost', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '退货数量', dataIndex: 'purchaseQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '退货金额', dataIndex: 'purchaseAmount', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
        ]
      } else { // 不抓单
        arr = [
          { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
          { title: '产品编码', dataIndex: 'productCode', width: '18%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '产品名称', dataIndex: 'productName', width: '18%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '仓库', dataIndex: 'warehouseName', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '仓位', dataIndex: 'warehouseLocationName', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '单位', dataIndex: 'productUnit', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '退货单价', dataIndex: 'purchaseCost', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '退货数量', dataIndex: 'purchaseQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
          { title: '退货金额', dataIndex: 'purchaseAmount', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
        ]
      }
      return arr
    }
  },
  methods: {
    // 打印预览/快捷打印
    handlePrint (type, printerType) {
      const _this = this
      _this.spinning = true
      let url = sparePartsRetDetailPrint
      const params = { sn: this.basicInfoData.sparePartsReturnSn }
      if (type == 'export') { //  导出
        url = sparePartsRetDetailExport
      }
      // 打印或导出
      hdPrint(printerType, type, url, params, this.basicInfoData && this.basicInfoData.isGrab == 1 ? '散件退货单(抓单)' : '散件退货单(不抓单)', function () {
        _this.spinning = false
      })
    },
    // 详情
    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>
