// components/questionnaire/questionnaire.js
import $ from '../../utils/Tool';
import {
  queryQuestion,
  submitAnswer,
  notShowQuestion
} from "../../requests/api";
Component({
  /**
   * 组件的属性列表
   */
  options: {
    styleIsolation: 'shared',
  },
  properties: {

  },
  lifetimes: {
    attached: function() {

      this.queryDate();
      // 在组件实例进入页面节点树时执行
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    startValue:0,
    checked:[],
    tipsshow:false,
    dialogRadio:'2',
    answer:false,
    questionDate:[],
    firstDate:{},
    otherDate:[],
    submitFlag:false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    preventTouchMove(){

    },
    emitClose(value){
      this.triggerEvent('surveyClose',value?{good:true}:{good:false})
    },
    queryDate(){
      let data={
        "criteria":{
            "type":0
        }
     }
      queryQuestion(data).then(res=>{
        const [firstDate,...otherDate]= res.content;
        otherDate.forEach(v=>{
          v.checked=[];
          v.remark=false;
          v.remarkTxt="";
        })
        this.setData({
          firstDate:firstDate||{},
          otherDate:otherDate||[],
          questionDate:res.content||[]
        })
      })
    },
    inputChange(e){
      let {index} = e.currentTarget.dataset;
      let mText = 'otherDate['+ index +'].remarkTxt';
      let {value} = e.detail;
      this.setData({
        [mText]:value
      })

    },
    closePage(){
      this.setData({
        tipsshow:true
      })
    },
    starChange(event){
      let that =this;
      if(event.detail!==5){
        this.setData({
          startValue: event.detail,
          answer:true
        });
      }else{
        this.setData({
          startValue: event.detail,
          submitFlag:true
        },()=>{
          that.submitFeed();
        });


        // this.emitClose(true);
      }

    },
    selectChange(event){
      let {index} = event.currentTarget.dataset;
      let mText = 'otherDate['+ index +'].checked';
      let remark = 'otherDate['+ index +'].remark';
      let flag = event.detail.includes("12");
      let that =this;
      this.setData({
        [mText]: event.detail,
        [remark]:flag
      },()=>{
        if(flag){
          wx.createSelectorQuery().in(that).select('#qus_listbox').boundingClientRect(function(rect){
            that.setData({
              textScroll:rect.height
            })
          }).exec()   
       }
        let falg = that.data.otherDate.filter((item)=>{
          return !item.checked.length
        });
        !falg.length?that.setData({submitFlag:true}):that.setData({submitFlag:false});
      });
    },
    radioChange(event){
      this.setData({
        dialogRadio: event.detail,
      });
    },
    closeDialog(){
      this.setData({
        tipsshow:false
      })
    },
    async submitMsg(){
      if(this.data.dialogRadio==1){
        let {id} = $.store.get('userInfo');
        let tenantId = $.store.get('tenantId');
        let projectId = $.store.get('projectId');
        let data={
            "projectId":projectId,
            "tenantId":tenantId,
            "userId":id,
          "startTime":this.data.firstDate.startTime,
            "endTime":this.data.firstDate.endTime,
            "isDisplay": false,
            "seasonType":this.data.firstDate.seasonType
        }

        await notShowQuestion(data)
      }else{
        $.storage.set('nextRemind',true);
      }
      this.setData({
        tipsshow:false
      })
     
      this.data.dialogRadio==1?this.emitClose(true):this.emitClose();
    },
    submitFeed(){
      if(!this.data.submitFlag){
        return
      }
      let falgRemark=true;
      let {id,nickname,phone,userName}= $.store.get('userInfo');
      let startTime = this.data.firstDate.startTime;
      let endTime = this.data.firstDate.endTime;
      let projectId= $.store.get('projectId');
      let tenantId= $.store.get('tenantId');
      let dateTemplate={
          "projectId":projectId,//项目id
          "tenantId":tenantId,
          "seasonType":this.data.firstDate.seasonType||'Warm',//季节类型
          "userId":id,//用户id
          "nickName":nickname,//微信昵称
          "phone":phone,//手机号
          "userName":userName,//手机号
          "type":"0",//类型 0 小程序
          "startTime":startTime,//调查问卷开始时间
          "endTime":endTime,//调查问卷结束时间
      }
      let firstDate={...dateTemplate};
      let startIndex=5-this.data.startValue;
      firstDate.questionId=this.data.firstDate.questionId;
      firstDate.questionAnswerList=[{answerId: this.data.firstDate.questionAnswerList[startIndex].answerId}];
      let data=[firstDate];
      if(this.data.answer){
        this.data.otherDate.forEach(item=>{
          let otherdate={...dateTemplate};
          otherdate.questionId=item.questionId;
          otherdate.questionAnswerList=[];
          item.checked.forEach(v=>{
            let obj={};
            obj.answerId=v;
            if(item.remark&&v==12){
              item.remarkTxt?(obj.remark=item.remarkTxt):(falgRemark=false);
            }
            otherdate.questionAnswerList.push(obj);
          })
          data.push(otherdate);
        })
      }
      if(!falgRemark){
        wx.showToast({
          title:"其他选项不能为空",
          icon:"none",
          duration: 1000
        })
        return
      }
      submitAnswer(data).then(res=>{
        let that =this;
        this.setData({dialogRadio:1},()=>{
          that.submitMsg();
        })
      })
    }
  }
})
