import { defineStore } from "pinia";
import { request } from "../api";

export const useSurveyStore = defineStore({
  id: "survey",
  state: () => {
    return {
      lists: [],
      details: [
        {
          answer: "",
          createTime: "",
          creatorId: "",
          creatorName: "",
          display: null,
          hasDiy: true,
          id: null,
          question: "",
          questionnaireId: null,
          type: null,
          updateTime: "",
        },
      ],
      pagination: {
        pageNum: 1,
        pageSize: 20,
        searchKey: "",
        type: "",
        total: 0,
        pages: 0,
        current: 0,
      },
    };
  },
  getters: {},
  actions: {
    async getLists(pageNum) {
      this.pagination.pageNum = pageNum || 1;
      const { data, status } = await request.post(
        "/show/questionnaire/pageList",
        {
          ...this.pagination,
        }
      );
      if (data.code === 0) {
        const { records, total, current, page } = data.data;
        this.lists = records;
        this.pagination.total = total;
        this.pagination.current = current;
        this.pagination.page = page;
      }
    },
    async getDetail(id) {
      const { data, status } = await request.get(
        `/show/questionnaire/detail/${id}`
      );
      if (data.code === 0) {
        this.details = data.data;
      }
    },
    async sendAnswer(answers) {
      const { data, status } = await request.post(
        "/show/questionnaire/saveAnswer",
        answers
      );
      console.log("data", data);
      if (data.code === 0) {
        return Promise.resolve(0);
      } else {
        return Promise.resolve(1);
      }
    },
  },
});
