const app = getApp<IAppOption>()

Page({
  data: {
    announcements: [] as any[],
    feedbackContent: ''
  },

  onLoad() {
    this.fetchAnnouncements();
  },

  fetchAnnouncements() {
    wx.request({
      url: `${app.globalData.baseUrl}/feedback/api/announcements/`,
      method: 'GET',
      success: (res: any) => {
        if (res.data.code === 200) {
          this.setData({ announcements: res.data.data });
        }
      }
    });
  },

  submitFeedback() {
    if (!this.data.feedbackContent.trim()) {
      wx.showToast({ title: '内容不能为空', icon: 'none' });
      return;
    }

    wx.showLoading({ title: '提交中...' });
    const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo');

    wx.request({
      url: `${app.globalData.baseUrl}/api/submit/`,
      method: 'POST',
      data: {
        user_id: userInfo ? userInfo.user_id : '',
        content: this.data.feedbackContent
      },
      success: (res: any) => {
        wx.hideLoading();
        if (res.data.code === 200) {
          wx.showToast({ title: '提交成功', icon: 'success' });
          this.setData({ feedbackContent: '' }); // 清空输入框
        } else {
          wx.showToast({ title: res.data.msg || '提交失败', icon: 'none' });
        }
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({ title: '网络异常', icon: 'none' });
      }
    });
  }
})