<template>
  <div class="expenseManagementDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="expenseManagementDetail-cont">
        <template slot="subTitle">
          <a href="javascript:;" @click="handleBack">
            <a-icon type="left"></a-icon>
            返回列表
          </a>
        </template>
        <!-- 操作区，位于 title 行的行尾 -->
        <template slot="extra">
          <a-button
            :disabled="detailData==null"
            key="1"
            type="default"
            v-if="$hasPermissions('B_eRPrint')"
            id="expenseManagementDetail-print-btn"
            @click="handlePrint('preview')">打印预览</a-button>
          <a-button
            :disabled="detailData==null"
            key="2"
            type="primary"
            class="button-error"
            v-if="$hasPermissions('B_eRPrint')"
            id="expenseManagementDetail-print-btn"
            @click="handlePrint('print')">快捷打印</a-button>
        </template>
      </a-page-header>
      <!-- 基础信息 -->
      <a-card size="small" :bordered="false" class="expenseManagementDetail-cont" v-if="detailData">
        <a-collapse :activeKey="['1']">
          <a-collapse-panel key="1" header="基础信息">
            <a-descriptions size="small" :column="{ xs: 2, sm: 3, md: 4}">
              <a-descriptions-item label="费用单号">{{ detailData&&detailData.expenseAccountNo || '--' }}</a-descriptions-item>
              <a-descriptions-item label="申请人">{{ detailData&&detailData.applyPersonName || '--' }}</a-descriptions-item>
              <a-descriptions-item label="申请部门">{{ detailData&&detailData.applyDepartmentName || '--' }}</a-descriptions-item>
              <a-descriptions-item label="费用类型">{{ detailData&&detailData.expenseTypeName || '--' }}<span v-if="detailData&&detailData.expenseType2">/{{ detailData&&detailData.expenseTypeName2 || '--' }}</span></a-descriptions-item>
              <a-descriptions-item label="费用发生月份">{{ detailData&&detailData.expenseDate?detailData.expenseDate.substring(0, 7) : '--' }}</a-descriptions-item>
              <a-descriptions-item label="合计金额">{{ (detailData.applyExpenseTotal || detailData.applyExpenseTotal == 0) ? detailData.applyExpenseTotal+' 元' : '--' }}</a-descriptions-item>
              <a-descriptions-item label="状态">{{ detailData&&detailData.stateDictValue || '--' }}</a-descriptions-item>
              <a-descriptions-item label="创建时间">{{ detailData&&detailData.createDate || '--' }}</a-descriptions-item>
              <a-descriptions-item label="审核时间">{{ detailData&&detailData.finishDate || '--' }}</a-descriptions-item>
              <a-descriptions-item label="主题" :span="3">{{ detailData&&detailData.title || '--' }}</a-descriptions-item>
              <a-descriptions-item label="详细说明" :span="4">
                {{ detailData.content.join('，')||'' }}
              </a-descriptions-item>
              <a-descriptions-item label="附件" :span="4">
                <a target="_blank" style="color: #00aaff;text-decoration: underline;margin-right: 15px;" :href="item.filePath" v-for="item in detailData.expenseAccountFilesList" :key="item.id">
                  {{ item.fileName }}
                </a>
              </a-descriptions-item>
            </a-descriptions>
          </a-collapse-panel>
        </a-collapse>
      </a-card>
      <a-card size="small" :bordered="false" class="pages-wrap">
        <a-alert type="info" style="margin-bottom:10px">
          <div slot="message">
            共<strong> {{ count }} </strong> 条费用明细，合计金额￥<strong v-if="detailData"> {{ (detailData.applyExpenseTotal || detailData.applyExpenseTotal == 0) ? detailData.applyExpenseTotal+' 元' : '--' }} </strong>
          </div>
        </a-alert>
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="record => record.id"
          :columns="columns"
          :data="loadData"
          :showPagination="false"
          :defaultLoadData="true"
          bordered>
          <!-- 记账名称 -->
          <template slot="jzname" slot-scope="text,record">
            <div>{{ text }}</div>
            <div v-if="record.accountNameFnumber">({{ record.accountNameFnumber }})</div>
          </template>
          <!-- 费用承担方 -->
          <template slot="fycdf" slot-scope="text,record">
            <div v-for="(item,index) in record.detailSplitList" :key="item.id+'-'+index">
              {{ item.splitObjName }}
              <span v-if="item.splitObjFnumber">({{ item.splitObjFnumber }})</span>
              <span v-if="item.childDetailSplit&&item.childDetailSplit.splitObjName">/</span>
              {{ item.childDetailSplit?item.childDetailSplit.splitObjName:'' }}: ￥{{ item.childDetailSplit?item.childDetailSplit.splitAmount:item.splitAmount }}
            </div>
          </template>
          <!-- 产品信息 -->
          <template slot="cpxx" slot-scope="text,record">
            <div v-for="item in record.detailProductsList" :key="item.id">
              <div v-if="item.productCode">{{ item.productCode }}: ￥{{ item.expense }}</div>
              <div v-else-if="item.productBrandName">{{ item.productBrandName }}<span v-if="item.productTypeShowName">/</span>{{ item.productTypeShowName }}: ￥{{ item.expense }}</div>
              <div v-else>{{ item.productTypeShowName }}: ￥{{ item.expense }}</div>
            </div>
            <span v-if="!record.detailProductsList||record.detailProductsList.length==0">--</span>
          </template>
        </s-table>
      </a-card>
    </a-spin>
    <!-- 打印预览 -->
    <previewModal :showModal="showModal" @close="showModal=false" :detailSn="$route.params.sn" :detailData="detailData"></previewModal>
    <iframe id="printfsqd" name="printfsqd" hidden></iframe>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { expenseAccountDetail, expenseAcctDetailFindList } from '@/api/expenseManagement'
import previewModal from './previewModal.vue'
import { jGPrint } from '@/libs/JGPrint.js'
export default {
  name: 'ExpenseReimbursementListDetail',
  mixins: [commonMixin],
  components: { STable, previewModal },
  data () {
    return {
      spinning: false,
      disabled: false,
      showModal: false, // 打印弹窗初始状态
      baseData: null,
      count: 0,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        const params = Object.assign({ expenseAccountSn: this.$route.params.sn })
        return expenseAcctDetailFindList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.count = res.data.length
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i].no = no + i + 1
            }
          }
          this.spinning = false
          return data
        })
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '记账类型', dataIndex: 'accountTypeDictValue', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '记账名称', dataIndex: 'accountName', scopedSlots: { customRender: 'jzname' }, align: 'center', width: '14%' },
        { title: '所属区域', dataIndex: 'subareaNames', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '所属省份', dataIndex: 'provinceName', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '所属城市', dataIndex: 'cityName', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '记账费用', dataIndex: 'expense', align: 'center', width: '10%', customRender: function (text) { return text || '--' } },
        { title: '费用承担方', scopedSlots: { customRender: 'fycdf' }, align: 'center', width: '15%' },
        { title: '产品信息', scopedSlots: { customRender: 'cpxx' }, align: 'center', width: '15%' },
        { title: '备注', dataIndex: 'remarks', align: 'center', width: '10%', customRender: function (text) { return text || '--' } }
      ],
      chooseLoadData: [],
      detailData: null, //  详情数据
      total: 0
    }
  },
  methods: {
    //  返回
    handleBack () {
      this.$router.push({ name: 'expenseReimbursementList' })
    },
    // 打印
    handlePrint (type) {
      const html = document.getElementById('printTest').innerHTML
      jGPrint(html, type)
    },
    //  详情
    getDetail () {
      this.spinning = true
      this.detailData = null
      expenseAccountDetail({ expenseAccountSn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.spinning = false
          this.detailData = res.data
          this.detailData.content = res.data.content.replace(/\n+/, '\n').split('\n')
          this.$refs.table.refresh()
        } else {
          this.detailData = null
        }
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.getDetail()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.getDetail()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
.expenseManagementDetail-wrap {
  .expenseManagementDetail-cont {
    margin-bottom: 10px;
  }
  .footer-cont{
    margin-top: 5px;
    text-align: center;
  }
}
</style>
