<template>
  <div>
    <a-card size="small" :bordered="false" class="relationshipBindingList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="getList()">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="经销商名称">
                <a-input id="relationshipBindingList-dealerName" v-model.trim="queryParam.dealerName" allowClear placeholder="请输入经销商名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="getList(1)" :disabled="disabled" id="relationshipBindingList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm()" :disabled="disabled" id="relationshipBindingList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <!-- 列表 -->
    <a-card size="small" :bordered="false">
      <a-tabs @change="tabChange" v-model="tabInd" :tabBarStyle="{'background':'#fff'}">
        <a-tab-pane key="1" tab="省市级关系绑定"></a-tab-pane>
        <a-tab-pane key="2" tab="特约店关系绑定"></a-tab-pane>
      </a-tabs>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.dealerSn"
          :columns="columns"
          :pagination="false"
          :data-source="list"
          @expand="expand"
          :expandedRowKeys="expandedRowKeys"
          :expandRowByClick="true"
          :loading="loading"
          :style="{'height':tableHeight+70+'px' }"
          :scroll="{ y: tableHeight +30 }">
          <a-table
            slot="expandedRowRender"
            :rowKey="(record) => record.dealerSn"
            :columns="innerColumns"
            :data-source="innerData"
            :pagination="false">
            <!-- 操作 -->
            <template slot="action" slot-scope="text, record">
              <a-button
                type="link"
                v-if="$hasPermissions('B_dealerRelationshipBinding_unrelation')"
                class="button-error"
                size="small"
                icon="close"
                :id="'relationshipBindingList-delSubItem'+record.id"
                @click.stop="handleDisassociate(record)">取消关联</a-button>
              <span v-else>--</span>
            </template>
          </a-table>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              type="link"
              v-if="$hasPermissions('B_dealerRelationshipBinding_relation')"
              class="button-info"
              size="small"
              icon="plus"
              :id="'relationshipBindingList-addSubItem'+record.id"
              @click.stop="handleAssociate(record)">关联下级</a-button>
            <span v-else>--</span>
          </template>
        </a-table>
        <!-- 分页 -->
        <tablePagination
          ref="tablePagination"
          :total="paginationProps.total"
          :current="paginationProps.current"
          :pageSize="paginationProps.pageSize"
          @changePageSize="changePageSize"
          @changePage="changePage" />
      </a-spin>
      <!-- 关联下级 -->
      <associate-modal
        v-drag
        :openModal="openModal"
        :nowData="nowData"
        :tabInd="tabInd"
        @ok="addItemOk"
        @close="openModal=false" />
    </a-card>
  </div>
</template>
I
<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import tablePagination from '@/views/common/tablePagination.vue'
import associateModal from './associateModal.vue'
import { dealerRelationList, querySubList, unbindRelation } from '@/api/dealerRelation'
export default {
  name: 'DealerRelationshipBindingList',
  mixins: [commonMixin],
  components: { STable, VSelect, associateModal, tablePagination },
  data () {
    return {
      tableHeight: 0,
      spinning: false,
      loading: false,
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      queryParam: {
        dealerName: ''
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '经销商名称', dataIndex: 'dealerName', align: 'left' },
        { title: '操作', scopedSlots: { customRender: 'action' }, align: 'center' }
      ],
      expandedRowKeys: [],
      list: [],
      paginationProps: {
        total: 0, //  分页总条数
        current: 1,
        pageSize: 20
      },
      innerColumns: [
        { title: '经销商名称', dataIndex: 'dealerName', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, align: 'center' }
      ],
      innerData: [],
      openModal: false, //  新增编辑  弹框
      nowData: null, //  当前记录数据
      tabInd: '1', //  tabs选中项
      dealerSn: []
    }
  },
  methods: {
    expand (expanded, record) {
      this.nowData = record
      this.expandedRowKeys = [] // 重置展开节点，只展开当前点击的节点（内部数据调用模板，无法做到同时几个内层表格数据直接缓存在页面）
      if (expanded) {
        this.getSubItem(record) // 获取表格内部数据
      }
    },
    // 获取子节点数据
    getSubItem (record) {
      this.innerData = []
      this.spinning = true
      querySubList({ parentDealerSn: record.dealerSn, relationType: 'PARENT' }).then(res => {
        if (res.status == 200 && res.data && res.data.length) {
          this.innerData = res.data
        } else {
          this.innerData = []
        }
        this.expandedRowKeys = [record.dealerSn]
        this.spinning = false
      })
    },
    // 添加子级成功
    addItemOk () {
      this.getSubItem(this.nowData)
    },
    // 获取列表数据
    getList (pageNo) {
      this.disabled = true
      this.paginationProps.current = pageNo || this.paginationProps.current
      this.disabled = true
      const params = {
        pageNo: this.paginationProps.current,
        pageSize: this.paginationProps.pageSize
      }
      this.loading = true
      dealerRelationList(Object.assign(params, this.queryParam, { dealerLevelList: this.tabInd == 1 ? ['PROVINCE'] : ['PROVINCE', 'CITY'] })).then(res => {
        this.loading = false
        if (res.status == 200) {
          const data = res.data
          this.paginationProps.total = Number(res.data.count) || 0
          this.paginationProps.current = data.pageNo
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.list = data.list
          this.disabled = false
        } else {
          this.paginationProps.total = 0
          this.paginationProps.current = 1
          this.paginationProps.pageSize = 20
          this.list = []
        }
      })
    },
    //  重置
    resetSearchForm (flag) {
      if (!flag) {
        this.queryParam.dealerName = ''
        this.expandedRowKeys = []
        this.paginationProps.total = 0
        this.paginationProps.current = 1
        this.paginationProps.pageSize = 20
      }
      this.getList()
    },
    //  分页  一页多少条change
    changePageSize (current, pageSize) {
      this.paginationProps.current = current
      this.paginationProps.pageSize = pageSize
      this.getList()
    },
    //  分页 页码change
    changePage (current) {
      this.paginationProps.current = current
      this.getList()
    },
    tabChange (key) {
      this.tabInd = key
      //  不能共用resetSearchForm方法，因切换tab时，每页多少条数据更改后，不能同步tab两边页码
      this.queryParam.dealerName = ''
      this.expandedRowKeys = []
      this.paginationProps.total = 0
      this.paginationProps.current = 1
      this.paginationProps.pageSize = 20
      this.getList()
    },
    // 关联下级
    handleAssociate (row) {
      this.nowData = row
      this.openModal = true
    },
    // 取消关联
    handleDisassociate (row) {
      const _this = this
      _this.$confirm({
        title: '提示',
        content: '确认要解除上下级的关系吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          unbindRelation({ subDealerSn: row.dealerSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.addItemOk()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    setHtable () {
      this.tableHeight = window.innerHeight - 346
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setHtable()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.setHtable()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.setHtable()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.setHtable()
      this.resetSearchForm(true)
    }
  }
}
</script>
<style lang="less" scoped>
  .relationshipBindingList-wrap{
    position: relative;
    height: 100%;
    box-sizing: border-box;
    >.ant-spin-nested-loading{
      overflow-y: scroll;
      height: 100%;
    }
  }
</style>
