document.addEventListener("alpine:init",()=>{Alpine.data("dataPaito",()=>({isLoad:!0,color:"",as:"",kop:"",kep:"",ekr:"",dataColor:["bg-primary","bg-info","bg-success","bg-warning","bg-danger","bg-secondary","bg-[#cbce10]","bg-dark","bg-[#e014d6]","bg-[#045f6d]"],dataColorImport:["!bg-primary","!bg-info","!bg-success","!bg-warning","!bg-danger","!bg-secondary","!bg-[#cbce10]","!bg-dark","!bg-[#e014d6]","!bg-[#045f6d]"],digitColor:{as:{},kop:{},kep:{},ekr:{}},option:{data:[],dataPasaran:[],dataTahun:[],minggu:[],pasaran:null,tahun:null},select2:{pasaran:null,tahun:null},init(){this.$watch("as",(t,a)=>{t==""?this.digitColor.as={}:delete this.digitColor.as[a]}),this.$watch("kop",(t,a)=>{t==""?this.digitColor.kop={}:delete this.digitColor.kop[a]}),this.$watch("kep",(t,a)=>{t==""?this.digitColor.kep={}:delete this.digitColor.kep[a]}),this.$watch("ekr",(t,a)=>{t==""?this.digitColor.ekr={}:delete this.digitColor.ekr[a]}),this.getData(!0),this.select2Pasaran(),this.select2Tahun()},select2Pasaran(){setTimeout(()=>{this.select2.pasaran!=null&&this.select2.pasaran.destroy(),this.select2.pasaran=NiceSelect.bind(document.getElementById("pasaran"),{searchable:!0,onChange:t=>{this.option.pasaran=parseInt(t.at().value),this.option.tahun=null,this.reset(),this.isLoad=!0,setTimeout(()=>this.getData())}})})},select2Tahun(){setTimeout(()=>{this.select2.tahun!=null&&this.select2.tahun.destroy(),this.select2.tahun=NiceSelect.bind(document.getElementById("tahun"),{onChange:t=>{this.option.tahun=parseInt(t.at().value),this.reset(),this.isLoad=!0,setTimeout(()=>this.getData())}})})},getData(t=!1){let a="/client/blog/paito/json",s={};this.option.pasaran!=null&&(s.pId=this.option.pasaran),this.option.tahun!=null&&(s.tahun=this.option.tahun);let i=Object.keys(s).reduce((e,o)=>(e.push(o+"="+s[o]),e),[]).join("&");a=a+(a.includes("?")?"&":"?")+i,axios.get(a).then(e=>{t?(this.option=e.data,this.select2Pasaran(),this.select2Tahun()):(s.pId&&(this.option.dataTahun=e.data.dataTahun,this.option.tahun=e.data.tahun,this.select2Tahun()),this.option.data=e.data.data),this.isLoad=!1,scrollInMenuHome()}).catch(e=>{this.isLoad=!1,Toast(e.message)})},validNumber(t){let a=t.target.getAttribute("x-model"),s=t.target.value.match(/\d/gm);t.target.value=s?s.at():"",t.target.value!=""&&(this.digitColor[a][t.target.value]||(this.digitColor[a][t.target.value]=this.getRandomColor()))},getRandomColor(){let t=this.dataColor[Math.floor(Math.random()*this.dataColor.length)];return[...Object.values(this.digitColor.as),...Object.values(this.digitColor.kep),...Object.values(this.digitColor.kep),...Object.values(this.digitColor.ekr)].includes(t)?this.getRandomColor():t},selectColor(t){let a=t.target,s=a.classList,i=[...s].filter(e=>e.includes("!bg"));this.color?(i.filter(e=>e!="!"+this.color).forEach(e=>s.remove(e)),a.classList.toggle("!"+this.color)):i.forEach(e=>s.remove(e))},reset(){this.digitColor={as:{},kop:{},kep:{},ekr:{}},this.as="",this.kop="",this.kep="",this.ekr="",[...document.querySelectorAll("tbody td")].forEach(a=>{let s=a.classList;[...s].filter(e=>e.includes("!bg")).forEach(e=>s.remove(e))})},getTdData(t){return JSON.parse(JSON.stringify(this.option.minggu)).map(s=>{let i=t.find(e=>e.weekName==s);if(i){let e=i.nomor.map((o,l)=>{let r=this.getType(l);return`
                            <td class="!p-0 !text-center dark:text-white text-[10px] sm:text-xs ${(i.week+1)%5==0?"bg-[#00000014] dark:bg-[#121e32]":""}" :class="digitColor['${r}']['${o}'] ? digitColor['${r}']['${o}'] : ''" @click="selectColor">${o}</td>
                        `});return e.push(`
                        <td class="!p-0 !text-center text-[#a4a2a2] text-[10px] sm:text-xs" @click="selectColor">${i.bar}</td>
                    `),e.join("")}else return Array.from({length:5},e=>'<td class="!p-0 !text-center text-[10px] sm:text-xs">-</td>').join("")}).join("")},get thData(){return this.option.minggu.map(t=>`
                    <th class="!text-center uppercase !p-0 dark:text-white text-[10px] sm:text-xs" colspan="4">${t}</th>
                    <th class="!text-center uppercase !p-0 dark:text-white text-[10px] sm:text-xs">D</th>
                `).join("")},getType(t){switch(t){case 0:return"as";case 1:return"kop";case 2:return"kep";case 3:return"ekr"}}}))});