<!--单列-->
<template>
  <el-dialog class='table-box'
             v-model='dialogVisible'
             top='114px'
             :style="{height:height,maxHeight:height,overflow:'hidden'}"
             :title='title'
             :width='width'
             :modal='false'>
    <div class='table-box'>
      <ProTable
          ref='proTable'
          :columns='columns'
          :data='userData'
      >
        <!-- 表格 header 按钮 -->
        <template #tableHeader='scope'>
          <el-button type='primary' :icon='CirclePlus' @click="openDrawer('新增')">新增用户</el-button>
        </template>
        <!-- 表格操作 -->
        <template #operation='scope'>
          <el-button type='primary' link :icon='View'
                     @click="openDrawer('查看', scope.row)">查看
          </el-button>
          <el-button type='primary' link :icon='EditPen' @click="openDrawer('编辑', scope.row)">编辑</el-button>
          <el-button type='primary' link :icon='Refresh' @click='resetPass(scope.row)'>重置密码</el-button>
          <el-button type='primary' link :icon='Delete' @click='deleteAccount(scope.row)'>删除</el-button>
        </template>
      </ProTable>
    </div>
  </el-dialog>
</template>
<script setup lang='tsx'>
import { CirclePlus, Delete, EditPen, Refresh, View } from '@element-plus/icons-vue'
import ProTable from '@/components/ProTable/index.vue'
import { useHandleData } from '@/hooks/useHandleData'
import { useAuthButtons } from '@/hooks/useAuthButtons'
import { reactive } from 'vue'
import { ColumnProps } from '@/components/ProTable/interface'
import { User } from '@/api/user/interface'
import { useDictStore } from '@/stores/modules/dict'

const dictStore = useDictStore()
import {
  changeUserStatus,
} from '@/api/user/user'
import userDataList from '@/api/user/userData'

const userData = userDataList
const proTable = ref()

const dialogVisible = ref(false)
const title = ref('单列弹出框')

const { BUTTONS } = useAuthButtons()
// 表格配置项
const columns = reactive<ColumnProps<User.ResUserList>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { type: 'index', fixed: 'left', width: 70, label: '序号' },
  {
    prop: 'username',
    label: '姓名',
    width: 120,
    search: { el: 'input' },
  },
  {
    prop: 'gender',
    label: '性别',
    // 字典数据（本地数据）
    enum: dictStore.getDictData('sex'),
    search: { el: 'select', props: { filterable: true } },
    fieldNames: { label: 'label', value: 'code' },
  },
  {
    prop: 'age',
    label: '年龄',
    search: {
      // 自定义 search 显示内容
      render: ({ searchParam }) => {
        return (
            <div class='flx-center'>
              <el-input vModel_trim={searchParam.minAge} placeholder='最小年龄' />
              <span class='mr10 ml10'>-</span>
              <el-input vModel_trim={searchParam.maxAge} placeholder='最大年龄' />
            </div>
        )
      },
    },
  },
  { prop: 'idCard', label: '身份证号', search: { el: 'input' } },
  { prop: 'email', label: '邮箱' },
  { prop: 'address', label: '居住地址', width: 120 },
  {
    prop: 'status',
    label: '状态',
    enum: dictStore.getDictData('status'),
    search: { el: 'tree-select', props: { filterable: true } },
    fieldNames: { label: 'userLabel', value: 'userStatus' },
    render: scope => {
      return (
          <>
            {BUTTONS.value.status ? (
                <el-switch
                    model-value={scope.row.status}
                    active-text={scope.row.status ? '启用' : '禁用'}
                    active-value={1}
                    inactive-value={0}
                    onClick={() => changeStatus(scope.row)}
                />
            ) : (
                <el-tag type={scope.row.status ? 'success' : 'danger'}>{scope.row.status ? '启用' : '禁用'}</el-tag>
            )}
          </>
      )
    },
  },
  {
    prop: 'createTime',
    label: '创建时间',
    width: 180,
    search: {
      el: 'date-picker',
      span: 1,
      props: { type: 'daterange', valueFormat: 'YYYY-MM-DD' },
      defaultValue: ['2024-11-12', '2024-12-12'],
    },
  },
  { prop: 'operation', label: '操作', fixed: 'right', width: 330 },
])


const open = (textTitle: string) => {
  dialogVisible.value = true
  title.value = textTitle
}


defineExpose({ open })


const props = defineProps({
  width: {
    type: String,
    default: '800px',
  },
  height: {
    type: String,
    default: '744px',
  },
})

// 切换用户状态
const changeStatus = async (row: User.ResUserList) => {
  await useHandleData(changeUserStatus, {
    id: row.id,
    status: row.status == 1 ? 0 : 1,
  }, `切换【${row.username}】用户状态`)
  proTable.value?.getTableList()
}

// 删除用户信息
const deleteAccount = async (params: User.ResUserList) => {

}
// 重置用户密码
const resetPass = async (params: User.ResUserList) => {

}

// 打开 drawer(新增、查看、编辑)
const openDrawer = (title: string, row: Partial<User.ResUserList> = {}) => {
}

</script>
<style>

</style>