import { client } from "../../../utils/api"
import { feedback, uploadavatar } from "../../../utils/request"

interface FeedbackType {
  key: number
  label: string
  icon: string
  active: boolean
}

interface submits {
  categoryes: number[],
  content: string,
  images: string[],
  phone: string
}

Page({
  data: {
    description: '',
    contact: '',
    types: [
      { key: 1, label: '功能问题', icon: '/static/images/my/debug.png', active: true },
      { key: 2, label: '体验优化', active: false, icon: '/static/images/my/star.png' },
      { key: 3, label: '内容建议', active: false, icon: '/static/images/my/msg.png' },
      { key: 4, label: '其他问题', active: false, icon: '/static/images/my/more.png' },
    ] as FeedbackType[],
    datas: {} as submits
  },
  onLoad() {
    this.setData({ datas: { categoryes: [], content: '', images: [], phone: '' } })
  },
  handleBack() {
    wx.navigateBack({
      fail: () => wx.switchTab({ url: '/pages/my/my' }),
    })
  },
  handleRecord() {
    wx.showToast({ title: '反馈记录', icon: 'none' })
  },
  handleTypeTap(e: WechatMiniprogram.TouchEvent) {
    const key = e.currentTarget.dataset.key

    this.setData({
      types: this.data.types.map((item) => ({
        ...item,
        active: item.key === key ? !item.active : item.active,
      })),
    })
  },
  handleDescriptionInput(e: any) {
    this.setData({ description: e.detail.value })
  },
  handleContactInput(e: any) {
    this.setData({ contact: e.detail.value })
  },
  handleUpload() {
    let that = this;
    wx.showLoading({ mask: true, title: '请稍后...' })
    wx.chooseMedia({
      count: 3 - this.data.datas.images.length,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        client.upload(res.tempFiles[0].tempFilePath, {}).then((res: any) => {
          wx.hideLoading();
          if (res.code == 0) {
            let images = that.data.datas;
            images.images.push(res.data.url);
            that.setData({ datas: images })
          }
        })
      },
      fail(err) {
        wx.hideLoading();
        console.log(err);
      }
    })
  },
  handleSubmit() {
    let that = this;
    if (!this.data.description.trim()) {
      wx.showToast({ title: '请填写问题描述', icon: 'none' })
      return
    }

    let datas = this.data.datas;
    let types: FeedbackType[] = [];
    for (let q = 0; q < this.data.types.length; q++) {
      let item = that.data.types[q];
      if (item.active) {
        datas.categoryes.push(item.key)
      }
      item.active = false;
      types.push(item);
    }

    datas.content = this.data.description;
    datas.phone = this.data.contact;

    feedback(JSON.stringify(datas)).then((res: any) => {
      if (!res.code) {
        this.setData({ datas: { categoryes: [], content: '', images: [], phone: '' }, contact: '', description: '', types: types })

        wx.showModal({ title: '温馨提示', content: '提交成功', showCancel: false });
      } else {
        wx.showModal({ title: '温馨提示', content: '提交失败, 请稍后重试', showCancel: false });
      }
    })
  },
})
