<template>
  <div class="pharmacy-dispensing-container">
    <el-card class="header-card">
      <div class="header-actions">
        <el-button type="primary" @click="fetchData" data-cy="refresh-btn">刷新数据</el-button>
        <span class="mode-tag">当前模式: {{ mode === '1' ? '需申请模式' : '自动模式' }}</span>
      </div>
    </el-card>

    <el-row :gutter="20" class="content-row">
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>发药明细单</span>
              <span class="count-badge" data-cy="detail-count">({{ details.length }})</span>
            </div>
          </template>
          <el-table :data="details" border v-loading="loading" data-cy="dispensing-detail-table" style="width: 100%">
            <el-table-column prop="patient_name" label="患者姓名" width="120" />
            <el-table-column prop="drug_name" label="药品名称" />
            <el-table-column prop="spec" label="规格" width="100" />
            <el-table-column prop="quantity" label="数量" width="80" align="center" />
            <el-table-column prop="apply_status" label="状态" width="100" />
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>发药汇总单</span>
              <span class="count-badge" data-cy="summary-count">({{ summaries.length }})</span>
            </div>
          </template>
          <el-table :data="summaries" border v-loading="loading" data-cy="dispensing-summary-table" style="width: 100%">
            <el-table-column prop="ward_name" label="病区" width="120" />
            <el-table-column prop="total_items" label="总项数" width="100" align="center" />
            <el-table-column prop="total_quantity" label="总数量" width="100" align="center" />
            <el-table-column prop="apply_status" label="状态" width="100" />
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getDispensingData } from '@/api/pharmacy/dispensing'

const loading = ref(false)
const details = ref([])
const summaries = ref([])
const mode = ref('1')

const fetchData = async () => {
  loading.value = true
  try {
    // 统一调用单一接口获取明细与汇总，确保数据源与触发逻辑完全同步
    const res = await getDispensingData()
    details.value = res.data.details || []
    summaries.value = res.data.summaries || []
    mode.value = res.data.mode || '1'
  } catch (error) {
    console.error('获取发药数据失败:', error)
  } finally {
    loading.value = false
  }
}

onMounted(fetchData)
</script>

<style scoped>
.pharmacy-dispensing-container {
  padding: 20px;
}
.header-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.mode-tag {
  color: #606266;
  font-size: 14px;
}
.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
}
.count-badge {
  color: #909399;
  font-size: 13px;
}
.content-row {
  margin-top: 20px;
}
</style>
