<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="isShow=false"
    width="80%">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="common-main">
        <div class="toolsBar">
          <div v-if="handlePlData.length">{{ handlePlData[0].settleClientName }}，共 {{ handlePlData && handlePlData.length }} 个备货单，合计 {{ toThousands(totalAmount) }}，已选择 {{ chooseData.length }} 个收款单</div>
          <a-button type="primary" class="button-primary" @click="handleCommonOk">{{ okText }}</a-button>
        </div>
        <div>
          <a-table :columns="columns" :scroll="{ y: 400 }" :pagination="false" :data-source="chooseData" 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>
            <!-- 操作 -->
            <template slot="action" slot-scope="text, record">
              <a-button
                size="small"
                type="link"
                class="button-error"
                @click="handleDel(record)"
              >
                删除
              </a-button>
            </template>
          </a-table>
        </div>
      </div>
      <a-divider>请选择收款单</a-divider>
      <div class="common-main">
        <!-- 搜索条件 -->
        <div ref="tableSearch" class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="15">
              <a-col :md="5" :sm="24">
                <a-form-item label="收款单号">
                  <a-input v-model.trim="queryParam.bookNo" allowClear placeholder="请输入收款单号"/>
                </a-form-item>
              </a-col>
              <a-col :md="5" :sm="24">
                <a-form-item label="财务收款事由">
                  <a-input v-model.trim="queryParam.bookReason" allowClear placeholder="请输入财务收款事由"/>
                </a-form-item>
              </a-col>
              <a-col :md="5" :sm="24">
                <a-form-item label="申请人">
                  <employee style="width: 100%;" placeholder="请选择申请人" v-model="queryParam.applyPersonSn"></employee>
                </a-form-item>
              </a-col>
              <a-col :md="5" :sm="24">
                <a-form-item label="申请时间">
                  <rangeDate ref="rangeApply" :value="applyDate" @change="dateApplyChange" />
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" :disabled="disabled" @click="$refs.table.refresh(true)">查询</a-button>
                  <a-button style="margin-left: 8px" :disabled="disabled" @click="resetSearchForm()">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 表格数据 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: 400 }"
          :pageSize="10"
          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>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              class="button-warning"
              v-if="!record.checked"
              @click="handleChoose(record)"
            >选择</a-button>
            <span style="color:green;" v-else>已选</span>
          </template>
        </s-table>
      </div>
    </a-spin>
    <!-- 查看财务收款详情 -->
    <commonModal
      modalTit="财务收款详情"
      bodyPadding="10px"
      width="70%"
      :showFooter="false"
      :openModal="showDetailModal"
      @cancel="showDetailModal=false">
      <detailModal ref="detailModal"></detailModal>
    </commonModal>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { financeBookQueryPage } from '@/api/financeBook.js'
import rangeDate from '@/views/common/rangeDate.vue'
import commonModal from '@/views/common/commonModal.vue'
import detailModal from '@/views/financialManagement/financialCollection/detail.vue'
import employee from '../../expenseManagement/expenseReimbursement/employee.js'
import { settleReceiptBookBatch } from '@/api/settleReceipt'
export default {
  name: 'GlDetailModal',
  mixins: [commonMixin],
  components: { STable, VSelect, employee, commonModal, detailModal, rangeDate },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    modalTit: { // 弹框标题
      type: String,
      default: null
    },
    okText: { // 确定 按钮文字
      type: String,
      default: '确定收款'
    },
    cancelText: { // 取消 按钮文字
      type: String,
      default: '取消'
    },
    isCancel: { // 是否显示  取消 按钮
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      disabled: false,
      spinning: false,
      showDetailModal: false,
      handlePlData: [],
      applyDate: [],
      queryParam: {
        bookNo: '',
        bookReason: '',
        applyPersonSn: undefined,
        status: 'AUDIT_PASS',
        auditBeginDate: undefined,
        auditEndDate: undefined
      },
      chooseData: [],
      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: 'payerName', width: '12%', align: 'left', 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: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '状态', dataIndex: 'statusDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '8%', align: 'center' }
      ],
      orginData: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return financeBookQueryPage(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              const index = this.chooseData.findIndex(item => data.list[i].bookSn == item.bookSn)
              data.list[i].checked = index >= 0
            }
            this.disabled = false
          }
          this.spinning = false
          this.orginData = data.list
          return data
        })
      },
      snList: []
    }
  },
  computed: {
    totalAmount () {
      let ret = 0
      this.handlePlData.map(item => {
        ret = ret + item.totalAmount
      })
      return ret.toFixed(2)
    }
  },
  methods: {
    // 财务收款详情
    viewDetail (row) {
      this.showDetailModal = true
      this.$nextTick(() => {
        this.$refs.detailModal.pageInit(row.bookSn)
      })
    },
    setData (data, snList) {
      this.handlePlData = data
      this.snList = snList
    },
    dateApplyChange (date) {
      this.queryParam.auditBeginDate = date[0]
      this.queryParam.auditEndDate = date[1]
    },
    // 删除
    handleDel (row) {
      const i = this.chooseData.findIndex(item => row.bookSn == item.bookSn)
      const oi = this.orginData.findIndex(item => row.bookSn == item.bookSn)
      this.chooseData.splice(i, 1)
      if (oi >= 0) {
        this.orginData[oi].checked = false
      }
    },
    // 选择
    handleChoose (row) {
      row.checked = true
      this.chooseData.push(row)
    },
    // 获取sn
    // 确定收款
    handleCommonOk () {
      const snList = this.snList
      const bookSnList = []
      this.chooseData.map(item => {
        bookSnList.push(item.bookSn)
      })

      if (bookSnList.length) {
        this.spinning = true
        settleReceiptBookBatch({
          bookSnList: bookSnList,
          snList: snList
        }).then(res => {
          this.spinning = false
          if (res.status == 200) {
            this.$emit('ok')
            this.$message.info(res.message)
          }
        })
      } else {
        this.$message.info('请选择关联的财务收款单')
      }
    },
    // 取消
    handleCommonCancel () {
      this.$emit('cancel')
      this.chooseData = []
    },
    // 重置
    resetSearchForm () {
      this.queryParam = {
        bookNo: '',
        bookReason: '',
        applyPersonSn: undefined,
        status: 'AUDIT_PASS'
      }
      this.orginData = []
      this.$nextTick(() => {
        this.$refs.rangeApply.resetDate()
        this.$refs.table.refresh(true)
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.handleCommonCancel()
      } else {
        this.resetSearchForm()
      }
    }
  }
}
</script>

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