ScanView.vue 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. <script setup>
  2. import { computed, onMounted, reactive, ref, watch } from 'vue'
  3. import { useRoute, useRouter } from 'vue-router'
  4. import { useToast } from 'primevue/usetoast'
  5. import Dialog from 'primevue/dialog'
  6. import LocationPicker from '@/components/LocationPicker.vue'
  7. import {
  8. fetchQrInfoApi,
  9. updatePersonProfileApi,
  10. updatePetProfileApi,
  11. updateGoodsInfoApi,
  12. verifyMaintenanceCodeApi,
  13. uploadFile
  14. } from '@/services/api'
  15. const route = useRoute()
  16. const router = useRouter()
  17. const toast = useToast()
  18. // Pull QR code from route params
  19. const qrCode = ref(route.params.qrCode?.toString().trim() || '')
  20. const queryInput = ref(qrCode.value)
  21. const loading = reactive({
  22. info: false,
  23. saving: false,
  24. verifying: false,
  25. photo: false
  26. })
  27. const qrDetail = ref(null)
  28. const profile = ref(null)
  29. const infoStatus = reactive({
  30. state: qrCode.value ? 'loading' : 'idle',
  31. message: ''
  32. })
  33. const maintenanceCode = ref('')
  34. const maintenancePassed = ref(false)
  35. const showMaintenancePanel = ref(false)
  36. const showMaintenanceDialog = ref(false)
  37. const isEditing = ref(false)
  38. const showLocationDialog = ref(false)
  39. const showLocationViewDialog = ref(false)
  40. const personKeys = [
  41. 'photoUrl',
  42. 'name',
  43. 'gender',
  44. 'phone',
  45. 'specialNote',
  46. 'emergencyContactName',
  47. 'emergencyContactPhone',
  48. 'emergencyContactEmail'
  49. ]
  50. const petKeys = ['photoUrl', 'name', 'contactName', 'contactPhone', 'contactEmail', 'location', 'remark']
  51. const goodsKeys = ['photoUrl', 'name', 'contactName', 'contactPhone', 'contactEmail', 'location', 'remark']
  52. const formData = reactive({
  53. photoUrl: '',
  54. name: '',
  55. gender: 'unknown',
  56. phone: '',
  57. specialNote: '',
  58. emergencyContactName: '',
  59. emergencyContactPhone: '',
  60. emergencyContactEmail: '',
  61. remark: '',
  62. contactName: '',
  63. contactPhone: '',
  64. contactEmail: '',
  65. location: ''
  66. })
  67. const qrType = computed(() => qrDetail.value?.qrType || 'person')
  68. const isPerson = computed(() => qrType.value === 'person')
  69. const isPet = computed(() => qrType.value === 'pet')
  70. const isGoods = computed(() => qrType.value === 'goods')
  71. const hasProfile = computed(() => Boolean(profile.value))
  72. const isFirstFill = computed(() => Boolean(qrDetail.value) && !hasProfile.value)
  73. const heroTitle = computed(() => {
  74. if (!qrDetail.value) return 'Contact Card'
  75. if (isPerson.value) return 'Person Card'
  76. if (isPet.value) return 'Pet Card'
  77. if (isGoods.value) return 'Item Card'
  78. return 'Contact Card'
  79. })
  80. const parseError = (error) => {
  81. if (!error) return 'Request failed'
  82. if (typeof error === 'string') return error
  83. if (error.message) return error.message
  84. if (error.detail) return error.detail
  85. if (error.response?.data?.message) return error.response.data.message
  86. if (error.data?.message) return error.data.message
  87. if (error.message === undefined && error?.error) return error.error
  88. return 'Request failed'
  89. }
  90. const resetForm = (source) => {
  91. const allKeys = Array.from(new Set([...personKeys, ...petKeys, ...goodsKeys]))
  92. allKeys.forEach((key) => {
  93. if (key === 'gender') {
  94. formData[key] = 'unknown'
  95. return
  96. }
  97. formData[key] = ''
  98. })
  99. let keys = personKeys
  100. if (isPet.value) keys = petKeys
  101. if (isGoods.value) keys = goodsKeys
  102. keys.forEach((key) => {
  103. if (source && Object.prototype.hasOwnProperty.call(source, key)) {
  104. formData[key] = source[key] ?? (key === 'gender' ? 'unknown' : '')
  105. }
  106. })
  107. }
  108. const setDocumentTitle = () => {
  109. if (typeof document === 'undefined') return
  110. let defaultName = 'Person'
  111. if (isPet.value) defaultName = 'Pet'
  112. if (isGoods.value) defaultName = 'Item'
  113. const name = profile.value?.name || defaultName
  114. document.title = `${name} | Emergency QR`
  115. }
  116. const fetchQrDetails = async () => {
  117. if (!qrCode.value) return
  118. loading.info = true
  119. infoStatus.state = 'loading'
  120. infoStatus.message = ''
  121. try {
  122. const data = await fetchQrInfoApi(qrCode.value)
  123. qrDetail.value = data
  124. profile.value = data.info || null
  125. resetForm(profile.value)
  126. infoStatus.state = 'ready'
  127. setDocumentTitle()
  128. } catch (error) {
  129. infoStatus.state = 'error'
  130. infoStatus.message = parseError(error)
  131. } finally {
  132. loading.info = false
  133. }
  134. }
  135. const handleQrSubmit = () => {
  136. if (!queryInput.value.trim()) {
  137. toast.add({ severity: 'warn', summary: 'Notice', detail: 'Please enter a QR code first.', life: 2600 })
  138. return
  139. }
  140. router.push({ name: 'scan', params: { qrCode: queryInput.value.trim() } })
  141. }
  142. const handleVerifyMaintenance = async () => {
  143. if (!qrCode.value || !maintenanceCode.value) {
  144. toast.add({ severity: 'warn', summary: 'Notice', detail: 'Please enter the maintenance code.', life: 2400 })
  145. return
  146. }
  147. loading.verifying = true
  148. try {
  149. await verifyMaintenanceCodeApi({
  150. qrCode: qrCode.value,
  151. maintenanceCode: maintenanceCode.value
  152. })
  153. maintenancePassed.value = true
  154. showMaintenancePanel.value = false
  155. showMaintenanceDialog.value = false
  156. isEditing.value = true
  157. toast.add({ severity: 'success', summary: 'Verified', detail: 'Maintenance editing unlocked.', life: 2600 })
  158. // Scroll to the form the first time we collect data
  159. if (isFirstFill.value) {
  160. setTimeout(scrollToForm, 300)
  161. }
  162. } catch (error) {
  163. maintenancePassed.value = false
  164. toast.add({ severity: 'error', summary: 'Verification failed', detail: parseError(error), life: 3200 })
  165. } finally {
  166. loading.verifying = false
  167. }
  168. }
  169. const buildProfilePayload = () => {
  170. let keys = personKeys
  171. if (isPet.value) keys = petKeys
  172. if (isGoods.value) keys = goodsKeys
  173. const payload = {
  174. qrCode: qrCode.value
  175. }
  176. keys.forEach((key) => {
  177. payload[key] = formData[key] ?? ''
  178. })
  179. if (maintenanceCode.value) {
  180. payload.maintenanceCode = maintenanceCode.value
  181. }
  182. return payload
  183. }
  184. const handleSaveProfile = async () => {
  185. if (!qrCode.value) return
  186. loading.saving = true
  187. try {
  188. const payload = buildProfilePayload()
  189. let updater = updatePersonProfileApi
  190. if (isPet.value) updater = updatePetProfileApi
  191. if (isGoods.value) updater = updateGoodsInfoApi
  192. await updater(payload)
  193. toast.add({ severity: 'success', summary: 'Saved', detail: 'Profile has been updated.', life: 3000 })
  194. await fetchQrDetails()
  195. // Exit edit mode after a successful save
  196. isEditing.value = false
  197. maintenancePassed.value = false
  198. maintenanceCode.value = ''
  199. } catch (error) {
  200. toast.add({ severity: 'error', summary: 'Save failed', detail: parseError(error), life: 3200 })
  201. } finally {
  202. loading.saving = false
  203. }
  204. }
  205. const photoInputRef = ref(null)
  206. const triggerPhotoPicker = () => {
  207. photoInputRef.value?.click?.()
  208. }
  209. const handleImageError = (event) => {
  210. // Handle broken image preview
  211. console.warn('Image load failed:', formData.photoUrl)
  212. event.target.style.display = 'none'
  213. }
  214. const handlePhotoChange = async (event) => {
  215. const file = event.target.files?.[0]
  216. if (!file) return
  217. // Validate mime type
  218. if (!file.type.startsWith('image/')) {
  219. toast.add({ severity: 'warn', summary: 'Notice', detail: 'Please choose an image file.', life: 2400 })
  220. return
  221. }
  222. // Validate file size (limit 15MB)
  223. const maxSize = 15 * 1024 * 1024
  224. if (file.size > maxSize) {
  225. toast.add({ severity: 'warn', summary: 'Notice', detail: 'Image size cannot exceed 15MB.', life: 2400 })
  226. return
  227. }
  228. loading.photo = true
  229. try {
  230. const response = await uploadFile(file)
  231. // Normalize upload response shape
  232. const url = response?.data?.url || response?.url || ''
  233. if (url) {
  234. formData.photoUrl = url
  235. toast.add({ severity: 'success', summary: 'Uploaded', detail: 'Photo has been updated.', life: 2400 })
  236. } else {
  237. throw new Error('Image URL not found')
  238. }
  239. } catch (error) {
  240. toast.add({ severity: 'error', summary: 'Upload failed', detail: parseError(error), life: 3200 })
  241. } finally {
  242. loading.photo = false
  243. // Reset input so the same file can trigger change
  244. if (event.target) {
  245. event.target.value = ''
  246. }
  247. }
  248. }
  249. const scrollToForm = () => {
  250. const el = document.getElementById('scan-form-section')
  251. if (el) {
  252. el.scrollIntoView({ behavior: 'smooth', block: 'start' })
  253. }
  254. }
  255. const callNumber = (phone) => {
  256. if (!phone) return
  257. window.location.href = `tel:${phone}`
  258. }
  259. const sendEmail = (email) => {
  260. if (!email) return
  261. window.location.href = `mailto:${email}`
  262. }
  263. const copyInfo = async (value, label = 'info') => {
  264. const content = value?.toString().trim()
  265. if (!content) {
  266. toast.add({ severity: 'info', summary: 'Notice', detail: `Nothing to copy for ${label}.`, life: 2200 })
  267. return
  268. }
  269. try {
  270. if (navigator?.clipboard?.writeText) {
  271. await navigator.clipboard.writeText(content)
  272. } else {
  273. const textarea = document.createElement('textarea')
  274. textarea.value = content
  275. textarea.style.position = 'fixed'
  276. textarea.style.opacity = '0'
  277. document.body.appendChild(textarea)
  278. textarea.focus()
  279. textarea.select()
  280. document.execCommand('copy')
  281. document.body.removeChild(textarea)
  282. }
  283. toast.add({ severity: 'success', summary: 'Copied', detail: `${label} copied to clipboard.`, life: 2000 })
  284. } catch (error) {
  285. console.error('Copy failed', error)
  286. toast.add({ severity: 'error', summary: 'Copy failed', detail: 'Please copy manually.', life: 2200 })
  287. }
  288. }
  289. const resetPageState = () => {
  290. qrDetail.value = null
  291. profile.value = null
  292. maintenanceCode.value = ''
  293. maintenancePassed.value = false
  294. showMaintenancePanel.value = false
  295. showMaintenanceDialog.value = false
  296. isEditing.value = false
  297. infoStatus.state = qrCode.value ? 'loading' : 'idle'
  298. infoStatus.message = ''
  299. resetForm()
  300. }
  301. watch(
  302. () => route.params.qrCode,
  303. (value) => {
  304. const normalized = value?.toString().trim() || ''
  305. queryInput.value = normalized
  306. qrCode.value = normalized
  307. resetPageState()
  308. if (normalized) {
  309. fetchQrDetails()
  310. }
  311. },
  312. { immediate: true }
  313. )
  314. watch([qrType, profile], () => {
  315. resetForm(profile.value)
  316. })
  317. watch([qrType, () => profile.value?.name], () => {
  318. setDocumentTitle()
  319. })
  320. // Auto open verification dialog on first fill
  321. watch(isFirstFill, (value) => {
  322. if (value && !maintenancePassed.value) {
  323. showMaintenanceDialog.value = true
  324. }
  325. })
  326. const handleOpenLocationDialog = () => {
  327. showLocationDialog.value = true
  328. }
  329. const handleSaveLocation = (location) => {
  330. formData.location = location
  331. toast.add({ severity: 'success', summary: 'Saved', detail: 'Address selected.', life: 2400 })
  332. }
  333. const handleOpenLocationView = () => {
  334. showLocationViewDialog.value = true
  335. }
  336. const openGoogleMaps = () => {
  337. const location = isPerson.value ? null : profile.value?.location
  338. if (!location) return
  339. // Try to parse coordinate format (lat, lng)
  340. const coordsMatch = location.match(/(-?\d+\.?\d*),\s*(-?\d+\.?\d*)/)
  341. if (coordsMatch) {
  342. const lat = coordsMatch[1]
  343. const lng = coordsMatch[2]
  344. // Open Google Maps with coordinates
  345. window.open(`https://www.google.com/maps?q=${lat},${lng}`, '_blank')
  346. } else {
  347. // Otherwise search by address string
  348. window.open(`https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(location)}`, '_blank')
  349. }
  350. }
  351. onMounted(() => {
  352. if (!qrCode.value) {
  353. setDocumentTitle()
  354. }
  355. })
  356. </script>
  357. <template>
  358. <div class="scan-page min-h-screen bg-slate-950 text-slate-100">
  359. <div class="relative isolate px-4 py-10 sm:px-6 lg:px-8">
  360. <div
  361. class="pointer-events-none absolute inset-x-0 top-0 -z-10 h-96 bg-gradient-to-br from-cyan-500/20 via-indigo-500/10 to-transparent blur-3xl" />
  362. <div class="mx-auto max-w-6xl space-y-8">
  363. <header class="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
  364. <div>
  365. <p class="text-xs uppercase tracking-[0.4em] text-cyan-200">Qr emergency link</p>
  366. <h1 class="mt-2 text-3xl font-semibold text-white sm:text-4xl">
  367. {{ heroTitle }}
  368. </h1>
  369. </div>
  370. </header>
  371. <section v-if="!qrCode"
  372. class="rounded-3xl border border-white/10 bg-white/5 p-6 shadow-xl shadow-cyan-500/10 backdrop-blur">
  373. <div class="flex items-start gap-4">
  374. <div class="flex h-12 w-12 items-center justify-center rounded-2xl bg-cyan-500/20">
  375. <i class="pi pi-qrcode text-2xl text-cyan-300" />
  376. </div>
  377. <div class="flex-1">
  378. <p class="text-lg font-semibold text-white">Welcome to the Emergency QR system</p>
  379. <p class="mt-2 text-sm text-slate-300">
  380. Scan the QR code or enter its value below to continue.
  381. </p>
  382. </div>
  383. </div>
  384. <div class="mt-6 flex flex-col gap-4 sm:flex-row sm:items-start">
  385. <div class="flex-1 space-y-2">
  386. <input v-model="queryInput" type="text" placeholder="Enter QR code"
  387. class="w-full rounded-2xl border border-white/20 bg-white/5 px-5 py-3.5 text-base text-white placeholder:text-slate-400 backdrop-blur-sm transition-all duration-200 focus:border-cyan-400 focus:bg-white/10 focus:outline-none focus:ring-2 focus:ring-cyan-400/30"
  388. @keyup.enter="handleQrSubmit" />
  389. <div class="flex items-center gap-2 px-1">
  390. <svg class="h-4 w-4 flex-shrink-0 text-cyan-400/70" fill="none" viewBox="0 0 24 24"
  391. stroke="currentColor">
  392. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
  393. d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
  394. </svg>
  395. <span class="text-sm text-slate-400/90">Example: <span
  396. class="font-mono text-slate-300/80">QRMIH76J350CE2CF6150A51F4E</span></span>
  397. </div>
  398. </div>
  399. <button type="button"
  400. class="rounded-2xl bg-gradient-to-r from-cyan-400 to-blue-400 px-8 py-3.5 text-base font-semibold text-slate-900 shadow-lg shadow-cyan-500/30 transition-all duration-200 hover:scale-105 hover:shadow-xl hover:shadow-cyan-500/40 sm:whitespace-nowrap"
  401. @click="handleQrSubmit">
  402. View info
  403. </button>
  404. </div>
  405. </section>
  406. <section v-else class="space-y-6">
  407. <div v-if="infoStatus.state === 'error'" class="rounded-3xl border border-red-500/40 bg-red-500/10 p-5">
  408. <p class="text-sm text-red-100">{{ infoStatus.message }}</p>
  409. <p class="mt-2 text-xs text-red-200">Please double-check the QR code or reach out for assistance.</p>
  410. </div>
  411. <div v-if="loading.info" class="rounded-3xl border border-white/10 bg-white/5 p-6 text-base text-white">
  412. Loading QR information...
  413. </div>
  414. <!-- Only render details after maintenance verification -->
  415. <div v-if="infoStatus.state === 'ready' && qrDetail && (maintenancePassed || hasProfile)" class="space-y-6">
  416. <div v-if="hasProfile && !isEditing"
  417. class="rounded-3xl border border-white/10 bg-white text-slate-900 shadow-2xl shadow-cyan-500/10 relative">
  418. <template v-if="hasProfile">
  419. <div class="space-y-6 p-6">
  420. <div class="flex flex-col items-center gap-4">
  421. <div class="relative w-full max-w-md overflow-hidden rounded-2xl bg-slate-100 ring-2 ring-slate-200"
  422. style="aspect-ratio: 1/1;">
  423. <img v-if="profile?.photoUrl" :src="profile.photoUrl" alt="profile"
  424. class="h-full w-full object-cover transition-transform duration-300 hover:scale-105"
  425. @error="(e) => e.target.style.display = 'none'" />
  426. <div v-else class="flex h-full w-full flex-col items-center justify-center gap-2 text-slate-400">
  427. <i class="pi pi-image text-4xl" />
  428. <span class="text-sm">No photo</span>
  429. </div>
  430. <!-- Edit button -->
  431. <button v-if="!isEditing" type="button"
  432. class="absolute top-3 right-3 flex h-9 w-9 items-center justify-center rounded-full bg-white/90 backdrop-blur-sm text-slate-600 hover:bg-white hover:text-slate-900 transition-all shadow-lg hover:shadow-xl"
  433. @click="showMaintenanceDialog = true" title="Edit profile">
  434. <i class="pi pi-pencil text-xs" />
  435. </button>
  436. </div>
  437. <p class="text-xs uppercase tracking-[0.4em] text-slate-400">
  438. {{ qrCode }}
  439. </p>
  440. </div>
  441. <div class="space-y-4">
  442. <div>
  443. <p class="mt-1 text-sm text-slate-500">
  444. {{ isPerson ? 'Emergency contact card' : isGoods ? 'Item information' : 'Pet information' }}
  445. </p>
  446. <p class="text-2xl font-semibold text-slate-900">
  447. {{ profile?.name || 'Unnamed' }}
  448. </p>
  449. </div>
  450. <div
  451. class="rounded-2xl border border-slate-200 bg-gradient-to-br from-slate-50 to-white p-5 transition hover:shadow-md">
  452. <div class="flex items-center gap-3">
  453. <div
  454. class="flex h-12 w-12 items-center justify-center rounded-full bg-emerald-100 shadow-inner">
  455. <i class="pi pi-user text-lg text-emerald-600" />
  456. </div>
  457. <p class="text-[11px] font-semibold uppercase tracking-[0.35em] text-slate-500">{{ isPerson ?
  458. 'EMERGENCY CONTACT' : 'CONTACT' }}</p>
  459. </div>
  460. <p class="mt-3 cursor-pointer text-3xl font-semibold leading-tight text-slate-800" title="Click to copy name"
  461. @click="copyInfo(isPerson ? profile?.emergencyContactName : profile?.contactName, 'Name')">
  462. {{ isPerson ? profile?.emergencyContactName || '-' : profile?.contactName || '-' }}
  463. </p>
  464. <div class="mt-4 space-y-3 text-sm text-slate-600">
  465. <div class="flex items-center gap-3">
  466. <i class="pi pi-phone text-lg text-slate-500" />
  467. <div class="leading-tight cursor-pointer select-text text-slate-600" title="Click to copy phone"
  468. @click="copyInfo(isPerson ? profile?.emergencyContactPhone : profile?.contactPhone, 'Phone')">
  469. <p class="text-[10px] uppercase tracking-[0.4em] text-slate-400">PHONE</p>
  470. <p class="text-base font-semibold text-slate-700">
  471. {{ isPerson ? profile?.emergencyContactPhone || '-' : profile?.contactPhone || '-' }}
  472. </p>
  473. </div>
  474. </div>
  475. <div v-if="(isPerson ? profile?.emergencyContactEmail : profile?.contactEmail)"
  476. class="flex items-center gap-3">
  477. <i class="pi pi-envelope text-lg text-slate-500" />
  478. <div class="leading-tight min-w-0 cursor-pointer select-text text-slate-600" title="Click to copy email"
  479. @click="copyInfo(isPerson ? profile?.emergencyContactEmail : profile?.contactEmail, 'Email')">
  480. <p class="text-[10px] uppercase tracking-[0.4em] text-slate-400">EMAIL</p>
  481. <p class="truncate text-base font-semibold text-slate-700">
  482. {{ isPerson ? profile?.emergencyContactEmail : profile?.contactEmail }}
  483. </p>
  484. </div>
  485. </div>
  486. <div v-if="!isPerson && profile?.location" class="flex items-start gap-3">
  487. <i class="pi pi-map-marker text-lg text-slate-500 mt-0.5 flex-shrink-0" />
  488. <div class="leading-tight cursor-pointer select-text text-slate-600" title="Click to copy address"
  489. @click="copyInfo(profile?.location, 'Address')">
  490. <p class="text-[10px] uppercase tracking-[0.4em] text-slate-400">LOCATION</p>
  491. <p class="flex-1 break-words whitespace-normal text-base font-medium text-slate-700">
  492. {{ profile.location }}
  493. </p>
  494. </div>
  495. </div>
  496. </div>
  497. <div class="mt-4 grid grid-cols-2 gap-2">
  498. <button v-if="profile?.emergencyContactPhone || profile?.contactPhone" type="button"
  499. class="inline-flex w-full items-center justify-center gap-1.5 rounded-full border border-emerald-200 bg-emerald-50 px-3 py-1.5 text-xs font-medium text-emerald-700 transition hover:bg-emerald-100"
  500. @click="callNumber(isPerson ? profile?.emergencyContactPhone : profile?.contactPhone)">
  501. <i class="pi pi-phone" /> Call
  502. </button>
  503. <button
  504. v-if="(isPerson && profile?.emergencyContactEmail) || (!isPerson && profile?.contactEmail)"
  505. type="button"
  506. class="inline-flex w-full items-center justify-center gap-1.5 rounded-full border border-blue-200 bg-blue-50 px-3 py-1.5 text-xs font-medium text-blue-700 transition hover:bg-blue-100"
  507. @click="sendEmail(isPerson ? profile?.emergencyContactEmail : profile?.contactEmail)">
  508. <i class="pi pi-envelope" /> Email
  509. </button>
  510. <button v-if="!isPerson && profile?.location" type="button"
  511. class="inline-flex w-full items-center justify-center gap-1.5 rounded-full border border-purple-200 bg-purple-50 px-3 py-1.5 text-xs font-medium text-purple-700 transition hover:bg-purple-100"
  512. @click="handleOpenLocationView">
  513. <i class="pi pi-map" /> Map
  514. </button>
  515. <button v-if="!isPerson && profile?.location" type="button"
  516. class="inline-flex w-full items-center justify-center gap-1.5 rounded-full border border-red-200 bg-red-50 px-3 py-1.5 text-xs font-medium text-red-700 transition hover:bg-red-100"
  517. @click="openGoogleMaps">
  518. <i class="pi pi-external-link" /> Google Maps
  519. </button>
  520. </div>
  521. </div>
  522. <div class="rounded-2xl border border-amber-200 bg-gradient-to-br from-amber-50 to-white p-5">
  523. <div class="flex items-center gap-2">
  524. <div class="flex h-8 w-8 items-center justify-center rounded-full bg-amber-100">
  525. <i class="pi pi-info-circle text-sm text-amber-600" />
  526. </div>
  527. <p class="text-xs font-medium uppercase tracking-wider text-slate-500">{{ isPerson ? 'Additional notes' :
  528. isGoods ? 'Item notes' : 'Extra description' }}</p>
  529. </div>
  530. <p class="mt-3 whitespace-pre-wrap text-sm leading-relaxed text-slate-700">
  531. {{ isPerson ? (profile?.specialNote || 'No extra details') : (profile?.remark || 'No extra details') }}
  532. </p>
  533. </div>
  534. </div>
  535. </div>
  536. </template>
  537. </div>
  538. </div>
  539. </section>
  540. <section v-if="qrCode && maintenancePassed" id="scan-form-section"
  541. class="rounded-3xl border border-white/10 bg-white p-6 text-slate-900 shadow-2xl shadow-cyan-500/10">
  542. <div class="flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
  543. <div>
  544. <p class="text-xs uppercase tracking-[0.3em] text-slate-400">Profile setup</p>
  545. <h2 class="mt-1 text-2xl font-semibold text-slate-900">
  546. {{ hasProfile ? 'Update profile' : 'First-time setup' }}
  547. </h2>
  548. </div>
  549. <p class="text-sm text-slate-500">
  550. QR Code: <span class="font-mono text-slate-700">{{ qrCode }}</span>
  551. </p>
  552. </div>
  553. <div v-if="isFirstFill" class="mt-4 rounded-2xl border border-emerald-200 bg-emerald-50 p-4">
  554. <div class="flex items-start gap-3">
  555. <i class="pi pi-info-circle text-emerald-600" />
  556. <div class="flex-1 text-sm text-emerald-700">
  557. <p class="font-semibold">First-time tip</p>
  558. <p class="mt-1">
  559. This is the first record for this QR code. Once submitted, it becomes active. Keep the maintenance code
  560. safe for future edits.
  561. </p>
  562. </div>
  563. </div>
  564. </div>
  565. <div v-else class="mt-4 rounded-2xl border border-cyan-200 bg-cyan-50 p-4">
  566. <div class="flex items-start gap-3">
  567. <i class="pi pi-check-circle text-cyan-600" />
  568. <div class="flex-1 text-sm text-cyan-700">
  569. <p class="font-semibold">Edit mode</p>
  570. <p class="mt-1">
  571. Maintenance code verified. You can now edit the profile—remember to save your changes.
  572. </p>
  573. </div>
  574. </div>
  575. </div>
  576. <form class="mt-6 space-y-6" @submit.prevent="handleSaveProfile">
  577. <div class="space-y-4">
  578. <div>
  579. <p class="text-sm font-medium text-slate-700">Display photo / item image</p>
  580. <p class="mt-1 text-xs text-slate-500">Recommended 640×640+, supports JPG/PNG, up to 15MB.</p>
  581. </div>
  582. <div
  583. class="relative w-full max-w-md mx-auto overflow-hidden rounded-2xl bg-slate-100 ring-2 ring-slate-200"
  584. style="aspect-ratio: 1/1;">
  585. <img v-if="formData.photoUrl && !loading.photo" :src="formData.photoUrl" alt="profile"
  586. class="h-full w-full object-cover transition-opacity duration-200" @error="handleImageError" />
  587. <div v-if="loading.photo" class="flex h-full w-full items-center justify-center">
  588. <i class="pi pi-spin pi-spinner text-4xl text-slate-400" />
  589. </div>
  590. <div v-else-if="!formData.photoUrl"
  591. class="flex h-full w-full flex-col items-center justify-center gap-2 text-slate-400">
  592. <i class="pi pi-image text-5xl" />
  593. <span class="text-sm">No image</span>
  594. </div>
  595. </div>
  596. <input ref="photoInputRef" type="file" accept="image/jpeg,image/jpg,image/png,image/webp" class="hidden"
  597. @change="handlePhotoChange" />
  598. <div class="flex justify-center gap-3">
  599. <button type="button"
  600. class="rounded-full border border-slate-300 bg-white px-6 py-2.5 text-sm font-medium text-slate-700 transition hover:bg-slate-50 disabled:opacity-50"
  601. :disabled="loading.photo" @click="triggerPhotoPicker">
  602. <i class="pi mr-2" :class="loading.photo ? 'pi-spin pi-spinner' : 'pi-upload'" />
  603. {{ loading.photo ? 'Uploading...' : 'Upload image' }}
  604. </button>
  605. <button v-if="formData.photoUrl" type="button"
  606. class="rounded-full border border-red-200 bg-red-50 px-4 py-2.5 text-sm font-medium text-red-600 transition hover:bg-red-100"
  607. @click="formData.photoUrl = ''">
  608. <i class="pi pi-times mr-1" />
  609. Clear image
  610. </button>
  611. </div>
  612. </div>
  613. <div class="grid gap-4 md:grid-cols-2">
  614. <label class="space-y-2 text-sm">
  615. <span class="text-slate-500">{{ isPerson ? 'Name' : isGoods ? 'Item name' : 'Pet name' }}</span>
  616. <input v-model="formData.name" type="text" class="w-full rounded-2xl border border-slate-200 px-4 py-3"
  617. :placeholder="isPerson ? 'e.g. John Doe' : isGoods ? 'e.g. MacBook Pro' : 'e.g. Snowy'" required />
  618. </label>
  619. <label v-if="isPerson" class="space-y-2 text-sm">
  620. <span class="text-slate-500">Gender</span>
  621. <div class="flex gap-2">
  622. <button v-for="option in [
  623. { label: 'Male', value: 'male' },
  624. { label: 'Female', value: 'female' },
  625. { label: 'Prefer not to say', value: 'unknown' }
  626. ]" :key="option.value" type="button" class="flex-1 rounded-2xl border px-4 py-3 text-sm"
  627. :class="formData.gender === option.value ? 'border-slate-900 bg-slate-900 text-white' : 'border-slate-200 text-slate-500'"
  628. @click="formData.gender = option.value">
  629. {{ option.label }}
  630. </button>
  631. </div>
  632. </label>
  633. </div>
  634. <div v-if="isPerson" class="grid gap-4 md:grid-cols-2">
  635. <label class="space-y-2 text-sm">
  636. <span class="text-slate-500">Owner phone</span>
  637. <input v-model="formData.phone" type="tel" class="w-full rounded-2xl border border-slate-200 px-4 py-3"
  638. required />
  639. </label>
  640. <label class="space-y-2 text-sm">
  641. <span class="text-slate-500">Emergency contact name</span>
  642. <input v-model="formData.emergencyContactName" type="text"
  643. class="w-full rounded-2xl border border-slate-200 px-4 py-3" required />
  644. </label>
  645. <label class="space-y-2 text-sm">
  646. <span class="text-slate-500">Emergency contact phone</span>
  647. <input v-model="formData.emergencyContactPhone" type="tel"
  648. class="w-full rounded-2xl border border-slate-200 px-4 py-3" required />
  649. </label>
  650. <label class="space-y-2 text-sm md:col-span-2">
  651. <span class="text-slate-500">Emergency contact email</span>
  652. <input v-model="formData.emergencyContactEmail" type="email"
  653. class="w-full rounded-2xl border border-slate-200 px-4 py-3" placeholder="Optional" />
  654. </label>
  655. </div>
  656. <div v-else class="grid gap-4 md:grid-cols-2">
  657. <label class="space-y-2 text-sm">
  658. <span class="text-slate-500">Contact name</span>
  659. <input v-model="formData.contactName" type="text"
  660. class="w-full rounded-2xl border border-slate-200 px-4 py-3" required />
  661. </label>
  662. <label class="space-y-2 text-sm">
  663. <span class="text-slate-500">Contact phone</span>
  664. <input v-model="formData.contactPhone" type="tel"
  665. class="w-full rounded-2xl border border-slate-200 px-4 py-3" required />
  666. </label>
  667. <label class="space-y-2 text-sm md:col-span-2">
  668. <span class="text-slate-500">Contact email</span>
  669. <input v-model="formData.contactEmail" type="email"
  670. class="w-full rounded-2xl border border-slate-200 px-4 py-3" placeholder="Optional" />
  671. </label>
  672. <label class="space-y-2 text-sm md:col-span-2">
  673. <span class="text-slate-500">Contact address</span>
  674. <div class="flex gap-2">
  675. <input v-model="formData.location" type="text" readonly
  676. class="flex-1 rounded-2xl border border-slate-200 px-4 py-3 bg-slate-50 cursor-pointer"
  677. placeholder="Click to select address" @click="handleOpenLocationDialog" />
  678. <button type="button"
  679. class="rounded-2xl border border-slate-300 bg-white px-6 py-3 text-sm font-medium text-slate-700 transition hover:bg-slate-50 whitespace-nowrap"
  680. @click="handleOpenLocationDialog">
  681. <i class="pi pi-map-marker mr-2" />
  682. Select address
  683. </button>
  684. </div>
  685. </label>
  686. </div>
  687. <label class="block space-y-2 text-sm">
  688. <span class="text-slate-500">{{ isPerson ? 'Additional notes / health tips' : isGoods ? 'Item notes' : 'Extra remarks' }}</span>
  689. <textarea v-if="isPerson" v-model="formData.specialNote" rows="4"
  690. class="w-full rounded-2xl border border-slate-200 px-4 py-3" placeholder="e.g. allergies, medical needs, carried items" />
  691. <textarea v-else v-model="formData.remark" rows="4"
  692. class="w-full rounded-2xl border border-slate-200 px-4 py-3"
  693. :placeholder="isGoods ? 'e.g. item features, usage notes, handling tips' : 'e.g. pet habits, health info, behavior notes'" />
  694. </label>
  695. <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
  696. <p class="text-sm text-slate-500">
  697. Submitted data will sync to the public scan page.
  698. </p>
  699. <button type="submit"
  700. class="rounded-full bg-slate-900 px-8 py-3 text-sm font-semibold text-white hover:bg-slate-800"
  701. :disabled="loading.saving">
  702. {{ loading.saving ? 'Saving...' : hasProfile ? 'Save changes' : 'Submit profile' }}
  703. </button>
  704. </div>
  705. </form>
  706. </section>
  707. <footer class="mt-8 border-t border-white/10 pt-6 text-center">
  708. <p class="text-xs text-slate-400">
  709. Data is used solely for emergency contact scenarios. Uploading means you consent to display it when scanned.
  710. </p>
  711. <p class="mt-2 text-xs text-slate-500">
  712. <i class="pi pi-shield text-cyan-400" /> Your privacy stays protected
  713. </p>
  714. </footer>
  715. </div>
  716. </div>
  717. <!-- Location picker dialog -->
  718. <LocationPicker v-model="showLocationDialog" :initial-location="formData.location" @save="handleSaveLocation" />
  719. <!-- Location preview dialog (read-only) -->
  720. <LocationPicker v-model="showLocationViewDialog" :initial-location="isPerson ? null : profile?.location"
  721. :closable="true" :readonly="true" />
  722. <!-- Maintenance code dialog -->
  723. <Dialog v-model:visible="showMaintenanceDialog" modal :closable="!isFirstFill" :closeOnEscape="!isFirstFill"
  724. :dismissableMask="!isFirstFill" :style="{ width: '90vw', maxWidth: '450px' }" :draggable="false">
  725. <template #header>
  726. <div class="flex items-center gap-3">
  727. <div class="flex h-10 w-10 items-center justify-center rounded-full bg-cyan-100">
  728. <i class="pi pi-lock text-lg text-cyan-600" />
  729. </div>
  730. <div>
  731. <h3 class="text-lg font-semibold text-slate-900">
  732. {{ isFirstFill ? 'Verification required for first use' : 'Verification required to edit' }}
  733. </h3>
  734. <p class="text-sm text-slate-500">
  735. {{ isFirstFill ? 'Enter the maintenance code that came with the QR tag.' : 'Enter the maintenance code to unlock editing.' }}
  736. </p>
  737. </div>
  738. </div>
  739. </template>
  740. <div class="space-y-4 py-4">
  741. <div>
  742. <label class="mb-2 block text-sm font-medium text-slate-700">Maintenance code</label>
  743. <input v-model="maintenanceCode" type="text" maxlength="8"
  744. class="w-full rounded-xl border border-slate-300 px-4 py-3 text-slate-900 placeholder:text-slate-400 focus:border-cyan-500 focus:outline-none focus:ring-2 focus:ring-cyan-500/20"
  745. placeholder="Enter the maintenance code" @keyup.enter="handleVerifyMaintenance" autofocus />
  746. </div>
  747. <div v-if="isFirstFill" class="rounded-xl border border-emerald-200 bg-emerald-50 p-3">
  748. <div class="flex items-start gap-2">
  749. <i class="pi pi-info-circle text-sm text-emerald-600" />
  750. <p class="text-xs text-emerald-700">
  751. This is the first record for this QR code. Verify to begin entering details.
  752. </p>
  753. </div>
  754. </div>
  755. </div>
  756. <template #footer>
  757. <div class="flex justify-end gap-3">
  758. <button v-if="!isFirstFill" type="button"
  759. class="rounded-xl border border-slate-300 px-5 py-2.5 text-sm font-medium text-slate-700 transition hover:bg-slate-50"
  760. @click="showMaintenanceDialog = false">
  761. Cancel
  762. </button>
  763. <button type="button"
  764. class="rounded-xl bg-cyan-600 px-6 py-2.5 text-sm font-semibold text-white transition hover:bg-cyan-700 disabled:opacity-50"
  765. :disabled="loading.verifying || !maintenanceCode" @click="handleVerifyMaintenance">
  766. {{ loading.verifying ? 'Verifying...' : 'Verify' }}
  767. </button>
  768. </div>
  769. </template>
  770. </Dialog>
  771. </div>
  772. </template>
  773. <style scoped>
  774. .scan-page {
  775. background-image: radial-gradient(circle at 20% 20%, rgba(14, 165, 233, 0.12), transparent 45%),
  776. radial-gradient(circle at 80% 0%, rgba(129, 140, 248, 0.12), transparent 50%),
  777. linear-gradient(135deg, #020617, #030712);
  778. }
  779. .fade-enter-active,
  780. .fade-leave-active {
  781. transition: opacity 0.3s ease;
  782. }
  783. .fade-enter-from,
  784. .fade-leave-to {
  785. opacity: 0;
  786. }
  787. </style>