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