<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <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="4" :sm="24">
              <a-form-model-item label="地区">
                <Area id="dealerZoneSearch-dealerProvinceSn" v-model="queryParam.dealer.provinceSn" placeholder="请选择省"></Area>
              </a-form-model-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="所属区域/分区">
                <subarea id="dealerZoneSearch-subareaSn" ref="subarea" @change="subareaChange"></subarea>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="经销商">
                <custList id="dealerZoneSearch-dealerName" placeholder="请输入经销商名称搜索" ref="indirectDealer" @change="dealerChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="客服/品类经理/区域负责人/销售总监">
                <a-input id="dealerZoneSearch-bizUserName" v-model.trim="queryParam.bizUserName" allowClear placeholder="请输入客服/品类经理/区域负责人/销售总监"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="dealerZoneSearch-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="dealerZoneSearch-reset">重置</a-button>
              <a-button
                style="margin-left: 5px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :loading="exportLoading"
                id="dealerZoneSearch-export">导出</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false" class="dealerZoneSearch-wrap">
      <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="kf" slot-scope="text, record">
            <a-tooltip placement="top" :id="'dealerZoneSearch-userNameKfs'+record.id" v-if="record.userNameKfs">
              <template slot="title">
                {{ record.userNameKfs }}
              </template>
              <div class="line-2">{{ record.userNameKfs }}</div>
            </a-tooltip>
            <span v-else>--</span>
          </template>
          <template slot="qyfzr" slot-scope="text, record">
            <a-tooltip placement="top" :id="'dealerZoneSearch-userNameQyfzrs'+record.id" v-if="record.userNameQyfzrs">
              <template slot="title">
                {{ record.userNameQyfzrs }}
              </template>
              <div class="line-2">{{ record.userNameQyfzrs }}</div>
            </a-tooltip>
            <span v-else>--</span>
          </template>
          <template slot="xszj" slot-scope="text, record">
            <a-tooltip placement="top" :id="'dealerZoneSearch-userNameXszjs'+record.id" v-if="record.userNameXszjs">
              <template slot="title">
                {{ record.userNameXszjs }}
              </template>
              <div class="line-2">{{ record.userNameXszjs }}</div>
            </a-tooltip>
            <span v-else>--</span>
          </template>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { exportExcel } from '@/libs/JGPrint.js'
// 组件
import { STable } from '@/components'
import subarea from '@/views/common/subarea.js'
import custList from '@/views/common/custList.vue'
import Area from '@/views/common/area.js'
// 接口
import { queryDealerWithBizuser, exportDealerWithBizuser } from '@/api/dealerRelation'
export default {
  name: 'DealerZoneSearch',
  mixins: [commonMixin],
  components: { STable, subarea, custList, Area },
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0, // 表格高度
      exportLoading: false, // 导出loading
      // 查询条件
      queryParam: {
        subareaSn: undefined, // 区域
        subareaAreaSn: undefined, // 分区
        dealer: {
          dealerSn: '', // 经销商sn
          dealerName: '', // 经销商名称
          provinceSn: undefined// 地区
        },
        bizUserName: '' // 客服/品类经理/区域负责人/销售总监
      },
      columns: [
        { title: '经销商名称', dataIndex: 'dealer.dealerName', width: '12%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '省份', dataIndex: 'dealer.provinceName', width: '7%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所属区域', dataIndex: 'subareaName', width: '7%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '所属分区', dataIndex: 'subareaAreaName', width: '7%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客服', scopedSlots: { customRender: 'kf' }, dataIndex: 'userNameKfs', width: '30%', align: 'center' },
        { title: '品类经理', dataIndex: 'userNamePljls', width: '14%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '区域负责人', scopedSlots: { customRender: 'qyfzr' }, dataIndex: 'userNameQyfzrs', width: '11%', align: 'center' },
        { title: '销售总监', scopedSlots: { customRender: 'xszj' }, dataIndex: 'userNameXszjs', width: '7%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        // 获取列表数据 有分页
        return queryDealerWithBizuser(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 导出
    handleExport () {
      const _this = this
      const params = this.queryParam
      this.spinning = true
      this.exportLoading = true
      exportExcel(exportDealerWithBizuser, params, '经销商所属分区导出', function () {
        _this.spinning = false
        _this.exportLoading = false
      })
    },
    // 区域分区
    subareaChange (val) {
      this.queryParam.subareaSn = val[0] ? val[0] : undefined
      this.queryParam.subareaAreaSn = val[1] ? val[1] : undefined
    },
    //  重置
    resetSearchForm () {
      this.queryParam.subareaSn = undefined
      this.queryParam.subareaAreaSn = undefined
      this.queryParam.dealer.dealerSn = ''
      this.queryParam.dealer.dealerName = ''
      this.queryParam.dealer.provinceSn = undefined
      this.queryParam.bizUserName = ''
      this.$refs.indirectDealer.resetForm()
      this.$refs.subarea.clearData()
      this.$refs.table.refresh(true)
    },
    // 选择经销商
    dealerChange (val) {
      this.queryParam.dealer.dealerSn = val.key
      this.queryParam.dealer.dealerName = val.key ? (val.label.replace('\n', '')).trim() : ''
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.$refs.table.refresh()
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 197
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less" scoped>
  .dealerZoneSearch-wrap{
    height: 100%;
    margin-top:6px;
  }
  /deep/.ant-select-combobox .ant-select-arrow{
    display:inline-block !important;
  }
  .line-2{
    width: 100%;
    overflow:hidden;
    text-overflow: ellipsis;
    -webkit-line-clamp: 2;
    display: -webkit-box;
    -webkit-box-orient: vertical;
  }
</style>
