<template>
  <div class="search-list-form">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="searchForm.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" placeholder="请选择状态">
          <el-option label="全部" value=""></el-option>
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据列表 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      header-row-class-name="headerBg"
      empty-text="暂无用户信息"
    >
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="nikeName" label="昵称"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="level" label="用户等级">
        <template slot-scope="scope">
          {{ scope.row.level === "0" ? "员工" : "游客" }}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{ scope.row.status === "1" ? "启用" : "禁用" }}
        </template>
      </el-table-column> -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.examine=='0'||scope.row.examine=='2'" @click="handleEdit(scope.$index, scope.row)"
            >审核</el-button
          >
          <!-- <el-button
              size="mini"
              :type="scope.row.status === '1' ? 'danger' : 'success'"
              @click="handleToggleStatus(scope.$index, scope.row)">
              {{ scope.row.status === '1' ? '禁用' : '启用' }}
            </el-button> -->
          <el-button type="danger" @click="handleDel(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
      >
      </el-pagination>
    </div>

    <!-- 编辑表单对话框 -->
    <el-dialog title="审核信息" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="微信名称" :label-width="formLabelWidth">
          <el-input v-model="editForm.nikeName" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="editForm.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="editForm.phone" disabled></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="danger" @click="submitAudit(1)">不通过</el-button>
        <el-button type="primary" @click="submitAudit(0)">通过</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import { getInfoList, deleteUserInfo, toExamine } from "@/api/applet";
import { status } from "nprogress";
export default {
  data() {
    return {
      searchForm: {
        name: "",
        status: "",
      },
      tableData: [
        /* { name: '张三', age: 25, status: '1' },
          { name: '李四', age: 30, status: '0' },
          { name: '王五', age: 35, status: '1' } */
      ],
      dialogFormVisible: false,
      editForm: {
        name: "",
        age: "",
      },
      formLabelWidth: "120px",
      editIndex: -1,
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  mounted() {
    this.search();
  },
  methods: {
    handleToggleStatus(index, row) {
      // 切换状态
      this.tableData[index].status = row.status === "1" ? "0" : "1";
      // 这里应该调用API来更新状态
    },
    submitEdit() {
      if (this.editIndex > -1) {
        // 更新表格数据
        this.tableData[this.editIndex] = {
          ...this.editForm,
          status: this.tableData[this.editIndex].status,
        };
        // 这里应该调用API来更新数据
        this.dialogFormVisible = false;
      }
    },
    /* 删除 */
    handleDel(index, row) {
      let _this = this;
      let par = {
        id: row.id,
      };
      _this
        .$confirm("您是否确认删除该记录？", "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then((res) => {
          // console.log("AAA")
          deleteUserInfo(par).then((res) => {
            _this.search();
          });
        })
        .catch(() => {});
    },
    search() {
      const params = {
        ...this.searchForm,
        page: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
      };

      getInfoList(params).then((res) => {
        if (res.status !== 200) return;
        this.tableData = res.data.dataList;
        // 使用 Number() 函数确保 currentPage 是数字类型
        this.pagination.currentPage = Number(res.data.page);
        this.pagination.pageSize = Number(res.data.pageSize);
        this.pagination.total = Number(res.data.totalRecord);
      });
    },

    handleSizeChange(val) {
      this.pagination.pageSize = val;
      this.search();
    },

    handleCurrentChange(val) {
      this.pagination.currentPage = val;
      this.search();
    },

    resetSearch() {
      this.searchForm = {
        name: "",
        status: "",
      };
      this.search();
    },
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.editForm = {
        id: row.id,
        name: row.name,
        nikeName: row.nikeName,
        phone: row.phone,
      };
      this.editIndex = index;
    },
    submitAudit(level) {
      const data = { id: this.editForm.id, level: level };
      toExamine(data)
        .then((res) => {
          if (res.status === 200) {
            this.$message.success(
              level === 0 ? "审核通过成功" : "审核不通过成功"
            );
            this.dialogFormVisible = false;
            this.search();
          } else {
            this.$message.error("审核操作失败");
          }
        })
        .catch((err) => {
          this.$message.error("审核操作失败：" + err.message);
        });
    },
  },
};
</script>
  
  <style scoped>
.search-list-form {
  padding: 20px;
}
.pagination-container {
  display: flex;
  justify-content: flex-end;
}
</style>