import{r as i,d as t,B as l,h as o}from"./p-9b6cc562.js";const e=class{constructor(l){i(this,l),this.floodteamCloseModal=t(this,"floodteamCloseModal",7),this.fireenjinFetch=t(this,"fireenjinFetch",7),this.colors=["#3e95cd","#8e5ea2","#3cba9f","#e8c3b9","#c45850"],this.payModels=[],this.job=void 0,this.amount=0,this.disableAmount=!1,this.disableReferral=!1,this.hasReferral=!1,this.jobId=void 0,this.payModelId=void 0,this.payables=[],this.selectedPayModel=void 0,this.totalPercentage=0}onChange(i){var t;"payModelId"===i.target.name?(this.payModelId=null===(t=null==i?void 0:i.target)||void 0===t?void 0:t.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 t,l,o,e,s,n;"findJob"===(null===(t=null==i?void 0:i.detail)||void 0===t?void 0:t.endpoint)?this.hasReferral=!!(null===(o=null===(l=this.job)||void 0===l?void 0:l.referral)||void 0===o?void 0:o.id):"listPayModels"===(null===(e=null==i?void 0:i.detail)||void 0===e?void 0:e.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(((t,l)=>{var o;const e=this.hasReferral&&(null===(o=null==t?void 0:t.percentages)||void 0===o?void 0:o.hasReferral)?t.percentages.hasReferral:t.percentages.default;return i+=e,{percentage:e,role:null==t?void 0:t.role,amount:e*this.amount,color:this.mixColor(this.colors,l)}})),this.totalPercentage=i}shadeColor(i,t){let l=!1;"#"===i[0]&&(i=i.slice(1),l=!0);const o=parseInt(i,16);let e=(o>>16)+t;e>255?e=255:e<0&&(e=0);let s=(o>>8&255)+t;s>255?s=255:s<0&&(s=0);let n=(255&o)+t;return n>255?n=255:n<0&&(n=0),(l?"#":"")+(n|s<<8|e<<16).toString(16)}mixColor(i,t){if(i[t])return i[t];{const l=Math.floor(t/i.length);return this.shadeColor(i[t-l*i.length],30*l)}}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==l?void 0:l.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 o("ion-content",{class:"ion-padding"},o("fireenjin-form",{submitButton:"Approve Payables",endpoint:"generatePayables"},o("ion-grid",null,o("ion-row",null,o("ion-col",{size:"12",sizeMd:"7"},o("ion-grid",null,o("ion-row",null,o("ion-col",null,o("fireenjin-input",{disabled:!!this.disableAmount,name:"received",label:"Amount Received",type:"tel",value:this.amount})),o("ion-col",null,o("fireenjin-select",{label:"Pay Model",name:"payModelId",endpoint:"listPayModels",resultsKey:"payModels",limit:100,value:this.payModelId})),o("ion-col",null,o("ion-item",null,o("ion-label",null,"Has Referral"),o("ion-toggle",{name:"referral",disabled:this.disableReferral,checked:this.hasReferral}))),o("ion-col",{class:"graph-col"},o("fireenjin-graph",{type:"pie",datasets:[{backgroundColor:this.payables.map((i=>i.color)),data:this.payables.map((i=>i.amount))}]}))))),o("ion-col",{size:"12",sizeMd:"5"},o("ion-list",null,this.payables.map((i=>{var t;return o("ion-item",null,o("ion-label",null,o("h2",null,(null===(t=null==i?void 0:i.role)||void 0===t?void 0:t.name)?i.role.name:"No Role Name..."),o("p",{style:{color:i.color}},o("b",null,this.formatUSD(i.amount))," @"," ",100*i.percentage," %")),o("ion-badge",{color:"success"}))}))))))))}};e.style="modal-pay-calculator floodteam-graph,modal-pay-calculator floodteam-graph canvas{display:table-cell;min-width:150px;min-height:150px;margin:auto}";export{e as modal_pay_calculator}