<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <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="经销商名称/别名">
                <a-input id="accountStatementBill-dealerName" v-model.trim="queryParam.nameLike" allowClear placeholder="请输入经销商名称/别名"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="区域">
                <subarea id="accountStatementBill-subarea" ref="subarea" @change="subareaChange"></subarea>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="商户级别">
                <v-select code="DEALER_LEVEL" id="accountStatementBill-dealerLevel" v-model="queryParam.dealerLevel" allowClear placeholder="请选择商户级别"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="商户类型">
                <dealerType id="accountStatementBill-dealerType" changeOnSelect v-model="dealerType" @change="getDealerType" allowClear></dealertype>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="客服">
                <customerService ref="customerName" id="matchSendOutOrderList-bizUserSn" v-model="queryParam.bizUserSn"></customerService>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="财务编码">
                <a-input id="accountStatementBill-bookNo" v-model.trim="queryParam.kdMidCustomerFnumber" allowClear placeholder="请输入财务编码"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="accountStatementBill-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="accountStatementBill-reset">重置</a-button>
              <a-button
                style="margin-left: 10px"
                type="primary"
                id="conFrontModal-export"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
              >导出</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false" class="accountStatementBill-wrap">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+80+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :pageSize="30"
          :defaultLoadData="false"
          bordered>
          <template slot="orderTotalAmount" slot-scope="text, record">
            <span v-if="$hasPermissions('B_fc_detail')" class="link-bule" @click="handleDetail(record)">{{ record.orderTotalAmount }}</span>
            <span v-else>{{ record.orderTotalAmount }}</span>
          </template>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { financeBookQueryPage } from '@/api/financeBook.js'
import subarea from '@/views/common/subarea.js'
import dealerType from '@/views/common/dealerType.js'
import customerService from '@/views/common/customerService.vue'
export default {
  name: 'AccountStatementBillList',
  mixins: [commonMixin],
  components: { STable, VSelect, subarea, dealerType, customerService },
  data () {
    return {
      spinning: false,
      exportLoading: false,
      tableHeight: 0,
      dealerType: [], // 商户类型
      queryParam: { //  查询条件
        nameLike: '', // 经销商名称/别名
        dealerType1: undefined, //  商户类型
        dealerType2: undefined, //  商户类型
        dealerLevel: null, // 商户级别
        subareaSn: undefined, // 区域
        subareaAreaSn: undefined, // 分区
        bizUserSn: undefined, // 客服
        kdMidCustomerFnumber: '' // 财务编码
      },
      disabled: false, //  查询、重置按钮是否可操作
      advanced: true,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        return financeBookQueryPage(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
          return data
        })
      },
      total: 0 // 合计
    }
  },
  // 根据权限显示列表字段
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '财务编码', dataIndex: 'createDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '经销商名称', dataIndex: 'payerName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '经销商别名', dataIndex: 'payerName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '余额', scopedSlots: { customRender: 'orderTotalAmount' }, width: '8%', align: 'center' },
        { title: '客服', dataIndex: 'auditDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '区域', dataIndex: 'updateDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '分区', dataIndex: 'updateDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '商户级别', dataIndex: 'statusDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '商户类型', dataIndex: 'statusDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      return arr
    }
  },
  methods: {
    // 区域分区
    subareaChange (val) {
      this.queryParam.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaAreaSn = val[1] ? val[1] : undefined
    },
    // 商户类型 change
    getDealerType (v, o) {
      this.queryParam.dealerType1 = v[0]
      this.queryParam.dealerType2 = v[1]
    },
    //  重置
    resetSearchForm () {
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.nameLike = ''
      this.queryParam.dealerType1 = undefined
      this.queryParam.dealerType2 = undefined
      this.queryParam.dealerLevel = undefined
      this.queryParam.subareaAreaSn = undefined
      this.queryParam.subareaSn = undefined
      this.$refs.table.refresh(true)
    },
    // 详情
    handleDetail (row) {
      this.$router.push({ name: 'accountStatementBillDetail', params: { sn: row.bookSn } })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 250
    }
  },
  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">
  .accountStatementBill-wrap{
    .active{
      color: #ed1c24;
      cursor: pointer;
    }
    .common{
      color: rgba(0, 0, 0);
    }
  }
</style>
