<template>
  <table-template :soul="this">
    <template #tags="{ record, index }">
      <a-switch
        :disabled="record.id == 1"
        checkedChildren="启用"
        unCheckedChildren="禁用"
        @click="setStatus(record)"
        :checked="dataSource[index].isEnabled"
      />
    </template>
    <template #action="{ record }">
      <a @click="editRow(record)" v-if="record.id != 1">编辑</a>
      <a-divider type="vertical" v-if="record.id != 1" />
      <a @click="resetPwd(record)" v-if="record.id != 1">重置密码</a>
    </template>
  </table-template>
</template>
<script>
import { TableTemplate, TableScript, SearchType } from '@/components/table'
import { changeStatus, getPage, resetPwd } from '@/api/system/sysUserApi'
import dayjs from 'dayjs'

const columns = [
  {
    title: '用户名',
    dataIndex: 'userName',
    width: 120,
    filter: { type: SearchType.STRING }
  },
  {
    title: '真实姓名',
    dataIndex: 'realName',
    width: 90,
    filter: { type: SearchType.STRING }
  },
  {
    title: '联系电话',
    dataIndex: 'phone',
    width: 100,
    filter: { type: SearchType.STRING }
  },
  { title: '邮箱', dataIndex: 'email', width: 150 },
  { title: '备注', dataIndex: 'remark' },
  {
    title: '创建时间',
    dataIndex: 'ctDate',
    width: 170,
    sorter: true,
    customRender: (text) => dayjs(text).format('YYYY-MM-DD HH:mm:ss')
  },
  {
    title: '修改时间',
    dataIndex: 'lmDate',
    width: 170,
    sorter: true,
    customRender: (text) => dayjs(text).format('YYYY-MM-DD HH:mm:ss')
  },
  {
    title: '状态',
    dataIndex: 'available',
    width: 80,
    align: 'center',
    scopedSlots: { customRender: 'tags' },
    enum: 'SYS0002',
    filter: { type: SearchType.ENUM }
  },
  {
    title: '操作',
    key: 'operation',
    width: 130,
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'SysUserList',
  mixins: [TableScript],
  components: { TableTemplate },
  data() {
    return {
      title: '系统用户',
      columns,
      codes: [['SYS0002'], []],
      toolbar: [
        {
          text: '新建',
          handler: 'addNew'
        }
      ]
    }
  },
  methods: {
    queryData: getPage,
      addNew() {
        this.editRow()
      },
      editRow(row) {
        this.$router.push({name: 'sysUserDetail', query: {id: row ? row.id : 0}})
      },
    setStatus(record) {
      changeStatus(record.id).then(({ code }) => {
        if (code == 200) {
          this.loadData()
        }
      })
    },
    resetPwd(rowData) {
      let _this = this
      let contText = '确定重置[' + rowData.realName + ']的密码？'
      this.$confirm({
        title: '提示',
        content: contText,
        onOk() {
          resetPwd(rowData.id).then((response) => {
            if (response && response.code == SYS_CONST.REQUEST.SUCCEED) {
              _this.$notification.success({
                message: '系统提示',
                description: '重置密码成功',
                duration: 4
              })
            }
          })
        },
        onCancel() {}
      })
    }
  }
}
</script>