import Event from"../../common/chat/eventTypes";import store from"../../store/index";import api from"../../common/api/index";let customAry=[];Component({data:{channelId:"",lotteryStatus:"list",winnerCode:"dIRxUGGk",prize:"特等奖",headText:"正在抽奖",handleList:[],lotteryShow:!1,collectInfo:[],customInfo:[],lotteryId:"",sessionId:""},properties:{zIndex:{type:Number,default:2e3}},lifetimes:{created(){this.chatEvent=this.getChatEvent(),this.chat=null,this.page=1,this.totalPages=null,this.lotteryWinner=!1,this.winnerList=[]},attached(){this.unsub=store.get({"main.chat":t=>{this.chat=t,t&&this.bindEvent(this.chatEvent)}}),this.handelWinnerList(this.winnerList)},detached(){if(this.unsub(),clearInterval(this.signTimer),!this.chat)return;const t=this.chatEvent;Object.keys(t).forEach((e=>{this.chat.off(e,t[e])})),this.chat=null}},methods:{bindEvent(t){const e=this.chat;e&&Object.keys(t).forEach((s=>{e.on(s,t[s])}))},getChatEvent(){const t=this;return{[Event.LOTTERY_START](e,s){t.startHandle(s)},[Event.ON_LOTTERY](e,s){t.startHandle(s)},[Event.LOTTERY_END](e,s){t.winnerList=[],t.totalPages=null,t.page=1,t.allowRequestList=!0,t.lotteryWinner=!1,t.endHandle(s),t.getListWinners()}}},startHandle(t){this.setData({lotteryStatus:"start",headText:this.updateHeadText("start"),lotteryShow:!0})},endHandle(t){const e=t.data[0];this.setData({sessionId:t.sessionId,lotteryId:t.lotteryId}),e?(this.winnerHandle(t,e),this.lotteryWinner=!0):(this.setData({lotteryStatus:"notWin",headText:this.updateHeadText("win")}),this.lotteryWinner=!1)},getListWinners(){const{page:t,totalPages:e}=this,{lotteryId:s}=this.data;null!=e&&t>e||!this.allowRequestList||(this.allowRequestList=!1,api.getListWinners({page:t,lotteryId:s}).then((t=>{this.allowRequestList=!0;const e=t.data.data;this.totalPages=e.totalPages,this.page+=1,this.winnerList=[...this.winnerList,...e.contents],this.getWinnerNick(this.winnerList),this.lotterying=!1})).catch((()=>this.allowRequestList=!0)))},winnerHandle(t,e){this.setData({channelId:e.channelId,lotteryStatus:"win",headText:this.updateHeadText("win"),winnerCode:e.winnerCode,lotteryShow:!0,prize:t.prize,collectInfo:JSON.parse(t.collectInfo)}),this.handleCustomInfo(this.data.collectInfo)},getName(t){customAry[0]=t.detail.value},checkPhone(t){/^1[3456789]\d{9}$/.test(t.detail.value)?customAry[1]=t.detail.value:this.lotteryToast("手机号码格式错误")},getCustom(t){const e=t.detail.value,s=t.target.dataset.index+2;customAry[s]=e},submitInfo(){const{channelId:t,sessionId:e,lotteryId:s,winnerCode:i}=this.data,{userId:a}=this.chat,n=JSON.parse(JSON.stringify(this.data.collectInfo));n.map(((t,e)=>{delete t.tips,t.value=customAry[e]})),n.some((t=>!t.value))?this.lotteryToast("请提交完整信息"):(api.addReceiveInfo({channelId:t,sessionId:e,lotteryId:s,viewerId:a,winnerCode:i,receiveInfo:JSON.stringify(n)}).then((t=>{200===t.data.code?(this.lotteryToast("提交成功"),this.setData({lotteryShow:!1})):this.lotteryToast("提交失败，请稍后重试")})).catch((()=>this.lotteryToast("网络错误，请稍后重试"))).finally((()=>this.triggerEvent("showPptPaint"))),this.setData({receiveInfo:n}))},lotteryToast(t){wx.showToast({title:t,icon:"none",duration:2e3})},updateHeadText:t=>({list:"中奖名单",submit:"填写联系信息",start:"正在抽奖",win:"中奖结果",notWin:"中奖结果"}[t]||"中奖结果"),handleClick(t){const{type:e}=t.currentTarget.dataset;switch(e){case"list":this.clickSetData(e);break;case"submit":customAry=[],this.clickSetData(e);break;case"back":this.clickSetData(this.lotteryWinner?"win":"notWin");break;case"close":this.setData({lotteryShow:!1}),this.triggerEvent("showPptPaint")}},clickSetData(t){this.setData({lotteryStatus:t,headText:this.updateHeadText(t)})},getWinnerList(){const{channelId:t,lotteryId:e}=this.data;api.getWinnerList({channelId:t,lotteryId:e,timestamp:Date.now()}).then((t=>{const e=t.data.data.map((t=>t.nick));this.handelWinnerList(e)}))},handleCustomInfo(t){const e=JSON.parse(JSON.stringify(t));0!==e.length&&this.setData({customInfo:e.splice(2)})},getWinnerNick(t=[]){if(0===t.length)return void this.setData({handleList:[]});const e=t.map((t=>t.viewerName));this.handelWinnerList(e)},handelWinnerList(t=[],e=3){if(0===!t.length)return void this.setData({handleList:[]});const s=t.length,i=s%e==0?s/e:Math.floor(s/e+1),a=[];for(let s=0;s<i;s++){const i=t.slice(s*e,s*e+e);a.push(JSON.parse(JSON.stringify(i)))}this.setData({handleList:a})}}});