<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" align="center" :reserve-selection="true">
        </el-table-column>
        <el-table-column prop="empuid" label="登录账号" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="cnname" label="显示姓名" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="email" 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 { queryUser } from '@/api/system_manage'
import EsbServiceListMixins from '@/mixins/esb_service_list_mixins'// 列表操作需要依赖
import QuickQuery from '@/components/quickquery/QuickQuery'

export default {
  mixins: [EsbServiceListMixins],
  components: {
    QuickQuery
  },
  data() {
    return {
      dialogVisible: false,
      tableLoading: false,
      formData: {},
      tableList: [],
      filters: [],
      multipleSelection: [],
      iechoEcho: false,
      checkList: [],
      quickQueryKeys: ['empuid', 'cnname', 'email'],
      sort: {
        orderby: 'empuid',
        sort: 'desc'
      },
      placeholder: '账号、姓名、邮箱',
      logic: 'or'
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    checkLists: {
      type: Array,
      default() {
        return []
      }
    }
  },
  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() {
    this.page.pageSize = 5
  },
  methods: {
    getRowKey(row) {
      return row.id
    },
    async getData() {
      this.tableLoading = true
      queryUser({
        logic: this.logic,
        filters: this.filters,
        ...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 => {
          _this.checkList.forEach(v => {
            if (item.id === v.id) {
              if (_this.$refs.multipleTable) {
                _this.$refs.multipleTable.toggleRowSelection(item, true)
              }
            }
          })
        })
        this.iechoEcho = false
        this.$forceUpdate()
      })
    },
    // 多选
    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
    },
    quickQueryData() {
      this.getData()
    }
  }
}
</script>

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