<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="link"
            class="button-default"
            id="allocateBillDetail-edit-btn"
            @click.stop="handleEdit">
            <a-icon type="edit"/>编辑
          </a-button>
          <a-button type="link" class="button-default" @click="showDetail=!showDetail">
            <a-icon :type="showDetail ? 'eye-invisible' : 'eye'"/> {{ showDetail?'隐藏':'查看' }}信息
          </a-button>
        </template>
        <!-- 操作区，位于 title 行的行尾 -->
        <template slot="extra" v-if="pageType=='pages'">
          <print :params="{allocateReturnSn: $route.params.sn || outBizSn}" :basicInfoData="basicInfoData" :disabled="localDataSource.length==0" @loading="spinning=true" @unloading="spinning=false"></print>
        </template>
      </a-page-header>
      <div ref="tableSearch">
        <a-card size="small" :bordered="false" :class="pageType=='pages'?'allocateBillDetail-cont':''" v-show="showDetail">
          <a-descriptions :column="3" v-if="pageType=='pages'">
            <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.warehouseName) || '--' }}</a-descriptions-item>
            <a-descriptions-item label="费用类型">
              {{ (basicInfoData&&basicInfoData.costTypeName) || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="调拨退货类型">
              <div>
                <span v-if="basicInfoData&&basicInfoData.allocateSortName">{{ basicInfoData.allocateSortName || '--' }}</span>
                <span v-if="basicInfoData&&basicInfoData.allocateReturnTypeName">/{{ basicInfoData.allocateReturnTypeName || '--' }}</span>
              </div>
            </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 :activeKey="['1']" v-else>
            <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.warehouseName) || '--' }}</a-descriptions-item>
                <a-descriptions-item label="费用类型">
                  {{ (basicInfoData&&basicInfoData.costTypeName) || '--' }}
                </a-descriptions-item>
                <a-descriptions-item label="调拨退货类型">
                  <div>
                    <span v-if="basicInfoData&&basicInfoData.allocateSortName">{{ basicInfoData.allocateSortName || '--' }}</span>
                    <span v-if="basicInfoData&&basicInfoData.allocateReturnTypeName">/{{ basicInfoData.allocateReturnTypeName || '--' }}</span>
                  </div>
                </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>
      </div>
      <a-card size="small" :bordered="false">
        <!-- 总计 -->
        <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 v-if="showPrice||$hasPermissions('M_transferReturnDetail_salesPrice')">退货总金额：<strong>{{ (basicInfoData&&(basicInfoData.totalPrice || basicInfoData.totalPrice==0)) ? toThousands(basicInfoData.totalPrice) : '--' }}</strong>；</span>
          </div>
        </a-alert>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :style="{ height:outBizSn? '260px': tableHeight+70+'px' }"
          :scroll="{ y:outBizSn?260: tableHeight }"
          :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: ''
    },
    showPrice: {// 是否显示价格
      type: Boolean,
      default: false
    },
    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: [], // 表格数据
      showDetail: true, // 显示隐藏基础信息
      tableHeight: 0// 表格高度
    }
  },
  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 _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'product.code', width: '10%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', width: '27%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '单位', dataIndex: 'product.unit', width: '5%', align: 'center', customRender: function (text) { return 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 : '--') } }
      ]

      if (this.basicInfoData && this.basicInfoData.grabFlag == '1') {
        arr.splice(1, 0, { title: '调拨单号', dataIndex: 'allocateNo', width: '10%', align: 'center', customRender: function (text) { return text || '--' } })
      }
      if (this.showPrice || this.$hasPermissions('M_transferReturnDetail_salesPrice')) {
        const ind = this.basicInfoData && this.basicInfoData.grabFlag == '1' ? 1 : 0
        arr.splice(4 + ind, 0, { title: '退货单价', dataIndex: 'price', width: '9%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
        arr.splice(8 + ind, 0, { title: '退货金额', dataIndex: 'totalReturnPrice', width: '9%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      return arr
    }
  },
  watch: {
    // 显示隐藏基础信息  监听表格高度变化
    showDetail (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    },
    // 根据页面类型计算表格高度
    outBizSn (newValue, oldValue) {
      if (newValue) {
        this.pageInit()
      }
    }
  },
  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()
        this.setTableH()
      }
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 285
    }
  },
  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()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

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