<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="4">
            <el-form-item label="关键字搜索" prop="KEYWORDS">
              <el-input v-model="searchForm.KEYWORDS" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="作业类别" prop="JOB_TYPE">
              <el-select
                v-model="searchForm.JOB_TYPE"
                clearable
                placeholder="请选择状态"
              >
                <el-option
                  v-for="item in jobtypeList"
                  :key="item.DICTIONARIES_ID"
                  :label="item.NAME"
                  :value="item.DICTIONARIES_ID"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPaginationTransfer">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        :data="list"
        @get-data="fnGetData"
        v-model:pagination="pagination"
        row-key="UNITS_ID"
      >
        <el-table-column reserve-selection type="selection" width="55" />
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="CREATOR_CORP_NAME" label="添加单位" />
        <el-table-column prop="CREATOR_NAME" label="添加人" width="200" />
        <el-table-column prop="UNITS_NAME" label="单位名称" width="150" />
        <el-table-column prop="JOBTYPE" label="作业类别" width="100" />
        <el-table-column prop="OPERATIONITEM" label="操作项目" />
        <el-table-column prop="NAME" label="姓名" />
        <el-table-column prop="SEX" label="性别" />
        <el-table-column prop="CERTIFICATE_NUM" label="作业证书编号" />
        <el-table-column prop="REVIEWTIME" label="复审时间" />
        <el-table-column label="操作" align="center" width="240">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="resetPwd(row.PERSONNELMANAGEMENT_ID, row.NAME)"
            >
              重置密码
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="fnView(row.PERSONNELMANAGEMENT_ID)"
            >
              查看
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="fnEdit(row.PERSONNELMANAGEMENT_ID)"
            >
              编辑
            </el-button>
            <el-button
              type="primary"
              text
              link
              @click="handleDelete(row.PERSONNELMANAGEMENT_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button type="primary" @click="fnAdd(row)"> 新增 </el-button>
          <el-button type="danger" @click="batchDel()"> 删除 </el-button>
        </template>
      </layout-table>
    </layout-card>
    <edit
      v-model:visible="data.editDialog.visible"
      v-model:form="data.editDialog.form"
      :type="data.editDialog.type"
      :title="data.editDialog.title"
      @get-data="fnResetPaginationTransfer"
    />
  </div>
</template>

<script setup>
import { layoutFnGetPersonnelManagementJobType } from "@/assets/js/data_dictionary";
import useListData from "@/assets/js/useListData";
import { serialNumber } from "@/assets/js/utils";
import {
  getPersonnelmanagementList,
  setPersonnelmanagementResetPwd,
  setPersonnelmanagementGoEdit,
  setPersonnelmanagementDelete,
  setPersonnelmanagementDeleteAll,
} from "@/request/keyprojects.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { reactive } from "vue";
import Edit from "./components/edit.vue";

const { list, pagination, searchForm, fnGetData, fnResetPagination, tableRef } =
  useListData(getPersonnelmanagementList);
const jobtypeList = await layoutFnGetPersonnelManagementJobType();
const data = reactive({
  type: "",
  title: "",
  editDialog: {
    visible: false,
    form: {},
  },
});

const resetPwd = async (PERSONNELMANAGEMENT_ID, NAME) => {
  await ElMessageBox.confirm(
    "是否将[" + NAME + "]的密码重置为 Aa@123456789 吗?",
    {
      type: "warning",
    }
  );
  await setPersonnelmanagementResetPwd({ PERSONNELMANAGEMENT_ID });
  ElMessage.success("重置成功");
};

const handleDelete = async (PERSONNELMANAGEMENT_ID) => {
  await ElMessageBox.confirm("确定要删除吗？", {
    type: "warning",
  });
  await setPersonnelmanagementDelete({ PERSONNELMANAGEMENT_ID });
  fnGetData();
};

const batchDel = async () => {
  const selectionData = tableRef.value.getSelectionRows();
  if (selectionData.length === 0) {
    ElMessage.warning("请选中要删除的项");
    return;
  }
  await ElMessageBox.confirm("确定要删除吗？", {
    type: "warning",
  });
  const ids = selectionData
    .map((item) => {
      return item.PERSONNELMANAGEMENT_ID;
    })
    .join(",");
  await setPersonnelmanagementDeleteAll({ DATA_IDS: ids });
  fnGetData();
};

const fnGoEdit = async (id) => {
  const resData = await setPersonnelmanagementGoEdit({
    PERSONNELMANAGEMENT_ID: id,
  });
  data.editDialog.form = resData.pd;
};

const fnResetPaginationTransfer = () => {
  fnGetData(searchForm);
};

const fnAdd = () => {
  data.editDialog.form = {};
  data.editDialog.visible = true;
  data.editDialog.type = "add";
  data.editDialog.title = "新增";
};

const fnEdit = async (id) => {
  data.editDialog.form = {};
  data.editDialog.visible = true;
  data.editDialog.type = "edit";
  data.editDialog.title = "编辑";
  fnGoEdit(id);
};

const fnView = async (id) => {
  data.editDialog.form = {};
  data.editDialog.visible = true;
  data.editDialog.type = "view";
  data.editDialog.title = "查看";
  fnGoEdit(id);
};
</script>

<style scoped></style>
