<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" id="epenseDetail-back-btn" >
            <a-icon type="left"></a-icon>
            返回列表
          </a>
          <span style="margin: 0 15px;color: #666;font-weight: bold;">单号：{{ detailData&&detailData.expenseAccountNo || '--' }}</span>
          <span style="margin: 0 10px;color: #666;">申请人：{{ detailData&&detailData.applyPersonName }}</span>
          <a-button id="epenseDetail-showDesc-btn" type="link" size="small" class="button-default" @click="showDetail=!showDetail">
            <a-icon :type="showDetail ? 'eye-invisible' : 'eye'"/> {{ showDetail?'隐藏':'查看' }}信息
          </a-button>
        </template>
        <!-- 操作区，位于 title 行的行尾 -->
        <template slot="extra">
          <a-checkbox v-model="printRemark" id="print-remark">打印备注</a-checkbox>
          <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-info"
            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-show="showDetail">
        <a-descriptions size="small" :column="{ xs: 2, sm: 3, md: 4}" v-if="detailData">
          <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><span v-if="detailData&&detailData.expenseType3">/{{ detailData&&detailData.expenseTypeName3 || '--' }}</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) ? toThousands(detailData.applyExpenseTotal)+' 元' : '--' }}</a-descriptions-item>
          <a-descriptions-item label="状态">{{ detailData&&detailData.stateDictValue || '--' }}</a-descriptions-item>
          <a-descriptions-item label="打印状态">{{ detailData&&detailData.printStatusDictValue || '--' }}</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="关联单号" v-if="detailData&&detailData.bizNo">{{ detailData&&detailData.bizNo || '--' }}</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-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) ? toThousands(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="false"
          bordered>
          <!-- 记账名称 -->
          <template slot="jzname" slot-scope="text,record">
            <div>{{ text }}</div>
            <div v-if="record.accountNameFnumber">({{ record.accountNameFnumber }})</div>
          </template>
          <!-- 原记账名称 -->
          <template slot="yjzname" slot-scope="text,record">
            <div>{{ text == record.accountName || !text ? '--' : text }}</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 }}</div>
              <div v-else-if="item.productBrandName">{{ item.productBrandName }}<span v-if="item.productTypeShowName">/</span>{{ item.productTypeShowName }}</div>
              <div v-else>{{ item.productTypeShowName }}</div>
            </div>
            <span v-if="!record.detailProductsList||record.detailProductsList.length==0">--</span>
          </template>
        </s-table>
      </a-card>
    </a-spin>
    <!-- 打印预览 -->
    <commonModal
      modalTit="费用报销单打印预览"
      width="700pt"
      okText="立即打印"
      :openModal="showTipModal"
      @cancel="showTipModal=false"
      @ok="$refs.printModal.prints()">
      <previewModal v-if="printType=='preview'" ref="printModal" @printOk="printOk"></previewModal>
    </commonModal>
    <!-- 快捷打印 -->
    <div style="height:0;overflow: hidden;">
      <previewModal v-if="printType=='print'" ref="printModal" @printOk="printOk"></previewModal>
    </div>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import commonModal from '@/views/common/commonModal.vue'
import { expenseAccountDetail, expenseAcctDetailFindList, getProcessInstance, expenseAccountUpdateBatch } from '@/api/expenseManagement'
import previewModal from './previewModal.vue'

export default {
  name: 'ExpenseReimbursementListDetail',
  mixins: [commonMixin],
  components: { STable, previewModal, commonModal },
  data () {
    const _this = this
    return {
      spinning: false,
      disabled: false,
      baseData: null,
      showTipModal: false,
      showDetail: false,
      printType: '',
      count: 0,
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '记账类型', dataIndex: 'accountTypeDictValue', align: 'center', width: '6%', customRender: function (text) { return text || '--' } },
        { title: '记账名称', dataIndex: 'accountName', scopedSlots: { customRender: 'jzname' }, align: 'center', width: '8%' },
        { title: '原记账名称', dataIndex: 'origAccountName', scopedSlots: { customRender: 'yjzname' }, align: 'center', width: '8%' },
        { title: '所属区域', dataIndex: 'subareaNames', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所属分区', dataIndex: 'subareaAreaName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所属省份', dataIndex: 'provinceName', align: 'center', width: '7%', customRender: function (text) { return text || '--' } },
        { title: '所属城市', dataIndex: 'cityName', align: 'center', width: '7%', customRender: function (text) { return text || '--' } },
        { title: '记账费用', dataIndex: 'expense', align: 'center', width: '8%', customRender: function (text) { return (text || text == 0) ? _this.toThousands(text) : '--' } },
        { title: '费用承担方', scopedSlots: { customRender: 'fycdf' }, align: 'center', width: '12%' },
        { title: '产品信息', scopedSlots: { customRender: 'cpxx' }, align: 'center', width: '10%' },
        { title: '销售单号', dataIndex: 'salesBillNo', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'remarks', align: 'center', width: '8%', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 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
        })
      },
      detailData: null, // 详情数据
      total: 0,
      printRemark: false // 是否打印备注
    }
  },
  methods: {
    //  返回
    handleBack () {
      this.$router.push({ name: 'expenseReimbursementList' })
    },
    // 打印
    async handlePrint (type) {
      this.printType = type
      this.spinning = true
      // 费用明细
      const fyListData = await expenseAcctDetailFindList({ expenseAccountSn: this.$route.params.sn }).then(res => res.data)
      // 审核明细,待提交状态不获取
      const spListData = this.detailData.state != 'WAIT_SUBMIT' ? await getProcessInstance({ businessType: 'EXPENSES', businessSn: this.$route.params.sn }).then(res => res.data) : null
      // 打印预览弹框
      if (type == 'preview') {
        this.showTipModal = true
      }
      this.$nextTick(() => {
        this.$refs.printModal.setData([{
          printRemark: this.printRemark,
          expenseInfo: this.detailData,
          fyListData,
          spListData: spListData || []
        }])
        // 立即打印
        if (type == 'print') {
          setTimeout(() => {
            this.$refs.printModal.prints()
          }, 500)
        }
        this.spinning = false
      })
    },
    // 打印成功
    printOk (data) {
      if (data && data.status == 200) {
        expenseAccountUpdateBatch([{
          id: this.detailData.id,
          printStatus: 'PRINT'
        }]).then(res => {
          if (res.status == 200) {
            this.getDetail()
          }
          this.spinning = false
          this.showTipModal = false
        })
      } else {
        this.spinning = false
      }
    },
    //  详情
    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.$nextTick(() => {
            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: 6px;
  }
  .pages-wrap{
    min-height: calc(100vh - 140px);
  }
  .footer-cont{
    margin-top: 5px;
    text-align: center;
  }
}
</style>
