<template>
  <div>
    <el-row>
      <!--查询-->
      <el-col :span="24">
      <Form inline onsubmit="return false" :label-width="60">
        <label class="label-sign" />
        <Form-item  label="账号:" style="width:300px">
          <el-input v-model="formObj.username" name="username" placeholder="请输入账号" @keyup.enter.native="_formSearch" clearable/>
        </Form-item>
        <Form-item class="search-btn">
          <el-button type="primary" @click="_formSearch">
            搜索
          </el-button>
        </Form-item>
      </Form>
      </el-col>
      <!--操作-->
      <el-col :span="24">
      <btn-list :msg="btn" class="contHide" @on-result-change="_btnClick" />
      </el-col>
      <!-- 表格 -->
      <el-col :span="24">
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="60"
        show-check-box
        :icon-msg="iconMsg"
        @on-result-change="_tableResultChange"
      >
        <el-table-column
          v-for="item in pageColumns"
          :key="item.key"
          show-overflow-tooltip
          sortable
          :prop="item.key"
          :label="item.title"
          :width="item.width"
          :min-width="200"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <div v-if="item.sex">
              {{ scope.row[item.key]? scope.row[item.key] === 0 ?'男':'女' :'' }}
            </div>
            <div v-else-if="item.status">
              {{ scope.row[item.key] === undefined ? '' : scope.row[item.key].display }}
            </div>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
      </element-table>
      </el-col>
    </el-row>
    <!--添加人员-->
    <assignPerson ref="userListModal" @on-result-change="_userResult" />
  </div>
</template>
<script>
import { sysUser, flowProcess } from '../../../api'
import assignPerson from '../../../components/user-info-multi/assignPerson'

export default {
  components: {
    assignPerson
  },
  data() {
    return {
      btn: [
        { type: 'success', id: '', name: '添加用户' },
        { type: 'danger', id: '', name: '移除' }
      ],
      iconMsg: [{ type: 'pt-trash', id: '', name: '移除' }],
      selectIds: [],
      formObj: {
        username: '',
        groupId: ''
      },
      pageColumns: [
        { title: '姓名', key: 'realname', width: 200 },
        { title: '性别', key: 'sex', width: 80, sex: true },
        { title: '手机号码', key: 'mobile' },
        { title: '电子邮箱', key: 'email' },
        { title: 'QQ', key: 'qq' },
        { title: '状态', key: 'status', width: 100, status: true }
      ],
      getPage: {}
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('search')
    }
  },
  methods: {
    _open(id) {
      if (id) {
        this.formObj.groupId = id
        this._formSearch()
      } else {
        this.$refs.pageTable._hideLoading()
      }
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await sysUser.pageFlowMemberShip(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _addSelected() {
      if (this.$string(this.formObj.groupId).isEmpty()) {
        this.$message.warning('请选择工作组后操作！')
      } else {
        this.$refs.userListModal._open('选择用户', this.formObj.groupId)
      }
    },
    _deleteByIds: async function(ids) {
      const result = await flowProcess.deleteMemberShip({
        groupId: this.formObj.groupId,
        userIds: ids
      })
      this._resultChange(result, '删除成功!')
    },
    _deleteConfirm(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || '确定删除该记录？',
        onOk: () => {
          this._deleteByIds(ids)
        }
      })
    },
    _deleteSelected() {
      // 批量删除
      if (this.selectIds.length === 0) {
        this.$message.warning('请选择一条或多条数据！')
      } else {
        this._deleteConfirm(
          this.selectIds,
          '确定删除 ' + this.selectIds.length + ' 条记录？'
        )
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '移除':
          this._deleteConfirm([data.id])
          break
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '添加用户':
          this._addSelected()
          break
        case '移除':
          this._deleteSelected()
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    },
    _resultChange(result, msg) {
      if (result) {
        this.$message.success(msg)
        this._page()
      }
    },
    _userResult: async function(data) {
      const userIds = []
      for (let i = 0; i < data.length; i++) {
        userIds.push(data[i].userId)
      }
      const result = await flowProcess.addMemberShip({
        groupId: this.formObj.groupId,
        userIds: userIds
      })
      this._resultChange(result, '添加成功')
    }
  }
}
</script>
