'use client';

import Passport from '@/assets/passport.jpg';
import { getBoxes } from '@/shared/config/api/testApi';
import formatPhone from '@/shared/lib/formatPhone';
import { useQuery } from '@tanstack/react-query';
import Image from 'next/image';
import { useParams } from 'next/navigation';

const Boxes = () => {
  const products = [
    {
      name: 'Quloqchin',
      nameRu: 'Наушники',
      trackId: 'TRK-001',
      quantity: 2,
      price: '120 000 UZS',
      weight: 0.4,
    },
    {
      name: 'Zaryadka',
      nameRu: 'Зарядка',
      trackId: 'TRK-002',
      quantity: 1,
      price: '60 000 UZS',
      weight: 0.2,
    },
  ];

  const { id } = useParams();

  const { data } = useQuery({
    queryKey: ['boxes_detail'],
    queryFn: () => getBoxes.boxesDetail({ id: Number(id) }),
    select(data) {
      return data.data.data;
    },
  });

  console.log(data);

  return (
    <div className="flex flex-col gap-10 container w-full justify-start items-center h-screen">
      <div className="bg-gray-100/50 shadow-xl p-4 py-6 rounded-lg w-[50%] max-lg:w-[90%] flex flex-col gap-6">
        <h1 className="text-3xl text-blue-700 text-center font-semibold">
          Karobka maʼlumotlari
        </h1>
        <div className="flex justify-between max-lg:flex-col">
          <p className="text-lg font-semibold">
            ID: <span className="font-normal">{data?.boxName}</span>
          </p>
          <p className="text-lg font-semibold">
            Partiya: <span className="font-normal">{data?.partyName}</span>
          </p>
          <p className="text-lg font-semibold">
            Paketlar soni:{' '}
            <span className="font-normal">{data?.packetCount}</span>
          </p>
        </div>
      </div>
      {data?.packets.map((e) => (
        <div
          className="bg-gray-100/50 shadow-xl p-4 py-6 rounded-lg w-[50%] max-lg:w-[90%] flex flex-col gap-6"
          key={e.id}
        >
          <div className="flex justify-between">
            <p className="text-md font-semibold text-purple-600">
              Paket:
              <span className="font-normal">{data?.boxName}</span>
              <span className="text-xs ml-2 bg-blue-100 text-blue-700 px-2 py-0.5 rounded">
                Status: {e.packetStatus}
              </span>
            </p>
            <p className="text-md text-foreground font-semibold">
              Cargo ID:<span className="font-normal">{e.userInfo.cargoID}</span>
            </p>
          </div>

          <div className="w-full h-[1px] bg-black" />

          <div className="grid grid-cols-2 gap-4 max-lg:grid-cols-1">
            <div className="bg-muted-foreground/10 rounded-md p-2 flex flex-col gap-2">
              <p>Foydalanuvchi</p>
              <p className="text-md font-semibold">
                Ismi:
                <span className="font-normal pl-2">{e.userInfo.fullName}</span>
              </p>
              <p className="text-md font-semibold">
                Cargo ID:
                <span className="font-normal pl-2">{e.userInfo.cargoID}</span>
              </p>
              <p className="text-md font-semibold">
                Manzil:
                <span className="font-normal pl-2">{e.userInfo.address}</span>
              </p>
              <p className="text-md font-semibold">
                Telefon:
                <span className="font-normal pl-2">
                  {formatPhone(e.userInfo.phone)}
                </span>
              </p>
            </div>
            <div className="bg-muted-foreground/10  rounded-md flex flex-col gap-2 p-2">
              <p>Passport</p>
              <p className="text-md font-semibold">
                Seriya:
                <span className="font-normal pl-2">
                  {e.passportInfo.passportSeries}
                </span>
              </p>
              <p className="text-md font-semibold">
                PINFL:
                <span className="font-normal pl-2">
                  {e.passportInfo.passportPin}
                </span>
              </p>
              <div className="text-md font-semibold">
                <p>Passport rasmi:</p>
                <div className="flex gap-4">
                  <a
                    href={Passport.src}
                    target="_blank"
                    rel="noopener noreferrer"
                  >
                    <Image
                      src={Passport}
                      alt="Passport"
                      className="w-16 mt-2"
                    />
                  </a>
                  <a
                    href={Passport.src}
                    target="_blank"
                    rel="noopener noreferrer"
                  >
                    <Image
                      src={Passport}
                      alt="Passport"
                      className="w-16 mt-2"
                    />
                  </a>
                </div>
              </div>
            </div>
          </div>

          <div>
            <h5 className="text-lg font-semibold mb-2">Mahsulotlar</h5>
            <div className="overflow-x-auto rounded-lg border border-gray-300">
              <table className="min-w-full divide-y divide-gray-200 text-sm text-left">
                <thead className="bg-blue-100 text-blue-800">
                  <tr>
                    <th className="px-3 py-2 border whitespace-nowrap">T/r</th>
                    <th className="px-3 py-2 border whitespace-nowrap">Nomi</th>
                    <th className="px-3 py-2 border whitespace-nowrap">
                      Nomi (Ru)
                    </th>
                    <th className="px-3 py-2 border whitespace-nowrap">
                      Trek ID
                    </th>
                    <th className="px-3 py-2 border whitespace-nowrap">Soni</th>
                    <th className="px-3 py-2 border whitespace-nowrap">
                      Narxi
                    </th>
                    <th className="px-3 py-2 border whitespace-nowrap">
                      Og‘irligi (kg)
                    </th>
                  </tr>
                </thead>
                <tbody className="bg-white divide-y divide-gray-200">
                  {e.items.map((item, index) => (
                    <tr key={index} className="hover:bg-gray-50">
                      <td className="px-3 py-2 border whitespace-nowrap">
                        {index + 1}
                      </td>
                      <td className="px-3 py-2 border whitespace-nowrap">
                        {item.name}
                      </td>
                      <td className="px-3 py-2 border whitespace-nowrap">
                        {item.nameRu}
                      </td>
                      <td className="px-3 py-2 border whitespace-nowrap">
                        {item.trekId}
                      </td>
                      <td className="px-3 py-2 border whitespace-nowrap">
                        {item.amount}
                      </td>
                      <td className="px-3 py-2 border whitespace-nowrap">
                        {item.price}
                      </td>
                      <td className="px-3 py-2 border whitespace-nowrap">
                        {item.netWeight}
                      </td>
                    </tr>
                  ))}
                </tbody>
              </table>
            </div>
          </div>
        </div>
      ))}
    </div>
  );
};

export default Boxes;
