<route lang="json5" type="page">
{
  layout: 'default',
  style: {
    navigationBarTitleText: '检验方案审核',
    navigationStyle: 'custom',
    disableScroll: true,
    'app-plus': {
      bounce: 'none',
    },
  },
}
</route>

<template>
  <view class="audit-container">
    <!-- 导航栏 -->
    <NavBar title="检验方案审核" />

    <!-- 筛选栏 -->
    <RadioFilterBar
      v-model="rightType"
      :options="rightTypeList"
      type="radio"
      @change="handleRightRefresh"
    />

    <!-- 列表 -->
    <scroll-view class="list-scroll" scroll-y @scrolltolower="loadMore">
      <view v-for="item in listData" :key="item.id" class="item-box">
        <Item :item="item" :operate-text="'审核'" @handle-operation="pushAction(item)" />
      </view>

      <view v-if="loading" class="loading-text">加载中...</view>
      <view v-if="!hasMore && listData.length > 0" class="no-more-text">没有更多了</view>
      <view v-if="!loading && listData.length === 0" class="empty-text">
        <text>暂无数据</text>
      </view>
    </scroll-view>
  </view>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { useUserStore } from '@/store/user'
import { useConfigStore } from '@/store/config'
import Item from '@/pages/inspectionPlanAudit/components/Item.vue'
import NavBar from '@/components/NavBar/NavBar.vue'
import RadioFilterBar from '@/components/RadioFilterBar/RadioFilterBar.vue'
import { requestFunc, TaskOrderFuncName } from '@/api/ApiRouter/taskOrder'
import { onShow } from '@dcloudio/uni-app'

defineOptions({
  name: 'InspectionPlanAuditList',
})

// 状态
const listData = ref<any[]>([])
const loading = ref(false)
const hasMore = ref(true)
const params = reactive({
  pageNo: 1,
  pageSize: 10,
  reportType: 600,
  status: '100',
  bpmUserId: '',
  approverId: '',
})

const userStore = useUserStore()
const userInfo = computed(() => userStore.userInfo)
const equipType = useConfigStore().getEquipType()

// 初始化参数
params.bpmUserId = userInfo.value?.id || ''

const rightType = ref('100')
const rightTypeList = [
  // { value: '全部', id: '' },
  { value: '审核中', id: '100' },
  { value: '已通过', id: '200' },
  { value: '已拒绝', id: '300' },
]

// 获取列表数据
const fetchList = async (refresh = false) => {
  if (loading.value) return

  if (params.status && params.status !== '100') {
    // 非审核中，查找当前用户审核审批流程结束后的数据
    params.bpmUserId = null
    params.approverId = userInfo.value?.id
  } else if (params.status && params.status === '100') {
    // 审核中，查找需要当前用户审核审批的数据
    params.bpmUserId = userInfo.value?.id || ''
    params.approverId = ''
  } else if (!params.status) {
    // 全部，包括需要当前用户审核审批、已处理的数据，但是这部分后端没有处理好，先把全部去掉了
    params.bpmUserId = userInfo.value?.id || ''
    params.approverId = null
    params.status = null
  }

  params.pageNo = refresh ? 1 : params.pageNo + 1

  loading.value = true
  try {
    const result: any = await requestFunc(TaskOrderFuncName.MajorIssuesAuditList, equipType, params)
    const newList = result?.data?.list || []

    if (refresh) {
      listData.value = newList
    } else {
      listData.value = [...listData.value, ...newList]
    }

    hasMore.value = newList.length >= params.pageSize
  } catch (error) {
    console.error('获取列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 加载更多
const loadMore = () => {
  if (!loading.value && hasMore.value) {
    fetchList(false)
  }
}

// 刷新列表
const refreshList = () => {
  params.pageNo = 1
  fetchList(true)
}

// 筛选刷新
const handleRightRefresh = (value: string | number | boolean) => {
  rightType.value = value as string
  params.status = rightType.value
  refreshList()
}

// 跳转操作
const pushAction = (item: any) => {
  const isAuditStage = item.approvalId == null ? 1 : 0
  uni.navigateTo({
    url: `/pages/inspectionPlanAudit/preViewReport/index?id=${item.id}&reportName=${item.reportName}&templateId=${item.templateId}&isAuditStage=${isAuditStage}&manualUrl=${encodeURIComponent(item.manualUrl)}`,
  })
}

// 监听刷新事件
const refreshListener = () => {
  refreshList()
}

onShow(() => {
  refreshList()
  uni.$on('onRefresh', refreshListener)
})
</script>

<style lang="scss" scoped>
.audit-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f5f5f5;
}

.list-scroll {
  flex: 1;
  overflow: hidden;
}

.loading-text,
.no-more-text {
  padding: 15px;
  font-size: 14px;
  color: #999;
  text-align: center;
}

.empty-text {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
  font-size: 14px;
  color: #999;
}

.item-box {
  margin: 13px;
  background-color: #fff;
  border-radius: 5px;
}
</style>
