<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="6" :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="6" :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="6" :sm="24">
              <a-form-item label="客户名称">
                <dealerSubareaScopeList id="collectionDetail-Employee" ref="settleClientName" @change="custChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :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="6" :sm="24">
              <a-form-item label="营业执照名称">
                <a-input id="collectionDetail-licenseName" placeholder="请输入营业执照名称" v-model="queryParam.licenseName"></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="4" :sm="24">
                <a-form-item label="汇入银行">
                  <a-select placeholder="请选择汇入银行" 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="所属区域">
                  <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>
      </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>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
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 { financeBookDetailQueryPage, financeBookDetailExport } from '@/api/financeBook.js'
export default {
  name: 'CollectionDetailStaticList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, employee, dealerSubareaScopeList, subarea, Area },
  data () {
    return {
      spinning: false,
      exportLoading: false,
      tableHeight: 0,
      queryParam: { //  查询条件
        bookNo: '',
        auditBeginDate: '',
        auditEndDate: '',
        receiptBeginDate: '',
        receiptEndDate: '',
        applyPersonSn: undefined,
        dealerSn: undefined,
        kdMidCustomerFnumber: '',
        licenseName: '',
        fullPaymentFlag: undefined,
        bankName: undefined,
        subareaSn: undefined,
        provinceSn: undefined
      },
      disabled: false, //  查询、重置按钮是否可操作
      receiptDate: [], //  收款时间
      auditDate: [], // 审核时间
      advanced: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return financeBookDetailQueryPage(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
            }
            this.total = data.count || 0
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      total: 0, // 合计
      bankNameList: ['东莞银行', '九江银行', '建设银行', '微信收款', '农业银行']
    }
  },
  // 根据权限显示列表字段
  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: '6%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '收款事由', dataIndex: 'bookReason', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收款日期', dataIndex: 'receiptDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'dealerName', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '客户编码', dataIndex: 'kdMidCustomerFnumber', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '营业执照名称', dataIndex: 'licenseName', 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: 'useTotalAmount', align: 'center', width: '5%', customRender: function (text) { return (text == 0 || text) ? text : '--' } },
        { title: '授信还款', dataIndex: 'payTotalAmount', 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: '10%', customRender: function (text) { return text || '--' } },
        { title: '汇入银行', dataIndex: 'bankName', align: 'center', width: '10%', customRender: function (text) { return text || '--' } },
        { title: '足额打款', dataIndex: 'fullPaymentFlagDictValue', align: 'center', width: '5%', customRender: function (text) { return (text == 0 || text) ? text : '--' } },
        { title: '备注', dataIndex: 'remarks', align: 'center', width: '13%', customRender: function (text) { return text || '--' } }
      ]
      return arr
    }
  },
  methods: {
    custChange (val) {
      this.queryParam.dealerSn = val.key
    },
    //  创建时间  change
    receiptDateChange (date) {
      this.queryParam.receiptBeginDate = date[0] ? date[0] + ' 00:00:00' : ''
      this.queryParam.receiptEndDate = date[1] ? date[1] + ' 23:59:59' : ''
    },
    //  审核时间  change
    dateAuditChange (date) {
      this.queryParam.auditBeginDate = date[0] ? date[0] + ' 00:00:00' : ''
      this.queryParam.auditEndDate = date[1] ? date[1] + ' 23:59:59' : ''
    },
    //  重置
    resetSearchForm () {
      this.receiptDate = []
      this.queryParam.receiptBeginDate = ''
      this.queryParam.receiptEndDate = ''
      if (this.advanced) {
        this.$refs.receiptDate.resetDate(this.receiptDate)
      }
      this.auditDate = []
      this.$refs.rangeAuditDate.resetDate(this.auditDate)
      this.queryParam.auditBeginDate = ''
      this.queryParam.auditEndDate = ''

      this.queryParam.bookNo = ''
      this.queryParam.dispatchBillNo = ''
      this.queryParam.applyPersonSn = undefined
      this.queryParam.kdMidCustomerFnumber = ''
      this.queryParam.dealerSn = undefined
      this.queryParam.licenseName = ''
      this.queryParam.fullPaymentFlag = undefined
      this.queryParam.bankName = undefined
      this.queryParam.subareaSn = undefined
      this.queryParam.provinceSn = undefined
      this.$refs.settleClientName.resetForm()
      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 - 195
    }
  },
  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>
