<template>
  <el-dialog :close-on-click-modal="false" title="选择节点" :visible.sync="dialogVisible" width="800px" top="5vh" append-to-body>
    <el-form :model="formData" label-width="50px" @submit.native.prevent>
      <div class="search-header">
        <div class="tool-common-wrap">
          <div class="search-wrap">
            <quick-query ref="quick" :placeholder="placeholder" :is-date="false" @quickSearch="quickSearch" />
          </div>
          <div class="btn-wrap">
            <el-button type="primary" @click="rest()"> 刷新</el-button>
          </div>
        </div>
      </div>
    </el-form>
    <div class="tableWrap">
      <el-table
        :data="tableList"
        v-loading="tableLoading"
        highlight-current-row
        style="width: 100%; margin-bottom: 10px"
        size="mini"
        ref="multipleTable"
        tooltip-effect="dark"
        :row-key="getRowKey"
        @row-click="clickRow"
        @selection-change="handleSelectionChange">
        <!--<el-table-column type="selection" width="50" :reserve-selection="true" align="center">-->
        <el-table-column type="selection" width="50" :reserve-selection="true" :selectable="forbidCheck" align="center">
        </el-table-column>
        <el-table-column prop="name" label="名称" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="host" label="地址" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="port" label="端口" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="domainInfoDTO.name" label="所属域" show-overflow-tooltip>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-size="page.pageSize"
        :current-page.sync="page.pageNum"
        background
        @current-change="getData()"
        :total="page.total"></el-pagination>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { queryServer } from '@/api/esb_service'
import QuickQuery from '@/components/quickquery/QuickQuery'
import EsbServiceListMixins from '@/mixins/esb_service_list_mixins'// 列表操作需要依赖
export default {
  mixins: [EsbServiceListMixins],
  components: {
    QuickQuery
  },
  data() {
    return {
      dialogVisible: false,
      tableLoading: false,
      formData: {},
      tableList: [],
      filters: [],
      multipleSelection: [],
      iechoEcho: false,
      checkList: [],
      quickQueryKeys: ['name'],
      sort: {
        orderby: 'createon',
        sort: 'desc'
      },
      placeholder: '名称'
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    checkLists: {
      type: Array,
      default() {
        return []
      }
    }
  },
  created() {
    this.page.pageSize = 5
    this.fullFilters = [{ logic: 'and', filters: [{ key: 'domain_id', value: '' }] }, { logic: 'and', filters: [] }]
  },
  watch: {
    value(val) {
      this.dialogVisible = val
      if (val) {
        this.iechoEcho = true
        this.$nextTick(() => {
          this.$refs.multipleTable.clearSelection()
          this.$refs.multipleTable.setCurrentRow()
        })
        this.page.pageNum = 1
        this.getData()
        this.checkList = JSON.parse(JSON.stringify(this.checkLists))
        this.multipleSelection = this.checkList
      }
    },
    dialogVisible(val) {
      this.$emit('input', val)
    }
  },
  mounted() {
  },
  methods: {
    getRowKey(row) {
      return row.id
    },
    async getData() {
      this.tableLoading = true
      queryServer({
        filters: this.filters,
        logic: this.logic,
        ...this.sort,
        ...this.page
      }).then((rt) => {
        this.tableLoading = false
        if (rt.status === '200') {
          this.tableList = rt.data.list || []
          this.page.total = rt.data.total
          this.userCheck()
        }
      }).catch(() => {
        this.tableLoading = false
      })
    },
    mergeUser() {
      const _this = this
      // let _ids = _this.tableList.map(item => item.id)
      const _userIds = _this.multipleSelection.map(item => item.id)
      const _userAllIds = _this.checkList.map(item => item.id)
      _this.multipleSelection.forEach(val => {
        if (_userAllIds.indexOf(val.id) === -1) {
          // 如果list不存在选中的则新增
          _this.checkList.push(val)
        }
      })
      // 获取未选中的listid
      const _listIds = _this.tableList
        .filter(item => !_userIds.includes(item.id))
        .map(item => item.id)
        // 获取未选中listid对应的obj
      const checkList = _this.checkList.filter(
        item => !_listIds.includes(item.id)
      )
      _this.checkList = checkList
    },
    userCheck() {
      const _this = this
      this.iechoEcho = true
      _this.$refs.multipleTable.clearSelection()
      this.$nextTick(() => {
        _this.tableList.forEach((item, index) => {
          _this.checkList.forEach(v => {
            if (item.id === v.id) {
              if (_this.$refs.multipleTable) {
                _this.$refs.multipleTable.toggleRowSelection(item, true)
                _this.tableList[index].isChecked = true
              }
            }
          })
        })
        this.iechoEcho = false
        this.$forceUpdate()
      })
    },
    // 已选中的禁止操作
    forbidCheck(row) {
      return row.isChecked !== true
    },
    quickQueryData() {
      this.getData()
    },
    // 多选
    handleSelectionChange(val) {
      if (!this.iechoEcho) {
        this.multipleSelection = val
        this.mergeUser()
      }
    },
    clickRow(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    submit() {
      // const data = JSON.parse(JSON.stringify(this.multipleSelection))
      this.$emit('confirm', this.checkList)
      this.dialogVisible = false
    },
    cancel() {
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
  .checkList {
    color: #ffffff;
    background: #3792d8;
    display: inline-block;
    padding: 5px 10px;
  }
</style>
