File size: 10,997 Bytes
7c280c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
820d0cd
7c280c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useCallback, useMemo, useState } from 'react'
import { codes as allCodes } from '@/lib/ltc3-data'

// ---------------------------------------------------------------------------
// Category definitions
// ---------------------------------------------------------------------------

const CATEGORIES = [
  { key: 'ALL', label: '全部' },
  { key: 'BA', label: 'BA 居家照顧' },
  { key: 'CA', label: 'CA 專業服務' },
  { key: 'DA', label: 'DA 交通接送' },
  { key: 'GA', label: 'GA 喘息服務' },
  { key: 'RA', label: 'RA 復能服務' },
  { key: 'SA', label: 'SA 智慧科技' },
  { key: 'TA', label: 'TA 失智專區' },
] as const

// ---------------------------------------------------------------------------
// Props
// ---------------------------------------------------------------------------

interface LTC3CodePickerProps {
  selectedCodes: string[]
  onChange: (codes: string[]) => void
}

// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------

export default function LTC3CodePicker({
  selectedCodes,
  onChange,
}: LTC3CodePickerProps) {
  const [search, setSearch] = useState('')
  const [activeCategory, setActiveCategory] = useState<string>('ALL')

  // -----------------------------------------------------------------------
  // Filtered code list
  // -----------------------------------------------------------------------

  const filteredCodes = useMemo(() => {
    const query = search.trim().toLowerCase()

    return allCodes.filter((c) => {
      // Category filter
      if (activeCategory !== 'ALL' && c.category !== activeCategory) return false

      // Search filter — match code, name, or any keyword
      if (query) {
        const inCode = c.code.toLowerCase().includes(query)
        const inName = c.name.toLowerCase().includes(query)
        const inKeywords = c.keywords.some((kw) =>
          kw.toLowerCase().includes(query),
        )
        if (!inCode && !inName && !inKeywords) return false
      }

      return true
    })
  }, [search, activeCategory])

  // -----------------------------------------------------------------------
  // Toggle selection
  // -----------------------------------------------------------------------

  const toggleCode = useCallback(
    (code: string) => {
      if (selectedCodes.includes(code)) {
        onChange(selectedCodes.filter((c) => c !== code))
      } else {
        onChange([...selectedCodes, code])
      }
    },
    [selectedCodes, onChange],
  )

  const removeCode = useCallback(
    (code: string) => {
      onChange(selectedCodes.filter((c) => c !== code))
    },
    [selectedCodes, onChange],
  )

  // -----------------------------------------------------------------------
  // Selected code details (for badge display)
  // -----------------------------------------------------------------------

  const selectedDetails = useMemo(() => {
    return selectedCodes
      .map((code) => allCodes.find((c) => c.code === code))
      .filter(Boolean)
  }, [selectedCodes])

  // -----------------------------------------------------------------------
  // Render
  // -----------------------------------------------------------------------

  return (
    <div className="rounded-lg border border-slate-200 bg-white shadow-sm">
      {/* Selected codes as badges */}
      {selectedDetails.length > 0 && (
        <div className="flex flex-wrap gap-2 border-b border-slate-100 px-4 py-3">
          {selectedDetails.map(
            (item) =>
              item && (
                <span
                  key={item.code}
                  className="inline-flex items-center gap-1 rounded-full bg-indigo-50 px-3 py-1 text-xs font-medium text-indigo-700"
                >
                  <span className="font-mono">{item.code}</span>
                  <span className="text-indigo-500">{item.name}</span>
                  <button
                    type="button"
                    onClick={() => removeCode(item.code)}
                    className="ml-0.5 rounded-full p-0.5 hover:bg-indigo-100 transition-colors"
                    aria-label={`移除 ${item.code}`}
                  >
                    <svg
                      className="h-3 w-3"
                      fill="none"
                      viewBox="0 0 24 24"
                      stroke="currentColor"
                      strokeWidth={2}
                    >
                      <path
                        strokeLinecap="round"
                        strokeLinejoin="round"
                        d="M6 18L18 6M6 6l12 12"
                      />
                    </svg>
                  </button>
                </span>
              ),
          )}
        </div>
      )}

      {/* Search input */}
      <div className="px-4 pt-3 pb-2">
        <div className="relative">
          <svg
            className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-400"
            fill="none"
            viewBox="0 0 24 24"
            stroke="currentColor"
            strokeWidth={2}
          >
            <path
              strokeLinecap="round"
              strokeLinejoin="round"
              d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z"
            />
          </svg>
          <input
            type="text"
            value={search}
            onChange={(e) => setSearch(e.target.value)}
            placeholder="搜尋服務代碼、名稱或關鍵字..."
            aria-label="搜尋服務代碼"
            className="w-full rounded-lg border border-slate-200 bg-slate-50 py-2 pl-10 pr-4 text-sm text-slate-700 placeholder:text-slate-400 focus:border-indigo-300 focus:bg-white focus:outline-none focus:ring-2 focus:ring-indigo-100 transition-colors"
          />
          {search && (
            <button
              type="button"
              onClick={() => setSearch('')}
              className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600"
              aria-label="清除搜尋"
            >
              <svg
                className="h-4 w-4"
                fill="none"
                viewBox="0 0 24 24"
                stroke="currentColor"
                strokeWidth={2}
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  d="M6 18L18 6M6 6l12 12"
                />
              </svg>
            </button>
          )}
        </div>
      </div>

      {/* Category tabs */}
      <div className="flex gap-1 overflow-x-auto px-4 py-2 scrollbar-thin">
        {CATEGORIES.map((cat) => (
          <button
            key={cat.key}
            type="button"
            onClick={() => setActiveCategory(cat.key)}
            className={[
              'shrink-0 rounded-full px-3 py-1 text-xs font-medium transition-colors whitespace-nowrap',
              activeCategory === cat.key
                ? 'bg-indigo-600 text-white shadow-sm'
                : 'bg-slate-100 text-slate-600 hover:bg-slate-200',
            ].join(' ')}
          >
            {cat.label}
          </button>
        ))}
      </div>

      {/* Code list */}
      <div className="max-h-[320px] overflow-y-auto border-t border-slate-100">
        {filteredCodes.length === 0 ? (
          <div className="px-4 py-8 text-center text-sm text-slate-400">
            找不到符合條件的服務代碼
          </div>
        ) : (
          <ul className="divide-y divide-slate-50">
            {filteredCodes.map((item) => {
              const isSelected = selectedCodes.includes(item.code)

              return (
                <li key={item.code}>
                  <button
                    type="button"
                    onClick={() => toggleCode(item.code)}
                    className={[
                      'flex w-full items-start gap-3 px-4 py-3 text-left transition-colors',
                      isSelected
                        ? 'bg-indigo-50/60'
                        : 'hover:bg-slate-50',
                    ].join(' ')}
                  >
                    {/* Checkbox */}
                    <span
                      className={[
                        'mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded border transition-colors',
                        isSelected
                          ? 'border-indigo-600 bg-indigo-600 text-white'
                          : 'border-slate-300 bg-white',
                      ].join(' ')}
                    >
                      {isSelected && (
                        <svg
                          className="h-3 w-3"
                          fill="none"
                          viewBox="0 0 24 24"
                          stroke="currentColor"
                          strokeWidth={3}
                        >
                          <path
                            strokeLinecap="round"
                            strokeLinejoin="round"
                            d="M5 13l4 4L19 7"
                          />
                        </svg>
                      )}
                    </span>

                    {/* Details */}
                    <div className="min-w-0 flex-1">
                      <div className="flex items-center gap-2">
                        <span className="shrink-0 rounded bg-slate-100 px-1.5 py-0.5 font-mono text-xs font-semibold text-slate-700">
                          {item.code}
                        </span>
                        <span className="truncate text-sm font-medium text-slate-800">
                          {item.name}
                        </span>
                        {item.isLtc30New && (
                          <span className="shrink-0 rounded bg-emerald-50 px-1.5 py-0.5 text-[10px] font-semibold text-emerald-600">
                            3.0新增
                          </span>
                        )}
                      </div>
                      <p className="mt-0.5 text-xs text-slate-400 truncate">
                        {item.keywords.join('、')}
                      </p>
                    </div>

                    {/* Unit */}
                    <span className="shrink-0 text-xs text-slate-400">
                      /{item.unit}
                    </span>
                  </button>
                </li>
              )
            })}
          </ul>
        )}
      </div>

      {/* Footer summary */}
      <div className="flex items-center justify-between border-t border-slate-100 px-4 py-2">
        <span className="text-xs text-slate-400">
          顯示 {filteredCodes.length} 項
          {search ? `(搜尋「${search}」)` : ''}
        </span>
        <span className="text-xs font-medium text-indigo-600">
          已選 {selectedCodes.length} 項
        </span>
      </div>
    </div>
  )
}