import{r as i,d as e,B as l,h as t}from"./p-03169580.js";import{s as o}from"./p-ac5f2e11.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(l){i(this,l);this.floodteamCloseModal=e(this,"floodteamCloseModal",7);this.fireenjinFetch=e(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 e;if(i.target.name==="payModelId"){this.payModelId=(e=i===null||i===void 0?void 0:i.target)===null||e===void 0?void 0:e.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 e,l,t,s,n,a;if(((e=i===null||i===void 0?void 0:i.detail)===null||e===void 0?void 0:e.endpoint)==="findJob"){this.hasReferral=!!((t=(l=o===null||o===void 0?void 0:o.job)===null||l===void 0?void 0:l.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 e=new Intl.NumberFormat("en-US",{style:"currency",currency:"USD",minimumFractionDigits:2});return e.format(i?i:0)}calculatePayables(){if(!this.selectedPayModel)return;let i=0;this.payables=this.selectedPayModel.payouts.map(((e,l)=>{var t;const o=this.hasReferral&&((t=e===null||e===void 0?void 0:e.percentages)===null||t===void 0?void 0:t.hasReferral)?e.percentages.hasReferral:e.percentages.default;i+=o;return{percentage:o,role:e===null||e===void 0?void 0:e.role,amount:o*this.amount,color:this.mixColor(this.colors,l)}}));this.totalPercentage=i}shadeColor(i,e){let l=false;if(i[0]==="#"){i=i.slice(1);l=true}const t=parseInt(i,16);let o=(t>>16)+e;if(o>255){o=255}else if(o<0){o=0}let s=(t>>8&255)+e;if(s>255){s=255}else if(s<0){s=0}let n=(t&255)+e;if(n>255){n=255}else if(n<0){n=0}return(l?"#":"")+(n|s<<8|o<<16).toString(16)}mixColor(i,e){if(i[e]){return i[e]}else{const l=Math.floor(e/i.length);const t=l*30;const o=e-l*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(!(l===null||l===void 0?void 0:l.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 e;return t("ion-item",null,t("ion-label",null,t("h2",null,((e=i===null||i===void 0?void 0:i.role)===null||e===void 0?void 0:e.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-94ff2ee2.entry.js.map