import { createStore } from "vuex";
import { getMeetingDetailById, getMeetingSetting } from "@/api/meeting";

const store = createStore({
  state: {
    joinUser: localStorage.getItem("JOIN_USER")
      ? JSON.parse(localStorage.getItem("JOIN_USER"))
      : {},
    meeting: {},
    meetingSetting: {},
    messagesList: localStorage.getItem("MESSAGES")
      ? JSON.parse(localStorage.getItem("MESSAGES"))
      : [],
    password: "", //参会密码
    liked: localStorage.getItem("LIKED")
      ? localStorage.getItem("LIKED")
      : false,
    joinType: localStorage.getItem("JOINTYPE")
      ? JSON.parse(localStorage.getItem("JOINTYPE"))
      : "",
    joinRole: localStorage.getItem("JOINROLE")
      ? localStorage.getItem("JOINROLE")
      : "",
  },
  mutations: {
    // 设置参会人员信息
    setJoinUser(state, data) {
      state.joinUser = data;
      localStorage.setItem("JOIN_USER", JSON.stringify(data));
    },
    // 设置会议信息
    setMeetingInfo(state, data) {
      state.meeting = data;
    },
    // 是否点赞
    setLiked(state, data) {
      state.liked = data;
      console.log(data);
      localStorage.setItem("LIKED", data);
    },
    // 设置参会密码
    setPassword(state, data) {
      state.password = data;
      // localStorage.setItem("PASSWORD", JSON.stringify(data));
    },
    setJoinType(state, data) {
      state.joinType = data;
      localStorage.setItem("JOINTYPE", JSON.stringify(data));
    },
    setMeetingSetting(state, data) {
      state.meetingSetting = data;
    },
    setMessagesList(state, data) {
      state.messagesList = data;
      localStorage.setItem("MESSAGES", JSON.stringify(data));
    },
    setJoinRole(state, data) {
      state.joinRole = data;
      localStorage.setItem("JOINROLE", data);
    },
  },
  actions: {
    // 请求会议信息
    async getMeetingInfo(context, meetingId) {
      const { data } = await getMeetingDetailById(meetingId);
      context.commit("setMeetingInfo", data);
      // context.commit("setJoinType", data.joinType);
    },
    async getMeetingSettings(context) {
      const { data } = await getMeetingSetting();
      console.log(data);
      context.commit("setMeetingSetting", data);
    },
  },
  getters: {
    nickname: (state) => state.joinUser.nickname, // 姓名
    username: (state) => state.joinUser.username, // 姓名
    phone: (state) => state.joinUser.phone,
    email: (state) => state.joinUser.email, //
    token: (state) => state.joinUser.token, //
    icCard: (state) => state.joinUser.icCard, //
    meetingPic: (state) => state.meeting.meetingPic, // 会议封面
    templateBackgroundPic: (state) => state.meeting.templateBackgroundPic, // 会议封面
    meetingNote: (state) => state.meeting.meetingNote, // 会议信息
    meetingSchedule: (state) => state.meeting.meetingSchedule, // 会议日程
    expertInfo: (state) => state.meeting.expertInfo, // 专家介绍
    meetingNumber: (state) => state.meeting.meetingNumber, // 姓名
    startTime: (state) => state.meeting.startTime, // 姓名
    templateId: (state) => state.meeting.templateId,
    meetingId: (state) => state.meeting.id,
    textLabelList: (state) => state.meeting.textLabelList,
    questionnaireId: (state) => state.meeting.questionnaireId,
    bankId: (state) => state.meeting.bankId,
    defaultCover: (state) => state.meetingSetting.defaultCover,
    defaultBackground: (state) => state.meetingSetting.defaultBackground,
  },
});

export default store;
