import { defineStore } from 'pinia';
import {
  getFeedbackList,
  getFeedbackDetail,
  processFeedback,
  assignFeedback,
  resolveFeedback,
  closeFeedback,
  rejectFeedback,
  confirmFeedback,
  getFeedbackStats,
  submitFeedback,
  FeedbackStatus
} from '@/api/feedback';
import type {
  FeedbackResponse,
  FeedbackDetail,
  FeedbackFilters
} from '@/api/feedback';
import { ElMessage } from 'element-plus';
import { useAuthStore } from '../stores/auth';

interface FeedbackState {
  feedbackList: FeedbackResponse[];
  total: number;
  loading: boolean;
  currentFeedbackDetail: FeedbackDetail | null;
  detailLoading: boolean;
  stats: {
    total: number;
    pending: number;
    confirmed: number;
    assigned: number;
    inProgress: number;
    resolved: number;
    closed: number;
    rejected: number;
  };
}

export const useFeedbackStore = defineStore('feedback', {
  state: (): FeedbackState => ({
    feedbackList: [],
    total: 0,
    loading: false,
    currentFeedbackDetail: null,
    detailLoading: false,
    stats: {
      total: 0,
      pending: 0,
      confirmed: 0,
      assigned: 0,
      inProgress: 0,
      resolved: 0,
      closed: 0,
      rejected: 0
    }
  }),

  actions: {
    async fetchFeedbackList(params: FeedbackFilters) {
      this.loading = true;
      const authStore = useAuthStore();
      const user = authStore.user;

      let apiParams: FeedbackFilters = { ...params };

      if (user && user.role === 'PUBLIC_SUPERVISOR') {
        apiParams.submitterId = user.id;
      }

      try {
        const response = await getFeedbackList(apiParams);
        this.feedbackList = response.content;
        this.total = response.totalElements;
      } catch (error) {
        console.error('获取反馈列表失败:', error);
        ElMessage.error('获取反馈列表失败，请检查网络或联系管理员');
        this.feedbackList = [];
        this.total = 0;
      } finally {
        this.loading = false;
      }
    },

    async fetchFeedbackDetail(id: number) {
      this.detailLoading = true;
      try {
        this.currentFeedbackDetail = await getFeedbackDetail(id);
      } catch (error) {
        console.error('获取反馈详情失败:', error);
        ElMessage.error('无法加载反馈详情');
        this.currentFeedbackDetail = null;
      } finally {
        this.detailLoading = false;
      }
    },

    async fetchFeedbackStats() {
      try {
        const statsData = await getFeedbackStats();
        this.stats = { ...this.stats, ...statsData };
      } catch (error) {
        console.error('获取反馈统计数据API错误:', error);
        ElMessage.error('获取反馈统计数据失败');
      }
    },

    async submitFeedback(formData: FormData) {
      try {
        await submitFeedback(formData);
        this.fetchFeedbackStats();
      } catch (error) {
        console.error('提交反馈失败:', error);
        throw error;
      }
    },

    // 可以在这里添加更多处理反馈的 actions, 例如:
    // async process(id: number, data: ProcessFeedbackRequest) { ... }
    // async assign(id: number, data: AssignFeedbackRequest) { ... }
  }
}); 
