<!-- @format -->

<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label=" 用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入 用户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="eMail">
        <el-input
          v-model="queryParams.eMail"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="身份证号" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="序号" align="center" type="index" width="100">
        <template #default="scope">
          <span>{{ handleTableIndex(scope.$index, queryParams) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column label=" 用户名称" align="center" prop="userName" />

      <el-table-column
        label="身份证号"
        width="200"
        align="center"
        prop="idNumber"
      >
        <template #default="scope">
          <span
            >{{ maskIdNumber(String(scope.row.idNumber), scope.row.isIdView) }}
            <el-icon
              style="cursor: pointer"
              @click="scope.row.isIdView = true"
              v-if="!scope.row.isIdView"
              ><View />
            </el-icon>
            <el-icon
              style="cursor: pointer"
              @click="scope.row.isIdView = false"
              v-else
              ><Hide
            /></el-icon>
          </span>
        </template>
      </el-table-column>

      <el-table-column width="200" label="手机号" align="center" prop="phone">
        <template #default="scope">
          <span
            >{{
              maskPhoneNumber(String(scope.row.phone), scope.row.isPhoneView)
            }}
            <el-icon
              style="cursor: pointer"
              @click="scope.row.isPhoneView = true"
              v-if="!scope.row.isPhoneView"
              ><View />
            </el-icon>
            <el-icon
              style="cursor: pointer"
              @click="scope.row.isPhoneView = false"
              v-else
              ><Hide
            /></el-icon>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" prop="eMail">
        <template #default="scope">
          <span
            >{{ desensiEmail(String(scope.row.eMail), scope.row.isEmailView) }}
            <el-icon
              style="cursor: pointer"
              @click="scope.row.isEmailView = true"
              v-if="!scope.row.isEmailView"
              ><View />
            </el-icon>
            <el-icon
              style="cursor: pointer"
              @click="scope.row.isEmailView = false"
              v-else
              ><Hide
            /></el-icon>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <!-- <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['apply:user:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['apply:user:remove']"
            >删除</el-button
          > -->
          <el-button
            link
            size="small"
            type="primary"
            icon="edit"
            @click="handleResetPassword(scope.row)"
            v-hasPermi="['apply:user:remove']"
            >重置密码</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="User">
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
} from "@/api/talentManagement/register";

const { proxy } = getCurrentInstance();

const userList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
    password: null,
    realName: null,
    phone: null,
    eMail: null,
    idNumber: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询申请端-用户信息列表 */
function getList() {
  loading.value = true;
  listUser(queryParams.value).then((response) => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userName: null,
    password: null,
    realName: null,
    phone: null,
    eMail: null,
    idNumber: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
  };
  proxy.resetForm("userRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加申请端-用户信息";
}

//重置密码
const handleResetPassword = (row) => {};

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getUser(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改申请端-用户信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["userRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateUser(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addUser(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除申请端-用户信息编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delUser(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "apply/user/export",
    {
      ...queryParams.value,
    },
    `user_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
