-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile.php
More file actions
704 lines (627 loc) · 40.4 KB
/
Copy pathmobile.php
File metadata and controls
704 lines (627 loc) · 40.4 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<?php
// 减肥记录管理系统 - 移动端页面
session_start();
require_once 'config.php';
require_once 'includes/WeightService.php';
// 检测设备类型,如果是PC端访问,重定向到PC端页面
if (!isMobileDevice() && !isTabletDevice()) {
header('Location: index.php');
exit();
}
// 初始化服务
$service = new WeightService();
$service->initialize();
// 获取数据 - 默认只加载最近90天,优化初始加载性能
$records = $service->getRecentRecords(90);
$stats = $service->getStatistics($records);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<title>减肥记录</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
<script>if(typeof Chart==='undefined'){var s=document.createElement('script');s.src='js/chart.js';document.head.appendChild(s);}</script>
<script>
tailwind.config = {
theme: {
extend: {
fontFamily: {
sans: ['Plus Jakarta Sans', 'sans-serif'],
},
colors: {
primary: {
50: '#eff6ff',
100: '#dbeafe',
200: '#bfdbfe',
300: '#93c5fd',
400: '#60a5fa',
500: '#3b82f6',
600: '#2563eb',
700: '#1d4ed8',
800: '#1e40af',
900: '#1e3a8a',
}
},
boxShadow: {
'soft': '0 4px 20px -2px rgba(0, 0, 0, 0.05)',
'glow': '0 0 15px rgba(59, 130, 246, 0.5)',
}
}
}
}
</script>
<style>
body { font-family: 'Plus Jakarta Sans', sans-serif; -webkit-tap-highlight-color: transparent; }
.safe-bottom { padding-bottom: env(safe-area-inset-bottom); }
.glass { background: rgba(255, 255, 255, 0.1); backdrop-filter: blur(10px); border: 1px solid rgba(255, 255, 255, 0.2); }
.hide-scrollbar::-webkit-scrollbar { display: none; }
.hide-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
</style>
</head>
<body class="bg-slate-50 text-slate-800 min-h-screen pb-24 selection:bg-primary-200">
<!-- 顶部导航 -->
<header class="relative bg-gradient-to-br from-blue-600 to-indigo-700 text-white pt-14 pb-8 px-5 rounded-b-[2.5rem] shadow-xl shadow-indigo-500/20 mb-8 overflow-hidden">
<!-- 装饰背景 -->
<div class="absolute top-0 right-0 -mr-16 -mt-16 w-64 h-64 rounded-full bg-white/10 blur-3xl pointer-events-none"></div>
<div class="absolute bottom-0 left-0 -ml-16 -mb-16 w-48 h-48 rounded-full bg-blue-400/20 blur-2xl pointer-events-none"></div>
<div class="relative z-10 flex justify-between items-center mb-6">
<div>
<p class="text-blue-100 text-xs font-medium tracking-wider uppercase mb-1">Welcome Back</p>
<h1 class="text-2xl font-bold tracking-tight">健康记录</h1>
</div>
<div class="bg-white/20 p-2.5 rounded-full backdrop-blur-md border border-white/10 shadow-lg">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-white" 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>
</div>
</div>
<!-- 概览卡片 -->
<div class="relative z-10 grid grid-cols-2 gap-3">
<!-- 早晨变化 -->
<div class="glass rounded-2xl p-4 flex flex-col justify-between h-24 relative overflow-hidden group">
<div class="absolute right-0 top-0 p-2 opacity-10 group-hover:opacity-20 transition-opacity">
<svg class="w-12 h-12" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M10 2a4 4 0 00-4 4v1H5a1 1 0 00-.994.89l-1 9A1 1 0 004 18h12a1 1 0 001-1.11l-1-9A1 1 0 0015 7h-1V6a4 4 0 00-4-4zm2 5V6a2 2 0 10-4 0v1h4zm-6 3a1 1 0 112 0 1 1 0 01-2 0zm7-1a1 1 0 100 2 1 1 0 000-2z" clip-rule="evenodd"/></svg>
</div>
<p class="text-xs text-blue-100 font-medium">早晨变化</p>
<p class="text-2xl font-bold tracking-tight flex items-center gap-1" id="m_morning_diff_container">
<span id="m_morning_diff_value"><?php echo number_format(abs($stats['morning_diff']), 1); ?></span>
<span class="text-sm font-normal text-blue-200 ml-1">斤</span>
</p>
</div>
<!-- 晚上变化 -->
<div class="glass rounded-2xl p-4 flex flex-col justify-between h-24 relative overflow-hidden group">
<div class="absolute right-0 top-0 p-2 opacity-10 group-hover:opacity-20 transition-opacity">
<svg class="w-12 h-12" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M3 3a1 1 0 000 2v8a2 2 0 002 2h2.586l-1.293 1.293a1 1 0 101.414 1.414L10 15.414l2.293 2.293a1 1 0 001.414-1.414L12.414 15H15a2 2 0 002-2V5a1 1 0 100-2H3zm11.707 4.707a1 1 0 00-1.414-1.414L10 9.586 8.707 8.293a1 1 0 00-1.414 0l-2 2a1 1 0 101.414 1.414L8 10.414l1.293 1.293a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/></svg>
</div>
<p class="text-xs text-blue-100 font-medium">晚上变化</p>
<p class="text-2xl font-bold tracking-tight flex items-center gap-1" id="m_evening_diff_container">
<span id="m_evening_diff_value"><?php echo number_format(abs($stats['evening_diff']), 1); ?></span>
<span class="text-sm font-normal text-blue-200 ml-1">斤</span>
</p>
</div>
<!-- 记录天数 -->
<div class="glass rounded-2xl p-4 flex flex-col justify-between h-24 relative overflow-hidden group">
<div class="absolute right-0 top-0 p-2 opacity-10 group-hover:opacity-20 transition-opacity">
<svg class="w-12 h-12" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M6 2a1 1 0 00-1 1v1H4a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V6a2 2 0 00-2-2h-1V3a1 1 0 10-2 0v1H7V3a1 1 0 00-1-1zm0 5a1 1 0 000 2h8a1 1 0 100-2H6z" clip-rule="evenodd"/></svg>
</div>
<p class="text-xs text-blue-100 font-medium">记录天数</p>
<p class="text-2xl font-bold tracking-tight flex items-center gap-1">
<span id="m_total_records_value"><?php echo $stats['total_records']; ?></span>
<span class="text-sm font-normal text-blue-200 ml-1">天</span>
</p>
</div>
<!-- 最新晨晚差 -->
<div class="glass rounded-2xl p-4 flex flex-col justify-between h-24 relative overflow-hidden group">
<div class="absolute right-0 top-0 p-2 opacity-10 group-hover:opacity-20 transition-opacity">
<svg class="w-12 h-12" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm1-12a1 1 0 10-2 0v4a1 1 0 00.293.707l2.828 2.829a1 1 0 101.415-1.415L11 9.586V6z" clip-rule="evenodd"/></svg>
</div>
<p class="text-xs text-blue-100 font-medium">最新晨晚差</p>
<p class="text-2xl font-bold tracking-tight flex items-center gap-1" id="m_mve_container">
<span id="m_mve_value"><?php echo number_format(abs($stats['morning_vs_evening_latest']), 1); ?></span>
<span class="text-sm font-normal text-blue-200 ml-1">斤</span>
</p>
</div>
</div>
</header>
<main class="px-5 -mt-2">
<!-- 趋势图表 -->
<div class="bg-white rounded-[1.5rem] p-5 shadow-soft border border-slate-100 mb-8">
<div class="flex justify-between items-center mb-5">
<h2 class="font-bold text-slate-800 text-lg flex items-center">
<span class="w-1 h-5 bg-primary-500 rounded-full mr-2"></span>
体重趋势
</h2>
</div>
<div class="flex bg-slate-100 p-1.5 rounded-2xl mb-4">
<button onclick="mUpdateChartRange('7')" class="flex-1 py-2 text-xs font-semibold rounded-xl transition-all shadow-sm bg-white text-slate-800" id="m-btn-7">7天</button>
<button onclick="mUpdateChartRange('14')" class="flex-1 py-2 text-xs font-semibold rounded-xl text-slate-500 transition-all hover:text-slate-700" id="m-btn-14">14天</button>
<button onclick="mUpdateChartRange('30')" class="flex-1 py-2 text-xs font-semibold rounded-xl text-slate-500 transition-all hover:text-slate-700" id="m-btn-30">30天</button>
<button onclick="mUpdateChartRange('all')" class="flex-1 py-2 text-xs font-semibold rounded-xl text-slate-500 transition-all hover:text-slate-700" id="m-btn-all">全部</button>
</div>
<div class="h-60 w-full">
<canvas id="mWeightChart"></canvas>
</div>
</div>
<!-- 历史记录 -->
<div class="flex justify-between items-end mb-4 px-1">
<h2 class="font-bold text-slate-800 text-lg flex items-center">
<span class="w-1 h-5 bg-indigo-500 rounded-full mr-2"></span>
历史记录
</h2>
<span class="text-xs font-medium text-slate-400 bg-slate-100 px-2 py-1 rounded-lg">最近30天</span>
</div>
<div class="space-y-3 pb-28">
<?php if (empty($records)): ?>
<div class="bg-white rounded-[1.5rem] p-10 text-center shadow-soft border border-slate-50">
<div class="inline-flex items-center justify-center w-20 h-20 rounded-full bg-slate-50 mb-4 text-slate-300">
<svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
</div>
<p class="text-slate-500 font-medium">暂无记录</p>
<p class="text-slate-400 text-sm mt-1">点击右下角按钮开始记录</p>
</div>
<?php else: ?>
<?php foreach ($records as $record):
$isSunday = (date('w', strtotime($record['record_date'])) == 0);
$cardClass = $isSunday ? 'bg-orange-50/40 border-orange-100' : 'bg-white border-slate-100';
$weekdayClass = $isSunday ? 'text-orange-500 bg-orange-100' : 'text-indigo-500 bg-indigo-50';
?>
<div class="group <?php echo $cardClass; ?> rounded-2xl p-5 shadow-sm border hover:shadow-md transition-all active:scale-[0.98] cursor-pointer relative overflow-hidden" onclick='openEditModal(<?php echo htmlspecialchars(json_encode($record, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP|JSON_UNESCAPED_UNICODE), ENT_QUOTES, "UTF-8"); ?>)'>
<div class="absolute left-0 top-4 bottom-4 w-1 bg-gradient-to-b from-blue-400 to-indigo-500 rounded-r-full opacity-0 group-hover:opacity-100 transition-opacity"></div>
<div class="flex items-center justify-between">
<div>
<div class="flex items-baseline gap-2 mb-2">
<span class="text-lg font-bold text-slate-800"><?php echo date('m', strtotime($record['record_date'])); ?><span class="text-sm text-slate-400">/</span><?php echo date('d', strtotime($record['record_date'])); ?></span>
<span class="text-xs font-medium <?php echo $weekdayClass; ?> px-2 py-0.5 rounded-md"><?php echo zhWeekday($record['record_date']); ?></span>
</div>
<div class="flex gap-4 text-sm">
<div class="flex items-center gap-1.5">
<div class="w-2 h-2 rounded-full bg-blue-500"></div>
<span class="text-slate-500 text-xs">早</span>
<span class="font-bold text-slate-700"><?php echo $record['morning_weight'] > 0 ? $record['morning_weight'] : '<span class="text-slate-300">--</span>'; ?></span>
</div>
<div class="flex items-center gap-1.5">
<div class="w-2 h-2 rounded-full bg-purple-500"></div>
<span class="text-slate-500 text-xs">晚</span>
<span class="font-bold text-slate-700"><?php echo $record['evening_weight'] > 0 ? $record['evening_weight'] : '<span class="text-slate-300">--</span>'; ?></span>
</div>
</div>
</div>
<div class="w-8 h-8 rounded-full bg-slate-50 flex items-center justify-center text-slate-300 group-hover:bg-blue-50 group-hover:text-blue-500 transition-colors">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
</svg>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</main>
<!-- 悬浮添加按钮 -->
<button onclick="openAddModal()" class="fixed bottom-8 right-6 w-14 h-14 bg-gradient-to-r from-primary-600 to-primary-500 text-white rounded-full shadow-glow flex items-center justify-center active:scale-90 hover:scale-105 transition-all z-40 group">
<svg xmlns="http://www.w3.org/2000/svg" class="h-7 w-7 group-hover:rotate-90 transition-transform duration-300" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
</svg>
</button>
<!-- 模态框 -->
<div id="modal" class="fixed inset-0 z-50 hidden">
<div class="absolute inset-0 bg-slate-900/60 backdrop-blur-sm transition-opacity opacity-0" id="modal-backdrop" onclick="closeModal()"></div>
<div class="absolute bottom-0 left-0 right-0 bg-white rounded-t-[2rem] p-6 transform transition-transform duration-300 translate-y-full shadow-2xl" id="modal-content">
<div class="w-16 h-1.5 bg-slate-200 rounded-full mx-auto mb-8"></div>
<div class="flex justify-between items-center mb-8">
<h3 class="text-2xl font-bold text-slate-800" id="modal-title">记录体重</h3>
<button onclick="closeModal()" class="p-2 bg-slate-50 rounded-full text-slate-400 hover:bg-slate-100">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
</button>
</div>
<form id="recordForm" onsubmit="handleFormSubmit(event)">
<input type="hidden" name="action" id="formAction" value="add_record">
<input type="hidden" name="id" id="recordId">
<div class="space-y-6 mb-8">
<div>
<label class="block text-sm font-semibold text-slate-600 mb-2">日期</label>
<div class="relative">
<input type="date" name="record_date" id="record_date" value="<?php echo date('Y-m-d'); ?>" class="w-full bg-slate-50 border-0 ring-1 ring-slate-200 rounded-2xl py-4 px-5 text-slate-800 font-semibold focus:ring-2 focus:ring-primary-500 focus:bg-white transition-all outline-none">
<div class="absolute right-4 top-1/2 -translate-y-1/2 text-slate-400 pointer-events-none">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"></path></svg>
</div>
</div>
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-sm font-semibold text-slate-600 mb-2">早晨 (斤)</label>
<div class="relative">
<input type="number" step="0.01" name="morning_weight" id="morning_weight" placeholder="0.0" class="w-full bg-slate-50 border-0 ring-1 ring-slate-200 rounded-2xl py-4 px-5 text-slate-800 font-semibold focus:ring-2 focus:ring-blue-500 focus:bg-white transition-all outline-none">
<span class="absolute right-4 top-1/2 -translate-y-1/2 text-xs font-bold text-slate-400 bg-slate-100 px-2 py-1 rounded-md">早</span>
</div>
</div>
<div>
<label class="block text-sm font-semibold text-slate-600 mb-2">晚上 (斤)</label>
<div class="relative">
<input type="number" step="0.01" name="evening_weight" id="evening_weight" placeholder="0.0" class="w-full bg-slate-50 border-0 ring-1 ring-slate-200 rounded-2xl py-4 px-5 text-slate-800 font-semibold focus:ring-2 focus:ring-purple-500 focus:bg-white transition-all outline-none">
<span class="absolute right-4 top-1/2 -translate-y-1/2 text-xs font-bold text-slate-400 bg-slate-100 px-2 py-1 rounded-md">晚</span>
</div>
</div>
</div>
</div>
<div class="flex gap-4">
<button type="button" id="deleteBtn" onclick="deleteCurrentRecord()" class="hidden w-14 h-14 rounded-2xl bg-rose-50 text-rose-500 flex items-center justify-center hover:bg-rose-100 transition-colors">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
</button>
<button type="submit" class="flex-1 bg-gradient-to-r from-primary-600 to-primary-500 text-white py-4 rounded-2xl font-bold shadow-lg shadow-primary-500/30 active:scale-[0.98] transition-transform flex justify-center items-center gap-2">
<span>保存记录</span>
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
</button>
</div>
</form>
<div class="safe-bottom"></div>
</div>
</div>
<!-- Notification -->
<div id="notification" class="fixed top-6 left-6 right-6 z-[70] transform transition-all duration-300 -translate-y-[200%] opacity-0">
<div class="bg-white/90 backdrop-blur-md rounded-2xl shadow-xl border border-white/20 p-4 flex items-center">
<div id="notification-icon" class="mr-3 p-2 rounded-full shrink-0"></div>
<div id="notification-message" class="font-bold text-slate-800 text-sm"></div>
</div>
</div>
<script>
const mAllRecords = <?php echo json_encode($records, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP|JSON_UNESCAPED_UNICODE); ?>;
let mChart = null;
const mParseISO = (s) => { const p = s.split('-').map(Number); return new Date(p[0], p[1]-1, p[2]); };
const mComputeStats = (records) => {
const asc = records.slice().sort((a,b)=>mParseISO(a.record_date)-mParseISO(b.record_date));
const desc = asc.slice().reverse();
// Latest Morning/Evening
const lmRec = desc.find(r=>Number(r.morning_weight)>0);
const leRec = desc.find(r=>Number(r.evening_weight)>0);
const lm = lmRec ? Number(lmRec.morning_weight) : 0;
const le = leRec ? Number(leRec.evening_weight) : 0;
// Diffs
const mValid = asc.filter(r=>Number(r.morning_weight)>0);
const eValid = asc.filter(r=>Number(r.evening_weight)>0);
const mDiff = mValid.length>=2 ? Number(mValid[mValid.length-1].morning_weight) - Number(mValid[0].morning_weight) : 0;
const eDiff = eValid.length>=2 ? Number(eValid[eValid.length-1].evening_weight) - Number(eValid[0].evening_weight) : 0;
// Morning vs Evening
let mve = 0;
if (lmRec) {
const d = mParseISO(lmRec.record_date);
const prev = new Date(d.getFullYear(), d.getMonth(), d.getDate()-1);
const prevStr = `${prev.getFullYear()}-${String(prev.getMonth()+1).padStart(2,'0')}-${String(prev.getDate()).padStart(2,'0')}`;
const prevE = asc.find(r=>r.record_date===prevStr && Number(r.evening_weight)>0);
if (prevE) {
mve = lm - Number(prevE.evening_weight);
} else if (Number(lmRec.evening_weight)>0) {
mve = lm - Number(lmRec.evening_weight);
} else if (lm>0 && le>0) {
mve = lm - le;
}
}
return {
total_records: asc.length,
morning_diff: mDiff,
evening_diff: eDiff,
mve: mve
};
};
const mUpdateStatsUI = (stats) => {
const mveEl = document.getElementById('m_mve_value');
const mdEl = document.getElementById('m_morning_diff_value');
const edEl = document.getElementById('m_evening_diff_value');
const trEl = document.getElementById('m_total_records_value');
// Helper to render value with color and arrow
const renderDiff = (el, val, unit = '斤') => {
if (!el) return;
const container = el.parentElement;
// Determine style based on value
let colorClass = 'text-white';
let arrow = '';
if (val < 0) {
colorClass = 'text-emerald-300';
arrow = `<svg class="w-4 h-4 mr-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M19 14l-7 7m0 0l-7-7m7 7V3"></path></svg>`;
} else if (val > 0) {
colorClass = 'text-rose-300';
arrow = `<svg class="w-4 h-4 mr-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 10l7-7m0 0l7 7m-7-7v18"></path></svg>`;
}
// Build HTML
container.className = `text-2xl font-bold tracking-tight flex items-center ${colorClass}`;
container.innerHTML = `
${arrow}
<span id="${el.id}">${Math.abs(val).toFixed(1)}</span>
<span class="text-sm font-normal text-blue-200 ml-1 opacity-80">${unit}</span>
`;
};
// Update DOM
renderDiff(mdEl, stats.morning_diff);
renderDiff(edEl, stats.evening_diff);
renderDiff(mveEl, stats.mve);
if (trEl) trEl.textContent = stats.total_records;
};
const mInitChart = (data) => {
const ctx = document.getElementById('mWeightChart').getContext('2d');
const labels = data.map(r=>r.record_date);
const mData = data.map(r=>Number(r.morning_weight)>0?Number(r.morning_weight):null);
const eData = data.map(r=>Number(r.evening_weight)>0?Number(r.evening_weight):null);
if (mChart) { mChart.destroy(); }
if (typeof Chart==='undefined') return;
mChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{ label:'早晨', data:mData, borderColor:'#3b82f6', backgroundColor:'rgba(59,130,246,0.1)', tension:0.4, fill:true, pointRadius:3, spanGaps:true, borderWidth: 2 },
{ label:'晚上', data:eData, borderColor:'#a855f7', backgroundColor:'rgba(168,85,247,0.1)', tension:0.4, fill:true, pointRadius:3, spanGaps:true, borderWidth: 2 }
]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position:'top',
align: 'end',
labels: {
usePointStyle: true,
boxWidth: 6,
font: { family: "'Plus Jakarta Sans', sans-serif", size: 10 }
}
}
},
scales: {
x: { display: false },
y: {
grid: { color:'#f1f5f9', tickLength: 0 },
border: { display: false },
ticks: { font: { family: "'Plus Jakarta Sans', sans-serif", size: 10 }, color: '#94a3b8' }
}
},
interaction: {
mode: 'index',
intersect: false,
}
}
});
};
const mUpdateChartRange = (range) => {
// Update buttons
['7','14','30','all'].forEach(r=>{
const btn=document.getElementById(`m-btn-${r}`);
if (!btn) return;
// Modern pill style
if (r===range) {
btn.className = 'flex-1 py-2 text-xs font-bold rounded-xl bg-white text-primary-600 shadow-sm transition-all scale-100';
} else {
btn.className = 'flex-1 py-2 text-xs font-medium rounded-xl text-slate-500 hover:text-slate-700 transition-all scale-95';
}
});
// Filter data
let filtered = [];
if (range==='all') {
filtered = mAllRecords.slice();
} else {
const days = parseInt(range);
const cutoff = new Date();
cutoff.setHours(0,0,0,0);
cutoff.setDate(cutoff.getDate()-days+1);
filtered = mAllRecords.filter(r => {
const d = mParseISO(r.record_date);
d.setHours(0,0,0,0);
return d >= cutoff;
});
}
// Sort old -> new for chart
filtered.sort((a,b) => mParseISO(a.record_date) - mParseISO(b.record_date));
// Update UI
mInitChart(filtered);
const stats = mComputeStats(filtered);
mUpdateStatsUI(stats);
};
// Initialize
document.addEventListener('DOMContentLoaded', function(){
mUpdateChartRange('14');
});
// Modal & Form functions...
function openAddModal() {
const modal = document.getElementById('modal');
const backdrop = document.getElementById('modal-backdrop');
const content = document.getElementById('modal-content');
modal.classList.remove('hidden');
// Trigger reflow
void modal.offsetWidth;
backdrop.classList.remove('opacity-0');
content.classList.remove('translate-y-full');
document.getElementById('modal-title').innerText = '记录体重';
document.getElementById('formAction').value = 'add_record';
document.getElementById('recordId').value = '';
document.getElementById('recordForm').reset();
document.getElementById('record_date').value = new Date().toISOString().split('T')[0];
document.getElementById('deleteBtn').classList.add('hidden');
}
function openEditModal(record) {
const modal = document.getElementById('modal');
const backdrop = document.getElementById('modal-backdrop');
const content = document.getElementById('modal-content');
modal.classList.remove('hidden');
void modal.offsetWidth;
backdrop.classList.remove('opacity-0');
content.classList.remove('translate-y-full');
document.getElementById('modal-title').innerText = '编辑记录';
document.getElementById('formAction').value = 'update_record';
document.getElementById('recordId').value = record.id;
document.getElementById('record_date').value = record.record_date;
document.getElementById('morning_weight').value = record.morning_weight > 0 ? record.morning_weight : '';
document.getElementById('evening_weight').value = record.evening_weight > 0 ? record.evening_weight : '';
document.getElementById('deleteBtn').classList.remove('hidden');
}
function closeModal() {
const modal = document.getElementById('modal');
const backdrop = document.getElementById('modal-backdrop');
const content = document.getElementById('modal-content');
backdrop.classList.add('opacity-0');
content.classList.add('translate-y-full');
setTimeout(() => {
modal.classList.add('hidden');
}, 300);
}
function handleFormSubmit(e) {
e.preventDefault();
const formData = new FormData(e.target);
if (formData.get('action') === 'add_record') {
formData.append('morning_date', formData.get('record_date'));
}
fetch('process.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
showNotification(data.message, 'success');
closeModal();
// 使用返回的数据更新UI,而不是刷新页面
if (data.records && data.stats) {
updateMobileUI(data.records, data.stats);
}
} else {
showNotification(data.message, 'error');
}
})
.catch(err => {
showNotification('发生错误,请重试', 'error');
});
}
function deleteCurrentRecord() {
const id = document.getElementById('recordId').value;
if (!id || !confirm('确定要删除这条记录吗?')) return;
const formData = new FormData();
formData.append('action', 'delete_record');
formData.append('id', id);
fetch('process.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
showNotification(data.message, 'success');
closeModal();
// 使用返回的数据更新UI,而不是刷新页面
if (data.records && data.stats) {
updateMobileUI(data.records, data.stats);
}
} else {
showNotification(data.message, 'error');
}
})
.catch(err => {
showNotification('发生错误,请重试', 'error');
});
}
function showNotification(message, type = 'success') {
const el = document.getElementById('notification');
const icon = document.getElementById('notification-icon');
const msg = document.getElementById('notification-message');
el.classList.remove('-translate-y-[200%]', 'opacity-0');
if (type === 'success') {
icon.className = "mr-3 p-2 rounded-full bg-emerald-100 text-emerald-600";
icon.innerHTML = `<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>`;
} else {
icon.className = "mr-3 p-2 rounded-full bg-rose-100 text-rose-600";
icon.innerHTML = `<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>`;
}
msg.textContent = message;
setTimeout(() => {
el.classList.add('-translate-y-[200%]', 'opacity-0');
}, 3000);
}
// 渲染移动端记录卡片
function renderMobileCards(records) {
const container = document.querySelector('.space-y-3.pb-28');
if (!container) return;
const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
if (records.length === 0) {
container.innerHTML = `
<div class="bg-white rounded-[1.5rem] p-10 text-center shadow-soft border border-slate-50">
<div class="inline-flex items-center justify-center w-20 h-20 rounded-full bg-slate-50 mb-4 text-slate-300">
<svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
</svg>
</div>
<p class="text-slate-500 font-medium">暂无记录</p>
<p class="text-slate-400 text-sm mt-1">点击右下角按钮开始记录</p>
</div>
`;
return;
}
container.innerHTML = records.map(record => {
const date = new Date(record.record_date);
const isSunday = date.getDay() === 0;
const cardClass = isSunday ? 'bg-orange-50/40 border-orange-100' : 'bg-white border-slate-100';
const weekdayClass = isSunday ? 'text-orange-500 bg-orange-100' : 'text-indigo-500 bg-indigo-50';
const morningDisplay = record.morning_weight > 0 ? record.morning_weight : '--';
const eveningDisplay = record.evening_weight > 0 ? record.evening_weight : '--';
return `
<div class="group ${cardClass} rounded-2xl p-5 shadow-sm border hover:shadow-md transition-all active:scale-[0.98] cursor-pointer relative overflow-hidden" onclick='openEditModal(${JSON.stringify(record)})'>
<div class="absolute left-0 top-4 bottom-4 w-1 bg-gradient-to-b from-blue-400 to-indigo-500 rounded-r-full opacity-0 group-hover:opacity-100 transition-opacity"></div>
<div class="flex items-center justify-between">
<div>
<div class="flex items-baseline gap-2 mb-2">
<span class="text-lg font-bold text-slate-800">${date.getMonth() + 1}<span class="text-sm text-slate-400">/</span>${date.getDate()}</span>
<span class="text-xs font-medium ${weekdayClass} px-2 py-0.5 rounded-md">${weekdays[date.getDay()]}</span>
</div>
<div class="flex gap-4 text-sm">
<div class="flex items-center gap-1.5">
<div class="w-2 h-2 rounded-full bg-blue-500"></div>
<span class="text-slate-500 text-xs">早</span>
<span class="font-bold text-slate-700">${morningDisplay}</span>
</div>
<div class="flex items-center gap-1.5">
<div class="w-2 h-2 rounded-full bg-purple-500"></div>
<span class="text-slate-500 text-xs">晚</span>
<span class="font-bold text-slate-700">${eveningDisplay}</span>
</div>
</div>
</div>
<div class="w-8 h-8 rounded-full bg-slate-50 flex items-center justify-center text-slate-300 group-hover:bg-blue-50 group-hover:text-blue-500 transition-colors">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
</svg>
</div>
</div>
</div>
`;
}).join('');
}
// 更新移动端UI
function updateMobileUI(records, stats) {
// 更新全局数据
if (typeof mAllRecords !== 'undefined') {
mAllRecords.length = 0;
mAllRecords.push(...records);
}
// 更新统计数据
if (stats) {
mUpdateStatsUI(stats);
}
// 更新图表 - 保持最近30天
const days = 30;
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - days);
const filteredData = records.filter(r => {
const recordDate = mParseISO(r.record_date);
return recordDate >= cutoffDate;
});
mInitChart(filteredData);
// 更新卡片列表
renderMobileCards(records);
}
</script>
</body>
</html>