<script setup>
import { PlusIcon, DottedMenu, EditIcon, TrashBinIcon } from "@/Utilities/Icons";
import Dropdown from "../Dropdown.vue";

const props = defineProps({
  person: Object,
  edit: { type: Boolean, default: true },
  enableSms: { type: Boolean, default: false },
});

const emit = defineEmits(['add', 'edit', 'delete', 'sms']);

const getPhones = (p) => (Array.isArray(p?.phones) ? p.phones : []);

const handleAdd = () => emit('add');
const handleEdit = (id) => emit('edit', id);
const handleDelete = (id, label) => emit('delete', id, label);
const handleSms = (phone) => emit('sms', phone);
</script>

<template>
  <div class="flex justify-end mb-3" v-if="edit">
      <button
        type="button"
        @click="handleAdd"
        class="inline-flex items-center gap-2 px-3 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-lg hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2 transition-all"
        title="Dodaj telefon"
      >
        <PlusIcon size="sm" />
        <span>Dodaj telefon</span>
      </button>
    </div>
  <div class="grid grid-rows-* grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3 mt-3">
    <template v-if="getPhones(person).length">
      <div
        class="rounded-lg p-4 bg-white border border-gray-200 shadow-sm hover:shadow-md transition-shadow"
        v-for="phone in getPhones(person)"
        :key="phone.id"
      >
        <div class="flex items-start justify-between mb-2">
          <div class="flex flex-wrap gap-2">
            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-yellow-100 text-yellow-800">
              +{{ phone.country_code }}
            </span>
            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800">
              {{ phone && phone.type && phone.type.name ? phone.type.name : "—" }}
            </span>
          </div>
          <div class="flex items-center gap-1">
            <!-- Send SMS only in ClientCase person context -->
            <button
              v-if="enableSms"
              @click="handleSms(phone)"
              title="Pošlji SMS"
              class="px-2.5 py-1 text-xs font-medium text-indigo-700 bg-indigo-50 border border-indigo-200 hover:bg-indigo-100 rounded-lg transition-colors"
            >
              SMS
            </button>
            <Dropdown v-if="edit" align="right" width="48">
              <template #trigger>
                <button
                  type="button"
                  class="p-1 rounded hover:bg-gray-100 border border-transparent hover:border-gray-200 transition-colors"
                  title="Možnosti"
                >
                  <DottedMenu size="sm" css="text-gray-600" />
                </button>
              </template>
              <template #content>
                <div class="py-1">
                  <button
                    @click="handleEdit(phone.id)"
                    class="w-full flex items-center gap-2 px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 transition-colors"
                  >
                    <EditIcon size="sm" />
                    <span>Uredi</span>
                  </button>
                  <button
                    @click="handleDelete(phone.id, phone.nu)"
                    class="w-full flex items-center gap-2 px-4 py-2 text-sm text-red-600 hover:bg-red-50 transition-colors"
                  >
                    <TrashBinIcon size="sm" />
                    <span>Izbriši</span>
                  </button>
                </div>
              </template>
            </Dropdown>
          </div>
        </div>
        <p class="text-sm font-medium text-gray-900 leading-relaxed">{{ phone.nu }}</p>
      </div>
    </template>
    <p v-else class="col-span-full p-4 text-sm text-gray-500 text-center bg-gray-50 rounded-lg border border-gray-200">
      Ni telefonov.
    </p>
  </div>
</template>

