import{r as i,d as l,B as e,h as t}from"./p-03169580.js";import{s as o}from"./p-81b30421.js";import"./p-59c95c5f.js";import"./p-c9699503.js";import"./p-bd8b186e.js";import"./p-795b799a.js";import"./p-3a8e6af1.js";const s="modal-pay-calculator floodteam-graph,modal-pay-calculator floodteam-graph canvas{display:table-cell;min-width:150px;min-height:150px;margin:auto}";let n=class{constructor(e){i(this,e);this.floodteamCloseModal=l(this,"floodteamCloseModal",7);this.fireenjinFetch=l(this,"fireenjinFetch",7);this.colors=["#3e95cd","#8e5ea2","#3cba9f","#e8c3b9","#c45850"];this.payModels=[];this.amount=0;this.disableAmount=false;this.disableReferral=false;this.hasReferral=false;this.payables=[];this.totalPercentage=0}onChange(i){var l;if(i.target.name==="payModelId"){this.payModelId=(l=i===null||i===void 0?void 0:i.target)===null||l===void 0?void 0:l.value;this.selectedPayModel=this.payModels.find((i=>i.id===this.payModelId));console.log(i,this.payModels,this.selectedPayModel)}else if(i.target.name==="received"){this.amount=i.target.value}else if(i.target.name==="referral"){this.hasReferral=i.target.checked}this.calculatePayables()}onSuccess(i){var l,e,t,s,n,a;if(((l=i===null||i===void 0?void 0:i.detail)===null||l===void 0?void 0:l.endpoint)==="findJob"){this.hasReferral=!!((t=(e=o===null||o===void 0?void 0:o.job)===null||e===void 0?void 0:e.referral)===null||t===void 0?void 0:t.id)}else if(((s=i===null||i===void 0?void 0:i.detail)===null||s===void 0?void 0:s.endpoint)==="listPayModels"){this.payModels=((a=(n=i===null||i===void 0?void 0:i.detail)===null||n===void 0?void 0:n.data)===null||a===void 0?void 0:a.payModels)?i.detail.data.payModels:[];if(this.payModelId){this.selectedPayModel=this.payModels.find((i=>i.id===this.payModelId));console.log(this.selectedPayModel);this.calculatePayables()}}}onReset(){this.floodteamCloseModal.emit({})}onSubmit(){this.floodteamCloseModal.emit({})}formatUSD(i){const l=new Intl.NumberFormat("en-US",{style:"currency",currency:"USD",minimumFractionDigits:2});return l.format(i?i:0)}calculatePayables(){if(!this.selectedPayModel)return;let i=0;this.payables=this.selectedPayModel.payouts.map(((l,e)=>{var t;const o=this.hasReferral&&((t=l===null||l===void 0?void 0:l.percentages)===null||t===void 0?void 0:t.hasReferral)?l.percentages.hasReferral:l.percentages.default;i+=o;return{percentage:o,role:l===null||l===void 0?void 0:l.role,amount:o*this.amount,color:this.mixColor(this.colors,e)}}));this.totalPercentage=i}shadeColor(i,l){let e=false;if(i[0]==="#"){i=i.slice(1);e=true}const t=parseInt(i,16);let o=(t>>16)+l;if(o>255){o=255}else if(o<0){o=0}let s=(t>>8&255)+l;if(s>255){s=255}else if(s<0){s=0}let n=(t&255)+l;if(n>255){n=255}else if(n<0){n=0}return(e?"#":"")+(n|s<<8|o<<16).toString(16)}mixColor(i,l){if(i[l]){return i[l]}else{const e=Math.floor(l/i.length);const t=e*30;const o=l-e*i.length;return this.shadeColor(i[o],t)}}async fetchData(){this.fireenjinFetch.emit({endpoint:"findJob",params:{id:this.jobId,withPayments:true,withUsers:true,withSites:false,withPayModel:true,withPhotos:true,withPayables:false,withReadings:false}})}componentDidLoad(){var i;if(!(e===null||e===void 0?void 0:e.isBrowser))return;if(((i=this.payModels)===null||i===void 0?void 0:i.length)&&this.payModelId){this.selectedPayModel=this.payModels[this.payModelId];setTimeout((()=>{this.calculatePayables()}),200)}}render(){return t("ion-content",{class:"ion-padding"},t("fireenjin-form",{submitButton:"Approve Payables",endpoint:"generatePayables"},t("ion-grid",null,t("ion-row",null,t("ion-col",{size:"12",sizeMd:"7"},t("ion-grid",null,t("ion-row",null,t("ion-col",null,t("fireenjin-input",{disabled:!!this.disableAmount,name:"received",label:"Amount Received",type:"tel",value:this.amount})),t("ion-col",null,t("fireenjin-select",{label:"Pay Model",name:"payModelId",endpoint:"listPayModels",resultsKey:"payModels",limit:100,value:this.payModelId})),t("ion-col",null,t("ion-item",null,t("ion-label",null,"Has Referral"),t("ion-toggle",{name:"referral",disabled:this.disableReferral,checked:this.hasReferral}))),t("ion-col",{class:"graph-col"},t("fireenjin-graph",{type:"pie",datasets:[{backgroundColor:this.payables.map((i=>i.color)),data:this.payables.map((i=>i.amount))}]}))))),t("ion-col",{size:"12",sizeMd:"5"},t("ion-list",null,this.payables.map((i=>{var l;return t("ion-item",null,t("ion-label",null,t("h2",null,((l=i===null||i===void 0?void 0:i.role)===null||l===void 0?void 0:l.name)?i.role.name:"No Role Name..."),t("p",{style:{color:i.color}},t("b",null,this.formatUSD(i.amount))," @"," ",i.percentage*100," %")),t("ion-badge",{color:"success"}))}))))))))}};n.style=s;export{n as modal_pay_calculator};
//# sourceMappingURL=p-b2e896dd.entry.js.map