<script setup>
import dayjs from 'dayjs'
import { getElevatorInfoApi } from '@/api/elevator'

const info = ref({})
const getElevatorInfo = async () => {
  const { id } = options.value
  const { error, data } = await getElevatorInfoApi({
    id
  })
  if (error) {
    return
  }
  info.value = data
}

const keyValue = ref([
  { key: '电梯编号', value: '', field: 'elevatorNum' },
  {
    key: '梯龄',
    value: '',
    field: '',
    format: (item) => {
      const { leaveFactoryDate } = item
      const now = dayjs()
      const leave = dayjs(leaveFactoryDate)
      const year = now.diff(leave, 'year')
      const month = now.diff(leave, 'month') % 12
      return `${year}年${month}个月`
    }
  },
  { key: '使用场景', value: '', field: 'useScenario' },
  { key: '电梯类型', value: '', field: 'type' },
  { key: '安装日期', value: '', field: 'installDate' },
  { key: '电梯地址', value: '', field: 'elevatorAddress' },
  { key: '制造单位', value: '', field: 'manufactureUnit' },
  { key: '使用单位', value: '', field: 'employName' },
  { key: '维保单位', value: '', field: 'maintenanceName' },
  { key: '下次年检日期', value: '', field: 'annualDate' }
])

const options = ref({})
onLoad(async (e) => {
  options.value = e
  getElevatorInfo()
})
</script>

<template>
  <up-card :title="info.elevatorName" :head-style="{ fontWeight: 'bold' }">
    <template #body>
      <up-cell-group :border="false">
        <up-cell
          v-for="(item, index) in keyValue"
          :key="index"
          :title="item.key"
          :value="item.format ? item.format(info) : info[item.field] || '无'"
        />
      </up-cell-group>
    </template>
  </up-card>
</template>
