<template>
  <div class="signWarehousingDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" @back="handleBack" class="signWarehousingDetail-cont" :style="{ padding: !outBizSn ? '16px 24px' : '0px 24px' }">
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="signWarehousingDetail-back-btn" href="javascript:;" @click="handleBack" v-if="!outBizSn"><a-icon type="left" /> 返回列表</a>
          <span class="billno">
            单号：{{ detail&&detail.purchaseBillNo }}
            <span v-if="detail&&detail.sendBillNo">（发货单号：{{ detail&&detail.sendBillNo || '--' }}）</span>
          </span>
        </template>
        <!-- 操作区，位于 title 行的行尾 -->
        <template slot="extra">
          <!-- <Print v-if="$hasPermissions('B_purchasePrint')" :disabled="localDataSource.length==0" @handlePrint="handlePrint"></Print> -->
          <a-button
            key="1"
            type="primary"
            class="button-warning"
            v-if="$hasPermissions('B_signWarehousingExport')"
            id="signWarehousingDetail-export-btn"
            :disabled="localDataSource.length==0"
            @click="handleExportProduct()">导出Excel</a-button>
        </template>
      </a-page-header>
      <!-- 基础信息 -->
      <a-card size="small" :bordered="false" class="purchaseOrderDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="基础信息">
            <a-descriptions :column="3">
              <a-descriptions-item label="供应商">{{ (detail&&detail.purchaseTargetName) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="支付方式">{{ (detail&&detail.purchaseBill&&detail.purchaseBill.settleStyleSnDictValue) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="采购员工" v-if="detail&&detail.purchaseBill&&detail.purchaseBill.purchaseBillSource=='PURCHASE'">{{ detail&&detail.purchaseBill&&detail.purchaseBill.operatorName || '--' }}</a-descriptions-item>
              <a-descriptions-item label="收货人">{{ detail&&detail.purchaseBill&&detail.purchaseBill.consigneeName || '--' }}<span v-if="detail&&detail.purchaseBill&&detail.purchaseBill.consigneeTel">（{{ detail&&detail.purchaseBill&&detail.purchaseBill.consigneeTel }}）</span></a-descriptions-item>
              <a-descriptions-item label="收货地址">
                <div v-if="detail&&detail.purchaseBill&&(detail.purchaseBill.shippingAddressProvinceName || detail.purchaseBill.shippingAddressCityName || detail.purchaseBill.shippingAddressCountyName || detail.purchaseBill.shippingAddress)">
                  {{ detail&&detail.purchaseBill&&detail.purchaseBill.shippingAddressProvinceName || '' }}
                  {{ detail&&detail.purchaseBill&&detail.purchaseBill.shippingAddressCityName || '' }}
                  {{ detail&&detail.purchaseBill&&detail.purchaseBill.shippingAddressCountyName || '' }}
                  {{ detail&&detail.purchaseBill&&detail.purchaseBill.shippingAddress || '' }}
                </div>
                <span v-else>--</span>
              </a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <a-card size="small" :bordered="false" class="signWarehousingDetail-cont">
        <!-- 总计 -->
        <a-alert type="info" style="margin-bottom:10px">
          <div slot="message">
            产品款数：<strong>{{ detail && (detail.totalPutCategory || detail.totalPutCategory==0) ? detail.totalPutCategory : '--' }}</strong>，
            本次发货数量合计：<strong>{{ detail && (detail.totalPutQty || detail.totalPutQty==0) ? detail.totalPutQty : '--' }}</strong>，
            <div style="display: inline-block;" v-if="$hasPermissions('M_ShowAllCost')">本次发货金额合计：<strong>{{ detail && (detail.totalPutAmount || detail.totalPutAmount==0) ? toThousands(detail.totalPutAmount, 2) : '--' }}</strong></div>
          </div>
        </a-alert>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :defaultLoadData="false"
          bordered>
          <!-- 采购数量 -->
          <template slot="qty" slot-scope="text, record">
            <div>{{ record.qty }}</div>
          </template>
          <!-- 缺货数量 -->
          <template slot="outOfStockNum" slot-scope="text, record">
            <span>{{ record.outOfStockNum }}</span>
          </template>
        </s-table>
      </a-card>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { purchaseDetailPrint, purchaseDetailExport, purchaseExportDetail } from '@/api/purchase'
import { receivingDetailSn, receivingDetailList, receivingExport } from '@/api/receiving'
import Print from '@/views/common/print.vue'
import { hdPrint } from '@/libs/JGPrint'
export default {
  name: 'SignWareHousingStockOrderDetail',
  components: { STable, VSelect, Print },
  mixins: [commonMixin],
  props: {
    outBizSn: { //  有值则为弹框，无值则为页面
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return receivingDetailList(Object.assign(parameter, { receivingBillSn: this.outBizSn || this.$route.params.sn })).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.localDataSource = data.list
          this.disabled = false
          return data
        })
      },
      detail: null, //  详情数据
      localDataSource: [],
      printerType: 'NEEDLE' //  打印机类型
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'dealerProductEntity.code', width: '18%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'dealerProductEntity.name', width: '17%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'dealerProductEntity.origCode', width: '16%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '采购数量', width: '6%', align: 'center', scopedSlots: { customRender: 'qty' } },
        { title: '本次发货数量', dataIndex: 'putQty', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'dealerProductEntity.unit', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '本次入库数量', dataIndex: 'realPutQty', width: '9%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      if (this.$hasPermissions('M_ShowAllCost')) {
        arr.splice(4, 0, { title: '采购单价', dataIndex: 'discountedPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text, 2) : '--') } })
        arr.splice(8, 0, { title: '本次发货金额', dataIndex: 'discountedAmount', width: '9%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text, 2) : '--') } })
      }
      return arr
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/purchasingManagement/signWarehousing/list', query: { closeLastOldTab: true } })
    },
    //  详情
    getDetail () {
      receivingDetailSn({ sn: this.outBizSn || this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.detail = res.data
        } else {
          this.detail = null
        }
      })
    },
    // 打印预览/快捷打印
    handlePrint (type, printerType) {
      const _this = this
      _this.spinning = true
      let url = purchaseDetailPrint
      let params = { sn: this.outBizSn || this.$route.params.sn, type: printerType }
      if (type == 'export') { //  导出
        url = purchaseExportDetail
        params = { sn: this.outBizSn || this.$route.params.sn }
      }
      // 打印或导出
      hdPrint(printerType, type, url, params, '备货单', function () {
        _this.spinning = false
      })
    },
    // 导出产品
    handleExportProduct () {
      const _this = this
      _this.spinning = true
      // 打印或导出
      hdPrint('', 'export', receivingExport, { receivingBillSn: this.outBizSn || this.$route.params.sn }, '备货单', function () {
        _this.spinning = false
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab || this.outBizSn) { // 页签刷新时调用
      this.$refs.table.refresh(true)
      this.getDetail()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.$refs.table.refresh(true)
      this.getDetail()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .signWarehousingDetail-cont{
    margin-bottom: 10px;
    .timeline-box{
      margin-top: 30px;
      .auxiliary-txt{
        color: #808695;
      }
    }
  }
  .signWarehousingDetail-wrap{
    .billno{
      font-size: 16px;
      font-weight: bold;
      margin: 0 15px;
    }
  }
</style>
