<template>
  <view class="pkg-list-page">
    <BillListView ref="viewRef" :module-key="moduleKey" :initial-status="initialStatus" />
    <!-- 新增入口（P2 写入能力，表单页按 schema 判定是否可编辑） -->
    <view v-if="canCreate" class="fab" @click="goCreate">
      <text class="fab__text">＋</text>
    </view>
  </view>
</template>

<script setup lang="ts">
/**
 * 分包通用列表页壳：注册表 module 参数驱动，挂接页面级生命周期
 */
import { ref, computed } from 'vue'
import { onLoad, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app'
import BillListView from '@/modules/bill/BillListView.vue'
import { getModule } from '@/modules/registry'
import { canCreateModule } from '@/modules/bill/formSchemas'
import { moduleEditPage } from '@/constants/routes'

const moduleKey = ref('')
const initialStatus = ref('')
const viewRef = ref<InstanceType<typeof BillListView> | null>(null)

const canCreate = computed(() => canCreateModule(getModule(moduleKey.value)))

onLoad((options) => {
  moduleKey.value = options?.module || ''
  initialStatus.value = options?.status || ''
})

onReachBottom(() => {
  viewRef.value?.loadMore()
})

onPullDownRefresh(async () => {
  await viewRef.value?.refresh()
  uni.stopPullDownRefresh()
})

function goCreate() {
  const m = getModule(moduleKey.value)
  if (!m) return
  uni.navigateTo({ url: `${moduleEditPage(m.pkg)}?module=${m.key}` })
}
</script>

<style lang="scss" scoped>
.pkg-list-page {
  position: relative;
}

.fab {
  position: fixed;
  right: 32rpx;
  bottom: calc(64rpx + env(safe-area-inset-bottom));
  width: 96rpx;
  height: 96rpx;
  border-radius: 50%;
  background: #2979ff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8rpx 24rpx rgba(41, 121, 255, 0.35);

  &:active {
    opacity: 0.85;
  }

  &__text {
    color: #fff;
    font-size: 48rpx;
    line-height: 1;
  }
}
</style>
