<template>
  <div class="allocateBillDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="allocateBillDetail-back" :style="{ padding: pageType=='pages' ? '16px 24px' : '0px 24px' }" >
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle" v-if="pageType=='pages'">
          <a id="allocateBillDetail-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="allocateBillDetail-edit-btn"
            @click.stop="handleEdit">编辑</a-button>
        </template>
        <!-- 操作区，位于 title 行的行尾 -->
        <template slot="extra" v-if="pageType=='pages'">
          <print :params="{allocateReturnSn: $route.params.sn || outBizSn}" :disabled="localDataSource.length==0" @loading="spinning=true" @unloading="spinning=false"></print>
        </template>
      </a-page-header>
      <a-card size="small" :bordered="false" class="allocateBillDetail-cont">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="基础信息">
            <a-descriptions :column="3">
              <a-descriptions-item label="调拨退货单号">{{ (basicInfoData&&basicInfoData.allocateReturnNo) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="调拨退货对象">{{ (basicInfoData&&basicInfoData.targetTypeDictValue) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="调拨退货对象名称">{{ (basicInfoData&&basicInfoData.targetName) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="调拨退货类型">{{ (basicInfoData&&basicInfoData.allocateReturnTypeName) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="是否抓单">{{ basicInfoData&&basicInfoData.grabFlagDictValue || '--' }}</a-descriptions-item>
              <a-descriptions-item label="业务状态">{{ (basicInfoData&&basicInfoData.stateDictValue) || '--' }}</a-descriptions-item>
              <a-descriptions-item label="备注">{{ (basicInfoData&&basicInfoData.remarks) || '--' }}</a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <a-card size="small" :bordered="false" class="allocateBillDetail-cont">
        <!-- 总计 -->
        <a-alert type="info" style="margin-bottom:10px">
          <div slot="message">
            退货总数量：<strong>{{ (basicInfoData&&(basicInfoData.totalQty || basicInfoData.totalQty==0)) ? basicInfoData.totalQty : '--' }}</strong> ，
            坏件总数量：<strong>{{ (basicInfoData&&(basicInfoData.totalBadQty || basicInfoData.totalBadQty==0)) ? basicInfoData.totalBadQty : '--' }}</strong> ，
            返库总数量：<strong>{{ (basicInfoData&&(basicInfoData.totalBackStockQty || basicInfoData.totalBackStockQty==0)) ? basicInfoData.totalBackStockQty : '--' }}</strong> ，
            <span>退货总金额（￥）：<strong>{{ (basicInfoData&&(basicInfoData.totalPrice || basicInfoData.totalPrice==0)) ? basicInfoData.totalPrice : '--' }}</strong></span>；
          </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-card>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import print from './print.vue'
import { allocReturnDetailQueryPage, allocateReturnQueryBySn } from '@/api/allocateReturn'
export default {
  name: 'TransferReturnDetail',
  mixins: [commonMixin],
  components: { STable, VSelect, print },
  props: {
    outBizSn: { //  有值则为弹框，无值则为页面
      type: [Number, String],
      default: ''
    },
    pageType: {
      type: String,
      default: 'pages'
    }
  },
  data () {
    return {
      spinning: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        return allocReturnDetailQueryPage(Object.assign(parameter, { allocateReturnSn: 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.localDataSource = data.list
          }
          this.spinning = false
          return data
        })
      },
      basicInfoData: null, //  基本信息
      localDataSource: [],
      openModal: false,
      nowType: null
    }
  },
  computed: {
    isEdit () {
      return this.basicInfoData && ((this.basicInfoData.state == 'WAIT_SUBMIT') || (this.basicInfoData.state == 'FINANCIAL_REJECT') || (this.basicInfoData.state == 'AUDIT_REJECT')) && this.$hasPermissions('B_transferReturnEdit')
    },
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'product.code', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', width: '27%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单位', dataIndex: 'product.unit', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '退货单价(￥)', dataIndex: 'price', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '退货数量', dataIndex: 'returnQty', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '坏件数量', dataIndex: 'badQty', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '返库数量', dataIndex: 'backStockQty', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '退货金额(￥)', dataIndex: 'totalReturnPrice', width: '9%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ]

      if (this.basicInfoData && this.basicInfoData.grabFlag == '1') {
        arr.splice(1, 0, { title: '调拨单号', dataIndex: 'allocateNo', width: '10%', align: 'center', customRender: function (text) { return text || '--' } })
      }
      return arr
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ name: 'transferReturnList', query: { closeLastOldTab: true } })
    },
    // 基本信息
    getDetail () {
      allocateReturnQueryBySn({ allocateReturnSn: this.outBizSn || this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.basicInfoData = res.data
        } else {
          this.basicInfoData = null
        }
      })
    },
    //  编辑
    handleEdit () {
      const row = this.basicInfoData
      this.$router.push({ name: row.grabFlag == 1 ? 'transferReturnGrpEdit' : 'transferReturnEdit', params: { sn: row.allocateReturnSn, targetType: row.targetType, dealerLevel: row.dealerLevel } })
    },
    pageInit () {
      if (this.outBizSn || this.$route.params.sn) {
        this.$refs.table.refresh(true)
        this.getDetail()
      }
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab || this.outBizSn) { // 页签刷新 或 为弹框时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.pageInit()
    }
  },
  watch: {
    outBizSn (newValue, oldValue) {
      if (newValue) {
        this.pageInit()
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .allocateBillDetail-wrap{
    .allocateBillDetail-back{
      margin-bottom: 10px;
    }
    .allocateBillDetail-cont{
      margin-bottom: 10px;
    }
  }
</style>
