<template>
  <div class="purchaseOrderDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" @back="handleBack" class="purchaseOrderDetail-cont" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="purchaseOrderDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <span class="billno">单号：{{ detail&&detail.purchaseBillNo }}</span>
          <a-tag color="blue">{{ detail&&detail.billStatusDictValue }}</a-tag>
          <a-tag color="orange">{{ detail&&detail.financialStatusDictValue }}</a-tag>
        </template>
        <!-- 操作区，位于 title 行的行尾 -->
        <template slot="extra" v-if="$hasPermissions('B_purchasePrint')">
          <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="purchaseOrderDetail-preview-btn" :disabled="localDataSource.length==0" @click="handlePrint('preview')">打印预览</a-button>
          <a-button key="1" type="primary" id="purchaseOrderDetail-print-btn" :disabled="localDataSource.length==0" @click="handlePrint('print')">快捷打印</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.settleStyleEntity.name }}</a-descriptions-item>
              <a-descriptions-item label="采购员工">{{ detail&&detail.operatorName }}</a-descriptions-item>
              <a-descriptions-item label="收货人">{{ detail&&detail.consigneeName }}<span v-if="detail&&detail.consigneeTel">（{{ detail&&detail.consigneeTel }}）</span></a-descriptions-item>
              <a-descriptions-item label="收货地址">
                {{ detail&&detail.shippingAddressProvinceName }}
                {{ detail&&detail.shippingAddressCityName }}
                {{ detail&&detail.shippingAddressCountyName }}
                {{ detail&&detail.shippingAddress }}
              </a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <!-- 产品详情 -->
      <a-card size="small" :bordered="false" class="purchaseOrderDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="产品详情">
            <!-- 总计 -->
            <a-alert type="info" showIcon style="margin-bottom:15px">
              <div slot="message">
                产品款数：<strong>{{ detail ? ((detail.totalCategory || detail.totalCategory==0) ? detail.totalCategory : '--') : '--' }}</strong>，
                采购数量合计：<strong>{{ detail ? ((detail.totalQty || detail.totalQty==0) ? detail.totalQty : '--') : '--' }}</strong>，
                已收货数量：<strong>{{ detail ? ((detail.totalPutQty || detail.totalPutQty==0) ? detail.totalPutQty : '--') : '--' }}</strong>，
                待收货数量：<strong>{{ detail ? ((detail.totalWaitPutQty || detail.totalWaitPutQty==0) ? detail.totalWaitPutQty : '--') : '--' }}</strong>，
                取消数量：<strong>{{ detail ? ((detail.totalCancelQty || detail.totalCancelQty==0) ? detail.totalCancelQty : '--') : '--' }}</strong>，
                采购金额合计：￥<strong>{{ detail ? ((detail.discountedAmount || detail.discountedAmount==0) ? detail.discountedAmount : '--') : '--' }}</strong>，
                已收货金额：￥<strong>{{ detail ? ((detail.totalPutAmount || detail.totalPutAmount==0) ? detail.totalPutAmount : '--') : '--' }}</strong>，
                待收货金额：￥<strong>{{ detail ? ((detail.totalWaitPutAmount || detail.totalWaitPutAmount==0) ? detail.totalWaitPutAmount : '--') : '--' }}</strong>，
                取消金额：￥<strong>{{ detail ? ((detail.totalCancelAmount || detail.totalCancelAmount==0) ? detail.totalCancelAmount : '--') : '--' }}</strong>
              </div>
            </a-alert>
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table"
              size="small"
              :rowKey="(record) => record.id"
              :columns="columns"
              :data="loadData"
              :scroll="{ x: 1480 }"
              bordered>
              <!-- 采购数量 -->
              <template slot="origqty" slot-scope="text, record">
                <div>{{ text }}</div>
                <div v-if="record.origqty">原采购数量：{{ record.origqty }}</div>
              </template>
              <!-- 缺货数量 -->
              <template slot="outOfStockNum" slot-scope="text, record">
                <span>{{ record.outOfStockNum }}</span>
              </template>
            </s-table>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <!-- 促销品详情 -->
      <a-card size="small" v-if="false" :bordered="false" class="purchaseOrderDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="促销品详情">
            <!-- 总计 -->
            <a-alert type="info" showIcon style="margin-bottom:15px">
              <div slot="message">产品款数 <strong>{{ detail&&detail.totalCategory }}</strong> ，
                采购数量合计 <strong>{{ detail&&detail.totalQty }}</strong> ，
                采购金额合计￥<strong>{{ detail&&detail.totalAmount }}</strong></div>
            </a-alert>
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table-1"
              size="small"
              :rowKey="(record) => record.id"
              :columns="columns"
              :data="loadData"
              :scroll="{ x: 1060 }"
              bordered>
              <!-- 采购数量 -->
              <template slot="origqty" slot-scope="text, record">
                <div>{{ text }}</div>
                <div v-if="record.origqty">原采购数量：{{ record.origqty }}</div>
              </template>
              <!-- 缺货数量 -->
              <template slot="outOfStockNum" slot-scope="text, record">
                <span>{{ record.outOfStockNum }}</span>
              </template>
            </s-table>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <!-- 单据记录 -->
      <a-card size="small" v-show="false" :bordered="false" class="purchaseOrderDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="单据记录">
            <a-timeline class="timeline-box">
              <a-timeline-item>2021-02-01 15:22:38   王凯   审核通过</a-timeline-item>
              <a-timeline-item>
                <p>2021-02-01 15:22:38   王凯   审核通过</p>
                <p class="auxiliary-txt">产品编号JA-11076P的采购数量，由【10】修改为【5】</p>
                <p class="auxiliary-txt">产品编号JA-11076P的采购数量，由【10】修改为【5】</p>
              </a-timeline-item>
              <a-timeline-item>Technical testing 2015-09-01</a-timeline-item>
              <a-timeline-item>Network problems being solved 2015-09-01</a-timeline-item>
            </a-timeline>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
    </a-spin>
    <!-- 打印 -->
    <div id="print"></div>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import { purchaseDetailBySn, purchaseDetailPrint } from '@/api/purchase'
import { purchaseDetailList } from '@/api/purchaseDetail'
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', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'dealerProductEntity.name', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'dealerProductEntity.origCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单位', dataIndex: 'dealerProductEntity.unit', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '采购单价', dataIndex: 'discountedPrice', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '采购金额', dataIndex: 'discountedAmount', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? '￥' + text : '--') } },
        { title: '采购数量', dataIndex: 'qty', width: 100, align: 'center', scopedSlots: { customRender: 'origqty' } },
        { title: '取消数量', dataIndex: 'cancelQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '已收货数量', dataIndex: 'putQty', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '待收货数量', dataIndex: 'waitPutQty', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return purchaseDetailList(Object.assign(parameter, { purchaseBillSn: 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' //  打印机类型
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/purchasingManagement/purchaseOrder/list' })
    },
    //  详情
    getDetail () {
      purchaseDetailBySn({ sn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.detail = res.data
        } else {
          this.detail = null
        }
      })
    },
    // 打印预览/快捷打印
    handlePrint (type) {
      const _this = this
      _this.spinning = true
      purchaseDetailPrint({ 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()
      }
    }
  },
  mounted () {
    this.getDetail()
  }
}
</script>

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