<template>
  <div>
    <a-card size="small" :bordered="false" class="dealerAccountNewList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="dealerAccountNewList-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="5" :sm="24">
              <a-form-item label="经销商名称">
                <custList ref="custList" dataAuthFlag="0" id="dealerAccountNewList-dealerName" placeholder="请输入经销商名称搜索" @change="custChange"></custList>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="区域">
                <subarea id="dealerAccountNewList-subarea" ref="subarea" @change="subareaChange"></subarea>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="商户级别">
                <v-select code="DEALER_LEVEL" id="dealerAccountNewList-dealerLevel" v-model="queryParam.dealerLevel" allowClear placeholder="请选择商户级别"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="商户类型">
                <dealerType id="dealerAccountNewList-dealerType" changeOnSelect v-model="dealerType" @change="getDealerType" allowClear></dealertype>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="dealerAccountNewList-refresh">查询</a-button>
              <a-button style="margin:0 5px;" @click="resetSearchForm" :disabled="disabled" id="dealerAccountNewList-reset">重置</a-button>
              <a-button
                type="primary"
                v-if="$hasPermissions('B_dealerAccountExport')"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                id="dealerAccountNewList-export">导出</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+70+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :pageSize="30"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 轮胎补贴金额 -->
          <template slot="moneyNum" slot-scope="text, record">
            <span v-if="$hasPermissions('M_dealerAccountDetail')&&(record.subsidyTire||record.subsidyTire==0)" :id="'dealerAccountNewList-tireMoney'+record.id" class="link-bule" @click="handleDetail(record)">{{ toThousands(record.subsidyTire) }}</span>
            <span v-else>{{ toThousands(record.subsidyTire) }}</span>
          </template>
          <!-- 商户类型 -->
          <template slot="dealerType" slot-scope="text, record">
            {{ record.dealerTypeName1 }}{{ record.dealerTypeName1?'>':'' }}{{ record.dealerTypeName2 }}
          </template>
        </s-table>
      </a-spin>
    </a-card>

  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import subarea from '@/views/common/subarea.js'
import dealerType from '@/views/common/dealerType.js'
import custList from '@/views/common/custList.vue'
import { hdExportExcel } from '@/libs/exportExcel'
// 接口
import { dealerAccountList, dealerAccountExport } from '@/api/dealerAccount'
export default {
  name: 'DealerAccountNewList',
  mixins: [commonMixin],
  components: { STable, VSelect, custList, subarea, dealerType },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出加载状态
      queryParam: { //  查询条件
        dealerSn: undefined, // 经销商sn
        subareaArea: {
          subareaSn: undefined, //  区域
          subareaAreaSn: undefined // 分区
        },
        dealerLevel: undefined, // 商户级别
        dealerType1: undefined, //  商户类型
        dealerType2: undefined //  商户类型
      },
      dealerType: [], // 所选商户类型 列表
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '经销商名称', dataIndex: 'dealerEntity.dealerName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '轮胎补贴金额（￥）', scopedSlots: { customRender: 'moneyNum' }, width: '12%', align: 'right' },
        { title: '区域', dataIndex: 'subareaArea.subareaName', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '分区', dataIndex: 'subareaArea.subareaAreaName', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '商户级别', dataIndex: 'dealerEntity.dealerLevelDictValue', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '商户类型', dataIndex: 'dealerType', scopedSlots: { customRender: 'dealerType' }, width: '16%', align: 'center', ellipsis: true },
        { title: '创建时间', dataIndex: 'createDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return dealerAccountList(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.disabled = false
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 客户名称 change
    custChange (val) {
      this.queryParam.dealerSn = val.key
    },
    // 区域  change
    subareaChange (val) {
      this.queryParam.subareaArea.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaArea.subareaAreaSn = val[1] ? val[1] : undefined
    },
    // 商户类型 change
    getDealerType (v, o) {
      this.queryParam.dealerType1 = v[0]
      this.queryParam.dealerType2 = v[1]
    },
    // 轮胎补贴金额  点击跳转详情页
    handleDetail (row) {
      this.$router.push({ name: 'dealerAccountDetail', params: { sn: row.dealerSn, name: row.dealerEntity.dealerName, num: row.subsidyTire } })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.dealerSn = undefined
      this.queryParam.subareaArea.subareaSn = undefined
      this.queryParam.subareaArea.subareaAreaSn = undefined
      this.queryParam.dealerLevel = undefined
      this.queryParam.dealerType1 = undefined
      this.queryParam.dealerType2 = undefined
      this.dealerType = []
      this.$refs.custList.resetForm()
      this.$refs.subarea.clearData()
      this.$refs.table.refresh(true)
    },
    //  导出
    handleExport () {
      const _this = this
      // _this.$store.state.app.curActionPermission = 'B_sales_export'
      _this.exportLoading = true
      _this.spinning = true
      hdExportExcel(dealerAccountExport, _this.queryParam, '经销商账户列表', function () {
        _this.exportLoading = false
        _this.spinning = false
        _this.showExport = true
        // _this.$store.state.app.curActionPermission = ''
      })
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 195
    }
  },
  watch: {
    '$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.$refs.table.refresh()
      this.pageInit()
    }
  }
}
</script>
