<template>
  <div class="financialCollectionDetailwrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="financialCollectionAddcont">
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="purchaseOrderEdit-back-btn" href="javascript:;" v-if="!bookSn" @click="handleBack(0)"><a-icon type="left" /> 返回列表</a>
          <span style="margin: 0 15px;color: #666;font-weight: bold;">单号：{{ detailData&&detailData.bookNo || '--' }}</span>
          <span style="margin: 0 10px;color: #666;">申请人：{{ detailData&&detailData.applyPersonName }}</span>
          <a-button type="link" size="small" class="button-default" @click="showDetail=!showDetail">
            <a-icon :type="showDetail ? 'eye-invisible' : 'eye'"/> {{ showDetail?'隐藏':'查看' }}信息
          </a-button>
        </template>
      </a-page-header>
      <a-card :bordered="false" class="financialCollectionAddcont" v-show="showDetail">
        <a-descriptions :column="{ xs: 2, sm: 3, md: 3}" v-if="detailData" ref="tableSearch">
          <a-descriptions-item label="创建时间">{{ detailData&&detailData.createDate || '--' }}</a-descriptions-item>
          <a-descriptions-item label="收款单号">
            {{ detailData&&detailData.bookNo || '--' }}
          </a-descriptions-item>
          <a-descriptions-item label="申请人">{{ detailData&&detailData.applyPersonName || '--' }}</a-descriptions-item>
          <a-descriptions-item label="审核时间">{{ detailData&&detailData.auditDate || '--' }}</a-descriptions-item>
          <a-descriptions-item span="2" label="收款事由">{{ detailData&&detailData.bookReason || '--' }}</a-descriptions-item>
          <a-descriptions-item label="状态">{{ detailData&&detailData.statusDictValue || '--' }}</a-descriptions-item>
          <a-descriptions-item label="付款方类型">{{ detailData&&detailData.payerTypeDictValue || '--' }}</a-descriptions-item>
          <a-descriptions-item label="付款方">{{ detailData&&detailData.payerName || '--' }}</a-descriptions-item>
          <a-descriptions-item label="备注" :span="3">{{ detailData&&detailData.remarks||'--' }}</a-descriptions-item>
          <a-descriptions-item label="附件" :span="3">
            <a target="_blank" style="color: #00aaff;text-decoration: underline;margin-right: 15px;" :href="item.filePath" v-for="item in detailData.attachmentList" :key="item.id">
              {{ item.fileName }}
            </a>
          </a-descriptions-item>
        </a-descriptions>
      </a-card>
      <!-- 财务明细 -->
      <a-card :bordered="false" size="small" class="financialCollectionAddcont" v-if="$route.params.sn||bookSn">
        <div slot="title">
          <div style="display: flex;justify-content: space-between;align-items: center;">
            <div>财务明细</div>
          </div>
        </div>
        <a-alert type="info" style="margin-bottom:10px" v-if="detailData">
          <div slot="message">
            共 {{ total }} 条，
            订单金额合计 {{ toThousands(detailData.orderTotalAmount ||0) }}，
            收款金额合计 {{ toThousands(detailData.receiptTotalAmount ||0) }}，
            使用授信合计 {{ toThousands(detailData.useTotalAmount||0) }}，
            授信还款合计 {{ toThousands(detailData.payTotalAmount||0) }}，
            余款抵扣合计 {{ toThousands(detailData.balanceTotalAmount||0) }}，
            跨月打款合计 {{ toThousands(detailData.nextMonthTotalAmount||0) }}，
            特殊申请发货合计 {{ toThousands(detailData.specialTotalAmount||0) }}
          </div>
        </a-alert>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          size="small"
          :style="{ height: tableHeight+5+'px' }"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          :showPagination="false"
          bordered>
          <template slot="expandedRowRender" slot-scope="record">
            <div style="padding:10px;overflow: hidden;" v-if="record.detailItemUseList.length || record.detailItemPayList.length">
              <a-table
                style="width:45%;background:#fff;float:left;border-bottom: 1px solid #eee;"
                :bordered="false"
                :pagination="false"
                :columns="useCol"
                :data-source="record.detailItemUseList">
              </a-table>
              <a-table
                :bordered="false"
                style="width:45%;background:#fff;float:left;border-bottom: 1px solid #eee;margin-left:2%;"
                :pagination="false"
                :columns="payCol"
                :data-source="record.detailItemPayList">
              </a-table>
            </div>
            <div style="padding:10px;text-align:center;" v-else>暂无数据</div>
          </template>
          <!-- 付款账户（营业执照） -->
          <template slot="payerAccountInfo" slot-scope="record">
            {{ record.payerAccountInfo || '--' }}
          </template>
        </s-table>
      </a-card>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect, STable } from '@/components'
import { financeBookFindBySn, financeBookDetailQueryList } from '@/api/financeBook.js'
export default {
  name: 'FinancialCollectionEdit',
  mixins: [commonMixin],
  components: { VSelect, STable },
  data () {
    return {
      spinning: false,
      detailData: null,
      openDepartUserModal: false,
      showDetail: false,
      total: 0,
      bookSn: null,
      tableHeight: 300,
      loadData: parameter => {
        const params = Object.assign(parameter, { bookSn: this.$route.params.sn || this.bookSn })
        return financeBookDetailQueryList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i].no = no + i + 1
              // 计算使用授信和授信还款的合计
              data[i].useTotalAmount = this.getTotal(data[i].detailItemUseList)
              data[i].payTotalAmount = this.getTotal(data[i].detailItemPayList)
            }
            this.total = data.length
            this.disabled = false
            this.spinning = false
          }
          return data
        })
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '收款日期', dataIndex: 'receiptDate', align: 'center', width: '6%', customRender: function (text) { return text || '--' } },
        { title: '付款方类型', dataIndex: 'payerTypeDictValue', align: 'center', width: '6%', customRender: function (text) { return text || '--' } },
        { title: '付款方', dataIndex: 'payerName', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '财务编码', dataIndex: 'dealerEntity.kdMidCustomerFnumber', align: 'center', width: '6%', customRender: function (text) { return text || '--' } },
        { title: '付款账户（营业执照）', scopedSlots: { customRender: 'payerAccountInfo' }, align: 'center', width: '8%' },
        { title: '订单金额', dataIndex: 'orderAmount', align: 'right', width: '5%', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '收款金额', dataIndex: 'receiptAmount', align: 'right', width: '5%', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '使用授信', dataIndex: 'useTotalAmount', align: 'right', width: '5%', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '授信还款', dataIndex: 'payTotalAmount', align: 'right', width: '5%', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '余款抵扣', dataIndex: 'balanceAmount', align: 'right', width: '5%', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '跨月打款', dataIndex: 'nextMonthAmount', align: 'right', width: '5%', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '特殊申请发货', dataIndex: 'specialSendAmount', width: '5%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '户名', dataIndex: 'bankAccount', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '汇入银行', dataIndex: 'bankName', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '足额打款', dataIndex: 'fullPaymentFlagDictValue', align: 'center', width: '5%', customRender: function (text) { return (text == 0 || text) ? text : '--' } },
        { title: '说明', dataIndex: 'explainInfo', align: 'center', width: '10%', customRender: function (text) { return text || '--' } }
      ],
      useCol: [
        { title: '使用授信项目', dataIndex: 'itemName', width: '50%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '使用授信金额', dataIndex: 'itemAmount', width: '50%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') }
      ],
      payCol: [
        { title: '授信还款项目', dataIndex: 'itemName', width: '50%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '授信还款金额', dataIndex: 'itemAmount', width: '50%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') }
      ]
    }
  },
  watch: {
    showDetail (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  methods: {
    setTableH () {
      if (this.bookSn) {
        return 300
      }
      const tableSearchH = this.showDetail ? this.$refs.tableSearch.offsetHeight : 0
      this.tableHeight = window.innerHeight - tableSearchH - 240
    },
    getTotal (data) {
      let ret = 0
      data.map(item => {
        ret = ret + item.itemAmount
      })
      return ret.toFixed(2)
    },
    //  财务单基础详情
    getDetail () {
      this.spinning = true
      this.disabled = true
      financeBookFindBySn({ bookSn: this.$route.params.sn || this.bookSn }).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
          this.spinning = false
          this.disabled = false
        }
      })
      this.$nextTick(() => {
        this.$refs.table.refresh(true)
        this.setTableH()
      })
    },
    // 返回
    handleBack (data) {
      this.$router.push({ name: 'financialCollectionList' })
    },
    pageInit (bookSn) {
      this.bookSn = bookSn
      if (this.$route.params.sn || this.bookSn) { //  编辑页
        this.getDetail()
      }
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      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">
  .financialCollectionDetailwrap{
    position: relative;
    height: 100%;
    box-sizing: border-box;
    .financialCollectionAddcont{
      margin-bottom: 6px;
    }
    .affix{
      .ant-affix{
        z-index: 101;
      }
    }
  }
</style>
