<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="80%">
    <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="总售价">
              {{ 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="收款类型">
              {{ detail.keepTypeDictValue || '--' }}
            </a-descriptions-item>
            <a-descriptions-item label="收款时间">
              {{ detail.settleTime || '--' }}
            </a-descriptions-item>
            <a-descriptions-item v-if="detail.keepType != 'RELATION_BOOK'" label="收款人">
              {{ detail.settlePersonName || '--' }}
            </a-descriptions-item>
          </a-descriptions>
        </div>
        <div v-if="detail.keepType == 'RELATION_BOOK'">
          <a-table :columns="columns" :pagination="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>
          </a-table>
        </div>
      </div>
      <div class="btn-box">
        <a-button @click="handleCommonCancel" v-if="isCancel">{{ cancelText }}</a-button>
        <a-button :loading="spinning" type="primary" @click="handlePrint()">收款打印</a-button>
      </div>
    </a-spin>
    <!-- 收款打印 -->
    <commonModal
      modalTit="收款打印预览"
      bodyPadding="10px"
      width="1024px"
      :showFooter="false"
      :openModal="showTipModal"
      @cancel="showTipModal=false">
      <printModel ref="printModel" @cancel="showTipModal=false"></printModel>
    </commonModal>
    <!-- 查看财务收款详情 -->
    <commonModal
      modalTit="财务收款详情"
      bodyPadding="10px"
      width="70%"
      :showFooter="false"
      :openModal="showDetailModal"
      @cancel="showDetailModal=false">
      <detailModal ref="detailModal"></detailModal>
    </commonModal>
  </a-modal>
</template>

<script>
import { STable, 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 { settleReceiptFindBySn } from '@/api/settleReceipt.js'
export default {
  name: 'VoucherModal',
  components: { STable, VSelect, commonModal, printModel, detailModal },
  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: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收款单号', scopedSlots: { customRender: 'bookNo' }, width: '8%', align: 'center' },
        { title: '申请人', dataIndex: 'applyPersonName', width: '8%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '财务收款事由', dataIndex: 'bookReason', width: '12%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '订单总金额', dataIndex: 'orderTotalAmount', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '收款总金额', dataIndex: 'receiptTotalAmount', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '使用授信总金额', dataIndex: 'useTotalAmount', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '授信还款总金额', dataIndex: 'payTotalAmount', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '余款抵扣总金额', dataIndex: 'balanceTotalAmount', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '审核时间', dataIndex: 'auditDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'statusDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      tableData: []
    }
  },
  methods: {
    // 财务收款详情
    viewDetail (row) {
      this.showDetailModal = true
      this.$nextTick(() => {
        this.$refs.detailModal.pageInit(row.bookSn)
      })
    },
    getData (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
      })
    },
    // 收款打印
    handlePrint () {
      this.showTipModal = true
      this.$nextTick(() => {
        this.$refs.printModel.getData(this.detail, true)
      })
    },
    // 取消
    handleCommonCancel () {
      this.$emit('cancel')
      this.detail = null
      this.tableData = []
    }
  },
  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>
