<template>
  <a-card size="small" :bordered="false" class="rebateSettingsList-wrap">
    <!-- 搜索条件 -->
    <div 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="rebateSettingsList-dealerName" v-model.trim="queryParam.dealerName" allowClear placeholder="请输入经销商名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button style="margin-bottom: 18px;" type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="rebateSettingsList-refresh">查询</a-button>
            <a-button style="margin: 0 0 18px 8px" @click="resetSearchForm" :disabled="disabled" id="rebateSettingsList-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ y: tableHeight }"
      bordered>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button
          size="small"
          type="link"
          v-if="$hasPermissions('B_rebateSettings_edit')"
          class="button-info"
          @click="handleEdit(record)"
          id="rebateSettingsList-edit-btn">编辑</a-button>
        <span v-else>--</span>
      </template>
      <!-- 经销商名称 -->
      <template slot="jxs" slot-scope="text, record">
        <span v-if="record.subDealer">{{ record.subDealer.dealerName }}</span>
        <span v-else>--</span>
      </template>
      <!-- 经销商级别 -->
      <template slot="dealerLevel" slot-scope="text, record">
        <span v-if="record.subDealer">{{ record.subDealer.provinceName }} > {{ record.subDealer.dealerLevelDictValue }}</span>
        <span v-else>--</span>
      </template>
      <!-- 差价归属 -->
      <template slot="cjgs" slot-scope="text, record">
        <span v-if="record.supDealer">{{ record.supDealer.dealerName }}</span>
        <span v-else>--</span>
      </template>
      <!-- 归属经销商级别 -->
      <template slot="gsjxsjb" slot-scope="text, record">
        <span v-if="record.supDealer">{{ record.supDealer.provinceName }} > {{ record.supDealer.dealerLevelDictValue }}</span>
        <span v-else>--</span>
      </template>
    </s-table>
    <!-- 关联下级 -->
    <associate-modal :openModal="openModal" :nowData="nowData" @ok="$refs.table.refresh(true)" @close="closeModal" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import associateModal from './associateModal.vue'
import { queryRebatePage } from '@/api/dealerRelation'
export default {
  components: { STable, VSelect, associateModal },
  data () {
    return {
      tableHeight: 0,
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      queryParam: {
        dealerName: ''
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '经销商名称', scopedSlots: { customRender: 'jxs' }, align: 'center', ellipsis: true },
        { title: '经销商级别', scopedSlots: { customRender: 'dealerLevel' }, align: 'center' },
        { title: '差价归属', scopedSlots: { customRender: 'cjgs' }, align: 'center' },
        { title: '归属经销商级别', scopedSlots: { customRender: 'gsjxsjb' }, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 380
        }
        return queryRebatePage(Object.assign(parameter, this.queryParam)).then(res => {
          const 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
          return data
        })
      },
      defaultExpandedRowKeys: [], //  树形数据默认展开项
      openModal: false, //  新增编辑  弹框
      nowData: null //  当前记录数据
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.dealerName = ''
      this.$refs.table.refresh(true)
    },
    // 编辑
    handleEdit (row) {
      this.nowData = row
      this.openModal = true
    },
    //  关闭弹框
    closeModal () {
      this.nowData = null
      this.openModal = false
    }
  }
}
</script>
