<script setup>
import { useUniApi } from 'hooks/use-uni-api'
import { getExpertConsultInfoApi } from '@/api/expert'
import { planTypeMap, consultTypeMap } from '@/constants/state'
const { previewImage } = useUniApi()

const dataDetail = ref({})
const getWorkDataInfo = async () => {
  const { error, data } = await getExpertConsultInfoApi(options.value)
  if (!error) {
    dataDetail.value = data
  }
}
onMounted(() => {
  getWorkDataInfo()
})

const operationStateMap = {
  2: '接收',
  3: '到场',
  4: '完成',
  7: '评价'
}
const actionRecord = ref([])
watchEffect(() => {
  const { operationRecords } = dataDetail.value

  if (operationRecords) {
    actionRecord.value = operationRecords.map((item) => {
      const { operationState } = item
      return {
        ...item,
        operationStateStr: operationStateMap[operationState]
      }
    })
  }
})

// 电梯名称 工作选项 工作状态 创建时间 发布人员 期望时间 工作描述 被困人数 受伤人数 备注 其它原因
const cellData = ref([
  { key: '电梯名称', value: '', field: 'elevatorName' },
  {
    key: '工作类型',
    value: '',
    format: (item) => {
      const { consultType } = item
      return consultTypeMap[consultType]
    }
  },
  {
    key: '工作状态',
    format: (item) => {
      const { state } = item
      if (state === 2) {
        return '已接收'
      } else if (state === 3) {
        return '已到场'
      } else if (state === 4) {
        return '已完成'
      } else if (state === 7) {
        return '已完成'
      }
    }
  },
  { key: '创建时间', value: '', field: 'createdTime' },
  // { key: '发布人员', value: '', field: 'publisher' },
  { key: '期望时间', value: '', field: 'expectTime' },
  { key: '工作描述', value: '', field: 'consultDesc' }
  // { key: '备注', value: '', field: '' },
  // { key: '其他原因', value: '', field: '' }
])

const workRecord = ref([
  // {
  //   title: '[图片]',
  //   label: 'https://img.yzcdn.cn/vant/cat.jpeg',
  //   time: '2024-01-01 13:00:00'
  // },
  // {
  //   title: '[维保签名]',
  //   label: 'https://img.yzcdn.cn/vant/cat.jpeg',
  //   time: '2024-01-01 13:00:00'
  // },
  // {
  //   title: '[图片]',
  //   label: 'https://img.yzcdn.cn/vant/cat.jpeg',
  //   time: '2024-01-01 13:00:00'
  // },
  // {
  //   title: '[维保签名]',
  //   label: 'https://img.yzcdn.cn/vant/cat.jpeg',
  //   time: '2024-01-01 13:00:00'
  // },
  // {
  //   title: '[图片]',
  //   label: 'https://img.yzcdn.cn/vant/cat.jpeg',
  //   time: '2024-01-01 13:00:00'
  // }
])

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

<template>
  <view class="maintenance-detail" px-28>
    <up-cell-group :border="false">
      <up-cell
        v-for="(item, index) in cellData"
        :key="index"
        :title="item.key"
        :value="item.format ? item.format(dataDetail) : dataDetail[item.field]"
      />
    </up-cell-group>

    <!-- 操作记录 -->
    <view v-if="false" mt-32>
      <view text-32 font-500>操作记录</view>
      <up-cell-group :border="false">
        <up-cell
          v-for="(item, index) in actionRecord"
          :key="index"
          :title="item.operator"
          :label="item.operationStateStr"
          :value="item.operationTime"
        />
      </up-cell-group>
    </view>

    <!-- 工作记录 -->
    <view v-if="false" mt-32>
      <view text-32 font-500>工作记录</view>
      <up-cell
        title="[工作表]"
        :label="planTypeMap[dataDetail.planType]"
        :value="dataDetail.finishTime"
        :is-link="true"
        :url="`/pages/views/elevator-maintenance/work-sheet?id=${dataDetail.id}`"
      />

      <up-cell-group v-if="workRecord.length" :border="false">
        <up-cell
          v-for="(item, index) in workRecord"
          :key="index"
          :title="item.title"
          :value="item.time"
        >
          <template #label>
            <up-image
              :show-loading="true"
              :src="item.label"
              width="120px"
              height="120px"
              @tap="previewImage(item.label)"
            />
          </template>
        </up-cell>
      </up-cell-group>
    </view>
  </view>
</template>
