<script lang="ts" setup>
/**
 * 商品信息合并列：图片 + 名称/ID/拼音/发布时间 + 说明书预览链接
 * 点击药名打开详情弹窗（由父级监听 name-click）
 * 搜索命中别名时展示匹配别名（对齐开方/仓库列表）
 */
import { computed, ref } from 'vue';

import { Image } from 'ant-design-vue';

const props = withDefaults(
  defineProps<{
    row: Record<string, any>;
    /** 药名是否可点击打开详情 */
    nameClickable?: boolean;
  }>(),
  { nameClickable: true },
);

const emit = defineEmits<{
  'name-click': [row: Record<string, any>];
}>();

const previewVisible = ref(false);

const instructionUrl = computed(() => {
  const url = props.row?.instruction;
  return typeof url === 'string' && url.trim() ? url.trim() : '';
});

/** 当前搜索命中的别名 */
const matchedAlias = computed(() =>
  String(props.row?.matched_alias || '').trim(),
);

/** 拼音：命中别名时用别名拼音，否则用药名首拼 */
const pinyinDisplay = computed(() => {
  const aliasPy = String(props.row?.matched_alias_pinyin || '').trim();
  if (matchedAlias.value && aliasPy) return aliasPy;
  return String(props.row?.pinyin_simple || '').trim();
});

function openInstruction() {
  if (!instructionUrl.value) return;
  previewVisible.value = true;
}

function onNameClick() {
  if (!props.nameClickable) return;
  emit('name-click', props.row);
}
</script>
<template>
  <div class="product-info-cell">
    <Image
      v-if="row.image"
      :src="row.image"
      :width="40"
      :height="40"
      class="product-info-cell__img"
    />
    <div v-else class="product-info-cell__img product-info-cell__img--empty">
      无图
    </div>
    <div class="product-info-cell__body">
      <div
        class="product-info-cell__name"
        :class="{ 'product-info-cell__name--link': nameClickable }"
        :title="
          matchedAlias
            ? `${row.drug_name || '-'}（别名：${matchedAlias}）`
            : row.drug_name || '-'
        "
        @click.stop="onNameClick"
      >
        {{ row.drug_name || '-' }}
      </div>
      <div v-if="matchedAlias" class="product-info-cell__alias">
        别名：{{ matchedAlias }}
        <span v-if="pinyinDisplay">（{{ pinyinDisplay }}）</span>
      </div>
      <div class="product-info-cell__meta">
        <span>ID：{{ row.id }}</span>
        <span v-if="pinyinDisplay && !matchedAlias">拼音：{{ pinyinDisplay }}</span>
        <span v-if="row.created_at">发布时间：{{ row.created_at }}</span>
      </div>
      <a
        v-if="instructionUrl"
        class="product-info-cell__link text-primary"
        @click.stop="openInstruction"
      >
        说明书
      </a>
      <Image
        v-if="instructionUrl"
        :src="instructionUrl"
        :style="{ display: 'none' }"
        :preview="{
          visible: previewVisible,
          onVisibleChange: (v: boolean) => (previewVisible = v),
        }"
      />
    </div>
  </div>
</template>
<style scoped>
.product-info-cell {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 4px 0;
}
.product-info-cell__img {
  flex-shrink: 0;
  border-radius: 4px;
  object-fit: cover;
}
.product-info-cell__img--empty {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: hsl(var(--muted-foreground));
  background: hsl(var(--muted) / 0.3);
  border: 1px solid hsl(var(--border));
  border-radius: 4px;
}
.product-info-cell__body {
  min-width: 0;
  flex: 1;
}
.product-info-cell__name {
  font-weight: 500;
  color: hsl(var(--foreground));
  line-height: 1.4;
  word-break: break-all;
}
.product-info-cell__name--link {
  color: hsl(var(--primary));
  cursor: pointer;
}
.product-info-cell__name--link:hover {
  text-decoration: underline;
}
.product-info-cell__alias {
  margin-top: 2px;
  font-size: 12px;
  color: hsl(var(--muted-foreground));
  line-height: 1.3;
  word-break: break-all;
}
.product-info-cell__meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  margin-top: 4px;
  font-size: 12px;
  color: hsl(var(--muted-foreground));
}
.product-info-cell__link {
  display: inline-block;
  margin-top: 4px;
  font-size: 12px;
  cursor: pointer;
}
.product-info-cell__link:hover {
  text-decoration: underline;
}
</style>
