<template>
  <div class="bulkWarehousingOrderDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="bulkWarehousingOrderDetail-cont" :style="{ padding: !outBizSn ? '16px 24px' : '0px 24px' }">
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="bulkWarehousingOrderDetail-back-btn" href="javascript:;" @click="handleBack" v-if="!outBizSn"><a-icon type="left" /> 返回列表</a>
          <a-button
            v-if="isEdit&&!outBizSn"
            type="primary"
            size="small"
            style="background-color: #1890ff;margin-left: 20px;border: #1890ff;"
            id="bulkWarehousingOrderDetail-edit-btn"
            @click.stop="handleEdit">编辑</a-button>
          <span class="head-order-number">单号：{{ (basicInfoData&&basicInfoData.sparePartsPurchaseNo) || '--' }}</span>
        </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" v-if="$hasPermissions('B_bulkWarehousingOrder_print')">
          <Print :disabled="localDataSource.length==0" :showExport="false" @handlePrint="handlePrint"></Print>
        </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="联系地址">
                <div v-if="basicInfoData&&(basicInfoData.supplierProvinceName || basicInfoData.supplierCityName || basicInfoData.supplierCountyName || basicInfoData.supplierAddress)">
                  {{ (basicInfoData&&basicInfoData.supplierProvinceName) || '' }}
                  {{ (basicInfoData&&basicInfoData.supplierCityName) || '' }}
                  {{ (basicInfoData&&basicInfoData.supplierCountyName) || '' }}
                  {{ (basicInfoData&&basicInfoData.supplierAddress) || '' }}
                </div>
                <span v-else>--</span>
              </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" style="margin-bottom:10px">
              <div slot="message">
                总款数 <strong>{{ (productTotal&&(productTotal.productTotalCategory || productTotal.productTotalCategory==0)) ? productTotal.productTotalCategory : '--' }}</strong> ，
                总数量 <strong>{{ (productTotal&&(productTotal.productTotalQty || productTotal.productTotalQty==0)) ? productTotal.productTotalQty : '--' }}</strong> ，
                总成本 <strong>{{ (productTotal&&(productTotal.productTotalCost || productTotal.productTotalCost==0)) ? toThousands(productTotal.productTotalCost,2) : '--' }}</strong>
              </div>
            </a-alert>
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table"
              size="small"
              :rowKey="(record) => record.id"
              :columns="columns"
              :data="loadData"
              :defaultLoadData="false"
              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>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { getOperationalPrecision } from '@/libs/tools.js'
import { sparePartsPurDetailBySn, sparePartsPurDetailList, sparePartsPurDetailCount, sparePartsPurDetailPrint } from '@/api/sparePartsPur'
import Print from '@/views/common/print.vue'
import { hdPrint } from '@/libs/JGPrint'
export default {
  name: 'BulkWarehousingDetail',
  components: { STable, VSelect, Print },
  mixins: [commonMixin],
  props: {
    outBizSn: { //  有值则为弹框，无值则为页面
      type: [Number, String],
      default: ''
    }
  },
  data () {
    const _this = this
    return {
      spinning: false,
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '采购单价', dataIndex: 'putCost', width: '7%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text, 2) : '--') } },
        { title: '采购数量', dataIndex: 'putQty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'productUnit', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '采购金额', dataIndex: 'purchaseAmount', width: '7%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '仓库', dataIndex: 'warehouseName', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '仓位', dataIndex: 'warehouseLocationName', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return sparePartsPurDetailList(Object.assign(parameter, { sparePartsPurchaseSn: this.outBizSn || this.$route.params.sn })).then(res => {
          let data
          if (res.status == 200) {
            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
          }
          this.spinning = false
          return data
        })
      },
      localDataSource: [],
      basicInfoData: null, //  基本信息
      productTotal: null //  合计
    }
  },
  computed: {
    isEdit () {
      return this.basicInfoData && this.basicInfoData.state != 'FINISH' && this.$hasPermissions('B_bulkWarehousingOrder_edit')
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ path: '/bulkManagement/bulkWarehousingOrder/list' })
    },
    // 打印预览/快捷打印
    handlePrint (type, printerType) {
      const _this = this
      _this.spinning = true
      const url = sparePartsPurDetailPrint
      const params = { sn: this.outBizSn || this.$route.params.sn, type: printerType }
      // 打印或导出
      hdPrint(printerType, type, url, params, '散件入库单', function () {
        _this.spinning = false
      })
    },
    // 基本信息
    getDetail () {
      sparePartsPurDetailBySn({ sn: this.outBizSn || this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    },
    // 合计
    getDetailCount () {
      sparePartsPurDetailCount({ sparePartsPurchaseSn: this.outBizSn || 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}/${this.basicInfoData.supplierSn}` })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab || this.outBizSn) { // 页签刷新时调用
      this.$refs.table.refresh(true)
      this.getDetail()
      this.getDetailCount()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.$refs.table.refresh(true)
      this.getDetail()
      this.getDetailCount()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

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