import { defineStore } from "pinia";
import { ref } from "vue";

/**
 * 全局"奖励结果"弹窗状态（AdminLayout 承载组件）
 * - 抽奖 / 签到 / 提交工单 三个场景成功后统一通过此 store 打开
 * - 用户点击"查看我的资产"时,触发 requestViewAccount 事件,由父级打开 AssetsDialog
 */
export const useRewardResultStore = defineStore("rewardResult", () => {
  const visible = ref(false);

  const title = ref("恭喜抽中");
  const icon = ref("");
  const points = ref(0);
  const batchGoal = ref(500);
  const batchCurrent = ref(0);
  const rewardName = ref("康小虎 AI 吉祥物");

  // 触发"查看我的资产"事件,AdminLayout 监听后打开 AssetsDialog
  const onViewAccount = ref(null);

  const open = (opts = {}) => {
    title.value = opts.title || "恭喜抽中";
    icon.value = opts.icon || "";
    points.value = Number(opts.points) || 0;
    batchGoal.value = Number(opts.batchGoal) || 500;
    batchCurrent.value = Number(opts.batchCurrent) || 0;
    rewardName.value = opts.rewardName || "康小虎 AI 吉祥物";
    visible.value = true;
  };

  const close = () => {
    visible.value = false;
  };

  // 关闭动画结束后清理数据
  const clear = () => {
    title.value = "恭喜抽中";
    icon.value = "";
    points.value = 0;
    batchGoal.value = 500;
    batchCurrent.value = 0;
    rewardName.value = "康小虎 AI 吉祥物";
  };

  const requestViewAccount = () => {
    if (typeof onViewAccount.value === "function") {
      onViewAccount.value();
    }
  };

  return {
    visible,
    title,
    icon,
    points,
    batchGoal,
    batchCurrent,
    rewardName,
    onViewAccount,
    open,
    close,
    clear,
    requestViewAccount,
  };
});
