<template>
  <view class="bg-main-r">
    <uni-nav-bar dark left-icon="left" background-color="transparent" :border="false" title="患者详情" @clickRight="onClickRight"
                 @clickLeft="onClickLeft">
      <!-- 切换至详情 -->
      <template v-slot:right>
        <view class="flex nav-right">
          切换至详情 <u-image class="icon-change" src="@/static/images/change.png" mode="aspectFill"></u-image>
        </view>
      </template>
    </uni-nav-bar>

    <view class="flex justify-between head">
      <view class="column text-white">
        <text class="u-font-xl font-bold">暴风眼质控</text>
        <text>高效 便捷 精准</text>
      </view>

      <view class="btn-create" @click="openThrombolysisRecord">
        <image class="icon-plus" src="@/static/images/plus.png" mode="scaleToFill" />
        急诊溶栓记录
      </view>
      <!-- <view></view> -->
      <image class="icon-doctor" src="@/static/images/doctor.png" mode="scaleToFill" />
    </view>

    <view class="flex u-m-l-40 u-m-r-40 align-top content">
      <view class="flex-1 u-m-r-20 column">
        <view class="item-card u-m-r-20 bg-white">
          <view class="flex justify-between u-m-b-16">
            <view class="u-font-xl font-bold flex-1">
              <text class="u-m-r-20">{{ currentPatient?.patientName }}</text>
              <text class="u-m-r-20">{{ GET_GENDER_TEXT_BY_CODE(currentPatient?.patientGender) }}</text>
              <text>{{ currentPatient?.patientAge }}</text>
            </view>
            <uni-icons type="compose" color="#888" size="24" />
          </view>

          <u-tag :text="firstAidZlType" class="u-m-r-20" type="primary" size="mini" />
          <u-tag text="发病时间" type="primary" size="mini" />

          <view class="u-tips-color u-font-sm u-p-t-20">
            身份证：<text>{{ currentPatient?.patientIdCardNo }}</text>
          </view>

          <view class="flex justify-between u-p-t-20">
            <view class="font-bold">2023-</view>
            <view class="u-tips-color">距发病时间 <text class="primary">194′′</text></view>
          </view>

          <view class="u-tips-color u-font-sm u-p-t-20 flex justify-between">
            <view>距CT25′′剩余9′′</view>
            <view>距CT25′′剩余9′′</view>
          </view>
        </view>

        <view class="item-card u-m-r-20 bg-white u-m-t-30">
          <view class="flex justify-between u-m-b-16">
            <view class="u-font-xl font-bold flex-1">
              mRS评分：<text class="u-m-r-20 primary">{{ mrs }}</text>
              NIHSS评分：<text class="u-m-r-20 primary">{{ nihss }}</text>
            </view>
            <uni-icons type="compose" color="#888" size="24" />
          </view>

          <view class="flex justify-between u-m-b-16">
            <view class="u-font-xl font-bold flex-1">
              舒张压/收缩压：<text class="u-m-r-20 primary">{{ szy }}/{{ ssy }}</text>
            </view>
          </view>

          <u-tag :text="zdjg" type="primary" size="mini" />

          <view class="u-tips-color u-font-sm u-p-t-20 flex ">
            身高：<text class="u-m-r-20">{{ height }}</text>
            体重：<text class="u-m-r-20">{{ weight }}</text>
            BMI：<text class="u-m-r-20">{{ bmi }}</text>
          </view>

          <view class="u-tips-color u-font-sm u-p-t-20 flex ">
            脉搏：<text class="u-m-r-20">{{ pulse }}</text>
          </view>
        </view>
      </view>


      <!-- timeline -->
      <view class="flex-1 item-card">
        <u-time-line>
          <u-time-line-item node-top="4">
            <template v-slot:node>
              <uni-icons type="smallcircle" color="#00C767" size="16" />
            </template>
            <template v-slot:content>
              <view class="flex justify-between">
                <text class="font-xl font-bold">分诊</text>
                <text>结束</text>
              </view>
              <view class="u-m-t-20">时间</view>
            </template>
          </u-time-line-item>
          <u-time-line-item node-top="4">
            <template v-slot:node>
              <uni-icons type="smallcircle" color="#00C767" size="16" />
            </template>
            <template v-slot:content>
              <view class="flex justify-between">
                <text class="font-xl font-bold">分诊</text>
                <text>结束</text>
              </view>
              <view class="u-m-t-20">时间</view>
            </template>
          </u-time-line-item>
          <u-time-line-item node-top="4">
            <template v-slot:node>
              <uni-icons type="smallcircle" color="#00C767" size="16" />
            </template>
            <template v-slot:content>
              <view class="flex justify-between">
                <text class="font-xl font-bold">分诊</text>
                <text>结束</text>
              </view>
              <view class="u-m-t-20">时间</view>
            </template>
          </u-time-line-item>
        </u-time-line>
      </view>
    </view>

    <!-- 创建患者 -->
    <!-- <CreatePatient @on-scan="uploadImage" @on-cancel="onCancel" @on-confirm="onConfirm" /> -->

    <!-- float button -->
    <!-- <FloatButton /> -->
  </view>
</template>

<script lang="ts" setup>
import { onLoad } from '@dcloudio/uni-app';
import { ref, computed } from 'vue';
import { useServiceStore } from '@/store/modules/service';
import { GET_GENDER_TEXT_BY_CODE } from '@/config/service';
import { computeBMI } from '@/utils/common';
import { FIRST_AID_ZL_TYPE } from '@/config/service'


const serviceStore = useServiceStore()
const firstAidId = ref('')

const currentPatient = computed(() => serviceStore.currentPatient)
const mrs = computed(() => serviceStore.getCodeValue('RYPG-MRS'))
const nihss = computed(() => serviceStore.getCodeValue('RYPG-NIHSS'))
const ssy = computed(() => serviceStore.getCodeValue('RYPG-SYSTOLIC-PRESSURE')) // 收缩压
const szy = computed(() => serviceStore.getCodeValue('RYPG-DIASTOLIC-PRESSURE')) // 舒张压
const height = computed(() => serviceStore.getCodeValue('RYPG-HEIGHT'))
const weight = computed(() => serviceStore.getCodeValue('RYPG-WEIGHT'))
const bmi = computed(() => computeBMI(height.value, weight.value))
const pulse = computed(() => serviceStore.getCodeValue("RYPG-PULSE")) // 脉搏
const zdjg = computed(() => serviceStore.getCodeValue("RYPG-ZDJG")) // 初步诊断
// 疑似诊断
const firstAidZlType = computed(() => {
  const value = currentPatient.value?.firstAidZlType
  return value === undefined ? '' : FIRST_AID_ZL_TYPE[value].text
})


function onClickLeft() {

  console.log('left');

}

function onClickRight() {
  uni.$u.openPage('detail2', true, `firstAidId=${firstAidId.value}`)
}

// 查病历数据
async function getAidInfo(firstAidId: string) {
  try {
    const data = await uni.$u.api.getAidInfo(firstAidId)
    serviceStore.setCurrentPatient(data)
  } catch (error) {
    serviceStore.setCurrentPatient(null)
    uni.$u.alertError(error)
  }
}

// 打开急诊溶栓记录
function openThrombolysisRecord() {
  uni.$u.openPage('thrombolysisRecord', false, `firstAidId=${firstAidId.value}`)
}

onLoad((option) => {
  if (!option?.firstAidId) {
    uni.$u.toast('url中缺少急救id参数')
    firstAidId.value = ''
    return
  }

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

<style lang="scss" scoped>
.bg-main-r {
  height: 60vh;

  :deep(.uni-navbar__header-btns) {
    width: 200rpx !important;
  }

  .icon-change {
    width: 40rpx !important;
    height: 40rpx !important;
    margin-left: 0.5em;
    opacity: .9 !important;
  }

  .item-card {
    padding: 1em;
    border-radius: 6px;
    background-color: #fff;
  }
}



.head {
  height: 140rpx;
  padding: 0 20px;
}

.btn-create {
  background-color: #fff;
  height: 3em;
  padding: 0 3em;
  display: flex;
  align-items: center;
  border-radius: 1.5em;

  .icon-plus {
    width: 32rpx;
    height: 32rpx;
    margin-right: 0.5em;
  }
}

.icon-doctor {
  position: relative;
  z-index: 0;
  width: 140rpx;
  height: 140rpx;
  transform: scale(1.1) translate3d(-50%, 24%, 0);
}

.content {
  position: relative;
  z-index: 99;
}
</style>
