import { computed } from "vue";
import { useAuthStore } from "@/stores/modules/auth";
import { useUserStore } from "@/stores/modules/user";
import { Edit, Plus, Menu } from "@element-plus/icons-vue";
import { useRoute, useRouter } from "vue-router";
import { detailAction } from "@/components/LjDetail/interface";
import { isArray, isString } from "@/utils/is";

interface BusinessButtonProps extends detailAction {
  /**
   * @argument 继承：ElButton的type
   */
  type?: any;
  /**
   * @argument 路由地址
   */
  path?: string;
}
/**
 * @description 页面按钮
 * @param {Object} t i18n
 * */
export const useAuthButtons = (t?: any) => {
  // const route = useRoute();
  const authStore = useAuthStore();
  const authPower: (number | string)[] = authStore.authButtonListGet || [];
  const authOption = authStore.authOptionListGet || [];
  // console.log("route.name :>> ", route.name);

  // const BUTTONS = computed(() => {
  //   let currentPageAuthButton: { [key: string]: boolean } = {};
  //   authButtons.forEach(item => (currentPageAuthButton[item] = true));
  //   return currentPageAuthButton;
  // });

  /**
   * 查询权限值
   * @param {number | string} num 权限值 | super
   * @returns {boolean} 是否存在
   */
  const CheckPower = (data: number | string) => {
    if (isString(data)) {
      const { userInfo } = useUserStore();
      return userInfo.usercode?.toLowerCase() === "super";
    }
    return authPower.includes(data);
  };

  /**
   * 查询系统选项
   * @param {string} val 系统选项
   * @returns {string}
   */
  const CheckOption = (val: string | number) => {
    let _option = authOption.find((item: any) => Number(item.optionid) === Number(val));
    return _option?.optionvalue;
  };

  /**
   * @description 业务按钮：新建
   */
  const buttonNew = (data: BusinessButtonProps): detailAction => {
    const router = useRouter();
    return {
      ...data,
      label: data.label ?? t("common.add"),
      icon: data.icon ?? Plus,
      // type: data.type ?? "success",
      // type: undefined,
      // plain: true,
      disabledTextCallBack: (item: any) => {
        return data.disabledTextCallBack ? data.disabledTextCallBack(item) : "";
      },
      clickFunc: (item: any) => {
        console.log("item :>> ", item);
        if (data.clickFunc) {
          data.clickFunc(item);
        } else {
          router.push({
            path: data.path ?? "/"
          });
        }
      }
    };
  };

  /**
   * @description 业务按钮：初审
   */
  const buttonDefault = (data: BusinessButtonProps): detailAction => {
    return {
      ...data,
      icon: data.icon ?? undefined
      // type: data.type ?? "",
      // type: undefined
      // plain: true
    };
  };

  /**
   * @description 过滤无权限，按钮组
   * @param action 筛选合法操作
   * @returns array
   */
  const funcFilterPower = (action: any) => {
    console.log("funcFilterPower _action :>> ", action);
    if (!action) return [];
    let _action = action.map((item: any) => {
      if (!Object.keys(item).includes("power")) {
        return item;
      }
      if (isArray(item)) {
        return item.filter((i: any) => {
          return CheckPower(i.power);
        });
      }
      return CheckPower(item.power) ? item : {};
    });

    _action = _action.filter((item: any) => {
      if (isArray(item)) {
        return item.length;
      }
      return Object.keys(item).length;
    });

    return _action;
  };

  /**
   * @description 过滤无权限，单据功能，
   * @param action 筛选合法操作
   * @returns array
   */
  const funcRightActionPower = (action: any) => {
    let rst = action;
    if (isArray(rst)) {
      rst = rst.map((itm: any) => {
        console.log(itm);
        if (itm?.groupAction && itm.groupAction.length) {
          itm.groupAction = funcRightActionPower(itm.groupAction);
          return itm;
        } else {
          return funcRightActionPower(itm);
        }
      });
      rst = rst.filter((item: any) => {
        if (isArray(item)) {
          return item.length;
        }
        return Object.keys(item).length;
      });
    } else {
      if (rst?.groupAction && rst.groupAction.length) {
        rst = funcRightActionPower(rst.groupAction);
      } else if (Object.keys(rst).includes("power")) {
        rst = CheckPower(rst.power) ? rst : {};
      }
    }
    return rst;
  };

  return {
    CheckPower,
    CheckOption,
    buttonNew,
    buttonDefault,
    funcFilterPower,
    funcRightActionPower
  };
};
