// pages/questionnaire/questionnaire.js
const Http = require("../../utils/HttpBasics");
var config = require("../../config/config.js");
const util = require("../../utils/util.js");
var app = getApp();
const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarHeight,
    id: "",
    question: {},
    userDetails: "",
    thenDate: "",
  },
  //设置选中项
  setLsit(e) {
    if (this.data.userDetails != "") {
      return
    }
    
    let index1 = e.currentTarget.dataset.index1
    let index2 = e.currentTarget.dataset.index2
    let tempList = this.data.question
    if (tempList.topicList[index1].type == 1) {
      tempList.topicList[index1].answers.map(item => {
        item.falg = false

      })
      tempList.topicList[index1].answers[index2].falg = !tempList.topicList[index1].answers[index2].falg
    } else {
      tempList.topicList[index1].answers[index2].falg = !tempList.topicList[index1].answers[index2].falg
    }

    this.setData({
      question: tempList
    })
  },
  //提交
  save() {
    let falg = false
    let logList = []
    let tmepData = this.data.question.topicList

    tmepData.map(item => {
      let tmepObj = {}
      let tempArr = []
      tmepObj.topicId = item.id
      item.answers.map(res => {
        if (res.falg) {
          tempArr.push(res.id)
          item.complete = true
        }
      })
      tmepObj.answer = JSON.stringify(tempArr)
      logList.push(tmepObj)
    })
    tmepData.map(item => {
      if (item.complete == null) {
        falg = true

      }
    })
    if (falg) {
      tt.showToast({
        title: "您还有未答的题目！",
        icon: 'none',
        duration: 2000,
        // mask: false
      });
      return true
    }
    let param = {
      questionId: this.data.id,
      logList: logList
    }
    console.log(tmepData)
    console.log(param, "param")
    Http.post({
      url: config.api.submitQuestin,
      data: param
    }).then(res => {
      let str = ""
      if (this.data.question.rewardCredit) {
        str = `感谢您的参与,
          ${this.data.question.rewardCredit}积分已经到账啦`
      } else {
        str = `感谢您的参与!`
      }
      tt.showToast({
        title: str,
        icon: 'none',
        duration: 2000,
        success: () => {
        setTimeout(() => {
            tt.reLaunch({
              url: '/index/index'
            })
          }, 2000)

        }
        // mask: false
      });
    }).catch(err => {
      tt.showToast({
        title: err.errMsg,
        icon: 'none',
        duration: 2000,
        // mask: false
      });
    })
  },
  //是否授权手机号
  ifPhoneInfo() {
    let that = this;
    Http.get({
      url: config.api.checkPhoneStatus,
      data: {}
    }).then(res => {
      return
    }).catch(err => {
      app.globalData.skip = 'navigateBack'
      tt.navigateTo({
        url: `/pages/getPhone/getPhone?skipUrl=1`,
      })
    })
  },

  /* 判断是否授权*/
  userAuthorization() {
    Http.get({
      url: config.api.checkUserStatus,
      data: {
        token: app.globalData.token
      }
    }).then(res => {
      this.ifPhoneInfo()
    }).catch(err => {
      console.log(this.data.id,"this.data.id");

      console.log(`/pages/getuserinfo/getuserinfo?fromflag=wj&wjId=${this.data.id}`);

      tt.navigateTo({
        url: `/pages/getuserinfo/getuserinfo?fromflag=wj&wjId=${this.data.id}`,
      })
    })
  },

  //获取问卷详情
  getDetail() {
    let param = {
      id: this.data.id
    }
    Http.get({
      url: config.api.questionnaireDetail,
      data: param
    }).then(res => {
      
    
      res.data.question.startDate_Sing = util.formatTime(res.data.question.startDate, "yyyy-MM-dd hh:mm")
      res.data.question.endDate_Sing = util.formatTime(res.data.question.endDate, "yyyy-MM-dd hh:mm")
      console.log(res.data.question)
      if (res.data.userDetails) {
        let temp = res.data.question.topicList
        temp.map((item, index) => {
          
          // res.data.userDetails
          item.answers.map(item2 => {
            let tempArr = JSON.parse(res.data.userDetails.logList[index].answer)
            if (tempArr.indexOf(item2.id) != -1) {
              item2.falg = true
            }
          })
        })
        this.setData({
          userDetails: res.data.userDetails
        })
      }
      this.setData({
        question: res.data.question
      })
    }).catch(err => {
      tt.showToast({
        title: err.errMsg,
        icon: 'none',
        duration: 2000,
        // mask: false
      });
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    this.setData({
      id: options.id,
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.userAuthorization()
    this.setData({
      thenDate: new Date().getTime()
    })
    this.getDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */ 
  onShareAppMessage: function() {
    var that = this;
    var shareObj = {
      title: "问卷详情",
      path: `/pages/main/index?id=${that.data.id}&type=wj`,
      success: function (res) {
        if (res.errMsg == 'shareAppMessage:ok') { }
      },
      fail: function (error) {
        if (res.errMsg == 'shareAppMessage:fail cancel') { } else if (res.errMsg == 'shareAppMessage:fail') { }
      }
    };
    console.log(shareObj)
    // 返回shareObj
    return shareObj;
  }
})