import { ref, reactive, computed, toRefs, inject, nextTick } from "vue";
import { CommonDynamicSelect, CommonBill } from "@/api/modules/common";
import { ElMessageBox, ElMessage } from "element-plus";
import mittBus from "@/utils/mittBus";
import { MittEnum } from "@/enums/mittEnum";
import { useUserStore } from "@/stores/modules/user";
import { OaFlowState } from "../interface/index";

export const useOaFlow = (powerid: number) => {
  const state = reactive<OaFlowState>({
    oaFlowList: [],
    tabsValue: 0,
    itemData: []
  });

  const { userInfo } = useUserStore();
  const oaDocStatus = [
    {
      label: "草稿",
      value: "0",
      type: "info"
    },
    {
      label: "处理中",
      value: "1",
      type: "primary"
    },
    {
      label: "被否决",
      value: "2",
      type: "danger"
    },
    {
      label: "完成",
      value: "11",
      type: "success"
    },
    {
      label: "作废",
      value: "12",
      type: "danger"
    }
  ];

  //`请输入${isAgree ? "同意" : "否决"}的审批意见`
  const handleOpenBox = (isAgree: boolean) => {
    ElMessageBox.prompt(`请输入${isAgree ? "同意" : "否决"}的审批意见`, "提示", {
      confirmButtonText: isAgree ? "同意" : "否决",
      cancelButtonText: "取消"
    })
      .then(({ value }) => {
        isAgree ? handleAgreeDoc(value) : handleOpposeDoc(value);
      })
      .catch(() => {
        ElMessage.warning("已经取消审批流程操作!");
      });
  };

  /**
   * @description 流程同意
   */
  const handleAgreeDoc = (content: string) => {
    let _index: any = state.tabsValue ?? 0;
    let { docid } = state.oaFlowList[_index];
    CommonBill({
      uoName: "uo_oa_doc",
      functionName: "p_gree",
      SParms: {
        arg_docid: docid,
        arg_msg: "",
        arg_empcontent: content,
        arg_ifpemp: 1, //1-当前用户不是审批流程正在处理的用户，上级用户可以待处理，0-只能由审批流程正在处理的用户处理
        arg_ifcommit: true
      },
      ifcommit: true,
      returnStrList: []
    }).then(() => {
      ElMessage.success("同意审批成功!");
    });
  };

  /**
   * @description 流程否决
   */
  const handleOpposeDoc = (content: string) => {
    let _index: any = state.tabsValue ?? 0;
    let { docid } = state.oaFlowList[_index];
    CommonBill({
      uoName: "uo_oa_doc",
      functionName: "p_oppose",
      SParms: {
        arg_docid: docid,
        arg_msg: "",
        arg_empcontent: content,
        arg_ifpemp: 1, //1-当前用户不是审批流程正在处理的用户，上级用户可以待处理，0-只能由审批流程正在处理的用户处理
        arg_ifcommit: true
      },
      ifcommit: true,
      returnStrList: []
    }).then(() => {
      ElMessage.success("否决审批成功!");
    });
  };

  const getOaFlowList = async (scid: number, billid: number) => {
    if (!billid) return;
    // 流转状况e
    let { datatable } = await CommonDynamicSelect({
      dsname: "web_doc_view_bill",
      queryparams: {
        powerid: powerid,
        scid: scid,
        billid: billid
      }
    });
    console.log("getOaFlowList datatable :>> ", datatable);
    state.oaFlowList = datatable;

    mittBus.emit(MittEnum.OAFlowChange, datatable);
  };

  /**
   * @description 当前账号需要审批的流程
   */
  const currentOaflow = computed(() => {
    if (!state.itemData) return undefined;
    return state.itemData.find(
      (item: any) => Number(item.oarowid) === 1 && Number(item.empid) === userInfo.empid && Number(item.passed) === 0
    );
  });

  const isCurEmp = (data: any) => {
    return Number(data.oarowid) === 1 && Number(data.empid) === userInfo.empid && Number(data.passed) === 0;
  };

  return {
    ...toRefs(state),
    oaDocStatus,
    currentOaflow,
    isCurEmp,
    handleOpenBox,
    getOaFlowList
  };
};
