import { useGiftStore } from "@/stores/modules/gift";
import { useUserStore } from "@/stores/modules/user";
import { computed } from "vue";
import { fomartDate } from "@/utils";

export default function () {
  const userStore = useUserStore();
  const giftStore = useGiftStore();
  // 用户是否三合一任务
  const oneOfThreePayIds = computed(() => userStore.userInfo.oneOfThreePayIds);

  // 当前三合一任务
  const currentOneOfThree = computed(() => {
    if (
      oneOfThreePayIds.value &&
      oneOfThreePayIds.value.length &&
      Object.keys(giftStore.threeInOneInfo).length !== 0
    ) {
      return giftStore.threeInOneInfo.rows.find((item: any) => {
        if (oneOfThreePayIds.value.includes(item._id)) {
          return item;
        }
      });
    }
  });

  // 是否显示三合一
  const hasShowOneOfThree = computed(() => {
    if (!userStore.userInfo.uid) {
      return false;
    }
    if (!oneOfThreePayIds.value) {
      return true;
    } else if (currentOneOfThree.value) {
      // 购买天数
      const buy = Number(
        fomartDate(giftStore.threeInOneInfo.oneOfThreeAt, "DD")
      );
      const date = Number(fomartDate(new Date(), "DD"));
      const diff = date - buy;
      if (diff < 0) return false;
      if (diff >= currentOneOfThree.value.keepDay) return false;
      if (oneOfThreePayIds.value.length) return true;
    }
    return false;
  });
  return {
    hasShowOneOfThree,
  };
}
