import { computed, reactive, ref } from 'vue'
import { initialRequests } from '../data/requests.js'

export function useRequests() {
  const requests = ref(initialRequests)
  const entityMap = {
    'Northstar China Ltd.': 'Northstar China Ltd.',
    'Northstar Singapore Pte.': 'Northstar Singapore Pte.',
    'Northstar US Inc.': 'Northstar US Inc.'
  }
  const search = ref('')
  const filters = reactive({ entity: '全部主体', category: '全部费用', risk: '全部风险' })
  const ranges = ['今日', '本周', '本月']
  const activeRange = ref('本周')

  const filteredRequests = computed(() => {
    const key = search.value.trim().toLowerCase()
    return requests.value.filter((item) => {
      const matchesSearch = !key || `${item.id}${item.person}${item.category}${item.risk}`.toLowerCase().includes(key)
      const matchesEntity = filters.entity === '全部主体' || item.entity === entityMap[filters.entity]
      const matchesCategory = filters.category === '全部费用' || item.category === filters.category
      const matchesRisk = filters.risk === '全部风险'
        || (filters.risk === '高风险' && item.status === 'danger')
        || (filters.risk === '需解释' && item.status === 'warning')
        || (filters.risk === '低风险' && item.status === 'success')
  const matchesRange = activeRange.value === 'custom'
    || activeRange.value === '本月'
    || (activeRange.value === '本周' && item.range !== '本月')
    || (activeRange.value === '今日' && item.range === '今日')
      return matchesSearch && matchesEntity && matchesCategory && matchesRisk && matchesRange
    })
  })

  function updateRequest(requestId, updates) {
    requests.value = requests.value.map((item) => (item.id === requestId ? { ...item, ...updates } : item))
  }

  function approveRequest(request) {
    updateRequest(request.id, {
      verdict: '已通过',
      status: 'success',
      risk: '已完成人工确认'
    })
    return `${request.id} 已标记为通过，审计日志已更新。`
  }

  function rejectRequest(request) {
    updateRequest(request.id, {
      verdict: '已退回补件',
      status: 'danger',
      risk: '待申请人补充差旅行程与票据'
    })
    return `${request.id} 已退回，系统将通知申请人补充材料。`
  }

  return {
    requests, search, filters, ranges, activeRange,
    filteredRequests, approveRequest, rejectRequest
  }
}
