<template>
  <uni-card>
    <template v-slot:title>
      <view class="flex u-p-30 justify-between">
        <text class="flex-1 u-font-16 font-bold">溶栓记录</text>
        <u-button size="mini" class="bg-main" type="primary" @click="onExport">导出</u-button>
      </view>
    </template>
    <u-time-line>
      <u-time-line-item node-top="4" v-for="(item, index) in data" :key="index">
        <template v-slot:node>
          <uni-icons type="smallcircle" :color="computeColor(item.exceed)" size="16" />
        </template>
        <template v-slot:content>
          <view class="flex justify-between">
            <text class="font-xl font-bold">{{ item.name }}</text>
          </view>
          <view class="u-m-t-20">{{ item.text }}</view>
        </template>
      </u-time-line-item>
    </u-time-line>

  </uni-card>
</template>

<script lang="ts" setup>
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue';

interface IDataItem {
  name: string;
  time: string;
  text: string;
  exceed: number
}

const firstAidId = ref('')
const data = ref<IDataItem[]>([])

// 导出
async function onExport() {
  try {
    const res = await uni.$u.api.exportThrombolysisRecord(firstAidId.value)
    if (!res) {
      uni.$u.alertError('返回路径无效')
      return
    }
    // #ifdef APP-PLUS
    uni.downloadFile({
      url: res,
      success(res) {
        var filePath = res.tempFilePath;
        uni.openDocument({
          filePath: filePath,
          showMenu: true,
        });
      }
    });
    // #endif
    // #ifdef H5
    location.href = res
    // #endif
  } catch (error) {
    uni.$u.alertError(error)
  }
}

// 获取数据
async function getData(id) {
  try {
    const res = await uni.$u.api.getThrombolysisRecord(id)
    if (res?.recordList?.length > 0) {
      data.value = res.recordList || []
    } else {
      data.value = []
    }
    console.log(res);

  } catch (error) {
    data.value = []
    uni.$u.alertError(error)
  }
}

// 计算时间轴的颜色
function computeColor(status: number) {
  const config = {
    0: '#00C767', // 正常
    1: '#FF4A44' // 超时
  }
  return config[status] || '#888'
}

onLoad((option) => {
  if (!option?.firstAidId) {
    uni.$u.alertError('url缺少急救id信息')
    return
  }

  firstAidId.value = option.firstAidId
  getData(option.firstAidId)
})
</script>

<style lang="scss" scoped>
</style>
