index.vue
9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<script setup lang="ts">
import { h, resolveComponent } from 'vue'
import { format, sub } from 'date-fns'
import type { TableColumn } from '@nuxt/ui'
import type {
DashboardOverviewResponse,
DashboardWorkOrderStatus,
Range
} from '~/types'
const { isNotificationsSlideoverOpen } = useDashboard()
const { t } = useAppI18n()
const toast = useToast()
const dashboardApi = useDashboardDataApi()
const UBadge = resolveComponent('UBadge')
const range = shallowRef<Range>({
start: sub(new Date(), { days: 14 }),
end: new Date()
})
const selectedLine = ref<'all' | string>('all')
const selectedWorkOrderStatus = ref<'all' | DashboardWorkOrderStatus>('all')
const loading = ref(false)
const exporting = ref(false)
const requestError = ref('')
const overview = ref<DashboardOverviewResponse>({
query: {},
filters: {
lines: [],
workOrderStatuses: []
},
workOrders: {
total: 0,
running: 0,
pendingQc: 0,
inException: 0
},
sn: {
completed: 0,
inProcess: 0,
frozen: 0
},
quality: {
firstPassRate: 0,
retestPassRate: 0,
exceptionRate: 0
},
latestWorkOrders: []
})
const lineItems = computed(() => {
return [{
label: t('home.filters.lineAll'),
value: 'all'
}, ...overview.value.filters.lines.map(line => ({
label: line,
value: line
}))]
})
const statusItems = computed(() => {
return [{
label: t('home.filters.workOrderStatusAll'),
value: 'all'
}, ...overview.value.filters.workOrderStatuses.map(status => ({
label: t(`home.workOrderStatus.${status}`),
value: status
}))]
})
const summaryCards = computed(() => [{
title: t('home.metrics.workOrders.total'),
value: overview.value.workOrders.total,
icon: 'i-lucide-clipboard-list'
}, {
title: t('home.metrics.workOrders.running'),
value: overview.value.workOrders.running,
icon: 'i-lucide-play-circle'
}, {
title: t('home.metrics.workOrders.pendingQc'),
value: overview.value.workOrders.pendingQc,
icon: 'i-lucide-flask-conical'
}, {
title: t('home.metrics.workOrders.inException'),
value: overview.value.workOrders.inException,
icon: 'i-lucide-alert-triangle'
}, {
title: t('home.metrics.sn.completed'),
value: overview.value.sn.completed,
icon: 'i-lucide-check-circle-2'
}, {
title: t('home.metrics.sn.inProcess'),
value: overview.value.sn.inProcess,
icon: 'i-lucide-loader-circle'
}, {
title: t('home.metrics.sn.frozen'),
value: overview.value.sn.frozen,
icon: 'i-lucide-snowflake'
}, {
title: t('home.metrics.quality.firstPassRate'),
value: `${overview.value.quality.firstPassRate.toFixed(2)}%`,
icon: 'i-lucide-badge-check'
}, {
title: t('home.metrics.quality.retestPassRate'),
value: `${overview.value.quality.retestPassRate.toFixed(2)}%`,
icon: 'i-lucide-rotate-cw'
}, {
title: t('home.metrics.quality.exceptionRate'),
value: `${overview.value.quality.exceptionRate.toFixed(2)}%`,
icon: 'i-lucide-octagon-alert'
}])
function formatDateInput(value: Date) {
return format(value, 'yyyy-MM-dd')
}
const query = computed(() => ({
start: formatDateInput(range.value.start),
end: formatDateInput(range.value.end),
line: selectedLine.value === 'all' ? undefined : selectedLine.value,
workOrderStatus: selectedWorkOrderStatus.value === 'all'
? undefined
: selectedWorkOrderStatus.value
}))
const columns = computed<TableColumn<DashboardOverviewResponse['latestWorkOrders'][number]>[]>(() => [{
accessorKey: 'orderNo',
header: t('home.table.orderNo')
}, {
accessorKey: 'line',
header: t('home.table.line')
}, {
accessorKey: 'status',
header: t('home.table.status'),
cell: ({ row }) => {
const status = row.getValue('status') as DashboardWorkOrderStatus
const color = ({
pending_dispatch: 'neutral',
running: 'primary',
pending_qc: 'warning',
in_exception: 'error',
completed: 'success'
} as const)[status]
return h(UBadge, { color, variant: 'subtle' }, () => t(`home.workOrderStatus.${status}`))
}
}, {
accessorKey: 'plannedDate',
header: t('home.table.plannedDate')
}, {
id: 'progress',
header: t('home.table.progress'),
cell: ({ row }) => {
const item = row.original
return `${item.completedSn}/${item.totalSn}`
}
}])
async function loadOverview() {
loading.value = true
requestError.value = ''
try {
overview.value = await dashboardApi.getDashboardOverview(query.value)
} catch {
requestError.value = t('common.requestFailed')
} finally {
loading.value = false
}
}
async function onManualRefresh() {
await loadOverview()
}
async function onExport() {
if (exporting.value) {
return
}
exporting.value = true
try {
const result = await dashboardApi.exportDashboardOverview(query.value)
if (!result.success || typeof result.content !== 'string' || !result.fileName) {
toast.add({
title: t('common.error'),
description: result.message || t('common.requestFailed'),
icon: 'i-lucide-circle-alert',
color: 'error'
})
return
}
if (import.meta.client) {
const bytes = new TextEncoder().encode(result.content)
const blob = new Blob([bytes], {
type: result.contentType || 'text/csv;charset=utf-8'
})
const url = URL.createObjectURL(blob)
const anchor = document.createElement('a')
anchor.href = url
anchor.download = result.fileName
anchor.click()
URL.revokeObjectURL(url)
}
toast.add({
title: t('home.export.successTitle'),
description: t('home.export.successDescription'),
icon: 'i-lucide-check',
color: 'success'
})
} catch {
toast.add({
title: t('common.error'),
description: t('common.requestFailed'),
icon: 'i-lucide-circle-alert',
color: 'error'
})
} finally {
exporting.value = false
}
}
watch(
[
() => range.value.start,
() => range.value.end,
selectedLine,
selectedWorkOrderStatus
],
() => {
loadOverview()
},
{ immediate: true }
)
</script>
<template>
<UDashboardPanel id="home">
<template #header>
<UDashboardNavbar :title="t('home.title')" :ui="{ right: 'gap-2' }">
<template #leading>
<UDashboardSidebarCollapse />
</template>
<template #right>
<UTooltip :text="t('notifications.title')" :shortcuts="['N']">
<UButton
color="neutral"
variant="ghost"
square
@click="isNotificationsSlideoverOpen = true"
>
<UChip color="error" inset>
<UIcon name="i-lucide-bell" class="size-5 shrink-0" />
</UChip>
</UButton>
</UTooltip>
<UButton
color="neutral"
variant="outline"
icon="i-lucide-refresh-cw"
:label="t('home.actions.refresh')"
:loading="loading"
@click="onManualRefresh"
/>
<UButton
icon="i-lucide-download"
:label="t('home.actions.export')"
:loading="exporting"
@click="onExport"
/>
</template>
</UDashboardNavbar>
<UDashboardToolbar>
<template #left>
<HomeDateRangePicker v-model="range" class="-ms-1" />
<USelect
v-model="selectedLine"
:items="lineItems"
value-key="value"
class="w-44"
/>
<USelect
v-model="selectedWorkOrderStatus"
:items="statusItems"
value-key="value"
class="w-44"
/>
</template>
</UDashboardToolbar>
</template>
<template #body>
<UAlert
v-if="requestError"
color="error"
variant="subtle"
icon="i-lucide-circle-alert"
:title="t('common.error')"
:description="requestError"
/>
<UPageGrid class="md:grid-cols-2 xl:grid-cols-5 gap-4 sm:gap-4">
<UPageCard
v-for="item in summaryCards"
:key="item.title"
:icon="item.icon"
:title="item.title"
variant="subtle"
:ui="{
container: 'gap-y-1.5',
wrapper: 'items-start',
leading: 'p-2.5 rounded-full bg-primary/10 ring ring-inset ring-primary/25 flex-col',
title: 'font-normal text-muted text-xs uppercase'
}"
>
<div class="text-2xl font-semibold text-highlighted">
{{ item.value }}
</div>
</UPageCard>
</UPageGrid>
<UCard>
<template #header>
<div class="flex items-center justify-between gap-2">
<div>
<p class="text-sm font-semibold text-highlighted">
{{ t('home.table.title') }}
</p>
<p class="text-xs text-muted">
{{ t('home.table.description') }}
</p>
</div>
<UBadge color="neutral" variant="subtle">
{{ t('home.table.count', { count: overview.latestWorkOrders.length }) }}
</UBadge>
</div>
</template>
<UTable
:data="overview.latestWorkOrders"
:columns="columns"
:loading="loading"
class="shrink-0"
:ui="{
base: 'table-fixed border-separate border-spacing-0',
thead: '[&>tr]:bg-elevated/50 [&>tr]:after:content-none',
tbody: '[&>tr]:last:[&>td]:border-b-0',
th: 'py-2 first:rounded-l-lg last:rounded-r-lg border-y border-default first:border-l last:border-r',
td: 'border-b border-default'
}"
/>
<div
v-if="!loading && overview.latestWorkOrders.length === 0"
class="text-sm text-muted pt-3"
>
{{ t('home.table.empty') }}
</div>
</UCard>
</template>
</UDashboardPanel>
</template>