<template>
  <a-modal
    title="选择经销商"
    v-model="isShow"
    :footer="null"
    centered
    class="lookUpCustomers-modal"
    @cancel="isShow=false"
    width="60%"
  >
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper newTableSearchName">
        <a-form id="chooseDealer-form" layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <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-model-item label="商户级别" prop="dealerLevel">
                <v-select
                  id="chooseDealer-dealerLevel"
                  code="DEALER_LEVEL"
                  v-model="queryParam.dealerLevel"
                  allowClear
                  placeholder="请选择商户级别"></v-select>
              </a-form-model-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">
              <a-button type="primary" @click="searchForm" :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" ghost id="chooseDealer-add-btn" :loading="loading" @click="handleBatchAudit">批量添加</a-button>
          <span style="margin-left: 5px">
            <template v-if="selectCount"> {{ `已选 ${selectCount} 项` }} </template>
          </span>
        </div>
      </div>
      <a-table
        style="height: 320px;"
        :scroll="{ y: 230 }"
        :bordered="true"
        :pagination="pagination"
        :row-key="record => record.dealerSn"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :columns="columns"
        :data-source="dealerList"
        @change="handlePage"
      >
        <template slot="addressInfo" slot-scope="text, record">
          {{ record.provinceName }}{{ '-'+ record.cityName }}{{ '-'+ record.districtName }}
        </template>
      </a-table>
    </a-spin>
  </a-modal>
</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, getDealerListInfo } from '@/api/dealer'
export default {
  name: 'ChooseDealer',
  mixins: [commonMixin],
  components: { STable, VSelect, subarea, AreaList, dealerType },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    chooseInfo: {// 已选经销商列表
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      loading: false, // 批量添加loading
      dealerType: [], // 商户类型
      //  查询条件
      queryParam: {
        nameLike: '', // 经销商名称/别名
        dealerType1: undefined, //  商户类型
        dealerType2: undefined, //  商户类型
        dealerLevel: null, // 商户级别
        subareaArea: {
          subareaSn: undefined, //  区域
          subareaAreaSn: undefined // 分区
        }
      },
      selectedRowKeys: [], // 已选活动数据
      dealerList: [], // 经销商列表数据
      rowSelectionInfo: null,
      columns: [
        { title: '经销商名称', dataIndex: 'dealerName', width: '25%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '商户别名', dataIndex: 'dealerAlias', align: 'left', width: '25%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '商户类型', dataIndex: 'dealerTypeName', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '商户级别', dataIndex: 'dealerLevelDictValue', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所在区域', dataIndex: 'subareaArea.subareaName', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所在分区', dataIndex: 'subareaArea.subareaAreaName', width: '20%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 分页
      pagination: {
        pageSize: 20,
        showSizeChanger: true,
        pageSizeOptions: ['20', '50', '100', '200', '500'],
        showTotal: total => `共 ${total} 条`
      },
      pageFlag: false
    }
  },
  computed: {
    // 计算选择活动条数
    selectCount () {
      return this.selectedRowKeys && this.selectedRowKeys.length
    }
  },
  methods: {
    // 已选活动数据
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    // 分页
    handlePage (pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.pageNo = pagination.pageSize != pager.pageSize ? 1 : pagination.current
      pager.pageSize = pagination.pageSize
      pager.current = pager.pageNo
      this.pagination = pager
      this.loadData()
    },
    // 获取列表数据
    loadData () {
      this.spinning = true
      const params = { pageSize: 20, pageNo: 1, ...this.pagination }
      dealerQueryList(Object.assign(params, this.queryParam)).then(res => {
        let data
        if (res.status == 200) {
          data = res.data
          const pagination = { ...this.pagination }
          pagination.total = data.count
          const no = 0
          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.dealerList = data.list
          this.pagination = pagination
          if (this.chooseInfo.length > 0 && !this.pageFlag) {
            this.pageFlag = true
            this.selectedRowKeys = this.chooseInfo
          }
        }
        this.spinning = false
      })
    },
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    // 地区
    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.loadData()
    },
    // 区域分区
    subareaChange (val) {
      this.queryParam.subareaArea.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaArea.subareaAreaSn = val[1] ? val[1] : undefined
    },
    // 获取商户类型
    getDealerType (v, o) {
      this.queryParam.dealerType1 = v[0]
      this.queryParam.dealerType2 = v[1]
    },
    //  重置
    resetSearchForm () {
      this.dealerType = []
      this.queryParam = {
        nameLike: '', // 经销商名称/别名
        dealerType1: undefined, //  商户类型
        dealerType2: undefined, //  商户类型
        dealerLevel: null, // 商户级别
        subareaArea: {
          subareaSn: undefined, //  区域
          subareaAreaSn: undefined // 分区
        }
      }
      this.dealerType = []
      this.$refs.subarea.clearData()
      this.pagination.pageNo = 1
      this.pagination.current = 1
      this.loadData()
      this.pageFlag = false
      this.selectedRowKeys = []
    },
    // 批量添加
    async handleBatchAudit () {
      const _this = this
      if (_this.selectedRowKeys && _this.selectedRowKeys.length < 1) {
        _this.$message.warning('请在列表勾选后再进行批量操作！')
        return
      }
      this.spinning = true
      const dealerInfoList = await getDealerListInfo({ dealerSnList: _this.selectedRowKeys })
      this.spinning = false
      this.$emit('ok', dealerInfoList.data)
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.$nextTick(() => {
          this.resetSearchForm()
        })
      }
    }
  }
}
</script>
