<template>
  <el-table v-loading="loading" :data="tableData" :stripe="true" class="m-t-20px">
    <el-table-column label="用户名" align="center" prop="name" />
    <el-table-column label="联系电话" align="center" prop="phone" />
    <el-table-column label="邮箱" align="center" prop="email" />
    <!-- <el-table-column label="性别" align="center" prop="sex">
      <template #default="scope">
        <dict-tag v-if="scope.row.sex !== '0'" :type="DICT_TYPE.MENDUNER_SEX" :value="scope.row.sex" />
        <span v-else></span>
      </template>
    </el-table-column> -->
    <el-table-column label="用户类型" align="center" prop="userType">
      <template #default="scope">
        {{ scope.row.userType === '1' ? '管理员': '普通用户' }}
      </template>
    </el-table-column>
    <el-table-column label="帐号状态" align="center" prop="status">
      <template #default="scope">
        <dict-tag :type="DICT_TYPE.MENDUNER_STATUS" :value="scope.row.status" />
      </template>
    </el-table-column>
    <el-table-column label="最后登录IP" align="center" prop="loginIp" />
    <el-table-column
      label="最后登录时间"
      align="center"
      prop="loginDate"
      :formatter="dateFormatter"
      width="180px"
    />
    <el-table-column
      label="创建时间"
      align="center"
      prop="createTime"
      :formatter="dateFormatter"
      width="180px"
    />
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button v-if="scope.row.status === '0'" link type="danger" @click="handleActions(scope.row.id, 'disabled')">禁用</el-button>
        <el-button v-if="scope.row.status === '1'" link type="success" @click="handleActions(scope.row.id, 'enable')">启用</el-button>
      </template>
    </el-table-column>
  </el-table>

  <Pagination
    :total="total"
    v-model:page="queryParams.pageNo"
    v-model:limit="queryParams.pageSize"
    @pagination="getList"
  />
</template>

<script setup>
defineOptions({ name: 'EnterpriseUserList'})
import { EnterpriseApi } from '@/api/menduner/system/enterprise/message'
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { EnterpriseUserBindApi } from '@/api/menduner/system/enterprise/userBind'

const props = defineProps({
  id: String
})

const message = useMessage() // 消息弹窗
const loading = ref(false)
const tableData = ref([])
const total = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  enterpriseId: props.id
})

const getList = async () => {
  loading.value = true
  try {
    const data = await EnterpriseApi.getEnterpriseUserPage(queryParams)
    tableData.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
getList()

// 禁用、启用账户
const handleActions = async (id, type) => {
  try {
    await message.confirm(type === 'disabled' ? '确定要禁用该账户吗？' : '确定要启用该账户吗？')
    type === 'disabled' ? await EnterpriseUserBindApi.disabledEnterpriseAccount(id) : await EnterpriseUserBindApi.enableEnterpriseAccount([id])
    message.success('操作成功')
    // 刷新列表
    await getList()
  } catch {}
}
</script>
