<template>
  <a-spin :spinning="spinning" tip="Loading...">
    <!-- 搜索条件 -->
    <div ref="tableSearch" class="table-page-search-wrapper newTableSearchName">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="15">
          <a-col :md="8" :sm="24">
            <a-form-item label="地区">
              <AreaList id="chooseDealer-areaList" changeOnSelect ref="areaList" @change="areaChange" defValKey="id"></AreaList>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="经销商名称/别名">
              <a-input id="chooseDealer-nameLike" v-model.trim="queryParam.nameLike" allowClear placeholder="请输入经销商名称/别名"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="商户类型">
              <dealerType id="chooseDealer-dealerType" changeOnSelect v-model="dealerType" @change="getDealerType" allowClear></dealertype>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="商户级别" prop="dealerLevel">
              <v-select code="DEALER_LEVEL" id="chooseDealer-dealerLevel" v-model="queryParam.dealerLevel" allowClear placeholder="请选择商户级别"></v-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="所在区域/分区">
              <subarea id="chooseDealer-subarea" ref="subarea" @change="subareaChange"></subarea>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24" style="margin-bottom: 10px;">
            <a-button type="primary" @click="searchForm(true)" :disabled="disabled" id="chooseDealer-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="chooseDealer-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
      <!-- 列表 -->
      <div style="margin-bottom: 10px">
        <a-button type="primary" id="chooseDealer-add" ghost :loading="loading" @click="handleBatchAudit">批量添加</a-button>
        <span style="margin-left: 5px">
          <template v-if="selectCount"> {{ `已选 ${selectCount} 项` }} </template>
        </span>
      </div>
    </div>
    <s-table
      class="sTable fixPagination"
      ref="table"
      :style="{ height: tableHeight+70+'px' }"
      :scroll="{ y: tableHeight -30 }"
      :rowKey="(record) => record.dealerSn"
      rowKeyName="dealerSn"
      :columns="columns"
      :data="loadData"
      :row-selection="{ columnWidth: 40 }"
      @rowSelection="rowSelectionFun"
      :defaultLoadData="false"
      bordered>
      <!-- 地区 -->
      <template slot="addressInfo" slot-scope="text, record">
        {{ record.provinceName }}{{ '-'+ record.cityName }}{{ '-'+ record.districtName }}
      </template>
    </s-table>
  </a-spin>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import AreaList from '@/views/common/areaList.js'
import subarea from '@/views/common/subarea.js'
import dealerType from '@/views/common/dealerType.js'
// 组件
import { dealerQueryList } from '@/api/dealer'
import { chooseDealerList } from '@/api/provinceServiceCenter'
export default {
  name: 'ChooseDealer',
  mixins: [commonMixin],
  components: { STable, VSelect, subarea, AreaList, dealerType },
  props: {
    pageType: {// 页面类型
      type: String,
      default: 'dealerPromotion'
    },
    itemSn: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      loading: false, // 批量添加loading,
      tableHeight: 0, // 表格高度
      dealerType: [], // 商户类型
      //  查询条件
      queryParam: {
        nameLike: '', // 经销商名称/别名
        dealerType1: undefined, //  商户类型
        dealerType2: undefined, //  商户类型
        dealerLevel: null, // 商户级别
        subareaArea: {
          subareaSn: undefined, //  区域
          subareaAreaSn: undefined // 分区
        },
        provinceSn: undefined, // 省sn
        citySn: undefined, // 市sn
        districtSn: undefined// 区sn
      },
      rowSelectionInfo: null, // 已选列表数据
      columns: [
        { title: '经销商名称', dataIndex: 'dealerName', width: '26%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '商户别名', dataIndex: 'dealerAlias', align: 'left', width: '26%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '商户类型', dataIndex: 'dealerTypeName', width: '18%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '商户级别', dataIndex: 'dealerLevelDictValue', width: '18%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '地区', scopedSlots: { customRender: 'addressInfo' }, width: '28%', align: 'center', ellipsis: true },
        { title: '所在区域', dataIndex: 'subareaArea.subareaName', width: '18%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所在分区', dataIndex: 'subareaArea.subareaAreaName', width: '18%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        if (this.pageType === 'dealerPromotion') {
          this.queryParam.notDealerSn = this.itemSn
          this.queryParam.pageType = this.pageType
        }
        const ajaxName = this.pageType === 'dealerPromotion' ? dealerQueryList : chooseDealerList
        return ajaxName(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
              if (data.list[i].dealerTypeName1) {
                data.list[i].dealerTypeName = data.list[i].dealerTypeName1 + '>' + data.list[i].dealerTypeName2
              }
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  computed: {
    // 计算选中数据条数
    selectCount () {
      return this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys.length
    }
  },
  methods: {
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    // 商户类型 change
    getDealerType (v, o) {
      this.queryParam.dealerType1 = v[0]
      this.queryParam.dealerType2 = v[1]
    },
    // 地区 change
    areaChange (val) {
      this.queryParam.provinceSn = val[0] ? val[0] : ''
      this.queryParam.citySn = val[1] ? val[1] : ''
      this.queryParam.districtSn = val[2] ? val[2] : ''
    },
    // 查询
    searchForm () {
      this.$refs.table.refresh()
    },
    // 所在区域/分区 change
    subareaChange (val) {
      this.queryParam.subareaArea.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaArea.subareaAreaSn = val[1] ? val[1] : undefined
    },
    //  重置
    resetSearchForm () {
      this.dealerType = []
      this.queryParam = {
        nameLike: '',
        dealerType1: undefined, //  商户类型
        dealerType2: undefined, //  商户类型
        dealerLevel: null,
        subareaArea: {
          subareaSn: undefined, //  区域
          subareaAreaSn: undefined // 分区
        },
        provinceSn: undefined,
        citySn: undefined,
        districtSn: undefined
      }
      this.$refs.subarea.clearData()
      this.$refs.areaList.clearData()
      this.$refs.table.refresh()
    },
    // 清除所选数据
    clearSelect () {
      this.$refs.table.clearSelected()
    },
    // 批量添加
    handleBatchAudit () {
      const _this = this
      if (_this.rowSelectionInfo && _this.rowSelectionInfo.selectedRowKeys && _this.rowSelectionInfo.selectedRowKeys.length < 1) {
        _this.$message.warning('请在列表勾选后再进行批量操作！')
        return
      }
      this.$emit('plAdd', _this.rowSelectionInfo.selectedRows)
    },
    // 初始化
    pageInit (data) {
      this.resetSearchForm()
      this.$nextTick(() => {
        this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const headHeight = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - headHeight - 190
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  }
}
</script>
