"use client";

import {
  createTours,
  hotelBadge,
  hotelTarif,
  hotelTransport,
  updateTours,
} from "@/pages/tours/lib/api";
import { TourformSchema } from "@/pages/tours/lib/form";
import { useTicketStore } from "@/pages/tours/lib/store";
import type { GetOneTours } from "@/pages/tours/lib/type";
import TicketsImagesModel from "@/pages/tours/ui/TicketsImagesModel";
import formatPrice from "@/shared/lib/formatPrice";
import { Badge } from "@/shared/ui/badge";
import { Button } from "@/shared/ui/button";
import { Calendar } from "@/shared/ui/calendar";
import {
  Command,
  CommandGroup,
  CommandItem,
  CommandList,
} from "@/shared/ui/command";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormMessage,
} from "@/shared/ui/form";
import { Input } from "@/shared/ui/input";
import IconSelect from "@/shared/ui/iocnSelect";
import { Label } from "@/shared/ui/label";
import { Popover, PopoverContent, PopoverTrigger } from "@/shared/ui/popover";
import { RadioGroup, RadioGroupItem } from "@/shared/ui/radio-group";
import { Textarea } from "@/shared/ui/textarea";
import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation, useQuery } from "@tanstack/react-query";
import * as LucideIcons from "lucide-react";
import { ChevronDownIcon, SquareCheckBig, XIcon } from "lucide-react";
import { useEffect, useState, type Dispatch, type SetStateAction } from "react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import z from "zod";

const StepOne = ({
  setStep,
  data,
  id,
  isEditMode,
}: {
  setStep: Dispatch<SetStateAction<number>>;
  data: GetOneTours | undefined;
  id: string | undefined;
  isEditMode: boolean;
}) => {
  const [displayPrice, setDisplayPrice] = useState("");
  const [tarifdisplayPrice, setTarifDisplayPrice] = useState<string[]>([]);
  const [transportPrices, setTransportPrices] = useState<string[]>([]);

  const { t } = useTranslation();
  const [openDate, setOpenDate] = useState(false);
  const [openDateTravel, setOpenDateTravel] = useState(false);

  const form = useForm<z.infer<typeof TourformSchema>>({
    resolver: zodResolver(TourformSchema),
    defaultValues: {
      title: "",
      title_ru: "",
      hotel_services: [],
      price: 0,
      departure: "",
      tarif: [],
      transport: [],
      departure_ru: "",
      destination: "",
      destination_ru: "",
      location_name: "",
      location_name_ru: "",
      departureDateTime: {
        date: undefined,
        time: "",
      },
      amenities: [],
      hotel_meals: [],
      travelDateTime: {
        date: undefined,
        time: "",
      },
      passenger_count: 1,
      min_person: 1,
      max_person: 1,
      extra_service: [],
      paid_extra_service: [],
      languages: "",
      duration: 1,
      badges: [],
      images: [],
      hotel_info: "",
      hotel_info_ru: "",
      hotel_meals_info: "",
      hotel_meals_info_ru: "",
      ticket_itinerary: [],
    },
  });

  const { addAmenity, setId } = useTicketStore();

  useEffect(() => {
    if (!isEditMode || !data?.data) return;

    const tour = data.data;

    // 🔹 Oddiy text maydonlar
    form.setValue("title", tour.title_uz ?? "");
    form.setValue("title_ru", tour.title_ru ?? "");
    form.setValue("price", tour.price ?? 0);
    setDisplayPrice(formatPrice(tour.price ?? 0));

    form.setValue("passenger_count", tour.passenger_count ?? 1);
    form.setValue("min_person", tour.min_person ?? 1);
    form.setValue("max_person", tour.max_person ?? 1);

    form.setValue("departure", tour.departure_uz ?? "");
    form.setValue("departure_ru", tour.departure_ru ?? "");
    form.setValue("destination", tour.destination_uz ?? "");
    form.setValue("destination_ru", tour.destination_ru ?? "");
    form.setValue("location_name", tour.location_name_uz ?? "");
    form.setValue("location_name_ru", tour.location_name_ru ?? "");

    form.setValue("hotel_info", tour.hotel_info_uz ?? "");
    form.setValue("hotel_info_ru", tour.hotel_info_ru ?? "");
    form.setValue("hotel_meals_info", tour.hotel_meals_uz ?? "");
    form.setValue("hotel_meals_info_ru", tour.hotel_meals_ru ?? "");

    form.setValue("languages", tour.languages ?? "");
    form.setValue("duration", tour.duration_days ?? 1);
    form.setValue("visa_required", tour.visa_required ? "yes" : "no");
    form.setValue("badges", tour.badge ?? []);

    // 🔹 Jo‘nash vaqti
    if (tour.departure_time) {
      const d = new Date(tour.departure_time);
      form.setValue("departureDateTime", {
        date: d,
        time: d.toTimeString().slice(0, 8),
      });
    }

    // 🔹 Qaytish vaqti
    if (tour.travel_time) {
      const d = new Date(tour.travel_time);
      form.setValue("travelDateTime", {
        date: d,
        time: d.toTimeString().slice(0, 8),
      });
    }

    // 🔹 Qulayliklar (amenities)
    form.setValue(
      "amenities",
      tour.ticket_amenities?.map((a) => ({
        name: a.name ?? "",
        name_ru: a.name_ru ?? "",
        icon_name: a.icon_name ?? "",
      })) ?? [],
    );

    // 🔹 Xizmatlar (hotel_services)
    form.setValue(
      "hotel_services",
      tour.ticket_included_services?.map((s) => ({
        image: s.image ?? null,
        title: s.title_uz ?? "",
        title_ru: s.title_ru ?? "",
        description: s.desc_uz ?? "",
        desc_ru: s.desc_ru ?? "",
      })) ?? [],
    );

    // 🔹 Taomlar (hotel_meals)
    form.setValue(
      "hotel_meals",
      tour.ticket_hotel_meals?.map((m) => ({
        image: m.image ?? null,
        title: m.name ?? "",
        title_ru: m.name_ru ?? "",
        description: m.desc ?? "",
        desc_ru: m.desc_ru ?? "",
      })) ?? [],
    );

    // 🔹 Transport
    const transports =
      tour.transports?.map((t, i) => ({
        transport: i + 1,
        price: t.price ?? 0,
      })) ?? [];
    form.setValue("transport", transports);
    setTransportPrices(transports.map((t) => formatPrice(t.price ?? 0))); // 👈 YANGI QO‘SHILGAN

    // 🔹 Tarif
    const tariffs =
      tour.tariff?.map((t) => ({
        tariff: t.tariff ?? 0,
        price: t.price ?? 0,
      })) ?? [];
    form.setValue("tarif", tariffs);
    setTarifDisplayPrice(tariffs.map((t) => formatPrice(t.price ?? 0)));

    // 🔹 Yo‘nalishlar (ticket_itinerary)
    form.setValue(
      "ticket_itinerary",
      tour.ticket_itinerary?.map((item) => ({
        ticket_itinerary_image:
          item.ticket_itinerary_image?.map((img) => ({
            image: img.image,
          })) ?? [],
        title: item.title ?? "",
        title_ru: item.title_ru ?? "",
        duration: item.duration ?? 1,
        ticket_itinerary_destinations:
          item.ticket_itinerary_destinations?.map((d) => ({
            name: d.name ?? "",
            name_ru: d.name_ru ?? "",
          })) ?? [],
      })) ?? [],
    );

    // 🔹 Banner va rasmlar
    form.setValue("banner", tour.image_banner ?? null);
    form.setValue("images", tour.ticket_images?.map((img) => img.image) ?? []);

    // 🔹 Bepul xizmatlar (extra_service)
    form.setValue(
      "extra_service",
      tour.extra_service?.map((s) => ({
        name: s.name_uz ?? s.name ?? "",
        name_ru: s.name_ru ?? "",
      })) ?? [],
    );

    // 🔹 Pullik xizmatlar (paid_extra_service)
    form.setValue(
      "paid_extra_service",
      tour.paid_extra_service?.map((s) => ({
        name: s.name_uz ?? s.name ?? "",
        name_ru: s.name_ru ?? "",
        price: s.price ?? 0,
      })) ?? [],
    );

    // 🔹 TicketStore uchun id
    setId(tour.id);
  }, [isEditMode, data, form, setId]);

  const { watch, setValue } = form;
  const selectedDate = watch("departureDateTime.date");
  const selectedDateTravel = watch("travelDateTime.date");
  const [selectedIcon, setSelectedIcon] = useState("");

  const { mutate: create } = useMutation({
    mutationFn: (body: FormData) => {
      return createTours({ body });
    },
    onSuccess: (res) => {
      setId(res.data.data.id);
      setStep(2);
    },
    onError: () => {
      toast.error(t("Xatolik yuz berdi"), {
        richColors: true,
        position: "top-center",
      });
    },
  });

  const { mutate: update } = useMutation({
    mutationFn: ({ body, id }: { id: number; body: FormData }) => {
      return updateTours({ body, id });
    },
    onSuccess: (res) => {
      setId(res.data.data.id);
      setStep(2);
    },
    onError: () => {
      toast.error(t("Xatolik yuz berdi"), {
        richColors: true,
        position: "top-center",
      });
    },
  });

  function onSubmit(value: z.infer<typeof TourformSchema>) {
    const formData = new FormData();

    formData.append("title", value.title);
    formData.append("location_name", value.location_name);
    formData.append("location_name_ru", value.location_name_ru);
    formData.append(
      "visa_required",
      value.visa_required === "yes" ? "true" : "false",
    );

    formData.append("title_ru", value.title_ru);
    formData.append("price", String(value.price));
    formData.append("min_person", String(value.min_person));
    formData.append("max_person", String(value.max_person));
    formData.append("departure", String(value.departure));
    formData.append("departure_ru", String(value.departure_ru));
    formData.append("destination", String(value.destination));
    formData.append("destination_ru", String(value.destination_ru));
    formData.append(
      "departure_time",
      value.departureDateTime.date?.toISOString(),
    );
    formData.append(
      "travel_time",
      String(value.travelDateTime.date.toISOString()),
    );
    formData.append("passenger_count", String(value.passenger_count));
    formData.append("languages", value.languages);
    formData.append("hotel_info", value.hotel_info);
    formData.append("hotel_info_ru", value.hotel_info_ru);
    formData.append("hotel_meals", value.hotel_meals_info);
    formData.append("hotel_meals_ru", value.hotel_meals_info_ru);
    formData.append("duration_days", String(value.duration));
    formData.append("rating", String("0.0"));
    if (value.banner instanceof File) {
      formData.append("image_banner", value.banner);
    }
    value.tarif.forEach((e, i) => {
      formData.append(`tariff[${i}]tariff`, String(e.tariff));
      formData.append(`tariff[${i}]price`, String(e.price));
    });
    value.transport.forEach((e, i) => {
      formData.append(`transports[${i}]transport`, String(e.transport));
      formData.append(`transports[${i}]price`, String(e.price));
    });
    value.badges?.forEach((e, i) => {
      formData.append(`badge[${i}]`, String(e));
    });
    value.images.forEach((e) => {
      if (e instanceof File) {
        formData.append("ticket_images", e);
      }
    });
    value.amenities.forEach((e, i) => {
      formData.append(`ticket_amenities[${i}]name`, e.name);
      formData.append(`ticket_amenities[${i}]name_ru`, e.name_ru);
      formData.append(`ticket_amenities[${i}]icon_name`, e.icon_name);
      addAmenity({
        icon_name: e.icon_name,
        name: e.name,
        name_ru: e.name_ru,
      });
    });
    value.hotel_services &&
      value.hotel_services.forEach((e, i) => {
        if (e instanceof File) {
          formData.append(`ticket_included_services[${i}]image`, e.image);
          formData.append(`ticket_included_services[${i}]title`, e.title);
          formData.append(`ticket_included_services[${i}]title_ru`, e.title_ru);
          formData.append(`ticket_included_services[${i}]desc_ru`, e.desc_ru);
          formData.append(`ticket_included_services[${i}]desc`, e.description);
        }
      });
    value.ticket_itinerary.forEach((e, i) => {
      e.ticket_itinerary_image.forEach((l, f) => {
        if (e instanceof File) {
          formData.append(`ticket_itinerary[${i}]title`, e.title);
          formData.append(`ticket_itinerary[${i}]title_ru`, e.title_ru);
          formData.append(`ticket_itinerary[${i}]duration`, String(e.duration));
          formData.append(
            `ticket_itinerary[${i}]ticket_itinerary_image[${f}]image`,
            l.image,
          );
          e.ticket_itinerary_destinations.forEach((e, f) => {
            formData.append(
              `ticket_itinerary[${i}]ticket_itinerary_destinations[${f}]name`,
              String(e.name),
            );
            formData.append(
              `ticket_itinerary[${i}]ticket_itinerary_destinations[${f}]name_ru`,
              String(e.name_ru),
            );
          });
        }
      });
    });
    value.hotel_meals.forEach((e, i) => {
      if (e instanceof File) {
        formData.append(`ticket_hotel_meals[${i}]image`, e.image);
        formData.append(`ticket_hotel_meals[${i}]name`, e.title);
        formData.append(`ticket_hotel_meals[${i}]name_ru`, e.title_ru);
        formData.append(`ticket_hotel_meals[${i}]desc`, e.description);
        formData.append(`ticket_hotel_meals[${i}]desc_ru`, e.desc_ru);
      }
    });
    value.extra_service &&
      value.extra_service.forEach((e, i) => {
        formData.append(`extra_service[${i}]name`, e.name);
        formData.append(`extra_service[${i}]name_ru`, e.name_ru);
      });
    value.paid_extra_service &&
      value.paid_extra_service.forEach((e, i) => {
        formData.append(`paid_extra_service[${i}]name`, e.name);
        formData.append(`paid_extra_service[${i}]name_ru`, e.name_ru);
        formData.append(`paid_extra_service[${i}]price`, String(e.price));
      });
    if (isEditMode && id) {
      update({
        body: formData,
        id: Number(id),
      });
    } else {
      create(formData);
    }
  }

  console.log(form.formState.errors);

  const { data: badge } = useQuery({
    queryKey: ["all_badge"],
    queryFn: () => hotelBadge({ page: 1, page_size: 10 }),
  });

  const { data: tariff } = useQuery({
    queryKey: ["all_tarif"],
    queryFn: () => hotelTarif({ page: 1, page_size: 10 }),
  });

  const { data: transport } = useQuery({
    queryKey: ["all_transport"],
    queryFn: () => hotelTransport({ page: 1, page_size: 10 }),
  });

  return (
    <Form {...form}>
      <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
        <div className="grid grid-cols-2 gap-4 max-lg:grid-cols-1 items-start">
          <FormField
            control={form.control}
            name="title"
            render={({ field }) => (
              <FormItem>
                <Label className="text-md">{t("Sarlavha")}</Label>
                <FormControl>
                  <Input
                    placeholder="Toshkent - Dubay"
                    {...field}
                    className="h-12 !text-md"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="title_ru"
            render={({ field }) => (
              <FormItem>
                <Label className="text-md">{t("Sarlavha")} (ru)</Label>
                <FormControl>
                  <Input
                    placeholder="Ташкент - Дубай"
                    {...field}
                    className="h-12 !text-md"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="price"
            render={() => (
              <FormItem>
                <Label className="text-md">
                  {t("Narx")} {t("(1 kishi uchun)")}
                </Label>
                <FormControl>
                  <Input
                    type="text"
                    inputMode="numeric"
                    placeholder="1 500 000"
                    value={displayPrice}
                    onChange={(e) => {
                      const raw = e.target.value.replace(/\D/g, "");
                      const num = Number(raw);
                      if (!isNaN(num)) {
                        form.setValue("price", num);
                        setDisplayPrice(raw ? formatPrice(num) : "");
                      }
                    }}
                    className="h-12 !text-md"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <div className="grid grid-cols-2 gap-4">
            <FormField
              control={form.control}
              name="min_person"
              render={({ field }) => (
                <FormItem>
                  <Label className="text-md">
                    {t("hamrohlar soni (eng kamida)")}
                  </Label>
                  <FormControl>
                    <Input
                      type="number"
                      inputMode="numeric"
                      placeholder="1"
                      {...field}
                      onChange={(e) =>
                        form.setValue("min_person", Number(e.target.value))
                      }
                      className="h-12 !text-md
                    [&::-webkit-inner-spin-button]:appearance-none
                    [&::-webkit-outer-spin-button]:appearance-none
                    [appearance:textfield]"
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />
            <FormField
              control={form.control}
              name="max_person"
              render={({ field }) => (
                <FormItem>
                  <Label className="text-md">
                    {t("hamrohlar soni (eng ko'pida)")}
                  </Label>
                  <FormControl>
                    <Input
                      type="number"
                      inputMode="numeric"
                      placeholder="1"
                      {...field}
                      onChange={(e) =>
                        form.setValue("max_person", Number(e.target.value))
                      }
                      className="h-12 !text-md
                    [&::-webkit-inner-spin-button]:appearance-none
                    [&::-webkit-outer-spin-button]:appearance-none
                    [appearance:textfield]"
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />
          </div>
        </div>

        <div className="grid grid-cols-2 gap-4 max-lg:grid-cols-1 items-start">
          <FormField
            control={form.control}
            name="departure"
            render={({ field }) => (
              <FormItem>
                <Label className="text-md">{t("Ketish joyi")}</Label>
                <FormControl>
                  <Input
                    placeholder="Toshkent"
                    {...field}
                    className="h-12 !text-md"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />

          <FormField
            control={form.control}
            name="departure_ru"
            render={({ field }) => (
              <FormItem>
                <Label className="text-md">{t("Ketish joyi")} (ru)</Label>
                <FormControl>
                  <Input
                    placeholder="Ташкент"
                    {...field}
                    className="h-12 !text-md"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />

          <FormField
            control={form.control}
            name="destination"
            render={({ field }) => (
              <FormItem>
                <Label className="text-md">{t("Borish joyi")}</Label>
                <FormControl>
                  <Input
                    placeholder="Dubai"
                    {...field}
                    className="h-12 !text-md"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />

          <FormField
            control={form.control}
            name="destination_ru"
            render={({ field }) => (
              <FormItem>
                <Label className="text-md">{t("Borish joyi")} (ru)</Label>
                <FormControl>
                  <Input
                    placeholder="Дубай"
                    {...field}
                    className="h-12 !text-md"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />

          <FormField
            control={form.control}
            name="location_name"
            render={({ field }) => (
              <FormItem>
                <Label className="text-md">{t("Manzil nomi")}</Label>
                <FormControl>
                  <Input
                    placeholder="Dubai"
                    {...field}
                    className="h-12 !text-md"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="location_name_ru"
            render={({ field }) => (
              <FormItem>
                <Label className="text-md">{t("Manzil nomi")} (ru)</Label>
                <FormControl>
                  <Input
                    placeholder="Дубай"
                    {...field}
                    className="h-12 !text-md"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />

          <FormField
            control={form.control}
            name="passenger_count"
            render={({ field }) => (
              <FormItem>
                <Label className="text-md">{t("Yo'lovchilar soni")}</Label>
                <FormControl>
                  <Input
                    type="number"
                    inputMode="numeric"
                    placeholder="1"
                    {...field}
                    onChange={(e) =>
                      form.setValue("passenger_count", Number(e.target.value))
                    }
                    className="h-12 !text-md
    [&::-webkit-inner-spin-button]:appearance-none
    [&::-webkit-outer-spin-button]:appearance-none
    [appearance:textfield]"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
        </div>
        <div className="grid grid-cols-2 max-lg:grid-cols-1 gap-4">
          <FormField
            control={form.control}
            name="departureDateTime.date"
            render={() => (
              <FormItem>
                <div>
                  <div className="flex flex-col gap-3">
                    <Label htmlFor="date-picker" className="px-1 text-md">
                      {t("Ketish sanasi")}
                    </Label>
                    <Popover open={openDate} onOpenChange={setOpenDate}>
                      <PopoverTrigger asChild>
                        <Button
                          variant="outline"
                          id="date-picker"
                          className="w-full justify-between font-normal h-12"
                        >
                          {selectedDate
                            ? selectedDate.toLocaleDateString()
                            : t("Sana tanlang")}
                          <ChevronDownIcon />
                        </Button>
                      </PopoverTrigger>
                      <PopoverContent
                        className="w-auto overflow-hidden p-0"
                        align="start"
                      >
                        <Calendar
                          mode="single"
                          selected={selectedDate}
                          captionLayout="dropdown"
                          onSelect={(date) => {
                            setValue("departureDateTime.date", date!);
                            setOpenDate(false);
                          }}
                        />
                      </PopoverContent>
                    </Popover>
                  </div>
                </div>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="departureDateTime.time"
            render={() => (
              <FormItem>
                <div className="flex flex-col gap-3">
                  <Label htmlFor="time-picker" className="px-1 text-md">
                    {t("Ketish vaqti")}
                  </Label>
                  <Input
                    type="time"
                    id="time-picker"
                    step="1"
                    value={watch("departureDateTime.time")}
                    onChange={(e) =>
                      setValue("departureDateTime.time", e.target.value)
                    }
                    className="bg-background !h-12 appearance-none [&::-webkit-calendar-picker-indicator]:hidden [&::-webkit-calendar-picker-indicator]:appearance-none"
                  />
                </div>
              </FormItem>
            )}
          />
        </div>

        <FormField
          control={form.control}
          name="travelDateTime.date"
          render={() => (
            <FormItem>
              <div className="grid grid-cols-2 gap-4 max-lg:grid-cols-1">
                <div className="flex flex-col gap-3">
                  <Label htmlFor="date-picker" className="px-1 text-md">
                    {t("Qaytish sanasi")}
                  </Label>
                  <Popover
                    open={openDateTravel}
                    onOpenChange={setOpenDateTravel}
                  >
                    <PopoverTrigger asChild>
                      <Button
                        variant="outline"
                        id="date-picker"
                        className="w-full justify-between font-normal h-12"
                      >
                        {selectedDateTravel
                          ? selectedDateTravel.toLocaleDateString()
                          : t("Sana tanlang")}
                        <ChevronDownIcon />
                      </Button>
                    </PopoverTrigger>
                    <PopoverContent
                      className="w-auto overflow-hidden p-0"
                      align="start"
                    >
                      <Calendar
                        mode="single"
                        selected={selectedDateTravel}
                        captionLayout="dropdown"
                        onSelect={(date) => {
                          setValue("travelDateTime.date", date!);
                          setOpenDateTravel(false);
                        }}
                      />
                    </PopoverContent>
                  </Popover>
                </div>
                <div className="flex flex-col gap-3">
                  <Label htmlFor="time-picker" className="px-1 text-md">
                    {t("Qaytish vaqti")}
                  </Label>
                  <Input
                    type="time"
                    id="time-picker"
                    step="1"
                    value={watch("travelDateTime.time")}
                    onChange={(e) =>
                      setValue("travelDateTime.time", e.target.value)
                    }
                    className="bg-background !h-12 appearance-none [&::-webkit-calendar-picker-indicator]:hidden [&::-webkit-calendar-picker-indicator]:appearance-none"
                  />
                </div>
              </div>
              <FormMessage />
            </FormItem>
          )}
        />
        <div className="grid grid-cols-2 max-lg:grid-cols-1 gap-6">
          <FormField
            control={form.control}
            name="languages"
            render={({ field }) => (
              <FormItem>
                <Label className="text-md">{t("Tillar")}</Label>
                <FormControl>
                  <Input
                    placeholder={t("Har bir tilni vergul (,) bilan ajrating")}
                    {...field}
                    className="h-12 !text-md"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />

          <FormField
            control={form.control}
            name="duration"
            render={({ field }) => (
              <FormItem>
                <Label className="text-md">{t("Tur davomiyligi")}</Label>
                <FormControl>
                  <Input
                    type="number"
                    inputMode="numeric"
                    placeholder="1"
                    onChange={(e) => field.onChange(Number(e.target.value))}
                    value={field.value}
                    onBlur={field.onBlur}
                    className="h-12 !text-md
    [&::-webkit-inner-spin-button]:appearance-none
    [&::-webkit-outer-spin-button]:appearance-none
    [appearance:textfield]"
                  />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
        </div>
        <div className="grid grid-cols-2 max-lg:grid-cols-1 gap-6 items-end">
          <FormField
            control={form.control}
            name="badges"
            render={() => (
              <FormItem>
                <Label className="text-md">{t("Belgilar (Badge)")}</Label>

                <div className="flex flex-wrap gap-2 mb-3">
                  {(form.watch("badges") ?? []).length > 0 &&
                    (form.watch("badges") ?? []).map((badgeId: number) => {
                      const badgeItem = badge?.data?.data?.results?.find(
                        (b: any) => b.id === badgeId,
                      );

                      return (
                        <Badge
                          key={badgeId}
                          variant="secondary"
                          className="px-3 py-1 text-sm flex items-center gap-2"
                        >
                          {badgeItem?.name || `Badge #${badgeId}`}
                          <button
                            type="button"
                            onClick={() => {
                              const current = form.getValues("badges") ?? [];
                              form.setValue(
                                "badges",
                                current.filter((b: number) => b !== badgeId),
                              );
                            }}
                            className="ml-1 text-muted-foreground hover:text-destructive"
                          >
                            <XIcon className="size-4" />
                          </button>
                        </Badge>
                      );
                    })}
                </div>

                <Popover>
                  <PopoverTrigger asChild>
                    <Button
                      variant="outline"
                      className="w-full h-12 justify-between font-normal"
                    >
                      {t("Belgilarni tanlang")}
                      <ChevronDownIcon />
                    </Button>
                  </PopoverTrigger>

                  <PopoverContent className="p-0 w-[250px]" align="start">
                    <Command>
                      <CommandList>
                        <CommandGroup heading={t("Mavjud belgilar")}>
                          {badge?.data?.data?.results?.map((item: any) => {
                            const currentBadges =
                              form.getValues("badges") ?? [];
                            const selected = currentBadges.includes(item.id);

                            return (
                              <CommandItem
                                key={item.id}
                                onSelect={() => {
                                  if (selected) {
                                    form.setValue(
                                      "badges",
                                      currentBadges.filter(
                                        (b: number) => b !== item.id,
                                      ),
                                    );
                                  } else {
                                    form.setValue("badges", [
                                      ...currentBadges,
                                      item.id,
                                    ]);
                                  }
                                }}
                                className="flex items-center gap-2"
                              >
                                <SquareCheckBig
                                  className={`size-4 ${
                                    selected
                                      ? "text-green-500"
                                      : "text-muted-foreground"
                                  }`}
                                />
                                {item.name}
                              </CommandItem>
                            );
                          })}
                        </CommandGroup>
                      </CommandList>
                    </Command>
                  </PopoverContent>
                </Popover>

                <FormMessage />
              </FormItem>
            )}
          />

          <FormField
            control={form.control}
            name="tarif"
            render={() => (
              <FormItem>
                <Label className="text-md">{t("Tariflar")}</Label>
                <div className="flex flex-col gap-3 mb-3">
                  {form.watch("tarif")?.map((tr, idx) => {
                    const transportItem = tariff?.data?.data?.results?.find(
                      (item: any) => item.id === tr.tariff,
                    );

                    return (
                      <div
                        key={idx}
                        className="flex items-center justify-between border rounded-lg p-2"
                      >
                        <div className="flex items-center gap-2">
                          <Badge
                            variant="secondary"
                            className="px-3 py-1 text-sm"
                          >
                            {transportItem?.name || `Tarif #${tr.tariff}`}
                          </Badge>
                          <Input
                            type="text"
                            inputMode="numeric"
                            placeholder="1 500 000"
                            value={tarifdisplayPrice[idx] || ""}
                            onChange={(e) => {
                              const raw = e.target.value.replace(/\D/g, "");
                              const num = Number(raw);
                              const updatedTransport = form
                                .getValues("tarif")
                                .map((t, i) =>
                                  i === idx ? { ...t, price: num } : t,
                                );

                              form.setValue("tarif", updatedTransport);

                              // Local holatda ham yangilaymiz:
                              const updatedPrices = [...tarifdisplayPrice];
                              updatedPrices[idx] = raw ? formatPrice(num) : "0";
                              setTarifDisplayPrice(updatedPrices);
                            }}
                            className="h-12 !text-md"
                          />
                        </div>
                        <button
                          type="button"
                          onClick={() => {
                            const current = form.getValues("tarif");
                            form.setValue(
                              "tarif",
                              current.filter((_, i) => i !== idx),
                            );
                            const updatedPrices = tarifdisplayPrice.filter(
                              (_, i) => i !== idx,
                            );
                            setTarifDisplayPrice(updatedPrices);
                          }}
                          className="ml-2 text-muted-foreground hover:text-destructive"
                        >
                          <XIcon className="size-4" />
                        </button>
                      </div>
                    );
                  })}
                </div>
                <Popover>
                  <PopoverTrigger asChild>
                    <Button
                      variant="outline"
                      className="w-full h-12 justify-between font-normal"
                    >
                      {t("Tarifni tanlang")}
                      <ChevronDownIcon />
                    </Button>
                  </PopoverTrigger>
                  <PopoverContent className="p-0 w-[250px]" align="start">
                    <Command>
                      <CommandList>
                        <CommandGroup heading={t("Mavjud tariflar")}>
                          {tariff?.data?.data?.results?.map((item: any) => {
                            const selected = form
                              .getValues("tarif")
                              .some((t) => t.tariff === item.id);

                            return (
                              <CommandItem
                                key={item.id}
                                onSelect={() => {
                                  const current = form.getValues("tarif");
                                  if (selected) {
                                    form.setValue(
                                      "tarif",
                                      current.filter(
                                        (t) => t.tariff !== item.id,
                                      ),
                                    );
                                  } else {
                                    form.setValue("tarif", [
                                      ...current,
                                      { tariff: item.id, price: 0 },
                                    ]);
                                  }
                                }}
                                className="flex items-center gap-2"
                              >
                                <SquareCheckBig
                                  className={`size-4 ${
                                    selected
                                      ? "text-green-500"
                                      : "text-muted-foreground"
                                  }`}
                                />
                                {item.name}
                              </CommandItem>
                            );
                          })}
                        </CommandGroup>
                      </CommandList>
                    </Command>
                  </PopoverContent>
                </Popover>

                <FormMessage />
              </FormItem>
            )}
          />
        </div>

        <div className="grid grid-cols-2 max-lg:grid-cols-1 gap-6 items-end">
          <FormField
            control={form.control}
            name="transport"
            render={() => (
              <FormItem>
                <Label className="text-md">{t("Transportlar")}</Label>

                <div className="flex flex-col gap-3 mb-3">
                  {form.watch("transport")?.map((tr, idx) => {
                    const transportItem = transport?.data?.data?.results?.find(
                      (item: any) => item.id === tr.transport,
                    );
                    return (
                      <div
                        key={idx}
                        className="flex items-center justify-between border rounded-lg p-2"
                      >
                        <div className="flex items-center gap-2">
                          <Badge
                            variant="secondary"
                            className="px-3 py-1 text-sm"
                          >
                            {transportItem?.name ||
                              `Transport #${tr.transport}`}
                          </Badge>
                          <Input
                            type="text"
                            inputMode="numeric"
                            placeholder="1 500 000"
                            value={transportPrices[idx] || ""}
                            onChange={(e) => {
                              const raw = e.target.value.replace(/\D/g, "");
                              const num = Number(raw);
                              const updatedTransport = form
                                .getValues("transport")
                                .map((t, i) =>
                                  i === idx ? { ...t, price: num } : t,
                                );

                              form.setValue("transport", updatedTransport);

                              // Local holatda ham yangilaymiz:
                              const updatedPrices = [...transportPrices];
                              updatedPrices[idx] = raw ? formatPrice(num) : "";
                              setTransportPrices(updatedPrices);
                            }}
                            className="h-12 !text-md"
                          />
                        </div>
                        <button
                          type="button"
                          onClick={() => {
                            const current = form.getValues("transport");
                            form.setValue(
                              "transport",
                              current.filter((_, i) => i !== idx),
                            );
                            const updatedPrices = transportPrices.filter(
                              (_, i) => i !== idx,
                            );
                            setTransportPrices(updatedPrices);
                          }}
                          className="ml-2 text-muted-foreground hover:text-destructive"
                        >
                          <XIcon className="size-4" />
                        </button>
                      </div>
                    );
                  })}
                </div>

                <Popover>
                  <PopoverTrigger asChild>
                    <Button
                      variant="outline"
                      className="w-full h-12 justify-between font-normal"
                    >
                      {t("Transport tanlang")}
                      <ChevronDownIcon />
                    </Button>
                  </PopoverTrigger>

                  <PopoverContent className="p-0 w-[250px]" align="start">
                    <Command>
                      <CommandList>
                        <CommandGroup heading={t("Mavjud transportlar")}>
                          {transport?.data?.data?.results?.map((item: any) => {
                            const selected = form
                              .getValues("transport")
                              .some((t) => t.transport === item.id);

                            return (
                              <CommandItem
                                key={item.id}
                                onSelect={() => {
                                  const current = form.getValues("transport");
                                  if (selected) {
                                    form.setValue(
                                      "transport",
                                      current.filter(
                                        (t) => t.transport !== item.id,
                                      ),
                                    );
                                  } else {
                                    form.setValue("transport", [
                                      ...current,
                                      { transport: item.id, price: 0 },
                                    ]);
                                  }
                                }}
                                className="flex items-center gap-2"
                              >
                                <SquareCheckBig
                                  className={`size-4 ${
                                    selected
                                      ? "text-green-500"
                                      : "text-muted-foreground"
                                  }`}
                                />
                                {item.name}
                              </CommandItem>
                            );
                          })}
                        </CommandGroup>
                      </CommandList>
                    </Command>
                  </PopoverContent>
                </Popover>

                <FormMessage />
              </FormItem>
            )}
          />
        </div>

        <FormField
          control={form.control}
          name="visa_required"
          render={({ field }) => (
            <FormItem className="space-y-3">
              <Label>{t("Visa talab qilinadimi")}?</Label>
              <FormControl>
                <RadioGroup
                  onValueChange={field.onChange}
                  value={field.value}
                  className="flex gap-6"
                >
                  <div className="flex items-center space-x-2">
                    <RadioGroupItem value="yes" id="visa_yes" />
                    <label htmlFor="visa_yes">{t("Ha")}</label>
                  </div>
                  <div className="flex items-center space-x-2">
                    <RadioGroupItem value="no" id="visa_no" />
                    <label htmlFor="visa_no">{t("Yo'q")}</label>
                  </div>
                </RadioGroup>
              </FormControl>
              <FormMessage />
            </FormItem>
          )}
        />

        <TicketsImagesModel
          form={form}
          name="banner"
          multiple={false}
          label={t("Banner")}
          imageUrl={data?.data?.image_banner}
        />

        <TicketsImagesModel
          form={form}
          name="images"
          label={t("Qo'shimcha rasmlar")}
          imageUrl={data?.data.ticket_images?.map((img) => img.image)}
        />

        <FormField
          control={form.control}
          name="amenities"
          render={() => (
            <FormItem>
              <Label className="text-md">{t("Qulayliklar")}</Label>

              <div className="flex flex-col gap-4">
                <div className="flex flex-wrap gap-2">
                  {form.watch("amenities").map((item, idx) => {
                    const Icon = (LucideIcons as any)[item.icon_name] || XIcon;
                    return (
                      <Badge
                        key={idx}
                        variant="secondary"
                        className="px-3 py-1 text-sm flex items-center gap-2"
                      >
                        <Icon className="size-4" />
                        <span>{item.name}</span>
                        <button
                          type="button"
                          onClick={() => {
                            const current = form.getValues("amenities");
                            form.setValue(
                              "amenities",
                              current.filter((_, i: number) => i !== idx),
                            );
                          }}
                          className="ml-1 text-muted-foreground hover:text-destructive"
                        >
                          <XIcon className="size-4" />
                        </button>
                      </Badge>
                    );
                  })}
                </div>
                <div className="flex gap-3 items-end flex-wrap">
                  <div className="flex-1 min-w-[200px]">
                    <IconSelect
                      setSelectedIcon={setSelectedIcon}
                      selectedIcon={selectedIcon}
                    />
                  </div>

                  <Input
                    id="amenity_name"
                    placeholder={t("Qulaylik nomi (masalan: Wi-Fi)")}
                    className="h-12 !text-md flex-1 min-w-[200px]"
                  />

                  <Input
                    id="amenity_name_ru"
                    placeholder={t("Qulaylik nomi (ru)")}
                    className="h-12 !text-md flex-1 min-w-[200px]"
                  />

                  <Button
                    type="button"
                    onClick={() => {
                      const nameInput = document.getElementById(
                        "amenity_name",
                      ) as HTMLInputElement;
                      const nameRuInput = document.getElementById(
                        "amenity_name_ru",
                      ) as HTMLInputElement;

                      if (selectedIcon && nameInput.value) {
                        const current = form.getValues("amenities");
                        form.setValue("amenities", [
                          ...current,
                          {
                            icon_name: selectedIcon,
                            name: nameInput.value,
                            name_ru: nameRuInput.value,
                          },
                        ]);
                        nameInput.value = "";
                        nameRuInput.value = "";
                        setSelectedIcon("");
                      }
                    }}
                    className="h-12"
                  >
                    {t("Qo'shish")}
                  </Button>
                </div>

                <FormMessage />
              </div>
            </FormItem>
          )}
        />

        <FormField
          control={form.control}
          name="extra_service"
          render={() => (
            <FormItem>
              <Label className="text-md">{t("Bepul xizmatlar")}</Label>

              <div className="flex flex-col gap-4">
                {/* Ko'rsatilayotgan xizmatlar */}
                <div className="flex flex-wrap gap-2">
                  {(form.watch("extra_service") ?? []).map((item, idx) => (
                    <Badge
                      key={idx}
                      variant="secondary"
                      className="px-3 py-1 text-sm flex items-center gap-2"
                    >
                      <span>{item.name}</span>
                      <button
                        type="button"
                        onClick={() => {
                          const current = form.getValues("extra_service") ?? [];
                          form.setValue(
                            "extra_service",
                            current.filter((_, i) => i !== idx),
                          );
                        }}
                        className="ml-1 text-muted-foreground hover:text-destructive"
                      >
                        <XIcon className="size-4" />
                      </button>
                    </Badge>
                  ))}
                </div>

                {/* Yangi xizmat qo'shish */}
                <div className="flex gap-3 items-end flex-wrap">
                  <Input
                    id="extra_service_name"
                    placeholder={t("Xizmat nomi (UZ)")}
                    className="h-12 !text-md flex-1 min-w-[200px]"
                  />

                  <Input
                    id="extra_service_name_ru"
                    placeholder={t("Xizmat nomi (RU)")}
                    className="h-12 !text-md flex-1 min-w-[200px]"
                  />

                  <Button
                    type="button"
                    onClick={() => {
                      const nameInput = document.getElementById(
                        "extra_service_name",
                      ) as HTMLInputElement;
                      const nameRuInput = document.getElementById(
                        "extra_service_name_ru",
                      ) as HTMLInputElement;

                      if (nameInput.value && nameRuInput.value) {
                        const current = form.getValues("extra_service") ?? [];
                        form.setValue("extra_service", [
                          ...current,
                          {
                            name: nameInput.value,
                            name_ru: nameRuInput.value,
                          },
                        ]);
                        nameInput.value = "";
                        nameRuInput.value = "";
                      }
                    }}
                    className="h-12"
                  >
                    {t("Qo'shish")}
                  </Button>
                </div>

                <FormMessage />
              </div>
            </FormItem>
          )}
        />

        <FormField
          control={form.control}
          name="paid_extra_service"
          render={() => (
            <FormItem>
              <Label className="text-md">{t("Pullik xizmatlar")}</Label>

              <div className="flex flex-col gap-4">
                {/* Ro'yxat */}
                <div className="flex flex-wrap gap-2">
                  {(form.watch("paid_extra_service") || []).map((item, idx) => (
                    <Badge
                      key={idx}
                      variant="secondary"
                      className="px-3 py-1 text-sm flex items-center gap-2"
                    >
                      <span>
                        {item.name} —{" "}
                        <strong>{formatPrice(item.price)} so‘m</strong>
                      </span>
                      <button
                        type="button"
                        onClick={() => {
                          const current =
                            form.getValues("paid_extra_service") ?? [];
                          form.setValue(
                            "paid_extra_service",
                            current.filter((_, i) => i !== idx),
                          );
                        }}
                        className="ml-1 text-muted-foreground hover:text-destructive"
                      >
                        <XIcon className="size-4" />
                      </button>
                    </Badge>
                  ))}
                </div>

                {/* Qo'shish formasi */}
                <div className="flex gap-3 items-end flex-wrap">
                  <Input
                    id="paid_service_name"
                    placeholder={t("Xizmat nomi (UZ)")}
                    className="h-12 !text-md flex-1 min-w-[200px]"
                  />

                  <Input
                    id="paid_service_name_ru"
                    placeholder={t("Xizmat nomi (RU)")}
                    className="h-12 !text-md flex-1 min-w-[200px]"
                  />

                  {/* Narx maydoni */}
                  <Input
                    id="paid_service_price"
                    type="text"
                    inputMode="numeric"
                    placeholder="1 500 000"
                    className="h-12 !text-md w-[150px]"
                    onInput={(e) => {
                      const input = e.target as HTMLInputElement;
                      const raw = input.value.replace(/\D/g, "");
                      input.value = raw
                        ? Number(raw).toLocaleString("ru-RU")
                        : "";
                    }}
                  />

                  {/* Qo'shish tugmasi */}
                  <Button
                    type="button"
                    onClick={() => {
                      const nameInput = document.getElementById(
                        "paid_service_name",
                      ) as HTMLInputElement;
                      const nameRuInput = document.getElementById(
                        "paid_service_name_ru",
                      ) as HTMLInputElement;
                      const priceInput = document.getElementById(
                        "paid_service_price",
                      ) as HTMLInputElement;

                      const raw = priceInput.value.replace(/\D/g, "");
                      const num = Number(raw);

                      if (
                        nameInput.value.trim() &&
                        nameRuInput.value.trim() &&
                        !isNaN(num)
                      ) {
                        const current =
                          form.getValues("paid_extra_service") ?? [];
                        form.setValue("paid_extra_service", [
                          ...current,
                          {
                            name: nameInput.value.trim(),
                            name_ru: nameRuInput.value.trim(),
                            price: num, // 🟢 0 ham bo‘lishi mumkin
                          },
                        ]);

                        // inputlarni tozalaymiz
                        nameInput.value = "";
                        nameRuInput.value = "";
                        priceInput.value = "";
                      }
                    }}
                    className="h-12"
                  >
                    {t("Qo'shish")}
                  </Button>
                </div>

                <FormMessage />
              </div>
            </FormItem>
          )}
        />

        <FormField
          control={form.control}
          name="hotel_info"
          render={({ field }) => (
            <FormItem>
              <Label className="text-md">{t("Mehmonxona haqida")}</Label>
              <FormControl>
                <Textarea
                  placeholder={t("Mehmonxona haqida")}
                  {...field}
                  className="min-h-48 max-h-60 !text-md"
                />
              </FormControl>
              <FormMessage />
            </FormItem>
          )}
        />

        <FormField
          control={form.control}
          name="hotel_info_ru"
          render={({ field }) => (
            <FormItem>
              <Label className="text-md">{t("Mehmonxona haqida")} (ru)</Label>
              <FormControl>
                <Textarea
                  placeholder={t("Mehmonxona haqida") + " (ru)"}
                  {...field}
                  className="min-h-48 max-h-60 !text-md"
                />
              </FormControl>
              <FormMessage />
            </FormItem>
          )}
        />

        <FormField
          control={form.control}
          name="hotel_services"
          render={() => (
            <FormItem>
              <Label className="text-md">{t("Mehmonxona xizmatlari")}</Label>

              <div className="flex flex-col gap-4">
                <div className="flex flex-wrap gap-4">
                  {form.watch("hotel_services")?.map((service, idx) => (
                    <div
                      key={idx}
                      className="relative w-48 border rounded-xl overflow-hidden shadow-sm"
                    >
                      <img
                        src={
                          service.image instanceof File
                            ? URL.createObjectURL(service.image)
                            : service.image
                        }
                        alt={service.title}
                        className="object-cover w-full h-32"
                      />
                      <div className="p-2 text-center">
                        <p className="font-semibold">{service.title}</p>
                        <p className="text-sm text-muted-foreground line-clamp-2">
                          {service.description}
                        </p>
                      </div>
                      <button
                        type="button"
                        onClick={() => {
                          const current =
                            form.getValues("hotel_services") ?? [];
                          form.setValue(
                            "hotel_services",
                            current.filter((_, i) => i !== idx),
                          );
                        }}
                        className="absolute top-1 right-1 bg-white/80 rounded-full p-1 hover:bg-white shadow"
                      >
                        <XIcon className="size-4 text-destructive" />
                      </button>
                    </div>
                  ))}
                </div>

                <div className="flex flex-col gap-3 border rounded-xl p-4 bg-muted/10">
                  <Label className="text-md font-semibold">
                    {t("Yangi xizmat qo'shish")}
                  </Label>

                  <Input
                    type="file"
                    id="hotel_service_image"
                    accept="image/*"
                    className="h-12"
                  />

                  <Input
                    id="hotel_service_title"
                    placeholder={t("Xizmat nomi")}
                    className="h-12 !text-md"
                  />

                  <Input
                    id="hotel_service_title_ru"
                    placeholder={t("Xizmat nomi") + " (ru)"}
                    className="h-12 !text-md"
                  />

                  <Textarea
                    id="hotel_service_desc"
                    placeholder={t("Xizmat tavsifi")}
                    className="min-h-24 !text-md"
                  />

                  <Textarea
                    id="hotel_service_desc_ru"
                    placeholder={t("Xizmat tavsifi") + " (ru)"}
                    className="min-h-24 !text-md"
                  />

                  <Button
                    type="button"
                    onClick={() => {
                      const imgInput = document.getElementById(
                        "hotel_service_image",
                      ) as HTMLInputElement;
                      const titleInput = document.getElementById(
                        "hotel_service_title",
                      ) as HTMLInputElement;
                      const titleRuInput = document.getElementById(
                        "hotel_service_title_ru",
                      ) as HTMLInputElement;
                      const descInput = document.getElementById(
                        "hotel_service_desc",
                      ) as HTMLTextAreaElement;
                      const descRuInput = document.getElementById(
                        "hotel_service_desc_ru",
                      ) as HTMLTextAreaElement;

                      const file = imgInput.files?.[0];
                      if (file && titleInput.value && descInput.value) {
                        const current = form.getValues("hotel_services") || [];
                        form.setValue("hotel_services", [
                          ...current,
                          {
                            image: file,
                            title: titleInput.value,
                            title_ru: titleRuInput.value,
                            description: descInput.value,
                            desc_ru: descRuInput.value,
                          },
                        ]);

                        imgInput.value = "";
                        titleInput.value = "";
                        titleRuInput.value = "";
                        descInput.value = "";
                        descRuInput.value = "";
                      }
                    }}
                    className="h-12"
                  >
                    {t("Qo'shish")}
                  </Button>
                </div>
              </div>

              <FormMessage />
            </FormItem>
          )}
        />

        <FormField
          control={form.control}
          name="hotel_meals_info"
          render={({ field }) => (
            <FormItem>
              <Label className="text-md">
                {t("Mehmonxona taomlari haqida")}
              </Label>
              <FormControl>
                <Textarea
                  placeholder={t("Mehmonxona taomlari haqida")}
                  {...field}
                  className="min-h-48 max-h-60"
                />
              </FormControl>
              <FormMessage />
            </FormItem>
          )}
        />

        <FormField
          control={form.control}
          name="hotel_meals_info_ru"
          render={({ field }) => (
            <FormItem>
              <Label className="text-md">
                {t("Mehmonxona taomlari haqida")} (ru)
              </Label>
              <FormControl>
                <Textarea
                  placeholder={t("Mehmonxona taomlari haqida") + " (ru)"}
                  {...field}
                  className="min-h-48 max-h-60"
                />
              </FormControl>
              <FormMessage />
            </FormItem>
          )}
        />

        <FormField
          control={form.control}
          name="hotel_meals"
          render={() => (
            <FormItem>
              <Label className="text-md">{t("Mehmonxona taomlari")}</Label>

              <div className="flex flex-col gap-4">
                <div className="flex flex-wrap gap-4">
                  {form.watch("hotel_meals")?.map((service, idx) => (
                    <div
                      key={idx}
                      className="relative w-48 border rounded-xl overflow-hidden shadow-sm"
                    >
                      <img
                        src={
                          service.image instanceof File
                            ? URL.createObjectURL(service.image)
                            : service.image
                        }
                        alt={service.title}
                        className="object-cover w-full h-32"
                      />
                      <div className="p-2 text-center">
                        <p className="font-semibold">{service.title}</p>
                        <p className="text-sm text-muted-foreground line-clamp-2">
                          {service.description}
                        </p>
                      </div>
                      <button
                        type="button"
                        onClick={() => {
                          const current = form.getValues("hotel_meals") || [];
                          form.setValue(
                            "hotel_meals",
                            current.filter((_, i) => i !== idx),
                          );
                        }}
                        className="absolute top-1 right-1 bg-white/80 rounded-full p-1 hover:bg-white shadow"
                      >
                        <XIcon className="size-4 text-destructive" />
                      </button>
                    </div>
                  ))}
                </div>

                <div className="flex flex-col gap-3 border rounded-xl p-4 bg-muted/10">
                  <Label className="text-md font-semibold">
                    {t("Mehmonxona taomlari ro'yxati")}
                  </Label>

                  <Input
                    type="file"
                    id="hotel_meals_image"
                    accept="image/*"
                    className="h-12"
                  />

                  <Input
                    id="hotel_meals_title"
                    placeholder={t("Taom nomi")}
                    className="h-12 !text-md"
                  />

                  <Input
                    id="hotel_meals_title_ru"
                    placeholder={t("Taom nomi") + " (ru)"}
                    className="h-12 !text-md"
                  />

                  <Textarea
                    id="hotel_meals_desc"
                    placeholder={t("Taom tavsifi")}
                    className="min-h-24 !text-md"
                  />

                  <Textarea
                    id="hotel_meals_desc_ru"
                    placeholder={t("Taom tavsifi") + " (ru)"}
                    className="min-h-24 !text-md"
                  />

                  <Button
                    type="button"
                    onClick={() => {
                      const imgInput = document.getElementById(
                        "hotel_meals_image",
                      ) as HTMLInputElement;
                      const titleInput = document.getElementById(
                        "hotel_meals_title",
                      ) as HTMLInputElement;
                      const titleRuInput = document.getElementById(
                        "hotel_meals_title_ru",
                      ) as HTMLInputElement;
                      const descInput = document.getElementById(
                        "hotel_meals_desc",
                      ) as HTMLTextAreaElement;
                      const descRuInput = document.getElementById(
                        "hotel_meals_desc_ru",
                      ) as HTMLTextAreaElement;

                      const file = imgInput.files?.[0];
                      if (file && titleInput.value && descInput.value) {
                        const current = form.getValues("hotel_meals");
                        form.setValue("hotel_meals", [
                          ...current,
                          {
                            image: file,
                            title: titleInput.value,
                            title_ru: titleRuInput.value,
                            description: descInput.value,
                            desc_ru: descRuInput.value,
                          },
                        ]);
                        imgInput.value = "";
                        titleInput.value = "";
                        titleRuInput.value = "";
                        descInput.value = "";
                        descRuInput.value = "";
                      }
                    }}
                    className="h-12"
                  >
                    {t("Qo'shish")}
                  </Button>
                </div>
              </div>

              <FormMessage />
            </FormItem>
          )}
        />

        <FormField
          control={form.control}
          name="ticket_itinerary"
          render={() => (
            <FormItem>
              <Label className="text-md">{t("Yo'nalishlar")}</Label>

              <div className="flex flex-col gap-4">
                {/* mavjud yo‘nalishlar */}
                <div className="flex flex-wrap gap-4">
                  {form.watch("ticket_itinerary")?.map((item, idx) => (
                    <div
                      key={idx}
                      className="relative w-48 border rounded-xl overflow-hidden shadow-sm"
                    >
                      <img
                        src={
                          item.ticket_itinerary_image[0]?.image instanceof File
                            ? URL.createObjectURL(
                                item.ticket_itinerary_image[0]?.image,
                              )
                            : item.ticket_itinerary_image[0]?.image // agar serverdan kelsa
                        }
                        alt={item.title}
                        className="object-cover w-full h-32"
                      />
                      <div className="p-2 text-center">
                        <p className="font-semibold">{item.title}</p>
                        <p className="text-sm text-muted-foreground">
                          {item.ticket_itinerary_destinations[0]?.name}
                        </p>
                        <p className="text-sm text-muted-foreground">
                          {item.duration} {t("kun")}
                        </p>
                      </div>
                      <button
                        type="button"
                        onClick={() => {
                          const current =
                            form.getValues("ticket_itinerary") || [];
                          form.setValue(
                            "ticket_itinerary",
                            current.filter((_, i) => i !== idx),
                          );
                        }}
                        className="absolute top-1 right-1 bg-white/80 rounded-full p-1 hover:bg-white shadow"
                      >
                        <XIcon className="size-4 text-destructive" />
                      </button>
                    </div>
                  ))}
                </div>

                {/* yangi yo‘nalish qo‘shish formasi */}
                <div className="flex flex-col gap-3 border rounded-xl p-4 bg-muted/10">
                  <Label className="text-md font-semibold">
                    {t("Yo'nalish qo'shish")}
                  </Label>

                  <Input
                    type="file"
                    id="ticket_itinerary_image"
                    accept="image/*"
                    className="h-12"
                  />

                  <Input
                    id="ticket_itinerary_title"
                    placeholder={t("Sarlavha")}
                    className="h-12 !text-md"
                  />

                  <Input
                    id="ticket_itinerary_title_ru"
                    placeholder={t("Sarlavha") + " (ru)"}
                    className="h-12 !text-md"
                  />

                  <Input
                    id="ticket_itinerary_duration"
                    type="number"
                    min={1}
                    placeholder={t("Davomiylik (kun)")}
                    className="h-12 !text-md"
                  />

                  <Input
                    id="ticket_itinerary_destination"
                    placeholder={t("Manzil")}
                    className="h-12 !text-md"
                  />

                  <Input
                    id="ticket_itinerary_destination_ru"
                    placeholder={t("Manzil") + " (ru)"}
                    className="h-12 !text-md"
                  />

                  <Button
                    type="button"
                    onClick={() => {
                      const imgInput = document.getElementById(
                        "ticket_itinerary_image",
                      ) as HTMLInputElement;
                      const titleInput = document.getElementById(
                        "ticket_itinerary_title",
                      ) as HTMLInputElement;
                      const titleRuInput = document.getElementById(
                        "ticket_itinerary_title_ru",
                      ) as HTMLInputElement;
                      const durationInput = document.getElementById(
                        "ticket_itinerary_duration",
                      ) as HTMLInputElement;
                      const destInput = document.getElementById(
                        "ticket_itinerary_destination",
                      ) as HTMLInputElement;
                      const destRuInput = document.getElementById(
                        "ticket_itinerary_destination_ru",
                      ) as HTMLInputElement;

                      const file = imgInput.files?.[0];
                      const title = titleInput.value.trim();
                      const titleRu = titleRuInput.value.trim();
                      const duration = Number(durationInput.value);
                      const dest = destInput.value.trim();
                      const destRu = destRuInput.value.trim();

                      if (
                        file &&
                        title &&
                        titleRu &&
                        duration &&
                        dest &&
                        destRu
                      ) {
                        const current =
                          form.getValues("ticket_itinerary") || [];

                        form.setValue("ticket_itinerary", [
                          ...current,
                          {
                            ticket_itinerary_image: [{ image: file }],
                            title,
                            title_ru: titleRu,
                            duration,
                            ticket_itinerary_destinations: [
                              { name: dest, name_ru: destRu },
                            ],
                          },
                        ]);

                        imgInput.value = "";
                        titleInput.value = "";
                        titleRuInput.value = "";
                        durationInput.value = "";
                        destInput.value = "";
                        destRuInput.value = "";
                      }
                    }}
                    className="h-12"
                  >
                    {t("Qo'shish")}
                  </Button>
                </div>
              </div>

              <FormMessage />
            </FormItem>
          )}
        />

        <div className="w-full flex justify-end">
          <Button
            type="submit"
            className="mt-6 px-8 py-3 bg-blue-600 text-white rounded-md hover:bg-blue-600 cursor-pointer"
          >
            {t("Saqlash")}
          </Button>
        </div>
      </form>
    </Form>
  );
};

export default StepOne;
