// pages/index/vote/detail/index.js
import HTTP from "../../../../../requestFn/Api"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: [],
    id: null,
    garden_id: null,
    indicatorDots: false, //小点
    indicatorColor: "white", //指示点颜色
    activeColor: "coral", //当前选中的指示点颜色
    autoplay: true, //是否自动轮播
    interval: 5000, //间隔时间
    duration: 3000, //滑动时间,
    topic_type: {
      1: "填空题",
      2: "单选题",
      3: "多选题",
      4: "评分题"
    },
    timestamp: 0,
    texts: ['非常不好', '不好', '一般', '很好', '非常好'],
  },

  onLoad(options) {
    const that = this;
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    const {
      garden_id
    } = app.globalData;
    that.setData({
      garden_id,
      timestamp
    })
    const eventChannel = that.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      const {
        id
      } = data.data[0]
      that.setData({
        detail: data.data,
        id
      })
    })
  },
  formSubmit(e) {
    const that = this;
    const {
      garden_id,
      id,
      detail,
    } = that.data;
    const {
      topics
    } = detail[0];
    const {
      value
    } = e.detail
    const answers = []
    console.log(value);
    for (let i = 0; i < topics.length; i++) {
      if (topics[i].must && [1, 2].includes(topics[i].topic_type) && value[topics[i].number] == "") {
        wx.showToast({
          title: `请输入问题${topics[i].number}的答案`,
          icon: "none"
        })
        return false
      } else if ([1].includes(topics[i].topic_type)) {
        answers.push({
          completion_answers: [value[topics[i].number]],
          number: topics[i].number,
          topic_type: topics[i].topic_type
        })
      } else if ([2].includes(topics[i].topic_type)) {
        const val = value[topics[i].number];
        answers.push({
          choice_answers: val?[val]:[],
          number: topics[i].number,
          topic_type: topics[i].topic_type
        })
      } else if (topics[i].must && [3].includes(topics[i].topic_type) && value[topics[i].number].length < 1) {
        wx.showToast({
          title: `请输入问题${topics[i].number}的答案`,
          icon: "none"
        })
        return false
      } else if ([3].includes(topics[i].topic_type)) {
        answers.push({
          choice_answers: value[topics[i].number],
          number: topics[i].number,
          topic_type: topics[i].topic_type
        })
      } else if (topics[i].must && [4].includes(topics[i].topic_type)&&topics[i].star_answers==0) {
        wx.showToast({
          title: `请输入问题${topics[i].number}的答案`,
          icon: "none"
        })
        break
      } else if ([4].includes(topics[i].topic_type)) {
        answers.push({
          star_answers: topics[i].star_answers,
          number: topics[i].number,
          topic_type: topics[i].topic_type
        })
      }
    }
    console.log(answers, id, garden_id);
    HTTP.VoteAnswer({
      answers,
      garden_id,
      id
    }).then(res => {
      if (res.code == 0) {
        wx.showToast({
          title: `提交成功`,
          icon: "success",
          success() {
            wx.navigateBack({
              delta: 1,
            })
          }
        })
      }
    })
  },
  onChange(e) {
    const { index } = e.currentTarget.dataset;
    const {
      value
    } = e.detail;
    this.setData({
      [`detail[0].topics[${index}].star_answers`]:value
    });
  },
})