import {
  deletePostList,
  getPostList,
  postPostList,
  putPostList
} from "@/api/system";
import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, ElMessageBox } from "element-plus";
import { reactive, ref, computed, onMounted } from "vue";

export function useUser() {
  const form: any = ref({
    postName: ""
  });
  const UpdateForm: any = ref({
    postCode: "", //岗位编码
    postName: "", //岗位名称
    postSort: "", //岗位排序
    remark: "" //岗位描述
  });
  const dialogType = ref(1);
  const dataList = ref([]);
  const loading = ref(false);
  const dialogUpdateVisible = ref(false);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      width: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "岗位ID",
      prop: "postId",
      minWidth: 130
    },
    {
      label: "岗位名称",
      prop: "postName",
      minWidth: 130
    },
    {
      label: "岗位描述",
      prop: "remark",
      minWidth: 130
    },
    {
      label: "操作",
      fixed: "right",
      width: 200,
      slot: "operation"
    }
  ];
  const buttonClass = computed(() => {
    return [
      "!h-[20px]",
      "reset-margin",
      "!text-gray-500",
      "dark:!text-white",
      "dark:hover:!text-primary"
    ];
  });

  async function UpdateFormSubmit() {
    if (dialogType.value == 1) {
      const { status, msg } = await postPostList(UpdateForm.value);
      if (status == 1) {
        ElMessage({
          message: "新增岗位成功",
          type: "success"
        });
        dialogUpdateVisible.value = false;
        onSearch();
      } else {
        ElMessageBox.alert(msg, "提示", {
          confirmButtonText: "关闭",
          type: "warning"
        });
      }
    } else {
      console.log(UpdateForm.value);
      const { status, msg } = await putPostList(UpdateForm.value);
      if (status == 1) {
        ElMessage({
          message: "岗位修改成功",
          type: "success"
        });
        dialogUpdateVisible.value = false;
        onSearch();
      } else {
        ElMessageBox.alert(msg, "提示", {
          confirmButtonText: "关闭",
          type: "warning"
        });
      }
    }
  }
  function handleDelete(row) {
    ElMessageBox.confirm(`是否删除名称为:${row.postName}的岗位? `, "提示", {
      confirmButtonText: "删除",
      cancelButtonText: "取消",
      type: "warning"
    }).then(async () => {
      const { status, msg } = await deletePostList(row.postId);
      if (status == 1) {
        ElMessage({
          message: "删除成功",
          type: "success"
        });
        onSearch();
      } else {
        ElMessageBox.alert(msg, "提示", {
          confirmButtonText: "关闭",
          type: "warning"
        });
      }
    });
  }
  function AddUser() {
    UpdateForm.value = {
      postCode: "", //岗位编码
      postName: "", //岗位名称
      postSort: "", //岗位排序
      remark: "" //岗位描述
    };
    dialogType.value = 1;
    dialogUpdateVisible.value = true;
  }
  function handleUpdate(row) {
    dialogType.value = 2;
    dialogUpdateVisible.value = true;
    UpdateForm.value = {
      postId: row.postId,
      postCode: row.postCode, //岗位编码
      postName: row.postName, //岗位名称
      postSort: row.postSort, //岗位排序
      remark: row.remark //岗位描述
    };
  }

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
    onSearch();
  }

  function handleCurrentChange(val: number) {
    if (typeof val !== "number") return;
    console.log(`current page: ${val}`);
    onSearch();
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
    onSearch();
  }

  // 搜索函数（请求表格数据列表）
  function submit(e: { keyCode: number }) {
    if (e.keyCode == 229) {
      return;
    } else {
      onSearch();
    }
  }
  async function onSearch(type = "") {
    if (type == "search" && !Object.values(form).some(item => !!item)) {
      return ElMessage({
        message: "请输入查询条件",
        type: "error"
      });
    }
    if (type == "all") {
      form.value.postName = "";
    }
    loading.value = true;
    const { data } = await getPostList({
      ...form.value,
      pageSize: pagination.pageSize,
      pageNum: pagination.currentPage
    });
    dataList.value = data.records;
    pagination.total = data.total;
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }
  onMounted(() => {
    onSearch();
  });

  return {
    loading,
    columns,
    dataList,
    pagination,
    buttonClass,
    dialogUpdateVisible,
    UpdateFormSubmit,
    submit,
    UpdateForm,
    dialogType,
    AddUser,
    onSearch,
    handleUpdate,
    handleDelete,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange,
    form
  };
}
