import request from '@/utils/request';
import { useCounterStore } from '@/stores/counter';

class AnswerApi {
  /* 获取问卷 */
  static getQuetions(params) {
    return request({
      method: 'GET',
      url: `/answer/surveys/${params.id}`,
      params: { ...params.data, time: Date.now() },
      headers: {
        'Cache-Control': 'no-cache',
        'answer-session-id': AnswerApi.getLocalStorageWithExpiration('answer-session-id')
      }
    });
  }
  /* 获取问卷详情 */
  static getQuetionDetail(params) {
    return request({
      method: 'GET',
      url: `/console/surveys/${params.id}/detail`,
      params: params.data,
      headers: {
        // store.state.answer.answerSessionId,
        'answer-session-id': AnswerApi.getLocalStorageWithExpiration('answer-session-id')
      }
    });
  }
  /* 答题 */
  static answer(params) {
    return request({
      method: 'POST',
      url: `/answer/surveys/${params.id}`,
      data: params.data,
      headers: {
        'answer-session-id': AnswerApi.getLocalStorageWithExpiration('answer-session-id')
      }
    });
  }
  /* 答题 */
  static surveyPrevious(params) {
    return request({
      method: 'POST',
      url: `/answer/surveys/${params.id}/prev`,
      data: params.data,
      headers: {
        'answer-session-id': AnswerApi.getLocalStorageWithExpiration('answer-session-id')
      }
    });
  }
  /* 获取验证码 */
  static getCode(params) {
    return request({
      method: 'POST',
      url: `/system/sms_codes`,
      data: params.data,
      headers: {
        // store.state.answer.answerSessionId,
        'answer-session-id': AnswerApi.getLocalStorageWithExpiration('answer-session-id')
      }
    });
  }
  /* 问卷密码 */
  static password(params) {
    return request({
      method: 'PATCH',
      url: `/answer/surveys/${params.id}/password`,
      data: params.data,
      headers: {
        // store.state.answer.answerSessionId,
        'answer-session-id': AnswerApi.getLocalStorageWithExpiration('answer-session-id')
      }
    });
  }
  /* word导出 */
  static download(sn, params = '') {
    return request({
      method: 'get',
      url: `/console/survey_word_export/${sn}?${params}`,
      headers: {
        // store.state.answer.answerSessionId,
        'answer-session-id': AnswerApi.getLocalStorageWithExpiration('answer-session-id')
      }
    });
  }
  // 获取localStorage数据，并检查过期时间
  static getLocalStorageWithExpiration(key) {
    const item = JSON.parse(localStorage.getItem(key));

    if (!item) {
      return useCounterStore?.state?.answer?.answerSessionId || '';
    }

    if (!item || new Date().getTime() > item.expirationDate) {
      // 数据过期，删除数据
      localStorage.removeItem(key);
      return null;
    }
    return item.value;
  }
}

class BrowsingRecordApi {
  /* 用户行为记录 */
  static browsingRecordSurveys(params) {
    return request({
      method: 'post',
      url: `/console/browsingRecord/surveys/${params.sn}/add`,
      data: params.data
    });
  }
  /* 获取3D资源 */
  static getSurveysScene(params) {
    return request({
      method: 'get',
      url: `/answer/surveys/${params.sn}/scene?question_index=${params.question_index}`
    });
  }
}

export { AnswerApi, BrowsingRecordApi };
