<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="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 label="工号" prop="jobNumber">
        <el-input
          v-model="queryParams.jobNumber"
          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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="工号" align="center" prop="jobNumber" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="员工类别" align="center" prop="staffTypeName" />
      <el-table-column label="员工状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.jobStatus === 'Y'" class="status-active"
            >在职</span
          >
          <span v-else-if="scope.row.jobStatus === 'N'" class="status-inactive"
            >离职</span
          >
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="定位卡号" align="center" prop="cardId" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="180"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-unlock"
            @click="handleUnbindCard(scope.row)"
            >解绑卡</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改人员信息对话框 -->
    <!-- 添加或修改人员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        :disabled="dialogType === 'view'"
      >
        <!-- 使用el-row和el-col实现一行两列布局 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名" prop="realName">
              <el-input v-model="form.realName" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="定位卡号" prop="cardId">
              <el-select
                v-model="form.cardId"
                placeholder="请选择定位卡号"
                clearable
              >
                <el-option
                  v-for="item in cardIdOptions"
                  :key="item.cardId"
                  :label="item.cardId"
                  :value="item.cardId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工号" prop="jobNumber">
              <el-input v-model="form.jobNumber" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="form.sex">
                <el-radio label="男">男</el-radio>
                <el-radio label="女">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="籍贯" prop="nativePlace">
              <el-input v-model="form.nativePlace" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="健康状态" prop="healthStatus">
              <el-input v-model="form.healthStatus" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birth">
              <el-date-picker
                clearable
                v-model="form.birth"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder=""
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="移动电话" prop="phone">
              <el-input v-model="form.phone" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idNumber">
              <el-input v-model="form.idNumber" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工类别" prop="personType">
              <el-select v-model="form.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-col>
          <el-col :span="12">
            <el-form-item label="入职日期" prop="hireDate">
              <el-date-picker
                clearable
                v-model="form.hireDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择入职日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工龄" prop="seniority">
              <el-input v-model="form.seniority" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职称" prop="professionalTitle">
              <el-input v-model="form.professionalTitle" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职称证书编号" prop="professionalTitleNumber">
              <el-input v-model="form.professionalTitleNumber" placeholder="" />
            </el-form-item>
          </el-col>
          <!-- 离职日期 - 仅在离职操作时显示 -->
          <el-col v-if="dialogType === 'dimission'" :span="12">
            <el-form-item label="离职日期" prop="dimissionDate" required>
              <el-date-picker
                clearable
                v-model="form.dimissionDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择离职日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!-- 返岗日期 - 仅在返岗操作时显示 -->
          <el-col v-if="dialogType === 'return'" :span="12">
            <el-form-item label="返岗日期" prop="returnDate" required>
              <el-date-picker
                clearable
                v-model="form.returnDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择返岗日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 仅在返岗操作时显示工号和定位卡号 -->
        <el-row v-if="dialogType === 'return'">
          <el-col :span="12">
            <el-form-item label="工号" prop="jobNumber" required>
              <el-input v-model="form.jobNumber" placeholder="请输入工号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="定位卡号" prop="cardId" required>
              <el-select
                v-model="form.cardId"
                placeholder="请选择定位卡号"
                clearable
              >
                <el-option
                  v-for="item in cardIdOptions"
                  :key="item.cardId"
                  :label="item.cardId"
                  :value="item.cardId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row v-if="dialogType !== 'dimission' && dialogType !== 'return'">
          <el-col :span="24">
            <el-form-item label="人员照片" prop="personPhoto">
              <el-upload
                class="avatar-uploader"
                action=""
                :auto-upload="false"
                :on-change="handlePhotoUpload"
                :show-file-list="false"
                accept="image/*"
              >
                <img
                  v-if="form.personPhoto"
                  :src="form.personPhoto"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
              <div class="upload-tip">点击上传照片</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="dialogType !== 'dimission' && dialogType !== 'return'">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-if="dialogType !== 'view'"
          type="primary"
          @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  delInfo,
  addInfo,
  updateInfo,
  unbindCard,
  getCardList, // 引入获取定位卡号列表的接口
} from "@/api/jinrun/yuangong";
import { getDict } from "@/api/jinrun/common";

export default {
  name: "Info",
  data() {
    return {
      dialogType: "",
      // 人员类型下拉列表数据
      personTypeOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        personType: "staff",
        realName: null,
        cardId: null,
        jobNumber: null,
      },
      // 表单参数
      form: {
        personType: "staff",
        sex: "男", // 默认选择男
      },
      // 表单校验
      rules: {
        personType: [
          { required: true, message: "人员类型不能为空", trigger: "change" },
        ],
        realName: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getPersonTypeOptions();
    // 获取定位卡号列表
    this.getCardIdList();
  },

  methods: {
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      listInfo({
        pageNum: 1,
        pageSize: 1,
        personId: row.personId,
      }).then((response) => {
        this.form = response.data[0];
        this.open = true;
        this.title = "查看人员信息";
        this.dialogType = "view";
      });
    },
    /** 获取人员类型下拉列表数据 */
    getPersonTypeOptions() {
      // 调用数据字典接口
      getDict({ dictType: "staff_type" })
        .then((response) => {
          this.personTypeOptions = response.data;
        })
        .catch((error) => {
          this.$modal.msgError("获取人员类型数据失败");
          console.error("获取人员类型数据失败:", error);
        });
    },
    /** 获取定位卡号列表数据 */
    getCardIdList() {
      getCardList()
        .then((response) => {
          this.cardIdOptions = response.data || [];
        })
        .catch((error) => {
          this.$modal.msgError("获取定位卡号列表失败");
          console.error("获取定位卡号列表失败:", error);
        });
    },
    /** 处理照片上传 */
    handlePhotoUpload(file) {
      // 创建FileReader来读取图片内容
      const reader = new FileReader();
      reader.onload = (e) => {
        // 将图片内容转为DataURL存储到form中
        this.form.personPhoto = e.target.result;
      };
      reader.readAsDataURL(file.raw);
    },
    /** 查询人员信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.data;
        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: "男",
        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 = "添加人员信息";
      this.dialogType = "add";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      listInfo({
        pageNum: 1,
        pageSize: 1,
        personId: row.personId,
      }).then((response) => {
        this.form = response.data[0];
        this.open = true;
        this.title = "修改人员信息";

        this.dialogType = "update";
      });
    },
    /** 提交按钮 */
    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(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/info/export",
        {
          ...this.queryParams,
        },
        `info_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
/* 照片上传样式 */
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
.upload-tip {
  margin-top: 10px;
  color: #909399;
  font-size: 12px;
}
</style>