<template>
  <div class="bulkWarehousingOrderDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="bulkWarehousingOrderDetail-cont" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="bulkWarehousingOrderDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <a-button
            v-if="isEdit"
            type="primary"
            size="small"
            style="background-color: #1890ff;margin-left: 20px;border: #1890ff;"
            id="bulkWarehousingOrderDetail-edit-btn"
            @click.stop="handleEdit">编辑</a-button>
          <p style=" display: inline-block;margin: 0 0 0 60px;color: #000;font-size: 16px;font-weight: 600;">单号：{{ (basicInfoData&&basicInfoData.sparePartsPurchaseNo) || '--' }}</p>
        </template>
        <template slot="tags">
          <a-tag color="red" v-if="basicInfoData&&basicInfoData.stateDictValue">{{ basicInfoData.stateDictValue }}</a-tag>
          <a-tag color="orange" v-if="basicInfoData&&basicInfoData.settleStateDictValue">{{ basicInfoData.settleStateDictValue }}</a-tag>
        </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="bulkWarehousingOrderDetail-preview-btn" :disabled="localDataSource.length==0" @click="handlePrint('preview')">打印预览</a-button>
          <a-button key="1" type="primary" id="bulkWarehousingOrderDetail-print-btn" :disabled="localDataSource.length==0" @click="handlePrint('print')">快捷打印</a-button>
        </template>
      </a-page-header>
      <!-- 基础信息 -->
      <a-card size="small" :bordered="false" class="bulkWarehousingOrderDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="基础信息">
            <a-descriptions :column="3">
              <a-descriptions-item label="供应商">{{ (basicInfoData&&basicInfoData.supplierName) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="散件入库类型">{{ (basicInfoData&&basicInfoData.sparePartsTypeName) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="备注">{{ (basicInfoData&&basicInfoData.remarks) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="联系人">
                {{ (basicInfoData&&basicInfoData.supplierContactPerson) || '--' }}
                <span v-if="basicInfoData&&basicInfoData.supplierContactTel">（{{ basicInfoData.supplierContactTel }}）</span>
              </a-descriptions-item>
              <a-descriptions-item label="联系地址">
                {{ (basicInfoData&&basicInfoData.supplierProvinceName) || '' }}
                {{ (basicInfoData&&basicInfoData.supplierCityName) || '' }}
                {{ (basicInfoData&&basicInfoData.supplierCountyName) || '' }}
                {{ (basicInfoData&&basicInfoData.supplierAddress) || '' }}
              </a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <!-- 产品详情 -->
      <a-card size="small" :bordered="false" class="bulkWarehousingOrderDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="产品详情">
            <!-- 总计 -->
            <a-alert type="info" showIcon style="margin-bottom:15px">
              <div slot="message">总款数 <strong>{{ (productTotal&&productTotal.productTotalCategory) || 0 }}</strong> ，总数量 <strong>{{ (productTotal&&productTotal.productTotalQty) || 0 }}</strong> ，总成本￥<strong>{{ (productTotal&&productTotal.productTotalCost) || 0 }}</strong></div>
            </a-alert>
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table"
              size="small"
              :rowKey="(record) => record.id"
              :columns="columns"
              :data="loadData"
              :scroll="{ x: 1180 }"
              bordered>
            </s-table>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <!-- 单据记录  二期 -->
      <!-- <a-card size="small" :bordered="false" class="bulkWarehousingOrderDetail-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 { getOperationalPrecision } from '@/libs/tools.js'
import { sparePartsPurDetail, sparePartsPurDetailList, sparePartsPurDetailCount, sparePartsPurDetailPrint } from '@/api/sparePartsPur'
export default {
  components: { STable, VSelect },
  data () {
    return {
      spinning: false,
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'productCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '采购单价', dataIndex: 'putCost', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '采购数量', dataIndex: 'putQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'productUnit', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '采购金额', dataIndex: 'purchaseAmount', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '仓库', dataIndex: 'warehouseName', width: 140, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: 140, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return sparePartsPurDetailList(Object.assign(parameter, { sparePartsPurchaseSn: 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
            // 采购金额  由于数据库内小数位数为4位，页面则需显示2位。因此会做小数运算精度处理
            data.list[i].purchaseAmount = getOperationalPrecision(data.list[i].putCost, data.list[i].putQty)
          }
          this.localDataSource = data.list
          this.disabled = false
          return data
        })
      },
      localDataSource: [],
      basicInfoData: null, //  基本信息
      productTotal: null, //  合计
      printerType: 'NEEDLE' //  打印机类型
    }
  },
  computed: {
    isEdit () {
      return this.basicInfoData && this.basicInfoData.state != 'FINISH'
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/bulkManagement/bulkWarehousingOrder/list' })
    },
    // 打印预览/快捷打印
    handlePrint (type) {
      const _this = this
      _this.spinning = true
      sparePartsPurDetailPrint({ 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 () {
      sparePartsPurDetail({ id: this.$route.params.id }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    },
    // 合计
    getDetailCount () {
      sparePartsPurDetailCount({ sparePartsPurchaseSn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    //  编辑
    handleEdit () {
      this.$router.push({ path: `/bulkManagement/bulkWarehousingOrder/edit/${this.basicInfoData.id}/${this.basicInfoData.sparePartsPurchaseSn}` })
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getDetail()
      vm.getDetailCount()
    })
  }
}
</script>

<style lang="less">
  .bulkWarehousingOrderDetail-cont{
    margin-bottom: 15px;
    .timeline-box{
      margin-top: 30px;
      .auxiliary-txt{
        color: #808695;
      }
    }
  }
</style>
