<template>
  <view class="page">
    <view class="borrow-list">
      <view v-for="item in detailList" :key="item.assetId" class="borrow-item card">
        <view class="item-header"><text class="asset-name">设备名称：{{ item.assetName }}</text></view>
        <view class="detail">使用人：<text class="value">{{ item.useManName }}</text></view>
        <view class="detail">设备购入日期：<text class="value">{{ item.purchaseDate }}</text></view>
        <view class="detail">负责人：<text class="value">{{ item.headerName }}</text></view>
        <view class="detail">备注：<text class="value">{{ item.note || '无' }}</text></view>
        <view class="detail">盘点状态：<text class="value">{{ item.isInventory }}</text></view>
        <view class="action-row" v-if="item.isInventory!='已盘点'">
          <view @tap="setInventory(0,item)" class="inventory-btn inventory-win">正常</view>
          <view @tap="setInventory(1,item)" class="inventory-btn inventory-lose">损坏</view>
          <view @tap="setInventory(2,item)" class="inventory-btn inventory-normal">丢失</view>
        </view>
      </view>
    </view>
    <view class="none" v-if="detailList.length===0">没有更多数据了</view>
  </view>
</template>

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

const planId = ref<string | number | null>(null)
const userInfo = uni.getStorageSync('staff') || {}
const detailList = ref<any[]>([])

const inventoryInfo = ref({
  assetId: '',
  inventoryManId: '',
  inventoryManMobile: '',
  inventoryPic: '',
  inventoryPlanId: '',
  inventoryResult: '',
  inventoryDate: ''
})

onLoad(async (options?: Record<string, any>) => {
  planId.value = options?.planId || null
  if (!planId.value) return
  await loadDetail()
})

async function loadDetail() {
  try {
    const res: any = await getInventoryDetailIng(planId.value)
    detailList.value = res?.data || []
  } catch (e) {
    console.error(e)
    uni.showToast({ title: '加载失败', icon: 'none' })
  }
}

function setInventory(status: number, item: any) {
  const statusMap: Record<number, string> = { 0: '正常', 1: '损坏', 2: '丢失' }
  const statusText = statusMap[status] || '未知'
  uni.showModal({
    title: '确认操作',
    content: `确定要将盘点状态设置为【${statusText}】吗？`,
    success: async (r) => {
      if (r.confirm) {
        const payload = {
          assetId: item.assetId,
          inventoryManId: userInfo.id,
          inventoryManMobile: userInfo.mobile,
          inventoryPic: '',
          inventoryPlanId: planId.value,
          inventoryResult: status
        }
        try {
          await inventory(payload)
          uni.showToast({ title: '提交成功', icon: 'success' })
          await loadDetail()
        } catch (e) {
          uni.showToast({ title: '提交失败', icon: 'none' })
        }
      }
    }
  })
}
</script>

<style scoped>
.page { min-height: 100vh; background: #f5f7fb; padding: 24rpx; }
.card { background: #fff; border-radius: 20rpx; box-shadow: 0 8rpx 24rpx rgba(0,0,0,0.06); padding: 20rpx; margin-bottom: 16rpx; }
.asset-name { font-size: 30rpx; color: #222; font-weight: 600; }
.detail { font-size: 24rpx; color: #666; margin-top: 6rpx; }
.action-row { display: flex; gap: 12rpx; margin-top: 10rpx; }
.inventory-btn { padding: 12rpx 20rpx; border-radius: 10rpx; color: #fff; font-size: 24rpx; }
.inventory-win { background: #38b000; }
.inventory-lose { background: #ff6b6b; }
.inventory-normal { background: #f39c12; }
.none { text-align: center; color: #9a9aa0; margin-top: 20rpx; }
.value { color: #333; }
</style>
