<template>
  <a-card size="small" :bordered="false" class="collectionDetail-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="审核时间">
                <rangeDate ref="rangeAuditDate" :value="auditDate" @change="dateAuditChange" />
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="收款单号">
                <a-input id="collectionDetail-bookNo" v-model.trim="queryParam.bookNo" allowClear placeholder="请输入收款单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="申请人">
                <employee style="width: 100%;" id="collectionDetail-Employee" placeholder="请选择申请人" v-model="queryParam.applyPersonSn"></employee>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="24">
              <a-form-item label="收款日期">
                <rangeDate ref="receiptDate" :value="receiptDate" @change="receiptDateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="付款方类型">
                <v-select
                  v-model="queryParam.payerType"
                  code="PAYER_TYPE"
                  placeholder="请选择付款方类型"
                ></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="付款方">
                <a-input id="financialCollectionList-payerName" v-model.trim="queryParam.payerName" allowClear placeholder="请输入付款方"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="财务编码">
                <a-input id="collectionDetail-kdMidCustomerFnumber" placeholder="请输入财务编码" v-model="queryParam.kdMidCustomerFnumber"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="付款账户">
                <a-input id="collectionDetail-payerAccountInfo" placeholder="请输入付款账户" v-model="queryParam.payerAccountInfo"></a-input>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="4" :sm="24">
                <a-form-item label="足额打款">
                  <v-select
                    v-model="queryParam.fullPaymentFlag"
                    ref="fullPaymentFlag"
                    id="collectionDetail-fullPaymentFlag"
                    code="FLAG"
                    placeholder="请选择是否足额打款"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="3" :sm="24">
                <a-form-item label="户名/汇入银行">
                  <a-select placeholder="请选择户名" allowClear v-model="queryParam.bankAccount" style="width: 100%" @change="handleChange">
                    <a-select-option v-for="item in bankList" :value="item.bankAccount">
                      {{ item.bankAccount }}
                    </a-select-option>
                  </a-select>
                </a-form-item></a-col>
              <a-col :md="3" :sm="24">
                <a-form-item>
                  <a-select placeholder="请选择汇入银行" allowClear v-model="queryParam.bankName" style="width: 100%">
                    <a-select-option v-for="item in bankNameList" :value="item">
                      {{ item }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="开票状态">
                  <a-select v-model="queryParam.invoiceDateType" style="width: 100%" placeholder="请选择开票状态">
                    <a-select-option value="1">
                      已开票
                    </a-select-option>
                    <a-select-option value="0">
                      未开票
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="开票日期">
                  <rangeDate ref="invoiceDate" :value="invoiceDate" @change="invoiceDateChange" />
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="所属区域">
                  <subarea id="collectionDetail-subarea" v-model="queryParam.subareaSn"></subarea>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="所属省份">
                  <Area id="collectionDetail-provinceSn" v-model="queryParam.provinceSn" placeholder="请选择省"></Area>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="collectionDetail-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="collectionDetail-reset">重置</a-button>
              <a-button
                style="margin-left: 10px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('M_FC_Details_Export')"
                id="sendOut-export">导出</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form>
        <a-alert type="info" style="margin-bottom:10px">
          <div slot="message">
            订单总金额：{{ countData&&countData.orderAmount||0 }}元；
            收款总金额：{{ countData&&countData.receiptAmount||0 }}元；
            使用授信总金额：{{ countData&&countData.useCreditAmount||0 }}元；
            授信还款总金额：{{ countData&&countData.payCreditAmount||0 }}元；
            余款抵扣总金额：{{ countData&&countData.balanceAmount||0 }}元；
            跨月打款总金额：{{ countData&&countData.nextMonthAmount||0 }}元；
          </div>
        </a-alert>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        bordered>
        <!-- 开票日期 -->
        <template slot="draftDate" slot-scope="text, record">
          <div @click="handleInvoice(record)">
            <span v-if="record.invoiceDate" class="link-bule">{{ record.invoiceDate }}</span>
            <span class="link-bule" v-else>--</span>
          </div>
        </template>
        <!-- 收款打印状态 -->
        <template slot="printStatus" slot-scope="text, record">
          <span v-if="record.printStatus=='NO_PRINT' || record.printStatus=='PRINT'">{{ record.printStatusDictValue }}</span>
          <span @click="handlePrint(record)" class="link-bule" v-else>{{ record.printStatusDictValue }}</span>
        </template>
        <!-- 备注 -->
        <template slot="note" slot-scope="text, record">
          <div @click="handleNote(record)">
            <span class="link-bule">{{ record.remarks || '--' }}</span>
          </div>
        </template>
      </s-table>
    </a-spin>
    <!-- 开票日期弹窗 -->
    <invoiceModal v-drag :show="invoiceIsShow" :info="invoiceInfo" @cancel="cancel"></invoiceModal>
    <!-- 备注弹窗 -->
    <noteModal ref="remarksModal" v-drag :show="noteIsShow" @cancel="cancel"></noteModal>
    <!-- 收款打印状态 -->
    <printModal v-drag :show="printIsShow" :info="printInfo" @cancel="cancel"></printModal>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
import { STable, VSelect } from '@/components'
import getDate from '@/libs/getDate.js'
import rangeDate from '@/views/common/rangeDate.vue'
import subarea from '@/views/common/subarea.js'
import Area from '@/views/common/area.js'
import { hdExportExcel } from '@/libs/exportExcel'
import employee from '../../expenseManagement/expenseReimbursement/employee.js'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
import invoiceModal from './invoiceModal.vue'
import noteModal from './noteModal.vue'
import printModal from './printModal.vue'
import { financeBookDetailQueryPage, financeBookDetailExport, financeBookReportDetailCount } from '@/api/financeBook.js'
export default {
  name: 'CollectionDetailStaticList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, employee, dealerSubareaScopeList, subarea, Area, invoiceModal, noteModal, printModal },
  data () {
    return {
      spinning: false,
      exportLoading: false,
      tableHeight: 0,
      queryParam: { //  查询条件
        bookNo: '',
        auditBeginDate: getDate.getCurrMonthDays().starttime,
        auditEndDate: getDate.getCurrMonthDays().endtime,
        receiptBeginDate: '',
        receiptEndDate: '',
        applyPersonSn: undefined,
        payerName: undefined,
        kdMidCustomerFnumber: '',
        payerAccountInfo: '',
        fullPaymentFlag: undefined,
        bankAccount: undefined,
        bankName: undefined,
        subareaSn: undefined,
        provinceSn: undefined,
        invoiceDateType: undefined,
        invoiceBeginDate: '',
        invoiceEndDate: '',
        payerType: undefined
      },
      invoiceIsShow: false,
      noteIsShow: false,
      printIsShow: false,
      disabled: false, //  查询、重置按钮是否可操作
      receiptDate: [], //  收款时间
      invoiceDate: [], // 开票日期
      auditDate: [
        getDate.getCurrMonthDays().starttime,
        getDate.getCurrMonthDays().endtime
      ], // 审核时间
      advanced: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        params.invoiceDate = params.invoiceDate ? moment(params.invoiceDate).format('YYYY-MM-DD') : ''
        return financeBookDetailQueryPage(params).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
            }
            this.total = data.count || 0
            this.disabled = false
          }
          this.spinning = false
          this.getCount(params)
          return data
        })
      },
      total: 0, // 合计
      countData: null,
      bankList: [
        {
          bankAccount: '张丽友',
          bankName: ['建设银行', '微信收款']
        },
        {
          bankAccount: '张丽添',
          bankName: ['农业银行']
        },
        {
          bankAccount: '东莞箭冠',
          bankName: ['东莞银行']
        },
        {
          bankAccount: '江西箭冠',
          bankName: ['九江银行']
        }
      ],
      bankNameList: [],
      invoiceInfo: null,
      printInfo: null
    }
  },
  // 根据权限显示列表字段
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '审核时间', dataIndex: 'auditDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收款单号', dataIndex: 'bookNo', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '申请人', dataIndex: 'applyPersonName', align: 'center', width: '5%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '收款事由', dataIndex: 'bookReason', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收款日期', dataIndex: 'receiptDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '付款方类型', dataIndex: 'payerTypeDictValue', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '付款方', dataIndex: 'payerName', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '财务编码', dataIndex: 'kdMidCustomerFnumber', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '付款账户（营业执照）', dataIndex: 'payerAccountInfo', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '订单金额', dataIndex: 'orderAmount', align: 'center', width: '5%', customRender: function (text) { return (text == 0 || text) ? text : '--' } },
        { title: '收款金额', dataIndex: 'receiptAmount', align: 'center', width: '5%', customRender: function (text) { return (text == 0 || text) ? text : '--' } },
        { title: '使用授信', dataIndex: 'useCreditAmount', align: 'center', width: '5%', customRender: function (text) { return (text == 0 || text) ? text : '--' } },
        { title: '授信还款', dataIndex: 'payCreditAmount', align: 'center', width: '5%', customRender: function (text) { return (text == 0 || text) ? text : '--' } },
        { title: '余款抵扣', dataIndex: 'balanceAmount', align: 'center', width: '5%', customRender: function (text) { return (text == 0 || text) ? text : '--' } },
        { title: '跨月打款', dataIndex: 'nextMonthAmount', align: 'center', width: '5%', customRender: function (text) { return (text == 0 || text) ? text : '--' } },
        { title: '户名', dataIndex: 'bankAccount', align: 'center', width: '5%', customRender: function (text) { return text || '--' } },
        { title: '汇入银行', dataIndex: 'bankName', align: 'center', width: '5%', customRender: function (text) { return text || '--' } },
        { title: '足额打款', dataIndex: 'fullPaymentFlagDictValue', align: 'center', width: '5%', customRender: function (text) { return (text == 0 || text) ? text : '--' } },
        { title: '说明', dataIndex: 'explainInfo', align: 'center', width: '8%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '开票日期', scopedSlots: { customRender: 'draftDate' }, align: 'center', width: '8%' },
        { title: '收款打印状态', scopedSlots: { customRender: 'printStatus' }, align: 'center', width: '8%' },
        { title: '备注', scopedSlots: { customRender: 'note' }, align: 'center', width: '8%' }
      ]
      return arr
    }
  },
  methods: {
    // 关闭弹窗刷新表格
    cancel () {
      this.invoiceIsShow = false
      this.noteIsShow = false
      this.printIsShow = false
      this.$refs.table.refresh(true)
    },
    getCount (params) {
      financeBookReportDetailCount(params).then(res => {
        this.countData = res.data
      })
    },
    handleChange (v) {
      if (v) {
        const row = this.bankList.find(item => item.bankAccount == v)
        this.bankNameList = row.bankName
        this.queryParam.bankName = undefined
      } else {
        this.queryParam.bankAccount = undefined
        this.queryParam.bankName = undefined
      }
    },
    //  收款日期  change
    receiptDateChange (date) {
      this.queryParam.receiptBeginDate = date[0] ? date[0] : ''
      this.queryParam.receiptEndDate = date[1] ? date[1] : ''
    },
    //  开票时间  change
    invoiceDateChange (date) {
      this.queryParam.invoiceBeginDate = date[0] ? date[0] : ''
      this.queryParam.invoiceEndDate = date[1] ? date[1] : ''
    },
    //  审核时间  change
    dateAuditChange (date) {
      this.queryParam.auditBeginDate = date[0] ? date[0] : ''
      this.queryParam.auditEndDate = date[1] ? date[1] : ''
    },
    //  重置
    resetSearchForm () {
      // 审核日期
      this.auditDate = [
        getDate.getCurrMonthDays().starttime,
        getDate.getCurrMonthDays().endtime
      ]
      this.$refs.rangeAuditDate.resetDate(this.auditDate)
      this.queryParam.auditBeginDate = getDate.getCurrMonthDays().starttime
      this.queryParam.auditEndDate = getDate.getCurrMonthDays().endtime
      // 收款日期
      this.receiptDate = []
      this.queryParam.receiptBeginDate = ''
      this.queryParam.receiptEndDate = ''
      this.$refs.receiptDate.resetDate(this.receiptDate)

      // 开票日期
      this.invoiceDate = []
      this.queryParam.invoiceBeginDate = ''
      this.queryParam.invoiceEndDate = ''
      if (this.advanced) {
        this.$refs.invoiceDate.resetDate(this.invoiceDate)
      }

      this.queryParam.bookNo = ''
      this.queryParam.dispatchBillNo = ''
      this.queryParam.applyPersonSn = undefined
      this.queryParam.kdMidCustomerFnumber = ''
      this.queryParam.payerName = undefined
      this.queryParam.payerAccountInfo = ''
      this.queryParam.fullPaymentFlag = undefined
      this.queryParam.bankName = undefined
      this.queryParam.bankAccount = undefined
      this.queryParam.subareaSn = undefined
      this.queryParam.provinceSn = undefined
      this.queryParam.invoiceDateType = undefined
      this.queryParam.invoiceDate = ''
      this.queryParam.payerType = undefined
      this.$refs.table.refresh(true)
    },
    //  导出
    handleExport () {
      const _this = this
      _this.exportLoading = true
      _this.spinning = true
      hdExportExcel(financeBookDetailExport, _this.queryParam, '财务收款明细统计', function () {
        _this.exportLoading = false
        _this.spinning = false
      })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 205
    },
    handleInvoice (con) {
      const obj = {
        no: con.bookNo,
        payerName: con.payerName,
        bookDetailSn: con.bookDetailSn
      }
      this.invoiceInfo = obj
      this.$nextTick(() => {
        this.invoiceIsShow = true
      })
    },
    // 修改备注
    handleNote (info) {
      const obj = {
        no: info.bookNo,
        payerName: info.payerName,
        id: info.id,
        remarks: info.remarks ? info.remarks : ''
      }
      this.noteIsShow = true
      const _this = this
      _this.$nextTick(() => {
        _this.$refs.remarksModal.setData(JSON.stringify(obj))
      })
    },
    handlePrint (item) {
      const obj = {
        no: item.bookNo,
        payerName: item.payerName,
        id: item.id,
        printStatus: item.printStatus
      }
      this.printInfo = obj
      this.$nextTick(() => {
        this.printIsShow = true
      })
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .collectionDetail-wrap{
    .active{
      color: #ed1c24;
      cursor: pointer;
    }
    .common{
      color: rgba(0, 0, 0);
    }
  }
</style>
