import { Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, Button, VStack, HStack, Box, FormControl, FormLabel, Select, SelectTrigger, SelectPlaceholder, SelectValue, SelectContent, SelectOption, SelectOptionText, SelectOptionIndicator, Tag, TagLabel, TagCloseButton, createDisclosure, Checkbox, Icon, } from "@hope-ui/solid" import { createSignal, For, Show, createEffect } from "solid-js" import { useT, useRouter, usePath } from "~/hooks" import { IoCheckmark } from "solid-icons/io" import { BsPlus } from "solid-icons/bs" import { createLabelFileBinding, createLabelFileBindingBatch, } from "~/utils/api" import { handleResp, notify } from "~/utils" import { StoreObj, Obj } from "~/types" interface Label { id: number name: string type: number description: string bg_color: string } export interface EditLabelDialogProps { isOpen: boolean onClose: () => void onSubmit: (selectedLabels: string[]) => void labels: Label[] obj: StoreObj isBatch?: boolean selectedObjs?: StoreObj[] } const EditLabelDialog = (props: EditLabelDialogProps) => { const t = useT() const { to } = useRouter() const { refresh } = usePath() const [selectedLabelIds, setSelectedLabelIds] = createSignal([]) const [loading, setLoading] = createSignal(false) const initializeSelectedLabels = () => { if (props.isOpen && props.obj.label_list?.length) { setSelectedLabelIds( props.obj.label_list.map((label) => label.id.toString()), ) } else { setSelectedLabelIds([]) } } createEffect(() => { if (props.isOpen) { initializeSelectedLabels() } }) const handleSubmit = async () => { try { setLoading(true) // 调用批量打标签接口 const objectsToProcess = props.isBatch && props.selectedObjs ? props.selectedObjs : [props.obj] const resp = await createLabelFileBindingBatch( selectedLabelIds(), objectsToProcess, ) handleResp(resp, () => { notify.success(t("home.tag.bind_success")) props.onSubmit(selectedLabelIds()) props.onClose() // 延迟刷新,等待对话框关闭完成 setTimeout(() => { refresh(false, false) }, 300) }) } catch (err: any) { notify.error(err.message || t("home.tag.bind_failed")) } finally { setLoading(false) } } const handleNewTag = () => { props.onClose() to("/@manage/settings/tag") } const handleSelect = (id: string) => { const current = selectedLabelIds() if (current.includes(id)) { setSelectedLabelIds(current.filter((labelId) => labelId !== id)) } else { setSelectedLabelIds([...current, id]) } } const removeLabel = (id: string) => { setSelectedLabelIds(selectedLabelIds().filter((labelId) => labelId !== id)) } const getLabelNameById = (id: string) => { return props.labels.find((label) => label.id.toString() === id)?.name || "" } return ( {t("home.tag.tag")} {t("home.tag.select")} ) } export default EditLabelDialog