import { post } from "@/common/http/request";
import { $t } from "@/common/languages";
import { ElMessage, ElMessageBox } from "element-plus";
import BasePageableTable from "@/components/base/base-pageable-table/BasePageableTable.vue";
import ExpandablePageableTable from "@/components/base/expandable-pageable-table/ExpandablePageableTable.vue";
import type TreeSidePageableTable from "@/components/base/treeside-pageable-table/TreeSidePageableTable.vue";

export const usePage = (
  tableRef: Ref<
    | InstanceType<typeof BasePageableTable>
    | InstanceType<typeof ExpandablePageableTable>
    | InstanceType<typeof TreeSidePageableTable>
    | null
  >
) => {
  const postForm = (url: string, form: any, visible: Ref<boolean>, type: string) => {
    post(url, form)
      .then(res => {
        if (res.code === 0) {
          ElMessage({
            type: "success",
            message: $t("_message.common." + type + "_success"),
          });
          visible.value = false;
          tableRef.value?.reload();
        } else {
          ElMessage({
            type: "error",
            message: $t("_message.common." + type + "_failure") + ": " + res.msg,
          });
        }
      })
      .catch(error => {
        ElMessage({
          type: "error",
          message: $t("_message.common." + type + "_failure") + ": " + error,
        });
      });
  };

  const useAdd = (url: string, form: any, visible: Ref<boolean>) => {
    postForm(url, form, visible, "add");
  };

  const useEdit = (url: string, form: any, visible: Ref<boolean>) => {
    postForm(url, form, visible, "edit");
  };

  const postId = (
    url: string,
    id: number,
    confirmMessage: string,
    successMessage: string,
    failMessage: string,
    cancelMessage: string
  ) => {
    ElMessageBox.confirm($t(confirmMessage), $t("_level.warning"), {
      confirmButtonText: $t("_button.confirm"),
      cancelButtonText: $t("_button.cancel"),
      type: "warning",
    })
      .then(() => {
        post(url, { id: id })
          .then(res => {
            if (res.code === 0) {
              ElMessage({
                type: "success",
                message: successMessage,
              });
              tableRef.value?.reload();
            } else {
              ElMessage({
                type: "error",
                message: failMessage + ": " + res.msg,
              });
            }
          })
          .catch(error => {
            ElMessage({
              type: "error",
              message: failMessage + ": " + error,
            });
          });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: cancelMessage,
        });
      });
  };

  const useRemove = (url: string, id: number, i18nMessage?: string) => {
    postId(
      url,
      id,
      $t(i18nMessage ?? "_message.common.delete_confirm"),
      $t("_message.common.delete_success"),
      $t("_message.common.delete_failure"),
      $t("_message.common.delete_cannel")
    );
  };
  const useApprove = (url: string, id: number, i18nMessage?: string) => {
    postId(
      url,
      id,
      $t(i18nMessage ?? "_message.common.approve_confirm"),
      $t("_message.common.approve_success"),
      $t("_message.common.approve_fail"),
      $t("_message.common.approve_cannel")
    );
  };
  const useReject = (url: string, id: number, i18nMessage?: string) => {
    postId(
      url,
      id,
      $t(i18nMessage ?? "_message.common.reject_confirm"),
      $t("_message.common.reject_success"),
      $t("_message.common.reject_fail"),
      $t("_message.common.reject_cannel")
    );
  };

  const useGeneralPageRef = () => {
    /**
     * dialog 标题
     */
    const title = ref("");

    /**
     * 是否展示，用于新增编辑框
     */
    const visible = ref(false);

    /**
     * 新增是 false
     * 编辑是 true
     */
    const formType = ref(false);

    /**
     * form 的数据
     */
    const form = ref();

    /**
     * 是否展示，用于明细展示框
     */
    const itemVisible = ref(false);

    /**
     * 明细父项 ID
     */
    const itemParentId = ref();

    return { title, visible, formType, form, itemVisible, itemParentId };
  };

  return {
    useAdd,
    useEdit,
    useRemove,
    useGeneralPageRef,
    useApprove,
    useReject,
  };
};
