<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form ref="queryRef" :model="queryParams" :inline="true" v-show="showSearch" label-width="68px">

        <el-form-item label="羊只耳号" prop="allEarNumbers">
          <div style="display: flex; align-items: center; gap: 10px; flex-wrap: wrap;">
            <!-- 主选择器：不显示已选标签 -->
            <el-select v-model="queryParams.allEarNumbers" multiple filterable remote reserve-keyword
              placeholder="输入耳号搜索" :remote-method="searchEarNumber" :loading="earNumberLoading" allow-create
              default-first-option collapse-tags :max-collapse-tags="0" style="width: 300px"
              @change="handleEarNumberChange">
              <el-option v-for="item in earNumberOptions" :key="item" :label="item" :value="item" />
            </el-select>


            <!-- 辅助粘贴输入框 -->
            <el-input v-model="pasteInput" placeholder="或粘贴多个耳号(空格/换行/逗号分隔)" style="width: 300px" @paste="handlePaste"
              @keyup.enter="handlePasteSubmit" clearable>
              <template #append>
                <el-button @click="handlePasteSubmit" :icon="Plus">添加</el-button>

              </template>

            </el-input>


            <!-- 耳号计数显示 -->
            <el-tag v-if="queryParams.allEarNumbers && queryParams.allEarNumbers.length > 0" type="info" effect="plain"
              size="large">
              已选: {{ queryParams.allEarNumbers.length }} 个
            </el-tag>


            <!-- 清空按钮 -->
            <el-button type="danger" plain @click="clearAllEarNumbers"
              v-if="queryParams.allEarNumbers && queryParams.allEarNumbers.length > 0" :icon="Delete">
              清空全部
            </el-button>

          </div>


          <!-- 已选耳号展示区域（默认显示2个，可展开） -->
          <div v-if="queryParams.allEarNumbers && queryParams.allEarNumbers.length > 0"
            class="selected-ear-numbers-display" style="margin-top: 10px;">
            <!-- 显示前2个或全部耳号 -->
            <el-tag v-for="(tag, index) in displayedEarNumbers" :key="tag" closable @close="handleRemoveEarNumber(tag)"
              style="margin: 4px;" type="success">
              {{ tag }}
            </el-tag>


            <!-- 展开/收起按钮 -->
            <el-button v-if="queryParams.allEarNumbers.length > defaultShowCount" type="primary" link
              @click="toggleExpand" style="margin-left: 8px;">
              {{ isExpanded ? '收起' : `展开剩余 ${queryParams.allEarNumbers.length - defaultShowCount} 个` }}
              <el-icon class="el-icon--right">
                <component :is="isExpanded ? ArrowUp : ArrowDown" />
              </el-icon>

            </el-button>

          </div>

        </el-form-item>

      <!-- <el-form-item label="羊只耳号" prop="sheepNo">
        <el-input v-model="queryParams.sheepNo" placeholder="请输入耳号,多个用空格分隔" clearable @keyup.enter="getList" />
      </el-form-item> -->
      <!-- 使用日期（新增） -->
      <el-form-item label="使用日期" style="width: 308px">
        <el-date-picker v-model="daterangeUseTime" type="daterange" range-separator="-" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="YYYY-MM-DD" />
      </el-form-item>
     <el-form-item label="使用名称" prop="name">
        <el-select v-model="queryParams.name" placeholder="请选择使用药品名称" clearable filterable style="width: 100%"
          @keyup.enter="handleQuery">
          <el-option v-for="item in medicines" :key="item.value" :label="item.label" :value="item.label" />
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker v-model="daterangeCreateTime" type="daterange" range-separator="-" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="YYYY-MM-DD" />
      </el-form-item>

      <el-form-item label="使用类型" prop="useType">
        <el-select v-model="queryParams.useType" placeholder="请选择使用类型" clearable style="width: 180px">
          <el-option v-for="dict in pres_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['biosafety:usage:export']">
          导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="usageList" @selection-change="handleSelectionChange" max-height="650px">
      <el-table-column label="使用时间" align="center" prop="datetime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.datetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="使用名称" align="center" prop="name" />
      <el-table-column label="羊只耳号" align="center" prop="sheepNo" />
      <el-table-column label="羊舍" align="center" prop="sheepfoldName" />
      <el-table-column label="使用类型" align="center" prop="useType">
        <template #default="scope">
          <dict-tag :options="pres_type" :value="scope.row.useType" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)"
            v-hasPermi="['biosafety:usage:query']">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" :page-sizes="[20, 50, 100, 200, 500, 1000, 2000]" @pagination="getList" />

    <!-- 详情弹窗（保持不变） -->
    <el-dialog :title="title" v-model="open" width="680px" top="8vh" append-to-body :close-on-click-modal="false"
      class="usage-detail-dialog">
      <!-- 基础信息 -->
      <el-card shadow="never" class="detail-card">
        <template #header>
          <div class="card-header">
            <el-icon>
              <Document />
            </el-icon> 基本信息
          </div>
        </template>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="使用名称">{{ form.name }}</el-descriptions-item>
          <el-descriptions-item label="使用类型">
            <el-tag size="small" :type="form.useType === 1 ? 'primary' : 'success'">
              {{ pres_type.find(it => it.value === form.useType)?.label || '' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="使用时间" :span="2">
            {{ parseTime(form.createTime, '{y}-{m}-{d} {h}:{i}') }}
          </el-descriptions-item>
        </el-descriptions>
      </el-card>

      <!-- 药品明细 -->
      <el-card shadow="never" class="detail-card" style="margin-top: 16px">
        <template #header>
          <div class="card-header">
            <el-icon>
              <List />
            </el-icon> 药品使用明细
          </div>
        </template>
        <el-table :data="swMedicineUsageDetailsList" stripe size="small"
          :row-class-name="rowSwMedicineUsageDetailsIndex" style="width: 100%">
          <el-table-column label="药品" prop="mediName" show-overflow-tooltip />
          <el-table-column label="用量" prop="dosage" width="80" />
          <el-table-column label="单位" prop="unit" align="center" :formatter="formatUnit" />
          <el-table-column label="使用方法" align="center" prop="usageId" :formatter="formatUsage" />
          <el-table-column v-if="form.useType==4" label="使用时间" align="center" prop="useTime" width="120">
            <template #default="scope">
              <span>{{ parseTime(scope.row.usetime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="生产厂家" prop="manufacturer" show-overflow-tooltip />
          <el-table-column label="生产批号" prop="batchNumber" width="120" />
        </el-table>
      </el-card>

      <template #footer>
        <el-button @click="cancel">关 闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Usage">
import { listMedicine} from "@/api/biosafety/medicine"
import { listUsageInfo, getUsageInfo } from '@/api/biosafety/usageInfo'
import { listUnit } from '@/api/biosafety/unit'
import { listUsage } from '@/api/biosafety/usage'
import { getCurrentInstance, reactive, ref, toRefs } from 'vue'
import { searchEarNumbers } from '@/api/common/sheep'

const { proxy } = getCurrentInstance()
const { pres_type } = proxy.useDict('pres_type')

const usageList = ref([])
const swMedicineUsageDetailsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const title = ref('')
const daterangeCreateTime = ref([])
const daterangeUseTime = ref([]) // 新增

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    allEarNumbers: [],
    name: null,
    useType: null,
    createTime: null
  },
  rules: {}
})

// 响应式数据
const pasteInput = ref('')           // 批量粘贴输入框
const earNumberOptions = ref([])     // 耳号下拉选项
const earNumberLoading = ref(false)  // 耳号加载状态
const isExpanded = ref(false)        // 控制耳号展开/折叠状态
const defaultShowCount = 2           // 默认显示的耳号数量

// 计算属性：控制显示的耳号列表
const displayedEarNumbers = computed(() => {
  if (!queryParams.allEarNumbers || queryParams.allEarNumbers.length === 0) {
    return []
  }

  // 如果展开或总数<=2，显示全部；否则只显示前2个
  if (isExpanded.value || queryParams.allEarNumbers.length <= defaultShowCount) {
    return queryParams.allEarNumbers
  } else {
    return queryParams.allEarNumbers.slice(0, defaultShowCount)
  }
})
/** 处理粘贴事件 */
function handlePaste(event) {
  nextTick(() => {
    handlePasteSubmit()
  })
}

/** 处理粘贴内容提交 */
function handlePasteSubmit() {
  if (!pasteInput.value || pasteInput.value.trim() === '') {
    return
  }

  // 支持多种分隔符: 空格、换行、逗号、制表符
  const separators = /[\s,，\n\r\t]+/
  const earNumbers = pasteInput.value
    .trim()
    .split(separators)
    .filter(item => item.trim() !== '')
    .map(item => item.trim())

  if (earNumbers.length === 0) {
    return
  }

  // 去重并添加到已选列表
  const existingSet = new Set(queryParams.value.allEarNumbers || [])
  const newEarNumbers = []
  const duplicates = []

  earNumbers.forEach(earNumber => {
    if (!existingSet.has(earNumber)) {
      newEarNumbers.push(earNumber)
      existingSet.add(earNumber)
    } else {
      duplicates.push(earNumber)
    }
  })

  // 添加新耳号
  if (newEarNumbers.length > 0) {
    queryParams.value.allEarNumbers = [
      ...(queryParams.value.allEarNumbers || []),
      ...newEarNumbers
    ]

    const message = `成功添加 ${newEarNumbers.length} 个耳号，当前共 ${queryParams.value.allEarNumbers.length} 个` +
      (duplicates.length > 0 ? `，已忽略 ${duplicates.length} 个重复耳号` : '')

    proxy.$modal.msgSuccess(message)
  } else if (duplicates.length > 0) {
    proxy.$modal.msgWarning(`所有耳号均已存在，当前共 ${queryParams.value.allEarNumbers.length} 个`)
  }

  // 清空输入框
  pasteInput.value = ''
}

/** 远程搜索耳号 */
function searchEarNumber(query) {
  if (query !== '') {
    earNumberLoading.value = true

    const queries = query.trim().split(/[\s,，]+/).filter(q => q)

    if (queries.length === 1) {
      // 单个耳号模糊搜索
      searchEarNumbers(query).then(response => {
        earNumberOptions.value = response.data || []
        earNumberLoading.value = false
      }).catch(() => {
        earNumberOptions.value = []
        earNumberLoading.value = false
      })
    } else {
      // 多个耳号直接作为选项
      earNumberOptions.value = queries
      earNumberLoading.value = false
    }
  } else {
    earNumberOptions.value = []
  }
}

/** 耳号选择变化处理 */
function handleEarNumberChange(value) {
  queryParams.value.allEarNumbers = [...new Set(value)]
  console.log(`已选择 ${queryParams.value.allEarNumbers.length} 个耳号:`, queryParams.value.allEarNumbers)
}

/** 切换展开/收起状态 */
function toggleExpand() {
  isExpanded.value = !isExpanded.value
}

/** 移除单个耳号 */
function handleRemoveEarNumber(tag) {
  const index = queryParams.value.allEarNumbers.indexOf(tag)
  if (index > -1) {
    queryParams.value.allEarNumbers.splice(index, 1)
    proxy.$modal.msgSuccess('已移除该耳号')
  }

  // 如果删除后剩余<=2个，自动收起
  if (queryParams.value.allEarNumbers.length <= defaultShowCount) {
    isExpanded.value = false
  }
}

/** 清空所有耳号 */
function clearAllEarNumbers() {
  proxy.$modal.confirm('确定要清空所有已选择的耳号吗？').then(() => {
    queryParams.value.allEarNumbers = []
    pasteInput.value = ''
    earNumberOptions.value = []
    isExpanded.value = false  // 重置展开状态
    proxy.$modal.msgSuccess('已清空所有耳号')
  }).catch(() => { })
}



const { queryParams, form, rules } = toRefs(data)

const units = ref([])
const usages = ref([])
const medicines = ref([])

/* 获取单位 */
function getUnit() {
  listUnit().then(res => {
    units.value = res.rows.map(item => ({ value: item.id, label: item.name }))
  })
}
/* 获取药品信息 */
function getMedicineOptions() {
  listMedicine().then(res => {
    medicines.value = res.rows.map(item => ({ value: item.id, label: item.name }))
  })
}

/* 获取使用方法 */
function getUsageOptions() {
  listUsage().then(res => {
    usages.value = res.rows.map(item => ({ value: item.id, label: item.name }))
  })
}
/* 格式化单位 */
function formatUnit(row) {
  const unit = units.value.find(u => String(u.value) === String(row.unit))
  return unit?.label ?? ''
}
/* 格式化使用方法 */
function formatUsage(row) {
  const usage = usages.value.find(u => String(u.value) === String(row.usageId))
  return usage?.label ?? ''
}

/* 查询列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}

  if (daterangeCreateTime.value && daterangeCreateTime.value.length === 2) {
    queryParams.value.params.beginCreateTime = daterangeCreateTime.value[0]
    queryParams.value.params.endCreateTime = daterangeCreateTime.value[1]
  }
  if (daterangeUseTime.value && daterangeUseTime.value.length === 2) {
    queryParams.value.params.beginUseTime = daterangeUseTime.value[0]
    queryParams.value.params.endUseTime = daterangeUseTime.value[1]
  }

  listUsageInfo(queryParams.value).then(res => {
    usageList.value = res.rows
    total.value = res.total
    loading.value = false
  })
}

function reset() {
  form.value = {
    id: null,
    name: null,
    useType: null,
    createBy: null,
    createTime: null
  }
  swMedicineUsageDetailsList.value = []
}
function cancel() {
  open.value = false
  reset()
}
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
function resetQuery() {
  daterangeCreateTime.value = []
  daterangeUseTime.value = [] // 新增
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleDetail(row) {
  reset()
  getUsageInfo(row.id).then(res => {
    form.value = res.data
    swMedicineUsageDetailsList.value = res.data.swMedicineUsageDetailsList || []
    open.value = true
    title.value = '药品使用记录详情'
  })
}
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
}
function rowSwMedicineUsageDetailsIndex({ rowIndex }) {
  return `row-${rowIndex}`
}
function handleExport() {
  proxy.download(
    'biosafety/usageInfo/export',
    { ...queryParams.value },
    `药品使用记录_${new Date().getTime()}.xlsx`
  )
}

/* 初始化 */
getList()
getUnit()
getMedicineOptions()
getUsageOptions()
</script>

<style scoped>
.usage-detail-dialog .detail-card {
  border-radius: 6px;
  margin: 0;
}
.usage-detail-dialog .card-header {
  display: flex;
  align-items: center;
  font-weight: 600;
  color: #303133;
}
.usage-detail-dialog .card-header .el-icon {
  margin-right: 6px;
}
</style>
<style scoped>
/* 已选耳号展示区样式 */
.selected-ear-numbers-display {
  max-height: 150px;
  overflow-y: auto;
  padding: 8px;
  background-color: #f5f7fa;
  border-radius: 4px;
  border: 1px dashed #dcdfe6;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}

.selected-ear-numbers-display::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

.selected-ear-numbers-display::-webkit-scrollbar-thumb {
  background-color: #dcdfe6;
  border-radius: 3px;
}

.selected-ear-numbers-display::-webkit-scrollbar-track {
  background-color: #f5f7fa;
}

.selected-ear-numbers-display .el-tag {
  margin: 4px;
  cursor: pointer;
}

.selected-ear-numbers-display .el-tag:hover {
  opacity: 0.8;
}

/* 隐藏 el-select 的下拉箭头（可选） */
:deep(.el-select__caret) {
  display: none;
}
</style>
