import { useState } from 'react' import { Dialog, Toast } from 'antd-mobile' import { columnApi } from '../../api/task.js' import { RETENTION_OPTIONS, RETENTION_LABEL, STATUS_OPTIONS, STATUS_LABEL } from './date.js' const MAX_COLUMNS = 10 // 列设置 (T-COL-*, T-BD-02/09/10, T-FL-01/02/15) // 1) 列管理: 改名 / 新增 / 删除 / 排序 // 2) 状态绑定: 每列独立选择 不绑定 / 未开始 / 执行中 / 完成 // 3) 保留选项: 每列独立 6 档 export default function ColumnSettings({ projectId, columns, onClose, onSaved }) { const ordered = [...(columns || [])].sort((a, b) => a.sortOrder - b.sortOrder) const [list, setList] = useState(ordered) const [editingName, setEditingName] = useState(null) // { id, name } const [pending, setPending] = useState(false) const patchLocal = (id, patch) => setList((prev) => prev.map((c) => (c.id === id ? { ...c, ...patch } : c))) async function save() { setPending(true) try { // 逐列保存变更(只提交与初始不同的字段) for (const c of list) { const orig = ordered.find((o) => o.id === c.id) if (!orig) continue const body = {} if (orig.name !== c.name) body.name = c.name if (orig.boundStatus !== c.boundStatus) body.boundStatus = c.boundStatus if (orig.retention !== c.retention) body.retention = c.retention if (Object.keys(body).length) await columnApi.update(c.id, body) } Toast.show({ content: '已保存' }) onSaved?.() } catch (e) { Toast.show({ content: e.message }) } finally { setPending(false) } } async function addColumn() { if (list.length >= MAX_COLUMNS) { Toast.show({ content: `最多 ${MAX_COLUMNS} 列` }); return } const name = `新列 ${list.length + 1}` try { const r = await columnApi.add(projectId, { name }) setList((prev) => [...prev, r.item]) } catch (e) { Toast.show({ content: e.message }) } } async function removeColumn(col) { if (list.length <= 1) { Toast.show({ content: '至少保留一列' }); return } const idx = list.findIndex((c) => c.id === col.id) const fallback = list[idx + 1] || list[idx - 1] const ok = await Dialog.confirm({ content: `删除「${col.name}」?其中的任务将迁移到「${fallback.name}」,其状态会按目标列的绑定重新确定。`, confirmText: '删除', }) if (!ok) return try { await columnApi.remove(col.id, fallback.id) setList((prev) => prev.filter((c) => c.id !== col.id)) Toast.show({ content: '已删除' }) } catch (e) { Toast.show({ content: e.message }) } } function move(idx, dir) { const next = [...list] const j = idx + dir if (j < 0 || j >= next.length) return ;[next[idx], next[j]] = [next[j], next[idx]] setList(next) // 立即持久化顺序 columnApi.order(projectId, next.map((c) => c.id)).catch((e) => Toast.show({ content: e.message })) } return (