<template>
  <div class="purchaseReturnDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="purchaseReturnDetail-cont" :style="{ padding: !outBizSn ? '16px 24px' : '0px 24px' }" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle" v-if="!outBizSn">
          <a id="purchaseReturnDetail-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <span style="margin: 0 15px;color: #666;">供应商名称：箭冠营销中心</span>
        </template>
        <!-- 操作区，位于 title 行的行尾 -->
        <template slot="extra" v-if="$hasPermissions('B_purchaseReturnApplyPrint')">
          <Print :disabled="dataSource.length==0" @handlePrint="handlePrint"></Print>
        </template>
      </a-page-header>
      <!-- 基础信息 -->
      <a-card size="small" :bordered="false" class="purchaseReturnDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="基础信息">
            <a-descriptions :column="3">
              <a-descriptions-item label="单据来源">{{ basicInfoData&&basicInfoData.billSourceDictValue || '--' }}</a-descriptions-item>
              <a-descriptions-item label="采购退货申请单号">{{ basicInfoData&&basicInfoData.purchaseReturnApplyNo || '--' }}</a-descriptions-item>
              <a-descriptions-item label="供应商">{{ basicInfoData&&basicInfoData.returnTargetName || '--' }}</a-descriptions-item>
              <a-descriptions-item label="业务状态">{{ basicInfoData&&basicInfoData.billStatusDictValue || '--' }}</a-descriptions-item>
              <a-descriptions-item label="创建时间">{{ basicInfoData&&basicInfoData.createDate || '--' }}</a-descriptions-item>
              <a-descriptions-item label="客服审核时间">{{ basicInfoData&&basicInfoData.auditTime || '--' }}</a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <!-- 产品详情 -->
      <a-card size="small" :bordered="false" class="purchaseReturnDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="产品详情">
            <!-- 总计 -->
            <a-alert type="info" style="margin-bottom:10px">
              <div slot="message">
                申请退货数量：<strong>{{ (basicInfoData&&(basicInfoData.totalInitialQty || basicInfoData.totalInitialQty==0)) ? basicInfoData.totalInitialQty : '--' }}</strong>；
                总公司实收数量：<strong>{{ (basicInfoData&&(basicInfoData.totalReceiveQty || basicInfoData.totalReceiveQty==0)) ? basicInfoData.totalReceiveQty : '--' }}</strong>；
                坏件数量：<strong>{{ (basicInfoData&&(basicInfoData.totalBadQty || basicInfoData.totalBadQty==0)) ? basicInfoData.totalBadQty : '--' }}</strong>；
                良品数量：<strong>{{ (basicInfoData&&(basicInfoData.totalGoodQty || basicInfoData.totalGoodQty==0)) ? basicInfoData.totalGoodQty : '--' }}</strong>；
                返库数量：<strong>{{ (basicInfoData&&(basicInfoData.totalBackStockQty || basicInfoData.totalBackStockQty==0)) ? basicInfoData.totalBackStockQty : '--' }}</strong>；
                退货金额：<strong>{{ (basicInfoData&&(basicInfoData.totalAmount || basicInfoData.totalAmount==0)) ? toThousands(basicInfoData.totalAmount, 2) : '--' }}</strong>；
              </div>
            </a-alert>
            <div v-if="basicInfoData&&basicInfoData.removeQty" style="margin-bottom:10px;display:flex;align-items:center;">
              <div>总公司删除产品明细：共 {{ basicInfoData.removeQty||'--' }} 款产品，{{ basicInfoData.removeCode&&basicInfoData.removeCode.replace(',','、') }}。</div>
              <a-button @click="exportDelProduct" type="link">导出删除产品明细</a-button>
            </div>
            <!-- 列表 -->
            <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-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import Print from '@/views/common/print.vue'
import { hdPrint } from '@/libs/JGPrint'
import { purchaseReturnDetail, purchaseReturnDetailList, purchaseReturnApplyPrint, purchaseReturnApplyExport, exportDeleteExcel } from '@/api/purchaseReturnApply'
export default {
  name: 'PurchaseReturnDetail',
  components: { STable, VSelect, Print },
  mixins: [commonMixin],
  props: {
    outBizSn: { //  有值则为弹框，无值则为页面
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      dataSource: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        return purchaseReturnDetailList(Object.assign(parameter, { purchaseReturnApplySn: 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
            }
            this.disabled = false
            this.dataSource = data.list
          }
          return data
        })
      },
      basicInfoData: null //  基本信息
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'product.code', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '申请退货数量', dataIndex: 'initQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总公司实收数量', dataIndex: 'receiveQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '坏件数量', dataIndex: 'badQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '良品数量', dataIndex: 'goodQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '返库数量', dataIndex: 'backStockQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'product.unit', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '退货原因', dataIndex: 'returnReason', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '总公司变更详情', dataIndex: 'modifyInfo', width: '15%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      if (this.$hasPermissions('M_ShowAllCost')) {
        arr.splice(9, 0, { title: '退货单价', dataIndex: 'price', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text, 2) : '--') } })
        arr.splice(10, 0, { title: '退货金额', dataIndex: 'totalAmount', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text, 2) : '--') } })
      }
      return arr
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ name: 'purchaseReturnApplyFormList' })
    },
    // 打印预览/快捷打印
    handlePrint (type, printerType) {
      const _this = this
      _this.spinning = true
      let url = purchaseReturnApplyPrint
      const params = { sn: this.$route.params.sn || this.outBizSn }
      if (type == 'export') { //  导出
        url = purchaseReturnApplyExport
      }
      // 打印或导出
      hdPrint(printerType, type, url, params, '采购退货申请单', function () {
        _this.spinning = false
      })
    },
    // 导出删除的产品
    exportDelProduct () {
      const _this = this
      _this.spinning = true
      const params = { sn: this.$route.params.sn || this.outBizSn }
      hdPrint('', 'export', exportDeleteExcel, params, '采购退货申请单删除产品明细', function () {
        _this.spinning = false
      })
    },
    // 基本信息
    getDetail () {
      purchaseReturnDetail({ sn: this.outBizSn || this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab || this.outBizSn) { // 页签刷新 或 为弹框时调用
      this.$refs.table.refresh(true)
      this.getDetail()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.$refs.table.refresh(true)
      this.getDetail()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

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