import store from"../../store/index";import Event from"../../common/chat/eventTypes";import Question from"../../common/question/index";import LANGUAGE from"../../common/question/Language";Component({data:{popupTitle:LANGUAGE.TITLE,submitTitle:LANGUAGE.SUBMIT_BTN,submitSuccess:LANGUAGE.SUBMIT_SUCCESS,cutOff:LANGUAGE.CUT_OFF,showQuestion:!1,questionTitle:"",subjectList:[],showModal:!1,modalData:{imgSrc:"",modalText:""}},properties:{zIndex:{type:Number,default:2e3}},lifetimes:{created(){this.chatEvent=this.getChatEvent(),this.chat=null,this.questionnaireRetryTime=3},attached(){this.unsub=store.get({"main.chat":t=>{this.chat=t,t&&this.bindEvent(this.chatEvent)}})}},methods:{getChatEvent(){const t=this;return{[Event.START_QUESTIONNAIRE](e,s){t.clearTimeoutClock();const i=Question.parseQuestin(s.content);t.setData({questionnaireId:i.questionnaireId,questionTitle:i.questionTitle,subjectList:i.subjectList,showQuestion:!0})},[Event.STOP_QUESTIONNAIRE](e,s){t.clearTimeoutClock(),t.data.showQuestion&&(t.setData({showQuestion:!1,modalData:{imgSrc:"../../assets/images/interact-cut-off.png",modalText:t.data.cutOff},showModal:!0}),t.selectComponent("#answerModal").show())}}},bindEvent(t){const e=this.chat;e&&Object.keys(t).forEach((s=>{e.on(s,t[s])}))},closeQuestion(){this.setData({showQuestion:!1}),this.triggerEvent("showPptPaint")},selectRadio(t){const{subjectIndex:e,optionIndex:s}=t.currentTarget.dataset,i=this.data.subjectList,n=i[e].subjectOptions.map((t=>({...t,isSelect:!1})));n[s].isSelect=!0,i[e].subjectOptions=n,this.setData({subjectList:i})},selectCheckbox(t){const{subjectIndex:e,optionIndex:s}=t.currentTarget.dataset,i=this.data.subjectList,n=i[e].subjectOptions;n[s].isSelect=!n[s].isSelect,this.setData({subjectList:i})},inputAnswerText(t){const{subjectIndex:e}=t.currentTarget.dataset,{value:s}=t.detail,i=this.data.subjectList;i[e].answerText=s,this.setData({subjectList:i})},submitQuestion(){const t=Question.buildAnswerData(this.data.subjectList);"string"!=typeof t?(this.questionnaireRetryTime=3,this.clearTimeoutClock(),this.setData({showQuestion:!1}),this.submitQuestionHandle(t)):wx.showToast({title:t,icon:"none"})},submitQuestionHandle(t){this.chat.sendQuestionAnswer(t,this.data.questionnaireId,(t=>{let e;this.clearTimeoutClock();try{e=JSON.parse(t).code}catch(t){e=400}this.questionnaireSubmitHandle(e)})),this.questionnaireOvertime=setTimeout((()=>{this.questionnaireRetryTime-=1,this.questionnaireRetryTime<0?this.questionnaireSubmitHandle(400):this.submitQuestionHandle(t)}),5e3)},onModalHide(){this.setData({showModal:!1})},clearTimeoutClock(){this.questionnaireOvertime&&clearTimeout(this.questionnaireOvertime)},questionnaireSubmitHandle(t){const e={301:LANGUAGE.END,302:LANGUAGE.SUBMITED,200:LANGUAGE.SUBMIT_SUCCESS,400:LANGUAGE.SUBMIT_FAILED}[t]||LANGUAGE.SUBMIT_FAILED;this.setData({showModal:!0,modalData:{imgSrc:"../../assets/images/"+(200===t?"interact-submit-success.png":"interact-cut-off.png"),modalText:e}}),this.selectComponent("#answerModal").show(),this.triggerEvent("showPptPaint")}}});