<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="6" :sm="24">
              <a-form-item label="用户名称" >
                <a-input v-model="queryParam.name" placeholder="请输入用户名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="账号" >
                <a-input v-model="queryParam.userName" placeholder="请输入账号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="手机号码" >
                <a-input v-model="queryParam.phone" placeholder="请输入手机号码"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" icon="plus" v-if="hasPerm('sys:user:add')" @click="$refs.userForm.add()">新增用户</a-button>
        <a-button type="danger" :disabled="selectedRowKeys.length < 1" v-if="hasPerm('sys:user:delete')" @click="batchDelete">
          <a-icon type="delete"/>批量删除
        </a-button>
      </div>

      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <span slot="sex" slot-scope="text, record">
          {{ record.sex | sexFilter }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('sys:user:edit')" @click="$refs.userForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('sys:user:edit')" />
          <a-dropdown v-if="hasPerm('sys:user:resetPwd') || hasPerm('sys:user:grantRole') || hasPerm('sys:user:del')">
            <a class="ant-dropdown-link">
              更多 <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item v-if="hasPerm('sys:user:resetPwd')">
                <a-popconfirm placement="topRight" title="确认重置密码？" @confirm="() => resetPwd(record)">
                  <a>重置密码</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item v-if="hasPerm('sys:user:grantRole')">
                <a @click="$refs.userRoleForm.userRole(record)">授权角色</a>
              </a-menu-item>
              <a-menu-item v-if="hasPerm('sys:user:delete')">
                <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </s-table>
      <user-form ref="userForm" @ok="handleOk" />
      <user-role-form ref="userRoleForm" @ok="handleOk"/>
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import { userPage, userDel, passwordReset } from '@/api/security/user'
import UserForm from './UserForm'
import UserRoleForm from './UserRoleForm'
import { sexMap } from '@/views/filterMap/sexMap'
export default {
  components: {
    STable,
    UserForm,
    UserRoleForm
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '账号',
          dataIndex: 'userName'
        },
        {
          title: '姓名',
          dataIndex: 'name'
        },
        {
          title: '昵称',
          dataIndex: 'nickName'
        },
        {
          title: '性别',
          dataIndex: 'sex',
          scopedSlots: { customRender: 'sex' }
        }, {
          title: '手机',
          dataIndex: 'phone'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return userPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res
        })
      },
      selectedRowKeys: [], // 选中行的key 出选择框时需要配置
      selectedRows: [], // 选中行的数据
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      }
    }
  },
  filters: {
    sexFilter (sex) {
      return sexMap[sex]
    }
  },
  created () {
    // 动态加载权限
    if (this.hasPerm('sys:user:edit') || this.hasPerm('sys:user:resetPwd') || this.hasPerm('sys:user:grantRole') || this.hasPerm('sys:user:del')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    // 重置密码
    resetPwd (record) {
      passwordReset({ id: record.id }).then(res => {
        if (res.code === 200) {
          this.$message.success('重置成功')
          // this.$refs.table.refresh()
        } else {
          this.$message.error('重置失败：' + res.msg)
        }
      })
    },
    // 单个删除
    singleDelete (record) {
      const param = { 'ids': record.id }
      this.sysUserDelete(param)
    },
    // 批量删除
    batchDelete () {
      const paramIds = this.selectedRowKeys.join(',')
      const param = { 'ids': paramIds }
      this.sysUserDelete(param)
    },
    sysUserDelete (param) {
      userDel(param).then((res) => {
        if (res.code === 200) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败：' + res.msg)
        }
      }).catch((err) => {
        this.$message.error('删除错误：' + err.msg)
      })
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
