<template>
  <el-dialog :visible.sync="crud.dmUserTempShow.user" title="选择用户" width="60%">
    <!--用户数据-->
    <el-row>
      <el-col :xs="15" :sm="18" :md="19" :lg="20" :xl="20">
        <!--工具栏-->
        <div class="head-container">
          <!-- 搜索 -->
          <el-input
            v-model="blurry"
            type="text"
            size="small"
            placeholder="输入名称/账号/邮箱搜索"
            style="width: 350px;"
            class="filter-item"
          />
          <span>
            <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="getUsers()">搜索</el-button>
            <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="resetSelect()">重置</el-button>
          </span>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="17">
        <el-table
          ref="multipleTable"
          v-loading="crud.loading"
          :data="users"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange"
          @select="select"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column :show-overflow-tooltip="true" prop="id" label="id" />
          <el-table-column :show-overflow-tooltip="true" prop="dmUser.name" label="用户名" />
          <el-table-column :show-overflow-tooltip="true" prop="dmUser.sex" label="性别" />
          <el-table-column :show-overflow-tooltip="true" prop="dmUser.contacts" width="100" label="联系方式" />
          <el-table-column :show-overflow-tooltip="true" width="135" prop="dmUser.email" label="邮箱" />
          <el-table-column :show-overflow-tooltip="true" width="135" prop="dept.name" label="部门" />
          <el-table-column :show-overflow-tooltip="true" prop="createTime" width="135" label="创建日期" />
        </el-table>
        <!--分页组件-->
        <el-pagination
          :page-size.sync="size"
          :total="total"
          :current-page.sync="page"
          style="margin-top: 8px;"
          layout="total, prev, pager, next, sizes"
          @size-change="getUsers()"
          @current-change="getUsers()"
        />
      </el-col>
      <el-col :span="6" :push="1">
        <el-card style="height: 100%;">
          <div>您已选择:</div>
          <div>{{ checkInfo }}</div>
        </el-card>
      </el-col>
    </el-row>
    <div slot="footer">
      <el-row>
        <el-col :xs="10" :sm="10" :md="10" :lg="15" :xl="15">
          <el-button style="" class="filter-item" @click="toggleSelection()">取消选择</el-button>
          <el-button class="filter-item" style="margin-left:20% ;" @click="selected()">选择</el-button>
        </el-col>
      </el-row>

    </div>
  </el-dialog>
</template>
<script>
import crudUser from '@/api/system/user'
import { mapGetters } from 'vuex'
import CRUD, { crud } from '@crud/crud'

export default {
  mixins: [crud()],
  // 数据字典
  dicts: ['user_status'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  // 查询会议用户
  cruds() {
    return CRUD({ title: '用户', url: 'api/users/queryAllUser', dmUserTempBtnShow: {
      user: true
    }, crudMethod: { ...crudUser }})
  },
  data() {
    return {
      users: [],
      multipleSelection: crud.selectDataDm || [],
      checkInfo: '',
      blurry: '',
      size: 10,
      page: 0,
      total: 0,
      selectSwitch: false
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  watch: {
    multipleSelection: {
      handler: function() {
        this.setCheckInfo()
      }
    },
    'params.searchType': {
      handler: function(val, oldVal) {
        if (this.params.max === 1) {
          // console.info('监听 || this.$refs.multipleTable ==>> ' + this.$refs.multipleTable)
          // console.log('监听 || this.$refs.multipleTable ==>> ' + this.$refs.multipleTable)
        }
        this.getUsers()
      }
    },
    selectSwitch: {
      handler: function() {
        if (this.$refs.multipleTable) {
          this.$nextTick(function() {
            this.toggleSelection()
            this.users.forEach((item, i) => {
              this.crud.selectDataDm.forEach(row => {
                if (this.users[i].id === row) {
                  this.$refs.multipleTable.toggleRowSelection(this.users[i])
                  this.setCheckInfo()
                }
              })
            })
          })
        }
      }
    }

    /* ,
    'crud.dmUserTempShow.user': {
      handler: function(val, oldVal) {
        if (val <= 0) {
          this.$refs.multipleTable.clearSelection()
        }
      }
    } */

  },
  created() {
    // console.info('params', this.user)
    // console.info('crudUser ==>> ' + JSON.stringify(crudUser))
    this.getUsers()
  },
  methods: {
    getUsers() {
      if (this.blurry !== '') {
        this.page = 0
      }
      crudUser.queryAllUser({
        blurry: this.blurry,
        enabled: 1,
        page: this.page - 1,
        size: this.size
      }).then(res => {
        this.total = res.totalElements
        this.users = res.content.map(function(obj) {
          return obj
        })
        this.selectSwitch = !this.selectSwitch
      })
      console.log()
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    select(selection, row) {
      // console.info('选择select || selection ==>> ' + JSON.stringify(selection))
      // console.info('选择select || row ==>> ' + JSON.stringify(row))
      if (this.params.max === 1) {
        this.$refs.multipleTable.clearSelection()
        // 当表格数据都没有被勾选的时候 就返回
        if (selection.length === 0) return
        // 主要用于将当前勾选的表格状态清除
        this.$refs.multipleTable.toggleRowSelection(row, true)
        return false
      }
      if (this.params.max !== 1 && selection.length > this.params.max) {
        this.$message({
          message: '最多选择' + this.params.max + '条数据',
          type: 'warning'
        })
        return false
      }
      if (this.params.max > 1 && selection.length <= this.params.max) {
        this.$refs.multipleTable.clearSelection()
        selection.forEach(obj => {
          this.$refs.multipleTable.toggleRowSelection(obj, true)
        })
      }
    },
    handleSelectionChange(val) {
      const newVal = []
      for (const index in val) {
        newVal.push(val[index].dmUser)
      }
      this.multipleSelection = newVal
    },
    selected() {
      // console.log(this.$refs.multipleTable.selection)
      this.$emit('on-change', this.$refs.multipleTable.selection, this.crud.openId)
      this.$refs.multipleTable.clearSelection()
      this.crud.doCloseDmUser()
    },
    setCheckInfo() {
      var checkedUsers = this.$refs.multipleTable.selection
      var info = ''
      checkedUsers.forEach(row => {
        info += row.dmUser.name + ','
      })
      this.checkInfo = info.substring(0, info.length - 1)
    },
    resetSelect() {
      this.blurry = ''
      this.getUsers()
    }
  }
}
</script>
