<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="人员类型" prop="personType">
        <el-select
          v-model="queryParams.personType"
          placeholder="请选择人员类型"
          clearable
        >
          <el-option
            v-for="item in personTypeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="人员姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入人员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="定位卡号" prop="cardId">
        <el-input
          v-model="queryParams.cardId"
          placeholder="请输入定位卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="人员类型" align="center" prop="personTypeName" />
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="所在部门" align="center" prop="deptName" />
      <el-table-column label="岗位名称" align="center" prop="positionName" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="身份证号" align="center" prop="idNumber" />
      <el-table-column label="定位卡号" align="center" prop="cardId" />
    </el-table>

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

<script>
import { listInfo } from "@/api/jinrun/renyuan";
import { getDict } from "@/api/jinrun/common";

export default {
  name: "Info",
  data() {
    return {
      // 人员类型下拉列表数据
      personTypeOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        personType: "",
        realName: null,
        cardId: null,
      },
      // 表单参数
      form: {
        personType: "",
      },
    };
  },
  created() {
    this.getList();
    this.getPersonTypeOptions();
  },

  methods: {
    /** 获取人员类型下拉列表数据 */
    getPersonTypeOptions() {
      // 调用数据字典接口
      getDict({ dictType: "person_type" })
        .then((response) => {
          this.personTypeOptions = response.records || [];
        })
        .catch((error) => {
          this.$modal.msgError("获取人员类型数据失败");
          console.error("获取人员类型数据失败:", error);
        });
    },
    /** 查询人员信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        createBy: null,
        createTime: null,
        updateTime: null,
        remark: null,
        personId: null,
        personType: null,
        personTypeName: null,
        staffType: null,
        staffTypeName: null,
        positionIconType: null,
        positionIconTypeName: null,
        realName: null,
        phone: null,
        sex: null,
        birth: null,
        email: null,
        nativePlace: null,
        nation: null,
        maritalStatus: null,
        politicsStatus: null,
        healthStatus: null,
        idType: null,
        idNumber: null,
        personIc: null,
        personCode: null,
        personSource: null,
        company: null,
        companyName: null,
        administratorName: null,
        administratorPhone: null,
        jobNumber: null,
        jobStatus: null,
        hireDate: null,
        hireDateAlias: null,
        dimissionDate: null,
        officePhone: null,
        duty: null,
        professionalTitle: null,
        professionalTitleNumber: null,
        seniority: null,
        experience: null,
        yearPlusExperience: null,
        highestEducation: null,
        highestDegree: null,
        profession: null,
        school: null,
        diplomaNumber: null,
        cengNumber: null,
        accessory: null,
        accessoryList: null,
        contractorId: null,
        contractorName: null,
        receiveLeader: null,
        receivePhone: null,
        gateThrough: null,
        leaveTime: null,
        visitorStatus: null,
        visitTime: null,
        process: null,
        lastUpdateDate: null,
        lastCreateDate: null,
        updateFail: null,
        onlineStatus: null,
        inOutStatus: null,
        cardType: null,
        cardTypeName: null,
        cardId: null,
        icCardId: null,
        cardPower: null,
        personPhoto: null,
        photoSign: null,
        deptName: null,
        reservationEntryStartTime: null,
        reservationEntryEndTime: null,
        reservationVisitingTime: null,
        curUserName: null,
        visitorId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.personId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      listInfo({
        pageNum: 1,
        pageSize: 1,
        personId: row.personId,
      }).then((response) => {
        this.form = response.data[0];
        this.open = true;
        this.title = "修改人员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.personType = "staff";
          if (this.form.personId != null) {
            updateInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 解绑卡按钮操作 */
    handleUnbindCard(row) {
      this.$modal
        .confirm("是否确认解绑该人员的卡？")
        .then(function () {
          return unbindCard({
            personId: row.personId,
            personType: row.personType,
          });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("解绑卡成功");
        })
        .catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除该数据项？")
        .then(function () {
          return delInfo({
            personIds: [row.personId],
            personType: row.personType,
          });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped lang="scss">
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 50px;
  height: 50px;
  background-color: #f5f7fa;
  color: #909399;
  font-size: 20px;
}
</style>