<template>
  <div class="chainTransferOutDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="chainTransferOutDetail-cont" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="chainTransferOutDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <a-button
            v-if="isEdit&&$hasPermissions('B_allocLinkageOutEdit')"
            type="primary"
            size="small"
            style="background-color: #1890ff;margin-left: 20px;border: #1890ff;"
            id="chainTransferOutDetail-edit-btn"
            @click.stop="handleEdit">编辑</a-button>
        </template>
        <!-- 操作区，位于 title 行的行尾 -->
        <template slot="extra" v-if="$hasPermissions('B_allocLinkageOutPrint')">
          <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="chainTransferOutDetail-preview-btn" :disabled="localDataSource.length==0" @click="handlePrint('preview')">打印预览</a-button>
          <a-button key="1" type="primary" id="chainTransferOutDetail-print-btn" :disabled="localDataSource.length==0" @click="handlePrint('print')">快捷打印</a-button>
        </template>
      </a-page-header>
      <!-- 基础信息 -->
      <a-card size="small" :bordered="false" class="chainTransferOutDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="基础信息">
            <a-descriptions :column="3">
              <a-descriptions-item label="调往对象">{{ (basicInfoData&&basicInfoData.putTenantName) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="调出单号">{{ (basicInfoData&&basicInfoData.allocationLinkageOutNo) || '--' }}</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.remark) || '--' }}</a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <!-- 产品详情 -->
      <a-card size="small" :bordered="false" class="chainTransferOutDetail-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: 1090 }"
              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 { getOperationalPrecision } from '@/libs/tools.js'
import { allocLinkageOutDetailSn, allocLinkageOutDetailList, allocLinkageOutDetailCount, allocLinkageOutDetailPrint } from '@/api/allocLinkageOut'
export default {
  components: { STable, VSelect },
  data () {
    return {
      spinning: false,
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'dealerProductEntity.code', width: 200, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'dealerProductEntity.name', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'dealerProductEntity.origCode', width: 200, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '成本价', dataIndex: 'outCost', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '调出仓库', dataIndex: 'warehouseName', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '调出数量', dataIndex: 'outQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '成本小计(￥)', dataIndex: 'costSubtotal', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        const params = Object.assign(parameter, { allocationLinkageOutSn: this.$route.params.sn })
        return allocLinkageOutDetailList(params).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].costSubtotal = getOperationalPrecision(data.list[i].outCost, data.list[i].outQty)
          }
          this.disabled = false
          this.getDetailCount(params)
          this.localDataSource = data.list
          return data
        })
      },
      basicInfoData: null, //  基本信息
      productTotal: null, //  合计
      localDataSource: [],
      printerType: 'NEEDLE' //  打印机类型
    }
  },
  computed: {
    isEdit () {
      return (this.basicInfoData && this.basicInfoData.state == 'WAIT_SUBMIT') || (this.basicInfoData && this.basicInfoData.state == 'WAIT_AUDIT')
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/allocationManagement/chainTransferOut/list' })
    },
    // 基本信息
    getDetail () {
      allocLinkageOutDetailSn({ sn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    },
    // 合计
    getDetailCount (params) {
      allocLinkageOutDetailCount(params).then(res => {
        if (res.status == 200) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    //  编辑
    handleEdit () {
      this.$router.push({ path: `/allocationManagement/chainTransferOut/edit/${this.basicInfoData.id}/${this.basicInfoData.allocationLinkageOutSn}` })
    },
    // 打印预览/快捷打印
    handlePrint (type) {
      const _this = this
      _this.spinning = true
      allocLinkageOutDetailPrint({ 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()
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getDetail()
    })
  }
}
</script>

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