<template>
  <scroll-view scroll-y class="page">
    <view v-if="inventoryPlanList.length === 0 && !loading" class="empty">暂无更多数据</view>

    <view class="inventory-list">
      <view class="inventory-item" v-for="item in inventoryPlanList" :key="item.inventoryId || item.id" @tap="gotoPlanDetail(item)">
        <view class="header">
          <text class="title">📋 盘点计划</text>
          <text class="status-tag" :class="statusClass(item.status)">{{ item.status || '未知' }}</text>
        </view>
        <view class="content">
          <view class="row">开始日期：<text class="value">{{ item.checkStartDate || '-' }}</text></view>
          <view class="row">结束日期：<text class="value">{{ item.checkEndDate || '-' }}</text></view>
          <view class="row">创建人：<text class="value">{{ item.createByName || '-' }}</text></view>
        </view>
      </view>
    </view>

    <view class="load-status">
      <text v-if="loading">加载中...</text>
      <text v-else-if="noMore">没有更多数据了</text>
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onLoad, onReachBottom } from '@dcloudio/uni-app'
// @ts-ignore JS模块无类型声明
import { getConsumpationPlan } from '@/pages/api/fixedAssets.js'

type Plan = Record<string, any>

const page = ref(1)
const pageSize = ref(10)
const loading = ref(false)
const noMore = ref(false)
const inventoryPlanList = ref<Plan[]>([])

function statusClass(status?: string) {
  if (status === '已结束') return 'ended'
  if (status === '正在进行') return 'running'
  return 'unknown'
}

async function fetchInventoryList() {
  if (loading.value || noMore.value) return
  loading.value = true
  try {
    const res = await getConsumpationPlan(page.value, pageSize.value)
    const list: any[] = (res?.data?.list) || (res?.data?.records) || (Array.isArray(res?.data) ? res.data : []) || []
    inventoryPlanList.value = inventoryPlanList.value.concat(list)
    if (list.length < pageSize.value) noMore.value = true
  } catch (e) {
    console.error('获取易耗品盘点计划失败', e)
    uni.showToast({ title: '加载失败', icon: 'none' })
  } finally {
    loading.value = false
  }
}

function gotoPlanDetail(item: any) {
  const planId = item?.id || item?.inventoryId
  if (!planId && planId !== 0) {
    uni.showToast({ title: '缺少计划ID', icon: 'none' })
    return
  }
  if (item.status === '已结束') {
    uni.navigateTo({ url: `/pages/consumables/inventory-end?planId=${planId}` })
  } else {
    uni.navigateTo({ url: `/pages/consumables/inventory-ing?planId=${planId}` })
  }
}

onLoad(() => {
  fetchInventoryList()
})

onReachBottom(() => {
  if (!loading.value && !noMore.value) {
    page.value += 1
    fetchInventoryList()
  }
})
</script>

<style scoped>
.page { min-height: 100vh; background: #f5f7fb; padding: 24rpx; box-sizing: border-box; }
.empty { color: #9a9aa0; text-align: center; padding: 24rpx 0; }
.inventory-list { display: flex; flex-direction: column; gap: 16rpx; }
.inventory-item { background: #fff; border-radius: 16rpx; padding: 20rpx; box-shadow: 0 6rpx 16rpx rgba(0,0,0,0.06); }
.header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12rpx; }
.title { font-size: 30rpx; color: #222; font-weight: 600; }
.status-tag { font-size: 24rpx; padding: 4rpx 10rpx; border-radius: 999rpx; }
.status-tag.running { color: #1e88e5; background: #e3f2fd; }
.status-tag.ended { color: #2e7d32; background: #e8f5e9; }
.status-tag.unknown { color: #757575; background: #eeeeee; }
.content .row { margin-top: 6rpx; color: #444; }
.value { color: #333; }
.load-status { text-align: center; color: #9a9aa0; padding: 20rpx 0; }
</style>
