<template>
  <div>
    <a-card size="small" :bordered="false" class="rebateSettingsList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" 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-form-item label="归属经销商名称">
                <a-input id="rebateSettingsList-dealerName1" v-model.trim="queryParam.rebateDealerName" allowClear placeholder="请输入归属经销商名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="上级经销商名称">
                <a-input id="rebateSettingsList-dealerName2" v-model.trim="queryParam.supDealerName" allowClear placeholder="请输入上级经销商名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="rebateSettingsList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="rebateSettingsList-reset">重置</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.no"
          rowKeyName="no"
          :columns="columns"
          :data="loadData"
          :pageSize="20"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_rebateSettings_edit') && record.subDealer&&record.subDealer.dealerLevel&&record.subDealer.dealerLevel!='PROVINCE'"
              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.rebateDealer">{{ record.rebateDealer.dealerName }}</span>
            <span v-else>--</span>
          </template>
          <!-- 归属经销商级别 -->
          <template slot="gsjxsjb" slot-scope="text, record">
            <span v-if="record.rebateDealer">{{ record.rebateDealer.provinceName }} > {{ record.rebateDealer.dealerLevelDictValue }}</span>
            <span v-else>--</span>
          </template>
          <!-- 上级差价归属 -->
          <template slot="sjcjgs" slot-scope="text, record">
            <span v-if="record.supDealer">{{ record.supDealer.dealerName }}</span>
            <span v-else>--</span>
          </template>
          <!-- 上级归属经销商级别 -->
          <template slot="sjgsjxsjb" slot-scope="text, record">
            <span v-if="record.supDealer">{{ record.supDealer.provinceName }} > {{ record.supDealer.dealerLevelDictValue }}</span>
            <span v-else>--</span>
          </template>
        </s-table>
      </a-spin>
      <!-- 关联下级 -->
      <associate-modal v-drag :openModal="openModal" :nowData="nowData" @ok="$refs.table.refresh(true)" @close="closeModal" />
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import associateModal from './associateModal.vue'
import { queryRebatePage } from '@/api/dealerRelation'
export default {
  name: 'RebateSettingsList',
  mixins: [commonMixin],
  components: { STable, VSelect, associateModal },
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      queryParam: {
        dealerName: '',
        supDealerName: '',
        rebateDealerName: ''
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '经销商名称', width: '15%', scopedSlots: { customRender: 'jxs' }, align: 'left', ellipsis: true },
        { title: '经销商级别', scopedSlots: { customRender: 'dealerLevel' }, align: 'left', width: '15%' },
        { title: '指定差价归属经销商', scopedSlots: { customRender: 'cjgs' }, align: 'left', width: '15%' },
        { title: '指定归属经销商级别', scopedSlots: { customRender: 'gsjxsjb' }, align: 'left', width: '15%' },
        { title: '上级经销商', scopedSlots: { customRender: 'sjcjgs' }, align: 'left', width: '15%' },
        { title: '上级经销商级别', scopedSlots: { customRender: 'sjgsjxsjb' }, align: 'left', width: '15%' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return queryRebatePage(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
        })
      },
      defaultExpandedRowKeys: [], //  树形数据默认展开项
      openModal: false, //  新增编辑  弹框
      nowData: null //  当前记录数据
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.dealerName = ''
      this.queryParam.supDealerName = ''
      this.queryParam.rebateDealerName = ''
      this.$refs.table.refresh(true)
    },
    // 编辑
    handleEdit (row) {
      this.nowData = row
      this.openModal = true
    },
    //  关闭弹框
    closeModal () {
      this.nowData = null
      this.openModal = false
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 200
    }
  },
  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.pageInit()
      this.$refs.table.refresh()
    }
  }
}
</script>
