<script setup lang="ts">
import { reactive, ref, onMounted, toRaw } from 'vue';
import { PageContainer } from 'antdv-pro-layout';
import { ProModal } from 'antdv-pro-modal';
import { message, Modal, Form } from 'ant-design-vue/es';
import { SizeType } from 'ant-design-vue/es/config-provider';
import { MenuInfo } from 'ant-design-vue/es/menu/src/interface';
import { ColumnsType } from 'ant-design-vue/es/table';
import UploadModal from '@/components/UploadModal/index.vue';
import {
  importData,
  importTemplate,
  exportUser,
  changeUserStatus,
  listUser,
  resetUserPwd,
  delUser,
  getUser,
  updateUser,
  addUser,
} from '@/api/system/user';
import { deptTreeSelect } from '@/api/system/dept';
import { saveAs } from 'file-saver';
import useI18n from '@/hooks/useI18n';
import { parseDateToStr } from '@/utils/date-utils';
import {
  regExpPasswd,
  regExpMobile,
  regExpNick,
  regExpEmail,
  regExpUserName,
} from '@/utils/regular-utils';
import useDictStore from '@/store/modules/dict';
import useUserStore from '@/store/modules/user';
import { DataNode } from 'ant-design-vue/es/tree';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
const { getDict } = useDictStore();
const userStore = useUserStore();
const { t } = useI18n();

/**字典数据 */
let dict: {
  /**状态 */
  sysNormalDisable: DictType[];
  /**性别 */
  sysUserSex: DictType[];
} = reactive({
  sysNormalDisable: [],
  sysUserSex: [],
});

/**开始结束时间 */
let queryRangePicker = ref<[string, string]>(['', '']);

/**查询参数 */
let queryParams = reactive({
  /**登录账号 */
  userName: '',
  /**手机号 */
  phonenumber: '',
  /**部门ID */
  deptId: undefined,
  /**用户状态 */
  status: undefined,
  /**记录开始时间 */
  beginTime: '',
  /**记录结束时间 */
  endTime: '',
  /**当前页数 */
  pageNum: 1,
  /**每页条数 */
  pageSize: 20,
});

/**查询参数重置 */
function fnQueryReset() {
  queryParams = Object.assign(queryParams, {
    userName: '',
    phonenumber: '',
    deptId: undefined,
    status: undefined,
    beginTime: '',
    endTime: '',
    pageNum: 1,
    pageSize: 20,
  });
  queryRangePicker.value = ['', ''];
  tablePagination.current = 1;
  tablePagination.pageSize = 20;
  fnGetList();
}

/**表格状态类型 */
type TabeStateType = {
  /**加载等待 */
  loading: boolean;
  /**紧凑型 */
  size: SizeType;
  /**搜索栏 */
  seached: boolean;
  /**记录数据 */
  data: object[];
  /**勾选记录 */
  selectedRowKeys: (string | number)[];
};

/**表格状态 */
let tableState: TabeStateType = reactive({
  loading: false,
  size: 'middle',
  seached: false,
  data: [],
  selectedRowKeys: [],
});

/**表格字段列 */
let tableColumns: ColumnsType = [
  {
    title: t('views.system.user.userNum'),
    dataIndex: 'userId',
    align: 'left',
    width: 120,
  },
  {
    title: t('views.system.user.account'),
    dataIndex: 'userName',
    align: 'left',
    width: 150,
  },
  {
    title: t('views.system.user.userName'),
    dataIndex: 'nickName',
    align: 'left',
    width: 150,
  },
  {
    title: t('views.system.user.permission'),
    key: 'roles',
    align: 'left',
    width: 200,
  },
  {
    title: t('views.system.user.className'),
    dataIndex: 'deptId',
    key: 'deptId',
    align: 'left',
    width: 200,
  },
  {
    title: t('views.system.user.loginIp'),
    dataIndex: 'loginIp',
    key: 'loginIp',
    align: 'left',
    width: 120,
  },
  {
    title: t('views.system.user.loginTime'),
    dataIndex: 'loginDate',
    align: 'left',
    width: 200,
    customRender(opt) {
      if (+opt.value <= 0) return '';
      return parseDateToStr(+opt.value);
    },
  },
  {
    title: t('views.system.user.status'),
    dataIndex: 'status',
    key: 'status',
    align: 'left',
    width: 100,
  },
  {
    title: t('common.operate'),
    key: 'userId',
    align: 'left',
  },
];

/**表格分页器参数 */
let tablePagination = reactive({
  /**当前页数 */
  current: 1,
  /**每页条数 */
  pageSize: 20,
  /**默认的每页条数 */
  defaultPageSize: 20,
  /**指定每页可以显示多少条 */
  pageSizeOptions: ['10', '20', '50', '100'],
  /**只有一页时是否隐藏分页器 */
  hideOnSinglePage: false,
  /**是否可以快速跳转至某页 */
  showQuickJumper: true,
  /**是否可以改变 pageSize */
  showSizeChanger: true,
  /**数据总数 */
  total: 0,
  showTotal: (total: number) =>
    t('common.tablePaginationTotal', { total: total }),
  onChange: (page: number, pageSize: number) => {
    tablePagination.current = page;
    tablePagination.pageSize = pageSize;
    queryParams.pageNum = page;
    queryParams.pageSize = pageSize;
    fnGetList();
  },
});

/**表格紧凑型变更操作 */
function fnTableSize({ key }: MenuInfo) {
  tableState.size = key as SizeType;
}

/**表格多选 */
function fnTableSelectedRowKeys(keys: (string | number)[]) {
  tableState.selectedRowKeys = keys;
}

type OptionsType = {
  /**岗位选择列表 */
  posts: Record<string, any>[];
  /**角色选择列表 */
  roles: Record<string, any>[];
};

/**对话框对象信息状态类型 */
type ModalStateType = {
  /**详情框是否显示 */
  openByView: boolean;
  /**新增框或修改框是否显示 */
  openByEdit: boolean;
  /**重置密码框是否显示 */
  openByResetPwd: boolean;
  /**标题 */
  title: string;
  /**表单数据 */
  from: Record<string, any>;
  /**确定按钮 loading */
  confirmLoading: boolean;
  /**选择列表 */
  options: OptionsType;
};

/**对话框对象信息状态 */
let modalState: ModalStateType = reactive({
  openByView: false,
  openByEdit: false,
  openByResetPwd: false,
  title: '用户',
  from: {
    userId: undefined,
    userName: '',
    password: '',
    deptId: '100',
    email: '',
    loginDate: 0,
    loginIp: '',
    nickName: '',
    phonenumber: '',
    postIds: [],
    roleIds: [],
    sex: '0',
    status: '0',
    remark: '',
    createTime: 0,
  },
  confirmLoading: false,
  options: {
    posts: [],
    roles: [],
  },
});

/**对话框内表单属性和校验规则 */
const modalStateFrom = Form.useForm(
  modalState.from,
  reactive({
    userName: [
      {
        required: true,
        pattern: regExpUserName,
        message: t('views.system.user.userNameTip'),
      },
    ],
    password: [
      {
        required: true,
        pattern: regExpPasswd,
        message: t('views.system.user.passwdTip'),
      },
    ],
    nickName: [
      {
        required: true,
        pattern: regExpNick,
        message: t('views.system.user.nickNameTip'),
      },
    ],
    email: [
      {
        required: false,
        pattern: regExpEmail,
        message: t('views.system.user.emailTip'),
      },
    ],
    phonenumber: [
      {
        required: false,
        pattern: regExpMobile,
        message: t('views.system.user.phoneTip'),
      },
    ],
  })
);

/**
 * 对话框弹出显示为 查看
 * @param userId 用户编号ID
 */
function fnModalVisibleByVive(userId: string | number) {
  if (!userId) {
    message.error(t('views.system.user.viewInfoErr'), 2);
    return;
  }
  if (modalState.confirmLoading) return;
  const hide = message.loading(t('common.loading'), 0);
  modalState.confirmLoading = true;
  getUser(userId).then(res => {
    modalState.confirmLoading = false;
    hide();
    if (res.code === RESULT_CODE_SUCCESS && res.data) {
      const roles = res.data.roles.map((m: Record<string, any>) => {
        const disabled = m.status === '0';
        Reflect.set(m, 'disabled', disabled);
        return m;
      });
      const posts = res.data.posts.map((m: Record<string, any>) => {
        const disabled = m.status === '0';
        Reflect.set(m, 'disabled', disabled);
        return m;
      });
      modalState.options.roles = roles;
      modalState.options.posts = posts;
      const { user, roleIds, postIds } = res.data;
      modalState.from = Object.assign(modalState.from, user);
      modalState.from.roleIds = roleIds;
      modalState.from.postIds = postIds;
      // 头像解析
      modalState.from.avatar = userStore.fnAvatar(modalState.from.avatar);
      modalState.title = t('views.system.user.userInfo');
      modalState.openByView = true;
    } else {
      message.error(t('views.system.user.tipRowErr'), 2);
    }
  });
}

/**
 * 对话框弹出显示为 新增或者修改
 * @param userId 用户编号ID, 不传为新增
 */
function fnModalVisibleByEdit(userId?: string | number) {
  if (!userId) {
    modalStateFrom.resetFields();
    if (modalState.confirmLoading) return;
    const hide = message.loading(t('common.loading'), 0);
    modalState.confirmLoading = true;
    // 查询用户详细获取岗位和角色列表
    getUser().then(res => {
      modalState.confirmLoading = false;
      hide();
      if (res.code === RESULT_CODE_SUCCESS && res.data) {
        const roles = res.data.roles.map((m: Record<string, any>) => {
          const disabled = m.status === '0';
          Reflect.set(m, 'disabled', disabled);
          return m;
        });
        const posts = res.data.posts.map((m: Record<string, any>) => {
          const disabled = m.status === '0';
          Reflect.set(m, 'disabled', disabled);
          return m;
        });
        modalState.options.roles = roles;
        modalState.options.posts = posts;
        const { user, roleIds, postIds } = res.data;
        modalState.from = Object.assign(modalState.from, user);
        modalState.from.roleIds = roleIds;
        modalState.from.postIds = postIds;
        modalState.from.deptId = deptTreeData.value[0].id;
        modalState.title =
          t('common.addText') + t('views.system.user.userInfo');
        modalState.openByEdit = true;
      } else {
        message.error(t('views.system.user.tipRowErr'), 2);
      }
    });
  } else {
    if (modalState.confirmLoading) return;
    const hide = message.loading(t('common.loading'), 0);
    modalState.confirmLoading = true;
    getUser(userId).then(res => {
      modalState.confirmLoading = false;
      hide();
      if (res.code === RESULT_CODE_SUCCESS && res.data) {
        const roles = res.data.roles.map((m: Record<string, any>) => {
          const disabled = m.status === '0';
          Reflect.set(m, 'disabled', disabled);
          return m;
        });
        const posts = res.data.posts.map((m: Record<string, any>) => {
          const disabled = m.status === '0';
          Reflect.set(m, 'disabled', disabled);
          return m;
        });
        modalState.options.roles = roles;
        modalState.options.posts = posts;
        const { user, roleIds, postIds } = res.data;
        modalState.from = Object.assign(modalState.from, user);
        modalState.from.roleIds = roleIds;
        modalState.from.postIds = postIds;
        modalState.title =
          t('common.editText') + t('views.system.user.userInfo');
        modalState.openByEdit = true;
      } else {
        message.error(t('views.system.user.tipRowErr'), 2);
      }
    });
  }
}

/**
 * 对话框弹出确认执行函数
 * 进行表达规则校验
 */
function fnModalOk() {
  let validateName = ['nickName', 'email', 'phonenumber'];
  if (!modalState.from.userId) {
    validateName.push('userName', 'password');
  }
  modalStateFrom
    .validate(validateName)
    .then(() => {
      modalState.confirmLoading = true;
      const from = toRaw(modalState.from);
      const user = from.userId ? updateUser(from) : addUser(from);
      const key = 'user';
      message.loading({ content: t('common.loading'), key });
      user
        .then(res => {
          if (res.code === RESULT_CODE_SUCCESS) {
            message.success({
              content: t('common.msgSuccess', { msg: modalState.title }),
              key,
              duration: 2,
            });
            modalState.openByEdit = false;
            modalStateFrom.resetFields();
            fnGetList(1);
          } else {
            message.error({
              content: `${res.msg}`,
              key,
              duration: 2,
            });
          }
        })
        .finally(() => {
          modalState.confirmLoading = false;
        });
    })
    .catch(e => {
      message.error(t('common.errorFields', { num: e.errorFields.length }), 3);
    });
}

/**
 * 对话框弹出关闭执行函数
 * 进行表达规则校验
 */
function fnModalCancel() {
  modalState.openByEdit = false;
  modalState.openByView = false;
  modalState.openByResetPwd = false;
  modalStateFrom.resetFields();
  modalState.options.roles = [];
  modalState.options.posts = [];
}

/**
 * 对话框弹出重置密码确认执行函数
 * 进行表达规则校验
 */
function fnModalOkResetPwd() {
  modalStateFrom
    .validate(['password'])
    .then(() => {
      modalState.confirmLoading = true;
      const key = 'user';
      message.loading({ content: t('common.loading'), key });
      resetUserPwd(modalState.from.userId, modalState.from.password)
        .then(res => {
          if (res.code === RESULT_CODE_SUCCESS) {
            message.success({
              content: t('common.msgSuccess', { msg: modalState.title }),
              key,
              duration: 2,
            });
            modalState.openByResetPwd = false;
            modalStateFrom.resetFields();
          } else {
            message.error({
              content: `${res.msg}`,
              key,
              duration: 2,
            });
          }
        })
        .finally(() => {
          modalState.confirmLoading = false;
        });
    })
    .catch(e => {
      message.error(t('common.errorFields', { num: e.errorFields.length }), 3);
    });
}

/**
 * 对话框弹出显示为 用户重置密码
 * @param row 用户记录对象
 */
function fnRecordResetPwd(row: Record<string, string>) {
  modalStateFrom.resetFields();
  modalState.from.userId = row.userId;
  modalState.from.userName = row.userName;
  modalState.title = t('views.system.user.resetPwd');
  modalState.openByResetPwd = true;
}

/**
 * 用户状态修改
 * @param row 用户记录对象
 */
function fnRecordStatus(row: Record<string, string>) {
  const text =
    row.status === '1'
      ? t('views.system.user.start')
      : t('views.system.user.stop');
  Modal.confirm({
    title: t('common.tipTitle'),
    content: t('views.system.user.sureTip', {
      text: text,
      userName: row.userName,
    }),
    onOk() {
      const key = 'changeUserStatus';
      message.loading({ content: t('common.loading'), key });
      changeUserStatus(row.userId, row.status).then(res => {
        if (res.code === RESULT_CODE_SUCCESS) {
          message.success({
            content: `${row.userName} ${t('common.msgSuccess', { msg: text })}`,
            key: key,
            duration: 2,
          });
        } else {
          message.error({
            content: `${res.msg}`,
            key: key,
            duration: 2,
          });
        }
        fnGetList();
      });
    },
    onCancel() {
      row.status = row.status === '1' ? '0' : '1';
    },
  });
}

/**
 * 用户删除
 * @param userId 用户编号ID
 */
function fnRecordDelete(userId: string = '0') {
  if (userId === '0') {
    userId = tableState.selectedRowKeys.join(',');
  }
  Modal.confirm({
    title: t('common.tipTitle'),
    content: t('views.system.user.delSure', { userId: userId }),
    onOk() {
      const key = 'delUser';
      message.loading({ content: t('common.loading'), key });
      delUser(userId).then(res => {
        if (res.code === RESULT_CODE_SUCCESS) {
          message.success({
            content: t('views.system.user.delSuss'),
            key,
            duration: 2,
          });
          fnGetList();
        } else {
          message.error({
            content: `${res.msg}`,
            key: key,
            duration: 2,
          });
        }
      });
    },
  });
}

/**列表导出 */
function fnExportList() {
  Modal.confirm({
    title: t('common.tipTitle'),
    content: t('views.system.user.exportSure'),
    onOk() {
      const key = 'exportUser';
      message.loading({ content: t('common.loading'), key });
      exportUser(toRaw(queryParams)).then(res => {
        if (res.code === RESULT_CODE_SUCCESS) {
          message.success({
            content: t('common.msgSuccess', {
              msg: t('views.system.user.export'),
            }),
            key,
            duration: 2,
          });
          saveAs(res.data, `user_${Date.now()}.xlsx`);
        } else {
          message.error({
            content: `${res.msg}`,
            key,
            duration: 2,
          });
        }
      });
    },
  });
}

/**对话框表格信息导入对象信息状态类型 */
type ModalUploadImportStateType = {
  /**是否显示 */
  open: boolean;
  /**标题 */
  title: string;
  /**是否上传中 */
  loading: boolean;
  /**是否更新已经存在的数据 */
  updateSupport: boolean;
  /**上传结果信息 */
  msg: string;
  /**导入模板下载触发 */
  templateDownload: boolean;
};

/**对话框表格信息导入对象信息状态 */
let uploadImportState: ModalUploadImportStateType = reactive({
  open: false,
  title: t('views.system.user.importTitle'),
  loading: false,
  updateSupport: false,
  msg: '',
  templateDownload: false,
});

/**对话框表格信息导入弹出窗口 */
function fnModalUploadImportOpen() {
  uploadImportState.updateSupport = false;
  uploadImportState.msg = '';
  uploadImportState.open = true;
}

/**对话框表格信息导入关闭窗口 */
function fnModalUploadImportClose() {
  uploadImportState.open = false;
}

/**对话框表格信息导入上传 */
function fnModalUploadImportUpload(file: File) {
  const hide = message.loading(t('common.loading'), 0);
  uploadImportState.loading = true;
  let formData = new FormData();
  formData.append('file', file);
  formData.append('updateSupport', `${uploadImportState.updateSupport}`);
  importData(formData)
    .then(res => {
      uploadImportState.msg = res.msg?.replaceAll(/<br\/>+/g, '\r');
    })
    .catch((err: { code: number; msg: string }) => {
      message.error(` ${err.msg}`);
    })
    .finally(() => {
      hide();
      uploadImportState.loading = false;
    });
}

/**对话框表格信息导入模板 */
function fnModalUploadImportExportTemplate() {
  if (uploadImportState.templateDownload) return;
  uploadImportState.templateDownload = true;
  const hide = message.loading(t('common.loading'), 0);
  importTemplate()
    .then(res => {
      if (res.code === RESULT_CODE_SUCCESS) {
        message.success({
          content: t('views.system.user.importOk'),
          duration: 2,
        });
        saveAs(res.data, `user_template_${Date.now()}.xlsx`);
      } else {
        message.error({
          content: `${res.msg}`,
          duration: 2,
        });
      }
    })
    .finally(() => {
      hide();
      uploadImportState.templateDownload = false;
    });
}

/**查询用户列表, pageNum初始页数 */
function fnGetList(pageNum?: number) {
  if (tableState.loading) return;
  tableState.loading = true;
  if (pageNum) {
    queryParams.pageNum = pageNum;
  }
  if (!queryRangePicker.value) {
    queryRangePicker.value = ['', ''];
  }
  queryParams.beginTime = queryRangePicker.value[0];
  queryParams.endTime = queryRangePicker.value[1];
  listUser(toRaw(queryParams)).then(res => {
    if (res.code === RESULT_CODE_SUCCESS && Array.isArray(res.rows)) {
      // 取消勾选
      if (tableState.selectedRowKeys.length > 0) {
        tableState.selectedRowKeys = [];
      }
      tablePagination.total = res.total;
      tableState.data = res.rows;
    }
    tableState.loading = false;
  });
}

/**初始部门树数据 */
let deptTreeData = ref<DataNode[]>([]);

/**查询部门下拉树结构 */
function fnGetDeptTree() {
  if (deptTreeData.value.length > 0) return;
  deptTreeSelect().then(res => {
    if (res.code === RESULT_CODE_SUCCESS && Array.isArray(res.data)) {
      deptTreeData.value = res.data;
    }
  });
}

onMounted(() => {
  // 初始字典数据
  Promise.allSettled([
    getDict('sys_normal_disable'),
    getDict('sys_user_sex'),
  ]).then(resArr => {
    if (resArr[0].status === 'fulfilled') {
      dict.sysNormalDisable = resArr[0].value;
    }
    if (resArr[1].status === 'fulfilled') {
      dict.sysUserSex = resArr[1].value;
    }
  });
  // 获取列表数据
  fnGetList();
  // 获取部门树结构数据
  fnGetDeptTree();
});
</script>

<template>
  <PageContainer>
    <a-card
      v-show="tableState.seached"
      :bordered="false"
      :body-style="{ marginBottom: '24px', paddingBottom: 0 }"
    >
      <!-- 表格搜索栏 -->
      <a-form :model="queryParams" name="queryParams" layout="horizontal">
        <a-row :gutter="16">
          <a-col :lg="18" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.className')"
              name="deptId"
            >
              <a-tree-select
                v-model:value="queryParams.deptId"
                show-search
                tree-default-expand-all
                :tree-data="deptTreeData"
                :field-names="{
                  children: 'children',
                  label: 'label',
                  value: 'id',
                }"
                tree-node-label-prop="label"
                tree-node-filter-prop="label"
                style="width: 100%"
                :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                :placeholder="t('common.selectPlease')"
              >
              </a-tree-select>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :xs="24">
            <a-form-item>
              <a-space :size="8">
                <a-button type="primary" @click.prevent="fnGetList(1)">
                  <template #icon><SearchOutlined /></template>
                  {{ t('common.search') }}
                </a-button>
                <a-button type="default" @click.prevent="fnQueryReset">
                  <template #icon><ClearOutlined /></template>
                  {{ t('common.reset') }}
                </a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="16">
          <a-col :lg="6" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.account')"
              name="userName"
            >
              <a-input
                v-model:value="queryParams.userName"
                allow-clear
                :maxlength="30"
                :placeholder="t('common.inputPlease')"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.phone')"
              name="phonenumber"
            >
              <a-input
                v-model:value="queryParams.phonenumber"
                allow-clear
                :maxlength="11"
                :placeholder="t('common.inputPlease')"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="4" :md="12" :xs="24">
            <a-form-item :label="t('views.system.user.status')" name="status">
              <a-select
                v-model:value="queryParams.status"
                allow-clear
                :options="dict.sysNormalDisable"
                :placeholder="t('common.selectPlease')"
              >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="8" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.loginTime')"
              name="queryRangePicker"
            >
              <a-range-picker
                v-model:value="queryRangePicker"
                allow-clear
                bordered
                :show-time="{ format: 'HH:mm:ss' }"
                format="YYYY-MM-DD HH:mm:ss"
                value-format="x"
                style="width: 100%"
              ></a-range-picker>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" :body-style="{ padding: '0px' }">
      <!-- 插槽-卡片左侧侧 -->
      <template #title>
        <a-space :size="8" align="center">
          <a-button
            type="primary"
            @click.prevent="fnModalVisibleByEdit()"
            v-perms:has="['system:user:add']"
          >
            <template #icon><PlusOutlined /></template>
            {{ t('common.addText') }}
          </a-button>
          <a-button
            type="default"
            danger
            :disabled="tableState.selectedRowKeys.length <= 0"
            @click.prevent="fnRecordDelete()"
            v-perms:has="['system:user:remove']"
          >
            <template #icon><DeleteOutlined /></template>
            {{ t('common.deleteText') }}
          </a-button>
          <a-button
            type="dashed"
            @click.prevent="fnModalUploadImportOpen()"
            v-perms:has="['system:user:import']"
          >
            <template #icon><ImportOutlined /></template>
            {{ t('common.import') }}
          </a-button>
          <a-button
            type="dashed"
            @click.prevent="fnExportList()"
            v-perms:has="['system:user:export']"
          >
            <template #icon><ExportOutlined /></template>
            {{ t('common.export') }}
          </a-button>
        </a-space>
      </template>

      <!-- 插槽-卡片右侧 -->
      <template #extra>
        <a-space :size="8" align="center">
          <a-tooltip>
            <template #title>{{ t('common.searchBarText') }}</template>
            <a-switch
              v-model:checked="tableState.seached"
              :checked-children="t('common.switch.show')"
              :un-checked-children="t('common.switch.hide')"
              size="small"
            />
          </a-tooltip>
          <a-tooltip>
            <template #title>{{ t('common.reloadText') }}</template>
            <a-button type="text" @click.prevent="fnGetList()">
              <template #icon><ReloadOutlined /></template>
            </a-button>
          </a-tooltip>
          <a-tooltip placement="topRight">
            <template #title>{{ t('common.sizeText') }}</template>
            <a-dropdown placement="bottomRight" trigger="click">
              <a-button type="text">
                <template #icon><ColumnHeightOutlined /></template>
              </a-button>
              <template #overlay>
                <a-menu
                  :selected-keys="[tableState.size as string]"
                  @click="fnTableSize"
                >
                  <a-menu-item key="default">
                    {{ t('common.size.default') }}
                  </a-menu-item>
                  <a-menu-item key="middle">
                    {{ t('common.size.middle') }}</a-menu-item
                  >
                  <a-menu-item key="small">
                    {{ t('common.size.small') }}
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </a-tooltip>
        </a-space>
      </template>

      <!-- 表格列表 -->
      <a-table
        class="table"
        row-key="userId"
        :columns="tableColumns"
        :loading="tableState.loading"
        :data-source="tableState.data"
        :size="tableState.size"
        :pagination="tablePagination"
        :scroll="{ x: tableColumns.length * 150 }"
        :row-selection="{
          type: 'checkbox',
          selectedRowKeys: tableState.selectedRowKeys,
          onChange: fnTableSelectedRowKeys,
        }"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'roles'">
            <a-tag color="cyan" v-for="r in record.roles">
              {{ r.roleName }}
            </a-tag>
          </template>
          <template v-if="column.key === 'deptId'">
            {{ record.dept?.deptName }}
          </template>
          <template v-if="column.key === 'status'">
            <DictTag
              v-if="
                record.userId === '1' || record.userName === userStore.userName
              "
              :options="dict.sysNormalDisable"
              :value="record.status"
            />
            <a-switch
              v-else
              v-perms:has="['system:user:edit']"
              v-model:checked="record.status"
              checked-value="1"
              :checked-children="dict.sysNormalDisable[0].label"
              un-checked-value="0"
              :un-checked-children="dict.sysNormalDisable[1].label"
              size="small"
              @change="fnRecordStatus(record)"
            />
          </template>
          <template v-if="column.key === 'userId'">
            <a-space :size="8" align="center" v-if="record.userId !== '1'">
              <a-tooltip>
                <template #title>{{ t('common.viewText') }}</template>
                <a-button
                  type="link"
                  @click.prevent="fnModalVisibleByVive(record.userId)"
                  v-perms:has="['system:user:query']"
                >
                  <template #icon><ProfileOutlined /></template>
                </a-button>
              </a-tooltip>
              <a-tooltip>
                <template #title>{{ t('common.editText') }}</template>
                <a-button
                  type="link"
                  @click.prevent="fnModalVisibleByEdit(record.userId)"
                  v-perms:has="['system:user:edit']"
                >
                  <template #icon><FormOutlined /></template>
                </a-button>
              </a-tooltip>
              <a-tooltip>
                <template #title>{{ t('common.deleteText') }}</template>
                <a-button
                  type="link"
                  @click.prevent="fnRecordDelete(record.userId)"
                  v-perms:has="['system:user:remove']"
                >
                  <template #icon><DeleteOutlined /></template>
                </a-button>
              </a-tooltip>
              <a-tooltip placement="topLeft">
                <template #title>
                  {{ t('views.system.user.resetPwd') }}
                </template>
                <a-button
                  type="link"
                  @click.prevent="fnRecordResetPwd(record)"
                  v-perms:has="['system:user:resetPwd']"
                >
                  <template #icon><SecurityScanOutlined /></template>
                </a-button>
              </a-tooltip>
            </a-space>
          </template>
        </template>
      </a-table>
    </a-card>

    <!-- 详情框 -->
    <ProModal
      :drag="true"
      :width="800"
      :destroyOnClose="false"
      :open="modalState.openByView"
      :title="modalState.title"
      @cancel="fnModalCancel"
    >
      <a-form layout="horizontal" :label-col="{ span: 6 }" :label-wrap="true">
        <a-row>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item :label="t('views.system.user.userNum')" name="userId">
              {{ modalState.from.userId }}
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.createTime')"
              name="createTime"
            >
              <span v-if="+modalState.from.createTime > 0">
                {{ parseDateToStr(+modalState.from.createTime) }}
              </span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item :label="t('views.system.user.loginIp')" name="loginIp">
              {{ modalState.from.loginIp }}
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.loginTime')"
              name="loginDate"
            >
              <span v-if="+modalState.from.loginDate > 0">
                {{ parseDateToStr(+modalState.from.loginDate) }}
              </span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item :label="t('views.system.user.userTop')" name="avatar">
              <a-avatar
                shape="circle"
                size="default"
                :src="modalState.from.avatar"
                :alt="modalState.from.userName"
              ></a-avatar>
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="12" :xs="24"> </a-col>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.account')"
              name="userName"
            >
              {{ modalState.from.userName }}
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.userName')"
              name="nickName"
            >
              {{ modalState.from.nickName }}
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item :label="t('views.system.user.sex')" name="sex">
              <DictTag
                :options="dict.sysUserSex"
                :value="modalState.from.sex"
              />
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item :label="t('views.system.user.status')" name="status">
              <DictTag
                :options="dict.sysNormalDisable"
                :value="modalState.from.status"
              />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.phone')"
              name="phonenumber"
            >
              {{ modalState.from.phonenumber }}
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item :label="t('views.system.user.email')" name="email">
              {{ modalState.from.email }}
            </a-form-item>
          </a-col>
        </a-row>

        <a-form-item
          :label="t('views.system.user.fromClass')"
          name="deptId"
          :label-col="{ span: 3 }"
          :label-wrap="true"
        >
          <a-tree-select
            :value="modalState.from.deptId"
            disabled
            :tree-data="deptTreeData"
            :field-names="{
              children: 'children',
              label: 'label',
              value: 'id',
            }"
            tree-node-label-prop="label"
            style="width: 100%"
            :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
          >
            <template #suffixIcon></template>
          </a-tree-select>
        </a-form-item>

        <a-row>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.userWork')"
              name="postIds"
            >
              <a-select
                :value="modalState.from.postIds"
                disabled
                mode="multiple"
                :placeholder="t('views.system.user.userWorkPlease')"
                option-label-prop="postName"
                :options="modalState.options.posts"
                :field-names="{ label: 'postName', value: 'postId' }"
              >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col
            :lg="12"
            :md="12"
            :xs="24"
            v-perms:has="['system:user:editRole']"
          >
            <a-form-item
              :label="t('views.system.user.permission')"
              name="roleIds"
            >
              <a-select
                :value="modalState.from.roleIds"
                disabled
                mode="multiple"
                option-label-prop="roleName"
                :options="modalState.options.roles"
                :field-names="{ label: 'roleName', value: 'roleId' }"
              >
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>

        <a-form-item
          :label="t('views.system.user.userTip')"
          name="remark"
          :label-col="{ span: 3 }"
          :label-wrap="true"
        >
          {{ modalState.from.remark }}
        </a-form-item>
      </a-form>
      <template #footer>
        <a-button key="cancel" @click="fnModalCancel">
          {{ t('common.close') }}
        </a-button>
      </template>
    </ProModal>

    <!-- 新增框或修改框 -->
    <ProModal
      :drag="true"
      :width="800"
      :destroyOnClose="true"
      :keyboard="false"
      :mask-closable="false"
      :open="modalState.openByEdit"
      :title="modalState.title"
      :confirm-loading="modalState.confirmLoading"
      @ok="fnModalOk"
      @cancel="fnModalCancel"
    >
      <a-form
        name="modalStateFromByEdit"
        layout="horizontal"
        :label-col="{ span: 6 }"
        :labelWrap="true"
      >
        <a-row v-if="!modalState.from.userId">
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.account')"
              name="userName"
              v-bind="modalStateFrom.validateInfos.userName"
            >
              <a-input
                v-model:value="modalState.from.userName"
                allow-clear
                :maxlength="30"
                :placeholder="t('common.inputPlease')"
              >
                <template #prefix>
                  <UserOutlined />
                </template>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.loginPwd')"
              name="password"
              v-bind="modalStateFrom.validateInfos.password"
            >
              <a-input-password
                v-model:value="modalState.from.password"
                :maxlength="26"
                :placeholder="t('common.inputPlease')"
              >
                <template #prefix>
                  <LockOutlined />
                </template>
              </a-input-password>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.userName')"
              name="nickName"
              v-bind="modalStateFrom.validateInfos.nickName"
            >
              <a-input
                v-model:value="modalState.from.nickName"
                allow-clear
                :maxlength="30"
                :placeholder="t('common.inputPlease')"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col
            :lg="12"
            :md="12"
            :xs="24"
            v-perms:has="['system:user:editPost']"
          >
            <a-form-item
              :label="t('views.system.user.userWork')"
              name="postIds"
            >
              <a-select
                v-model:value="modalState.from.postIds"
                allow-clear
                mode="multiple"
                show-search
                option-filter-prop="postName"
                option-label-prop="postName"
                :options="modalState.options.posts"
                :field-names="{ label: 'postName', value: 'postId' }"
                :placeholder="t('common.selectPlease')"
              >
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.phone')"
              name="phonenumber"
              v-bind="modalStateFrom.validateInfos.phonenumber"
            >
              <IntlTelInput
                v-model:value="modalState.from.phonenumber"
                allow-clear
                :maxlength="20"
                :placeholder="t('common.inputPlease')"
              ></IntlTelInput>
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item
              :label="t('views.system.user.email')"
              name="email"
              v-bind="modalStateFrom.validateInfos.email"
            >
              <a-input
                v-model:value="modalState.from.email"
                allow-clear
                :maxlength="40"
                :placeholder="t('common.inputPlease')"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :lg="12" :md="12" :xs="24">
            <a-form-item :label="t('views.system.user.sex')" name="sex">
              <a-select
                v-model:value="modalState.from.sex"
                default-value="1"
                :options="dict.sysUserSex"
                :placeholder="t('common.selectPlease')"
              >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col
            :lg="12"
            :md="12"
            :xs="24"
            v-if="
              modalState.from.userId !== '1' &&
              modalState.from.userId !== userStore.userId
            "
          >
            <a-form-item :label="t('views.system.user.status')" name="status">
              <a-select
                v-model:value="modalState.from.status"
                default-value="0"
                :options="dict.sysNormalDisable"
                :placeholder="t('common.selectPlease')"
              >
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>

        <a-form-item
          :label="t('views.system.user.permission')"
          name="roleIds"
          :label-col="{ span: 3 }"
          v-perms:has="['system:user:editRole']"
        >
          <a-select
            v-model:value="modalState.from.roleIds"
            :allow-clear="false"
            mode="multiple"
            show-search
            option-filter-prop="roleName"
            option-label-prop="roleName"
            :options="modalState.options.roles"
            :field-names="{ label: 'roleName', value: 'roleId' }"
            :placeholder="t('common.selectPlease')"
          >
          </a-select>
        </a-form-item>

        <a-form-item
          :label="t('views.system.user.fromClass')"
          name="deptId"
          :label-col="{ span: 3 }"
        >
          <a-tree-select
            v-model:value="modalState.from.deptId"
            show-search
            tree-default-expand-all
            :tree-data="deptTreeData"
            :field-names="{
              children: 'children',
              label: 'label',
              value: 'id',
            }"
            tree-node-label-prop="label"
            tree-node-filter-prop="label"
            style="width: 100%"
            :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
            :placeholder="t('common.selectPlease')"
          >
          </a-tree-select>
        </a-form-item>

        <a-form-item
          :label="t('views.system.user.userTip')"
          name="remark"
          :label-col="{ span: 3 }"
        >
          <a-textarea
            v-model:value="modalState.from.remark"
            :auto-size="{ minRows: 4, maxRows: 6 }"
            :maxlength="450"
            :show-count="true"
            :placeholder="t('common.inputPlease')"
          />
        </a-form-item>
      </a-form>
    </ProModal>

    <!-- 重置密码修改框 -->
    <ProModal
      :drag="true"
      :destroyOnClose="true"
      :keyboard="false"
      :mask-closable="false"
      :open="modalState.openByResetPwd"
      :title="modalState.title"
      :confirm-loading="modalState.confirmLoading"
      @ok="fnModalOkResetPwd"
      @cancel="fnModalCancel"
    >
      <a-form name="modalStateFromByResetPwd" layout="horizontal">
        <a-form-item
          :label="t('views.system.user.account')"
          name="userName"
          v-bind="modalStateFrom.validateInfos.userName"
        >
          <a-input :value="modalState.from.userName" disabled :maxlength="30">
            <template #prefix>
              <UserOutlined />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item
          :label="t('views.system.user.loginPwd')"
          name="password"
          v-bind="modalStateFrom.validateInfos.password"
        >
          <a-input-password
            v-model:value="modalState.from.password"
            :maxlength="26"
          >
            <template #prefix>
              <LockOutlined />
            </template>
          </a-input-password>
        </a-form-item>
      </a-form>
    </ProModal>

    <!-- 上传导入表格数据文件框 -->
    <UploadModal
      :title="uploadImportState.title"
      :loading="uploadImportState.loading"
      @upload="fnModalUploadImportUpload"
      @close="fnModalUploadImportClose"
      v-model:open="uploadImportState.open"
      :ext="['.xls', '.xlsx']"
      :size="10"
    >
      <template #default>
        <a-row justify="space-between" align="middle">
          <a-col :span="12">
            <a-checkbox v-model:checked="uploadImportState.updateSupport">
              {{ t('views.system.user.updateSure') }}
            </a-checkbox>
          </a-col>
          <a-col :span="6">
            <a-button
              type="link"
              :title="t('views.system.user.downloadObj')"
              @click.prevent="fnModalUploadImportExportTemplate"
            >
              {{ t('views.system.user.downloadObj') }}
            </a-button>
          </a-col>
        </a-row>
        <a-textarea
          :disabled="true"
          :hidden="!uploadImportState.msg"
          :value="uploadImportState.msg"
          :auto-size="{ minRows: 2, maxRows: 8 }"
          style="background-color: transparent; color: rgba(0, 0, 0, 0.85)"
        />
      </template>
    </UploadModal>
  </PageContainer>
</template>

<style lang="less" scoped>
.table :deep(.ant-pagination) {
  padding: 0 24px;
}
</style>
