cmms/frontend/dist/assets/usePreventiveMaintenanceDis...

2 lines
14 KiB
JavaScript

import{bc as y,aO as m}from"./index-BUdRbiwj.js";class S extends y{constructor(){super("/preventive-maintenance")}async search(e={}){return await this.request("/search","POST",{criteria:{page:0,size:10,sortBy:"nextDue",direction:"ASC",...e}})}async getUpcomingMaintenance(e,t=30){return await this.request("/upcoming","GET",null,{params:{assetId:e,days:t}})}async generateWorkOrders(e,t={}){return await this.request(`/${e}/work-orders`,"POST",t)}async getHistory(e){return await this.request(`/${e}/history`,"GET")}async pauseSchedule(e,t=""){return await this.request(`/${e}/status`,"PATCH",{status:"PAUSED",reason:t})}async resumeSchedule(e){return await this.request(`/${e}/status`,"PATCH",{status:"ACTIVE"})}async getByAsset(e){return await this.request(`/asset/${e}`,"GET")}async getOverdue(e){const t=e?{days:e}:{};return await this.request("/overdue","GET",null,{params:t})}async getByPriority(e){return await this.request(`/priority/${e}`,"GET")}async getByFrequency(e){return await this.request(`/frequency/${e}`,"GET")}async bulkUpdateStatus(e,t,a=""){return await this.request("/bulk/status","PATCH",{scheduleIds:e,status:t,reason:a})}async getCalendarEvents(e,t,a=[]){return await this.request("/calendar","GET",null,{params:{startDate:e.toISOString(),endDate:t.toISOString(),assetIds:a.join(",")}})}async reschedule(e,t,a=""){return await this.request(`/${e}/reschedule`,"PATCH",{newDate:t.toISOString(),reason:a})}async getStatistics(e={}){const t={};return e.assetId&&(t.assetId=e.assetId),e.startDate&&(t.startDate=e.startDate.toISOString()),e.endDate&&(t.endDate=e.endDate.toISOString()),await this.request("/statistics","GET",null,{params:t})}async duplicate(e,t={}){return await this.request(`/${e}/duplicate`,"POST",t)}async getTemplates(){return await this.request("/templates","GET")}async createFromTemplate(e,t){return await this.request("/templates/create","POST",{templateId:e,...t})}}let c=new S;const I=m("preventive-maintenance",{state:()=>({schedules:[],currentSchedule:null,loading:!1,error:null,initialized:!1,totalElements:0,totalPages:0,searchCriteria:{page:0,size:10,sortBy:"nextDue",direction:"ASC",search:"",filters:{}},upcomingMaintenance:[],overdueSchedules:[],calendarEvents:[],statistics:{totalSchedules:0,activeSchedules:0,pausedSchedules:0,overdueSchedules:0,dueSoon:0,dueThisWeek:0,completionRate:0,totalValue:0,avgDuration:0}}),getters:{getScheduleById:r=>e=>r.schedules.find(t=>t.id===e),filteredSchedules:r=>{let e=[...r.schedules];if(r.searchCriteria.search){const a=r.searchCriteria.search.toLowerCase();e=e.filter(n=>{var i,d,h;return((i=n.name)==null?void 0:i.toLowerCase().includes(a))||((d=n.assetName)==null?void 0:d.toLowerCase().includes(a))||((h=n.description)==null?void 0:h.toLowerCase().includes(a))})}const t=r.searchCriteria.filters||{};return t.status&&(e=e.filter(a=>a.status===t.status)),t.priority&&(e=e.filter(a=>a.priority===t.priority)),t.assetId&&(e=e.filter(a=>a.assetId===t.assetId)),t.frequency&&(e=e.filter(a=>a.frequency===t.frequency)),e},getSchedulesByStatus:r=>e=>r.schedules.filter(t=>t.status===e),getSchedulesByPriority:r=>e=>r.schedules.filter(t=>t.priority===e),getSchedulesForAsset:r=>e=>r.schedules.filter(t=>t.assetId===e),overdueSchedulesList:r=>{const e=new Date;return r.schedules.filter(t=>t.nextDue?new Date(t.nextDue)<e&&t.status==="ACTIVE":!1)},schedulesDeadlineToday:r=>{const e=new Date;e.setHours(0,0,0,0);const t=new Date(e);return t.setDate(t.getDate()+1),r.schedules.filter(a=>{if(!a.nextDue)return!1;const n=new Date(a.nextDue);return n>=e&&n<t&&a.status==="ACTIVE"})},schedulesDueSoon:r=>{const e=new Date,t=new Date(e.getTime()+3*24*60*60*1e3);return r.schedules.filter(a=>{if(!a.nextDue)return!1;const n=new Date(a.nextDue);return n>e&&n<=t&&a.status==="ACTIVE"})},inventoryStats:r=>{const e=r.schedules.filter(n=>n.status==="ACTIVE"),t=r.schedules.filter(n=>n.nextDue?new Date(n.nextDue)<new Date&&n.status==="ACTIVE":!1),a=r.schedules.filter(n=>{if(!n.nextDue)return!1;const i=new Date(n.nextDue),d=new Date,h=new Date(d.getTime()+3*24*60*60*1e3);return i>d&&i<=h&&n.status==="ACTIVE"});return{totalSchedules:r.schedules.length,activeSchedules:e.length,pausedSchedules:r.schedules.filter(n=>n.status==="PAUSED").length,overdueSchedules:t.length,dueSoon:a.length,dueThisWeek:r.schedules.filter(n=>{if(!n.nextDue)return!1;const i=new Date(n.nextDue),d=new Date,h=new Date(d.getTime()+7*24*60*60*1e3);return i>d&&i<=h&&n.status==="ACTIVE"}).length,completionRate:r.statistics.completionRate||0,totalValue:r.schedules.reduce((n,i)=>n+(i.estimatedCost||0)*(i.completedCount||0),0),avgDuration:r.schedules.length>0?Math.round(r.schedules.reduce((n,i)=>n+(i.estimatedDuration||0),0)/r.schedules.length):0}}},actions:{async initializeSchedules(){if(!this.initialized)try{await this.fetchSchedules(),this.initialized=!0}catch(r){console.error("Failed to initialize schedules:",r),this.error=r.message}},async fetchSchedules(r={}){this.loading=!0,this.error=null;try{const e={...this.searchCriteria,...r},t=await c.search(e);return this.schedules=t.data||[],this.totalElements=t.totalElements||0,this.totalPages=t.totalPages||0,this.searchCriteria={...this.searchCriteria,...r},t}catch(e){throw console.error("Failed to fetch schedules:",e),this.error=e.message,e}finally{this.loading=!1}},async fetchScheduleById(r){try{const e=await c.getById(r),t=this.schedules.findIndex(a=>a.id===r);return t!==-1&&(this.schedules[t]=e),e}catch(e){throw console.error("Failed to fetch schedule:",e),e}},async createSchedule(r){this.loading=!0,this.error=null;try{const e=await c.create(r);return this.schedules.unshift(e),this.totalElements+=1,e}catch(e){throw console.error("Failed to create schedule:",e),this.error=e.message,e}finally{this.loading=!1}},async updateSchedule(r,e){this.loading=!0,this.error=null;try{const t=await c.update(r,e),a=this.schedules.findIndex(n=>n.id===r);return a!==-1&&(this.schedules[a]=t),t}catch(t){throw console.error("Failed to update schedule:",t),this.error=t.message,t}finally{this.loading=!1}},async deleteSchedule(r){this.loading=!0,this.error=null;try{await c.delete(r);const e=this.schedules.findIndex(t=>t.id===r);e!==-1&&(this.schedules.splice(e,1),this.totalElements-=1)}catch(e){throw console.error("Failed to delete schedule:",e),this.error=e.message,e}finally{this.loading=!1}},async generateWorkOrders(r,e={}){try{const t=await c.generateWorkOrders(r,e);return t&&t.length>0&&this.getScheduleById(r)&&await this.fetchScheduleById(r),t}catch(t){throw console.error("Failed to generate work orders:",t),t}},async pauseSchedule(r,e=""){try{const t=await c.pauseSchedule(r,e),a=this.schedules.findIndex(n=>n.id===r);return a!==-1&&(this.schedules[a]={...this.schedules[a],...t}),t}catch(t){throw console.error("Failed to pause schedule:",t),t}},async resumeSchedule(r){try{const e=await c.resumeSchedule(r),t=this.schedules.findIndex(a=>a.id===r);return t!==-1&&(this.schedules[t]={...this.schedules[t],...e}),e}catch(e){throw console.error("Failed to resume schedule:",e),e}},async fetchUpcomingMaintenance(r=null,e=30){try{const t=await c.getUpcomingMaintenance(r,e);return this.upcomingMaintenance=t.data||[],this.upcomingMaintenance}catch(t){throw console.error("Failed to fetch upcoming maintenance:",t),t}},async fetchOverdueSchedules(r=null){try{const e=await c.getOverdue(r);return this.overdueSchedules=e.data||[],this.overdueSchedules}catch(e){throw console.error("Failed to fetch overdue schedules:",e),e}},async fetchCalendarEvents(r,e,t=[]){try{const a=await c.getCalendarEvents(r,e,t);return this.calendarEvents=a.data||[],this.calendarEvents}catch(a){throw console.error("Failed to fetch calendar events:",a),a}},async rescheduleMaintenacne(r,e,t=""){try{const a=await c.reschedule(r,e,t),n=this.schedules.findIndex(i=>i.id===r);return n!==-1&&(this.schedules[n]={...this.schedules[n],...a}),a}catch(a){throw console.error("Failed to reschedule maintenance:",a),a}},async duplicateSchedule(r,e={}){try{const t=await c.duplicate(r,e);return this.schedules.unshift(t),this.totalElements+=1,t}catch(t){throw console.error("Failed to duplicate schedule:",t),t}},async exportSchedules(r={}){try{return await c.exportSchedules(r)}catch(e){throw console.error("Failed to export schedules:",e),e}},async createTemplate(r){try{return await c.createTemplate(r)}catch(e){throw console.error("Failed to create template:",e),e}},async fetchStatistics(r={}){try{const e=await c.getStatistics(r);return this.statistics={...this.statistics,...e.data},this.statistics}catch(e){throw console.error("Failed to fetch statistics:",e),e}},clearSchedules(){this.schedules=[],this.totalElements=0,this.totalPages=0,this.currentSchedule=null},setSearchCriteria(r){this.searchCriteria={...this.searchCriteria,...r}},clearError(){this.error=null},setCurrentSchedule(r){this.currentSchedule=r}}});function k(){return{formatFrequency:(s,o=1)=>({DAILY:o===1?"Daily":`Every ${o} days`,WEEKLY:o===1?"Weekly":`Every ${o} weeks`,MONTHLY:o===1?"Monthly":`Every ${o} months`,QUARTERLY:o===1?"Quarterly":`Every ${o} quarters`,YEARLY:o===1?"Yearly":`Every ${o} years`})[s==null?void 0:s.toUpperCase()]||"Unknown frequency",getStatusVariant:s=>({ACTIVE:"success",PAUSED:"warning",INACTIVE:"secondary",OVERDUE:"error",DUE_TODAY:"error",DUE_SOON:"warning",SCHEDULED:"info",COMPLETED:"success"})[s]||"secondary",getPriorityVariant:s=>({LOW:"success",MEDIUM:"info",HIGH:"warning",CRITICAL:"error"})[s]||"secondary",formatStatus:s=>({ACTIVE:"Active",PAUSED:"Paused",INACTIVE:"Inactive",OVERDUE:"Overdue",DUE_TODAY:"Due Today",DUE_SOON:"Due Soon",SCHEDULED:"Scheduled",COMPLETED:"Completed"})[s]||s,formatPriority:s=>({LOW:"Low",MEDIUM:"Medium",HIGH:"High",CRITICAL:"Critical"})[s]||s,formatDuration:s=>{if(!s)return"0 min";if(s<60)return`${s} min`;if(s<1440){const o=Math.floor(s/60),l=s%60;return l>0?`${o}h ${l}m`:`${o}h`}else{const o=Math.floor(s/1440),l=Math.floor(s%1440/60);return l>0?`${o}d ${l}h`:`${o}d`}},formatDate:s=>{if(!s)return"Not scheduled";const o=new Date(s);return isNaN(o.getTime())?"Invalid date":o.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"})},formatDateTime:s=>{if(!s)return"Not scheduled";const o=new Date(s);return isNaN(o.getTime())?"Invalid date":o.toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric",hour:"numeric",minute:"2-digit",hour12:!0})},formatRelativeTime:s=>{if(!s)return"Unknown";const o=new Date(s);if(isNaN(o.getTime()))return"Invalid date";const l=new Date,g=Math.floor((o.getTime()-l.getTime())/1e3),f=Math.abs(g);if(f<60)return"Just now";if(f<3600){const u=Math.floor(f/60);return g<0?`${u} min ago`:`in ${u} min`}else if(f<86400){const u=Math.floor(f/3600);return g<0?`${u} hour${u>1?"s":""} ago`:`in ${u} hour${u>1?"s":""}`}else{const u=Math.floor(f/86400);return g<0?`${u} day${u>1?"s":""} ago`:`in ${u} day${u>1?"s":""}`}},getOverdueIndicator:s=>{if(s<=0)return{text:"",variant:"success",show:!1};let o="warning";return(s>7||s>3)&&(o="error"),{text:`${s} day${s>1?"s":""} overdue`,variant:o,show:!0}},formatMaintenanceStats:s=>s?[{name:"Total Schedules",value:s.totalSchedules||0,subtitle:"Active schedules",icon:{template:'<svg fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v10a2 2 0 002 2h8a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01" /></svg>'},iconBg:"bg-blue-100",iconColor:"text-blue-600",change:0},{name:"Overdue",value:s.overdueSchedules||0,subtitle:"Require attention",icon:{template:'<svg fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.728-.833-2.498 0L4.316 16.5c-.77.833.192 2.5 1.732 2.5z" /></svg>'},iconBg:"bg-red-100",iconColor:"text-red-600",change:0},{name:"Due This Week",value:s.dueThisWeek||0,subtitle:"Upcoming tasks",icon:{template:'<svg fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>'},iconBg:"bg-yellow-100",iconColor:"text-yellow-600",change:0},{name:"Completion Rate",value:`${s.completionRate||0}%`,subtitle:"This month",icon:{template:'<svg fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" /></svg>'},iconBg:"bg-green-100",iconColor:"text-green-600",change:0}]:[],getScheduleTableColumns:(s=!1)=>{const o=[{key:"name",label:"Schedule Name",sortable:!0},{key:"asset",label:"Asset",sortable:!0},{key:"frequency",label:"Frequency",sortable:!0},{key:"priority",label:"Priority",sortable:!0},{key:"nextDue",label:"Next Due",sortable:!0},{key:"status",label:"Status",sortable:!0},{key:"actions",label:"Actions",sortable:!1}];return s?o.filter(l=>["name","nextDue","status","actions"].includes(l.key)):o},getScheduleTabs:s=>s?[{key:"all",label:"All Schedules",badge:s.totalSchedules||0},{key:"active",label:"Active",badge:s.activeSchedules||0},{key:"overdue",label:"Overdue",badge:s.overdueSchedules||0},{key:"due-soon",label:"Due Soon",badge:s.dueSoon||0},{key:"paused",label:"Paused",badge:s.pausedSchedules||0}]:[{key:"all",label:"All Schedules",badge:0}],formatAssetDisplay:s=>s?typeof s=="string"?s:s.name||s.assetNumber||`Asset ${s.id}`:"Unknown Asset",getCalendarEventStyling:s=>{const o={borderRadius:"4px",padding:"2px 8px",fontSize:"12px",fontWeight:"500"},l={LOW:{backgroundColor:"#10B981",borderColor:"#059669",color:"#FFFFFF"},MEDIUM:{backgroundColor:"#3B82F6",borderColor:"#2563EB",color:"#FFFFFF"},HIGH:{backgroundColor:"#F59E0B",borderColor:"#D97706",color:"#FFFFFF"},CRITICAL:{backgroundColor:"#EF4444",borderColor:"#DC2626",color:"#FFFFFF"}};return{...o,...l[s.priority]||l.MEDIUM}}}}export{k as a,I as u};