<template>
  <div class="warehousingAuditDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="warehousingAuditDetail-cont" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="warehousingAuditDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <p style=" display: inline-block;margin: 0 0 0 60px;color: #000;font-size: 16px;font-weight: 600;">单号：{{ (basicInfoData&&basicInfoData.receivingBillNo) || '--' }}</p>
        </template>
        <!-- 操作区，位于 title 行的行尾 -->
        <template slot="extra">
          <a-radio-group key="3" v-model="printerType">
            <a-radio value="NEEDLE">针式</a-radio>
            <a-radio value="INK">喷墨</a-radio>
          </a-radio-group>
          <a-button key="2" id="warehousingAuditDetail-preview-btn" @click="handlePrint('preview')">打印预览</a-button>
          <a-button key="1" type="primary" id="warehousingAuditDetail-print-btn" @click="handlePrint('print')">快捷打印</a-button>
        </template>
      </a-page-header>
      <!-- 基础信息 -->
      <a-card size="small" :bordered="false" class="warehousingAuditDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="基础信息">
            <a-descriptions :column="3" v-if="basicInfoData">
              <a-descriptions-item label="供应商">{{ basicInfoData&&basicInfoData.purchaseTargetName ? basicInfoData.purchaseTargetName : '--' }}</a-descriptions-item>
              <a-descriptions-item label="入库时间">{{ basicInfoData&&basicInfoData.stockPutTime ? basicInfoData.stockPutTime : '--' }}</a-descriptions-item>
              <a-descriptions-item label="采购单号">{{ basicInfoData&&basicInfoData.purchaseBillNo ? basicInfoData.purchaseBillNo : '--' }}</a-descriptions-item>
            </a-descriptions>
            <a-descriptions :column="3" bordered v-if="basicInfoData">
              <a-descriptions-item label="采购总款数">{{ (basicInfoData.totalCategory&&basicInfoData.totalCategory!=0) ? basicInfoData.totalCategory : 0 }}</a-descriptions-item>
              <a-descriptions-item label="采购总数量">{{ (basicInfoData.totalQty&&basicInfoData.totalQty!=0) ? basicInfoData.totalQty : 0 }}</a-descriptions-item>
              <a-descriptions-item label="采购总成本">￥{{ (basicInfoData.totalAmount&&basicInfoData.totalAmount!=0) ? basicInfoData.totalAmount : 0 }}</a-descriptions-item>
              <a-descriptions-item label="入库总款数">{{ (basicInfoData.totalPutCategory&&basicInfoData.totalPutCategory!=0) ? basicInfoData.totalPutCategory : 0 }}</a-descriptions-item>
              <a-descriptions-item label="入库总数量">{{ (basicInfoData.totalPutQty&&basicInfoData.totalPutQty!=0) ? basicInfoData.totalPutQty : 0 }}</a-descriptions-item>
              <a-descriptions-item label="入库总成本">￥{{ (basicInfoData.totalPutAmount&&basicInfoData.totalPutAmount!=0) ? basicInfoData.totalPutAmount : 0 }}</a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <!-- 入库明细 -->
      <a-card size="small" :bordered="false" class="warehousingAuditDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="入库明细">
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table"
              size="small"
              :rowKey="(record) => record.id"
              :columns="columns"
              :data="loadData"
              :scroll="{ x: 1520 }"
              bordered>
            </s-table>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
    </a-spin>
    <!-- 打印 -->
    <div id="print"></div>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import { receivingDetailList, receivingDetailSn, receivingDetailPrint } from '@/api/receiving'
export default {
  components: { STable, VSelect },
  data () {
    return {
      spinning: false,
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'dealerProductEntity.code', width: 220, align: 'center' },
        { title: '产品名称', dataIndex: 'dealerProductEntity.name', align: 'center', ellipsis: true },
        { title: '原厂编码', dataIndex: 'dealerProductEntity.origCode', width: 220, align: 'center' },
        { title: '采购数量', dataIndex: 'qty', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库数量', dataIndex: 'putQty', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '成本价', dataIndex: 'price', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'dealerProductEntity.unit', width: 100, align: 'center' },
        { title: '入库小计', dataIndex: 'totalAmount', width: 100, align: 'center' },
        { title: '仓库', dataIndex: 'warehouseEntity.name', width: 140, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '仓位', dataIndex: 'warehouseLocationEntity.name', width: 140, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return receivingDetailList(Object.assign(parameter, { receivingBillSn: 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.disabled = false
          return data
        })
      },
      basicInfoData: null, //  基本信息
      printerType: 'NEEDLE' //  打印机类型
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/financialManagement/warehousingAudit/list' })
    },
    // 打印预览/快捷打印
    handlePrint (type) {
      const _this = this
      _this.spinning = true
      receivingDetailPrint({ sn: this.$route.params.sn, type: this.printerType }).then(res => {
        _this.spinning = false
        if (res.type == 'application/json') {
          var reader = new FileReader()
          reader.addEventListener('loadend', function () {
            const obj = JSON.parse(reader.result)
            _this.$notification.error({
              message: '提示',
              description: obj.message
            })
          })
          reader.readAsText(res)
        } else {
          this.print(res, type)
        }
      })
    },
    print (data, type) {
      if (!data) {
        return
      }
      const url = window.URL.createObjectURL(new Blob([data], { type: 'application/pdf' }))
      document.getElementById('print').innerHTML = '<iframe id="printf" name="printf" src="' + url + '" hidden></iframe>'
      if (type == 'preview') { //  预览
        window.open(url)
      } else if (type == 'print') { //  打印
        window.frames['printf'].focus()
        window.frames['printf'].print()
      }
    },
    getDetail () {
      receivingDetailSn({ sn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getDetail()
    })
  }
}
</script>

<style lang="less">
  .warehousingAuditDetail-cont{
    margin-bottom: 15px;
  }
</style>
