import React, { useState } from 'react'; import { Trash2, Users, Tag as TagIcon } from 'lucide-react'; import { useTourStore } from '@/store/useTourStore'; export const CreateTourModal = ({ isOpen, onClose, onSuccess }: { isOpen: boolean, onClose: () => void, onSuccess: (tour: any) => void }) => { const [title, setTitle] = useState(''); const [description, setDescription] = useState(''); const [startDate, setStartDate] = useState(''); const [endDate, setEndDate] = useState(''); const [adultCount, setAdultCount] = useState(2); const [childCount, setChildCount] = useState(1); const [childDiscount, setChildDiscount] = useState(30); const [isLoading, setIsLoading] = useState(false); const createTour = useTourStore((state) => state.createTour); const availableTags = ['Văn hóa', 'Mạo hiểm', 'Nghỉ dưỡng', 'Thư giãn', 'Ẩm thực', 'Khám phá', 'Gia đình']; const [selectedTags, setSelectedTags] = useState([]); const [customTag, setCustomTag] = useState(''); const [members, setMembers] = useState([]); const [query, setQuery] = useState(''); const [results, setResults] = useState([]); const [error, setError] = useState(''); if (!isOpen) return null; const searchUsers = async (value: string) => { setQuery(value); if (!value.trim()) { setResults([]); return; } try { const res = await fetch(`/api/v1/users?q=${encodeURIComponent(value)}`, { headers: { Authorization: `Bearer ${localStorage.getItem('token')}` }, }); if (!res.ok) throw new Error('Không thể tải người dùng'); const data = await res.json(); setResults(Array.isArray(data) ? data : []); } catch (e: any) { setResults([]); setError(e.message || 'Không thể tải người dùng'); } }; const confirmAddMember = (user: any) => { setMembers((prev) => (prev.some((m) => m.id === user.id) ? prev : [...prev, user])); setQuery(''); setResults([]); setError(''); }; const removeMember = (userId: string) => { setMembers((prev) => prev.filter((m) => m.id !== userId)); }; const toggleTag = (tag: string) => { setSelectedTags(prev => prev.includes(tag) ? prev.filter(t => t !== tag) : [...prev, tag] ); }; const addCustomTag = () => { const tag = customTag.trim(); if (tag && !selectedTags.includes(tag)) { setSelectedTags([...selectedTags, tag]); setCustomTag(''); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); setError(''); try { const membersPayload = members.map((m) => { if (m.isManual) { return { displayName: m.name }; } else { return { userId: m.id }; } }); const tour = await createTour({ title, description, startDate, endDate, members: membersPayload, adultCount, childCount, childDiscount, tags: selectedTags }); onSuccess(tour); onClose(); } catch (e: any) { setError(e.message || 'Lỗi khi tạo tour'); } finally { setIsLoading(false); } }; return (

Tạo Tour mới

setTitle(e.target.value)} placeholder="VD: Khám phá Đà Lạt" />
{availableTags.map(tag => ( ))}
setCustomTag(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && (e.preventDefault(), addCustomTag())} placeholder="Thêm nhãn tùy chỉnh..." className="flex-1 px-3 py-2 bg-gray-50 border border-gray-100 rounded-xl text-xs outline-none focus:ring-2 focus:ring-blue-500" />