<script setup>
import { computed, ref } from 'vue';
import { Button, message } from 'ant-design-vue';
import { usePrescriptionStore } from '#/store/prescription';
import { getDrugsByRegisterId } from '../api/index.ts';

const props = defineProps({
  content: {
    type: Object,
    required: true,
  },
});

const prescriptionStore = usePrescriptionStore();
const addingDrug = ref(false);

// 解析内容
const experienceData = computed(() => {
  if (typeof props.content === 'string') {
    try {
      return JSON.parse(props.content);
    } catch {
      return {};
    }
  }
  return props.content;
});

const hasVisitedText = computed(() => {
  return experienceData.value.has_visited === '1' || experienceData.value.has_visited === 1
    ? '已就诊过'
    : '未就诊过';
});

const hasUsedDrugText = computed(() => {
  return experienceData.value.has_used_drug === '1' || experienceData.value.has_used_drug === 1
    ? '已使用过药物'
    : '未使用过药物';
});

// 添加药品到处方单
const handleAddToPrescription = async () => {
  // 检查处方store是否已初始化
  if (!prescriptionStore.currentRegisterId) {
    message.warning('请先接诊患者');
    return;
  }

  // 获取门店ID
  let storeId = prescriptionStore.myStoreId;
  if (!storeId || storeId === 0) {
    if (prescriptionStore.myStoreList && prescriptionStore.myStoreList.length > 0) {
      storeId = prescriptionStore.myStoreList[0].id;
    } else {
      message.warning('请先选择诊所');
      return;
    }
  }

  addingDrug.value = true;
  try {
    // 根据 register_id 获取药品信息
    const res = await getDrugsByRegisterId(prescriptionStore.currentRegisterId, storeId);
    const drugList = res?.result || res?.data || res || [];

    if (!drugList || drugList.length === 0) {
      message.warning('未找到药品信息');
      return;
    }

    // 遍历药品列表，添加到处方单
    let addedCount = 0;
    for (const drugData of drugList) {
      const success = prescriptionStore.addProducts(drugData);
      if (success) {
        addedCount++;
      }
    }

    if (addedCount > 0) {
      message.success(`已成功添加 ${addedCount} 个药品到处方单`);
    } else {
      message.warning('所有药品已在处方中');
    }
  } catch (error) {
    console.error('添加药品失败:', error);
    message.error('添加药品失败，请重试');
  } finally {
    addingDrug.value = false;
  }
};
</script>

<template>
  <div class="patient-experience-card">
    <!-- 卡片头部 -->
    <div class="card-header">
      <div class="header-icon">
        <i class="fas fa-notes-medical"></i>
      </div>
      <div class="header-title">患者就诊经历</div>
      <div class="header-tags">
        <span :class="['tag', hasVisitedText === '已就诊过' ? 'tag-success' : 'tag-warning']">
          {{ hasVisitedText }}
        </span>
        <span :class="['tag', hasUsedDrugText === '已使用过药物' ? 'tag-info' : 'tag-default']">
          {{ hasUsedDrugText }}
        </span>
      </div>
    </div>

    <!-- 卡片内容 -->
    <div class="card-body">
      <!-- 症状描述 -->
      <div v-if="experienceData.symptom_description" class="info-row">
        <div class="info-icon">
          <i class="fas fa-stethoscope"></i>
        </div>
        <div class="info-content">
          <div class="info-label">症状描述</div>
          <div class="info-value">{{ experienceData.symptom_description }}</div>
        </div>
      </div>

      <!-- 病情信息 -->
      <div v-if="experienceData.illness_info" class="info-row">
        <div class="info-icon">
          <i class="fas fa-file-medical"></i>
        </div>
        <div class="info-content">
          <div class="info-label">病情信息</div>
          <div class="info-value">{{ experienceData.illness_info }}</div>
        </div>
      </div>

      <!-- 使用药物 -->
      <div v-if="experienceData.drug_name" class="info-row drug-row">
        <div class="info-icon">
          <i class="fas fa-pills"></i>
        </div>
        <div class="info-content">
          <div class="info-label">使用药物</div>
          <div class="info-value drug-name">{{ experienceData.drug_name }}</div>
        </div>
      </div>
    </div>

    <!-- 卡片底部操作 -->
    <div v-if="experienceData.drug_name" class="card-footer">
      <Button
        type="primary"
        :loading="addingDrug"
        @click="handleAddToPrescription"
        class="add-btn"
      >
        <i class="fas fa-plus-circle mr-1"></i>
        添加药品到处方单
      </Button>
    </div>
  </div>
</template>

<style scoped>
.patient-experience-card {
  width: 100%;
  min-width: 420px;
  max-width: 520px;
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  border-radius: 16px;
  border: 1px solid #e2e8f0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  transition: all 0.3s ease;
}

.patient-experience-card:hover {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

/* 卡片头部 */
.card-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  color: white;
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

.header-title {
  flex: 1;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.header-tags {
  display: flex;
  gap: 8px;
}

.tag {
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}

.tag-success {
  background: rgba(34, 197, 94, 0.2);
  color: #86efac;
}

.tag-warning {
  background: rgba(251, 191, 36, 0.2);
  color: #fcd34d;
}

.tag-info {
  background: rgba(96, 165, 250, 0.2);
  color: #93c5fd;
}

.tag-default {
  background: rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 0.8);
}

/* 卡片内容 */
.card-body {
  padding: 20px;
}

.info-row {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  padding: 14px 16px;
  background: white;
  border-radius: 12px;
  margin-bottom: 12px;
  border: 1px solid #e2e8f0;
  transition: all 0.2s ease;
}

.info-row:last-child {
  margin-bottom: 0;
}

.info-row:hover {
  border-color: #3b82f6;
  background: #f8fafc;
}

.info-icon {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #3b82f6;
  font-size: 14px;
  flex-shrink: 0;
}

.drug-row .info-icon {
  background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
  color: #d97706;
}

.info-content {
  flex: 1;
  min-width: 0;
}

.info-label {
  font-size: 12px;
  color: #64748b;
  margin-bottom: 4px;
  font-weight: 500;
}

.info-value {
  font-size: 14px;
  color: #1e293b;
  line-height: 1.6;
  word-break: break-word;
}

.drug-name {
  color: #d97706;
  font-weight: 600;
  /* background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%); */
  padding: 4px 12px;
  border-radius: 6px;
  display: inline-block;
}

/* 卡片底部 */
.card-footer {
  padding: 16px 20px;
  background: #f8fafc;
  border-top: 1px solid #e2e8f0;
  display: flex;
  justify-content: flex-end;
}

.add-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 20px;
  font-size: 14px;
  font-weight: 500;
  border-radius: 8px;
  transition: all 0.2s ease;
}

.add-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

/* 暗黑模式适配 */
.dark .patient-experience-card {
  background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
  border-color: #334155;
}

.dark .info-row {
  background: #1e293b;
  border-color: #334155;
}

.dark .info-row:hover {
  background: #334155;
}

.dark .info-value {
  color: #e2e8f0;
}

.dark .card-footer {
  background: #0f172a;
  border-color: #334155;
}
</style>

