<template>
  <a-modal
    centered
    class="collection-detail-modal"
    :footer="null"
    :maskClosable="false"
    v-model="isShow"
    :title="modalTit"
    :bodyStyle="{padding: modalTit?'25px 32px 20px':'50px 32px 15px'}"
    @cancel="handleCommonCancel"
    width="60%">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="common-main" v-if="detail">
        <div class="toolsBar">
          <a-descriptions>
            <a-descriptions-item label="销售单号">
              {{ detail.bizNo || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="备货单号">
              {{ detail.dispatchBillNo || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="发货编号">
              {{ detail.sendNo || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="客户名称">
              {{ detail.settleClientName || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="收货客户名称">
              {{ detail.receiverName || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="收款方式">
              {{ detail.settleStyleDictValue || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="产品款数">
              {{ detail.totalCategory }}
            </a-descriptions-item>
            <a-descriptions-item label="产品数量">
              {{ detail.qty }}
            </a-descriptions-item>
            <a-descriptions-item label="总售价">
              {{ $hasPermissions('B_salesCollection_record_salesPrice') ? toThousands(detail.totalAmount) : '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="业务状态">
              {{ detail.billStatusDictValue || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="财务状态">
              {{ detail.settleStateDictValue || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="单据状态">
              {{ detail.voidFlagDictValue || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="收款类型">
              <span>{{ detail.keepTypeDictValue || '--' }}</span>
              <span v-if="detail.keepTypeDictValue =='仅标记收款'" @click="handleAssociated"><a-button type="link" :loading="associatedLoading" style="margin-left:10px;" class="link-bule">
                关联收款单
              </a-button></span>
            </a-descriptions-item>
            <a-descriptions-item label="收款时间">
              {{ detail.settleTime || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="收款人">
              {{ detail.settlePersonName || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="操作时间">
              {{ detail.operateTime || '--' }}
            </a-descriptions-item>
          </a-descriptions>
        </div>
        <div v-if="detail.keepType == 'RELATION_BOOK'">
          <v-table
            class="sTable"
            :columns="columns"
            :scroll="{ y: 300 }"
            :showPagination="false"
            :data-source="tableData"
            bordered>
            <!-- 收款单号 -->
            <template slot="bookNo" slot-scope="text, record">
              <span v-if="$hasPermissions('B_fc_detail')" class="link-bule" @click="viewDetail(record)">{{ record.bookNo }}</span>
              <span v-else>{{ record.bookNo }}</span>
            </template>
          </v-table>
        </div>
      </div>
      <div class="btn-box">
        <a-button @click="handleCommonCancel" v-if="isCancel">{{ cancelText }}</a-button>
        <a-button v-if="detail&&detail.keepType =='RELATION_BOOK'" :loading="spinning" type="primary" @click="handlePlPrint()">收款打印</a-button>
      </div>
    </a-spin>
    <!-- 收款打印 -->
    <commonModal modalTit="收款打印预览" width="700pt" :showFooter="false" :openModal="showTipModal" @cancel="canselPrintView">
      <printModel ref="printModel" @cancel="canselPrintView" @ok="canselPrintView"></printModel>
    </commonModal>
    <!-- 查看财务收款详情 -->
    <commonModal
      modalTit="财务收款详情"
      bodyPadding="10px"
      width="70%"
      :showFooter="false"
      :openModal="showDetailModal"
      @cancel="showDetailModal=false">
      <detailModal ref="detailModal"></detailModal>
    </commonModal>
    <!-- 关联收款单 -->
    <collectDetailModal
      v-drag
      ref="collectDetailModal"
      modalTit="关联收款单"
      @ok="relationSuccess"
      :openModal="showCollectDetail"
      @cancel="showCollectDetail=false"></collectDetailModal>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VTable, VSelect } from '@/components'
import commonModal from '@/views/common/commonModal.vue'
import printModel from '../receiptPrint/printModel.vue'
import detailModal from '@/views/financialManagement/financialCollection/detail.vue'
import collectDetailModal from './detailModal.vue'
import { settleReceiptFindBySn } from '@/api/settleReceipt.js'
import { getProcessInstance } from '@/api/expenseManagement'
export default {
  name: 'VoucherModal',
  mixins: [commonMixin],
  components: { VTable, VSelect, commonModal, printModel, detailModal, collectDetailModal },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    modalTit: { // 弹框标题
      type: String,
      default: null
    },
    cancelText: { // 取消 按钮文字
      type: String,
      default: '关闭'
    },
    isCancel: { // 是否显示  取消 按钮
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      showTipModal: false, // 收款打印预览弹框
      showDetailModal: false, // 财务收款详情弹框
      disabled: false,
      spinning: false,
      detail: null, // 单据详情数据
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收款单号', scopedSlots: { customRender: 'bookNo' }, width: '10%', align: 'center' },
        { title: '申请人', dataIndex: 'applyPersonName', width: '6%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '财务收款事由', dataIndex: 'bookReason', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '订单总金额', dataIndex: 'orderTotalAmount', width: '8%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '收款总金额', dataIndex: 'receiptTotalAmount', width: '8%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '使用授信总金额', dataIndex: 'useTotalAmount', width: '8%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '授信还款总金额', dataIndex: 'payTotalAmount', width: '8%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '余款抵扣总金额', dataIndex: 'balanceTotalAmount', width: '8%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') },
        { title: '审核时间', dataIndex: 'auditDate', width: '11%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'statusDictValue', width: '7%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      tableData: [], // 列表数据
      associatedLoading: false, // 关联收款单 按钮 loading
      showCollectDetail: false, // 关联收款单弹窗
      handlePlData: null // 批量操作数据
    }
  },
  methods: {
    // 财务收款详情
    viewDetail (row) {
      this.showDetailModal = true
      this.$nextTick(() => {
        this.$refs.detailModal.pageInit(row.bookSn)
      })
    },
    // 获取列表数据
    getData (data) {
      this.handlePlData = data
      this.spinning = true
      settleReceiptFindBySn({ sn: data.accountReceiptSn }).then(res => {
        this.detail = res.data || null
        // 收款类型
        if (res.data.keepType !== 'LABEL_RECEIPT') {
          this.tableData = res.data && res.data.financeBookList || []
          const no = 1
          for (var i = 0; i < this.tableData.length; i++) {
            this.tableData[i].no = no + i
          }
        }
        this.spinning = false
      })
    },
    // 获取审核信息
    getAuditInfo (auditList) {
      let auditStr = ''
      if (auditList && auditList.length) {
        const auditLists = auditList.filter(item => item.processTaskType == '审批人')
        auditLists.map((a, i) => {
          auditStr = auditStr + a.approvalName + '(' + a.approvalStateDictValue + ')' + (i == auditLists.length - 1 ? '。' : '；')
        })
      }
      return auditStr
    },
    // 收款打印
    async handlePlPrint () {
      this.spinning = true
      let rows = []
      const bookSns = []
      this.tableData.map(item => {
        if (item.financeBookDetailList) {
          rows = rows.concat(item.financeBookDetailList)
        }
      })
      rows.map(item => {
        if (bookSns.indexOf(item.bookSn) < 0) {
          bookSns.push(item.bookSn)
        }
      })
      const retArr = []
      for (let i = 0; i < bookSns.length; i++) {
        // 授信明细
        const item = bookSns[i]
        const rs = rows.filter(a => a.bookSn == item)
        let detailItemUseStr = ''
        rs.map(b => {
          if (b.detailItemUseList) {
            b.detailItemUseList.map((k, i) => {
              detailItemUseStr = detailItemUseStr + k.itemName + '(' + k.itemAmount + ')' + (i == b.detailItemUseList.length - 1 ? '。' : '；')
            })
          }
        })
        const auditInfo = await getProcessInstance({ 'businessType': 'FINANCE_BOOK', 'businessSn': item }).then(res => (res.data[0] && res.data[0].opinionList) ? res.data[0].opinionList : [])
        // 审核信息
        const auditStr = this.getAuditInfo(auditInfo)
        retArr.push({ audit: auditStr, detail: detailItemUseStr, subList: rs })
      }

      if (retArr.length) {
        this.handlePrint(retArr, rows)
      } else {
        this.$message.info('此收款单明细为空或没有相关数据权限')
        this.spinning = false
      }
    },
    // 收款打印
    handlePrint (row, list) {
      this.spinning = true
      this.showTipModal = true
      this.$nextTick(() => {
        // 审核信息
        this.$refs.printModel.getData(row, list, true)
      })
    },
    // 打印预览弹框关闭
    canselPrintView () {
      this.spinning = false
      this.showTipModal = false
      this.$refs.printModel.clearData()
    },
    // 取消
    handleCommonCancel () {
      this.$emit('cancel')
      this.detail = null
      this.tableData = []
    },
    // 打开关联收款单弹窗
    handleAssociated () {
      const snList = []
      snList.push(this.handlePlData.accountReceiptSn)
      this.showCollectDetail = true
      this.$nextTick(() => {
        this.$refs.collectDetailModal.setData([this.handlePlData], snList)
      })
    },
    // 关联收款成功
    relationSuccess () {
      this.handleCommonCancel()
      this.showCollectDetail = false
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('cancel')
      }
    }
  }
}
</script>

<style lang="less">
  .collection-detail-modal{
    .common-main{
        min-height:20vh;
       .toolsBar{
         display: flex;
         align-items: center;
         margin-bottom: 10px;
         .ant-btn{
           margin-left: 30px;
         }
       }
    }
    .btn-box{
      text-align: center;
      margin-top: 30px;
      .ant-btn{
        margin:0 10px;
      }
    }
  }
</style>
