<template>
  <a-card :bordered="false" class="mt15">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <!-- 搜索区域 -->
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="8" :sm="12">
            <a-form-item label="用户账号" style="margin-left: 8px">
              <a-input placeholder="请输入名称查询" v-model="queryParams.username"></a-input>
            </a-form-item>
          </a-col>
          <!--<a-col :md="8" :sm="8">-->
          <!--<a-form-item label="用户名称" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">-->
          <!--<a-input placeholder="请输入名称查询" v-model="queryParams.realname"></a-input>-->
          <!--</a-form-item>-->
          <!--</a-col>-->
          <!--					<span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">-->
          <a-col :md="16" :sm="24">
            <a-button type="primary" @click="searchQuery" icon="search" class="mt6 mr10">查询</a-button>
            <a-button type="primary" @click="resetQuery" icon="reload" class="mt6 mr10">重置</a-button>
            <a-button
              v-if="isDisabledAuth('departUserList:addUser')"
              @click="handleAdd"
              type="primary"
              icon="plus"
              class="mt6 mr10"
              >用户录入
            </a-button>
            <a-button
              v-if="isDisabledAuth('departUserList:addExistingUser')"
              @click="handleAddUserDepart"
              type="primary"
              icon="plus"
              class="mt6"
              >添加已有用户
            </a-button>
          </a-col>
          <!--					</span>-->
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator" :md="24" :sm="24">
      <!--<a-button @click="handleAdd" type="primary" icon="plus">用户录入</a-button>-->
      <!--<a-button @click="handleEdit" type="primary" icon="edit">用户编辑</a-button>-->
      <!--<a-button @click="handleAddUserDepart" type="primary" icon="plus">添加已有用户</a-button>-->

      <a-dropdown v-if="selectedRowKeys.length > 0 && isDisabledAuth('departUserList:batchDel')">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除关系
          </a-menu-item>
        </a-menu>
        <a-button class="ml10">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="table-detail">
        已选择&nbsp;<a>{{ selectedRowKeys.length }}</a
        >项
        <a class="ml20" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="small"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="pagination"
        :loading="tableLoading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handlePaginationChange"
      >
        <span slot="action" slot-scope="text, record">
          <a v-if="isDisabledAuth('departUserList:edit')" class="mr5" @click="handleEdit(record)">编辑</a>

          <a-dropdown v-if="isDisabledAuth('departUserList:details') || isDisabledAuth('departUserList:delRelation')">
            <a class="ant-dropdown-link"> 更多 <a-icon type="down" /> </a>
            <a-menu slot="overlay">
              <a-menu-item v-if="isDisabledAuth('departUserList:details')">
                <a href="javascript:;" @click="handleView(record)">详情</a>
              </a-menu-item>

              <a-menu-item v-if="isDisabledAuth('departUserList:delRelation')">
                <a-popconfirm title="确定要删除关系吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除关系</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <UserModal ref="modalForm" @ok="handleOk"></UserModal>
    <UserModal ref="modalViewRef" @ok="handleOk"></UserModal>
    <SelectUserModal ref="selectUserModal" @selectFinished="selectOK"></SelectUserModal>
  </a-card>
</template>

<script>
import { PageListMixin } from '@/mixins/PageListMixin'
import { deleteAction, postAction } from '@/api/manage'
import UserModal from './UserModal'
import SelectUserModal from './SelectUserModal'

export default {
  name: 'DeptUserInfo',
  components: {
    SelectUserModal,
    UserModal
  },
  mixins: [PageListMixin],
  data() {
    return {
      description: '用户信息',
      currentDeptId: '',
      selectedRowKeys: [],
      // 表头
      columns: [
        {
          title: '用户账号',
          align: 'center',
          dataIndex: 'username'
        },
        {
          title: '用户名称',
          align: 'center',
          dataIndex: 'realname'
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center',
          width: 170
        }
      ],
      interface: {
        list: '/sys/user/departUserList',
        edit: '/sys/user/editSysDepartWithUser',
        delete: '/sys/user/deleteUserInDepart',
        deleteBatch: '/sys/user/deleteUserInDepartBatch'
      }
    }
  },
  created() {},

  methods: {
    loadData(arg) {
      if (!this.interface.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.pagination.current = 1
      }
      if (this.currentDeptId === '') return
      var params = this.getQueryParams() //查询条件
      params.value.depId = this.currentDeptId
      postAction(this.interface.list, params).then(res => {
        if (res.code === '200') {
          this.dataSource = res.value.records
          this.pagination.total = res.value.total
        }
      })
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    batchDel: function () {
      if (!this.interface.deleteBatch) {
        this.$message.error('请设置url.deleteBatch属性!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        this.$confirm({
          title: '确认删除',
          content: '是否删除选中数据?',
          onOk: function () {
            deleteAction(that.url.deleteBatch, {
              depId: that.currentDeptId,
              userIds: ids
            }).then(res => {
              if (res.code === '200') {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          }
        })
      }
    },
    handleDelete: function (id) {
      if (!this.interface.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      var that = this
      deleteAction(that.url.delete, { depId: this.currentDeptId, userId: id }).then(res => {
        if (res.code === '200') {
          that.$message.success(res.message)
          if (this.selectedRowKeys.length > 0) {
            for (let i = 0; i < this.selectedRowKeys.length; i++) {
              if (this.selectedRowKeys[i] === id) {
                this.selectedRowKeys.splice(i, 1)
                break
              }
            }
          }
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    open(record) {
      this.currentDeptId = record.id
      this.loadData(1)
    },
    clearList() {
      this.currentDeptId = ''
      this.dataSource = []
    },
    hasSelectDept() {
      if (this.currentDeptId == null) {
        this.$message.error('请选择一个部门!')
        return false
      }
      return true
    },
    handleAddUserDepart() {
      if (this.currentDeptId === '') {
        this.$message.error('请选择一个部门!')
      } else {
        this.$refs.selectUserModal.visible = true
      }
    },
    handleEdit: function (record) {
      this.$refs.modalForm.title = '编辑'
      this.$refs.modalForm.departDisabled = true
      this.$refs.modalForm.disableSubmit = false
      this.$refs.modalForm.handleEdit(record)
    },
    handleAdd: function () {
      if (this.currentDeptId === '') {
        this.$message.error('请选择一个部门!')
      } else {
        this.$refs.modalForm.departDisabled = true
        this.$refs.modalForm.userDepartModel.departIdList = [this.currentDeptId] //传入一个部门id
        this.$refs.modalForm.handleAdd()
        this.$refs.modalForm.title = '新增'
      }
    },
    selectOK(data) {
      let params = {}
      params.depId = this.currentDeptId
      params.userIdList = []
      for (var a = 0; a < data.length; a++) {
        params.userIdList.push(data[a])
      }
      postAction(this.interface.edit, params).then(res => {
        if (res.code === '200') {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },

    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
    }
  }
}
</script>
<style scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 3px;
}

.ant-card {
  /*margin-left: -30px;*/
  /*margin-right: -30px;*/
}

.table-page-search-wrapper {
  margin-top: -16px;
  margin-bottom: 16px;
}

.ant-drawer-left.ant-drawer-open,
.ant-drawer-right.ant-drawer-open {
  width: 100%;
  -webkit-transition: -webkit-transform 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);
  transition: -webkit-transform 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);
  transition: transform 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);
  transition: transform 0.3s cubic-bezier(0.7, 0.3, 0.1, 1), -webkit-transform 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);
  height: 100% !important;
}

.ant-drawer-wrapper-body {
  height: 95% !important;
  overflow: auto;
}
</style>
