<template>
  <div
    ref="tableWrapper"
    tabindex="0"
    class="table-container"
    @keyup="handleKeyDown"
  >
    <el-table
      ref="adviceBaseRef"
      v-loading="loading"
      height="350"
      :data="adviceBaseList"
      highlight-current-row
      row-key="adviceCode"
      @current-change="handleCurrentChange"
      @cell-click="clickRow"
    >
      <el-table-column
        label="名称"
        align="center"
        prop="adviceName"
      />
      <el-table-column
        label="类型"
        align="center"
        prop="categoryCodeText"
      />
      <el-table-column
        label="医保等级"
        align="center"
        prop="chrgitmLv_dictText"
      />
      <el-table-column
        label="包装单位"
        align="center"
        prop="unitCode_dictText"
      />
      <el-table-column
        label="最小单位"
        align="center"
        prop="minUnitCode_dictText"
      />
      <el-table-column
        label="库存数量"
        align="center"
      >
        <template #default="scope">
          {{ handleQuantity(scope.row) }}
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <div class="pagination-wrapper">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        :page-size="20"
        :total="total"
        layout="prev, pager, next"
        @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, watch, nextTick } from 'vue';
import { getTcmMedicine } from './api';
import { debounce } from 'lodash-es';

const props = defineProps({
  searchKey: {
    type: String,
    default: '',
  },
  organizationId: {
    type: String,
    default: '',
  },
});

const emit = defineEmits(['selectMedicine']);

const total = ref(0);
const loading = ref(false);
const adviceBaseRef = ref();
const tableWrapper = ref();
const currentIndex = ref(0);
const currentSelectRow = ref({});
const queryParams = ref({
  pageSize: 20,
  pageNum: 1,
  searchKey: '',
  organizationId: props.organizationId,
});
const adviceBaseList = ref([]);

// 前端缓存 - 使用 sessionStorage 持久化缓存
const TCM_CACHE_PREFIX = 'tcm_advice_cache_';
const TCM_CACHE_EXPIRE = 5 * 60 * 1000; // 缓存5分钟

function getTcmCacheKey(orgId, pageNum, searchKey) {
  return TCM_CACHE_PREFIX + orgId + '_' + pageNum + '_' + (searchKey || 'none');
}

function getFromTcmCache(orgId, pageNum, searchKey) {
  try {
    const key = getTcmCacheKey(orgId, pageNum, searchKey);
    const cachedData = sessionStorage.getItem(key);
    if (!cachedData) return null;
    
    const cacheData = JSON.parse(cachedData);
    if (Date.now() - cacheData.timestamp > TCM_CACHE_EXPIRE) {
      sessionStorage.removeItem(key);
      return null;
    }
    console.log('从TCM前端缓存获取:', key);
    return cacheData;
  } catch (e) {
    console.error('读取TCM缓存失败:', e);
    return null;
  }
}

function setToTcmCache(orgId, pageNum, searchKey, data, total) {
  try {
    const key = getTcmCacheKey(orgId, pageNum, searchKey);
    const cacheData = {
      data: data,
      total: total,
      timestamp: Date.now()
    };
    sessionStorage.setItem(key, JSON.stringify(cacheData));
    console.log('写入TCM前端缓存:', key);
  } catch (e) {
    console.error('写入TCM缓存失败:', e);
  }
}

// 防抖函数 - 避免重复请求
const debouncedGetList = debounce(
  () => {
    // 只有当 organizationId 有效时才请求
    if (!queryParams.value.organizationId) {
      console.log('organizationId 无效，跳过请求');
      return;
    }
    getList();
  },
  300,
  { leading: false, trailing: true }
);

// 监听搜索关键字变化
watch(
  () => props.searchKey,
  (newValue) => {
    queryParams.value.searchKey = newValue || '';
    debouncedGetList();
  },
  { deep: true }
);

// 监听 organizationId 变化
watch(
  () => props.organizationId,
  (newValue) => {
    queryParams.value.organizationId = newValue;
    // organizationId 变化时重新加载
    if (newValue) {
      debouncedGetList();
    }
  },
  { deep: true }
);

// 不再页面加载时立即请求，等待用户点击时再请求

function getList() {
  const orgId = queryParams.value.organizationId;
  const pageNum = queryParams.value.pageNum;
  const searchKey = queryParams.value.searchKey;
  
  // 尝试从本地缓存获取（只有第一页且无搜索关键字时使用缓存）
  if (pageNum === 1 && !searchKey) {
    const cached = getFromTcmCache(orgId, pageNum, searchKey);
    if (cached) {
      adviceBaseList.value = cached.data;
      total.value = cached.total;
      return;
    }
  }
  
  // 显示 loading
  loading.value = true;
  
  getTcmMedicine(queryParams.value).then((res) => {
    adviceBaseList.value = res.data.records || [];
    total.value = res.data.total || 0;
    
    // 缓存第一页数据
    if (pageNum === 1 && !searchKey) {
      setToTcmCache(orgId, pageNum, searchKey, adviceBaseList.value, total.value);
    }
    
    nextTick(() => {
      currentIndex.value = 0;
      if (adviceBaseList.value.length > 0 && adviceBaseRef.value) {
        adviceBaseRef.value.setCurrentRow(adviceBaseList.value[0]);
      }
    });
  })
  .finally(() => {
    loading.value = false;
  });
}

// 当前页改变
function handlePageChange(page) {
  queryParams.value.pageNum = page;
  getList();
}

// 处理键盘事件
const handleKeyDown = (event) => {
  const key = event.key;
  const data = adviceBaseList.value;

  switch (key) {
    case 'ArrowUp':
      event.preventDefault();
      if (currentIndex.value > 0) {
        currentIndex.value--;
        setCurrentRow(data[currentIndex.value]);
      }
      break;
    case 'ArrowDown':
      event.preventDefault();
      if (currentIndex.value < data.length - 1) {
        currentIndex.value++;
        setCurrentRow(data[currentIndex.value]);
      }
      break;
    case 'Enter':
      event.preventDefault();
      if (currentSelectRow.value) {
        emit('selectMedicine', currentSelectRow.value);
      }
      break;
  }
};

function handleQuantity(row) {
  if (row.inventoryList) {
    const totalQuantity = row.inventoryList.reduce((sum, item) => sum + (item.quantity || 0), 0);
    return totalQuantity.toString() + (row.minUnitCode_dictText || '');
  }
  return 0;
}

// 设置选中行（带滚动）
const setCurrentRow = (row) => {
  adviceBaseRef.value.setCurrentRow(row);
  const tableBody = adviceBaseRef.value.$el.querySelector('.el-table__body-wrapper');
  const currentRowEl = adviceBaseRef.value.$el.querySelector('.current-row');
  if (tableBody && currentRowEl) {
    currentRowEl.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
  }
};

// 当前行变化时更新索引
const handleCurrentChange = (currentRow) => {
  currentIndex.value = adviceBaseList.value.findIndex((item) => item === currentRow);
  currentSelectRow.value = currentRow;
};

function clickRow(row) {
  emit('selectMedicine', row);
}

defineExpose({
  handleKeyDown,
});
</script>

<style scoped>
.table-container {
  display: flex;
  flex-direction: column;
}
.pagination-wrapper {
  display: flex;
  justify-content: center;
  padding: 10px 0;
  background: #fff;
}
.popover-table-wrapper:focus {
  outline: 2px solid #3B82F6;
}
</style>
