<template>
  <view class="">
    <uni-section type="line" title="患者急救记录" titleFontSize="16px"> </uni-section>
    <uni-forms :modelValue="codeAidForm" :label-width="160" class="white uni-radius-lg uni-pa-10">
      <uni-forms-item :name="key" v-for="(item, key) in codeAidForm" :key="key">
        <template v-slot:label>
          <view class="full-height flex" style="width: 135px">
            <!-- @ts-ignore -->
            {{ CODE_DICT[key]?.text }} <text v-if="CODE_DICT[key].description"
                  class="uni-secondary-color">{{ CODE_DICT[key].description }}</text>
          </view>
        </template>
        <!-- {{ key }} -->
        <CodeFormItem :code="key" :value="item" @on-change="onChange(key, $event)" />
      </uni-forms-item>
    </uni-forms>

    <uni-section type="line" title="疑似诊断" titleFontSize="16px"> </uni-section>
    <!--     <uni-data-checkbox mode="list" v-model="baseInfo.firstAidZlType" :localdata="FIRST_AID_ZL_TYPE" class="white uni-radius-lg uni-pa-6"
                       @change="onBaseChange('firstAidZlType', $event.detail.value)">
    </uni-data-checkbox> -->
  </view>
</template>

<script lang="ts" setup>
import { reactive, computed, inject } from 'vue';
import type { Ref } from 'vue'
import { GET_GENDER_TEXT_BY_CODE } from '@/config/service'
import { useServiceStore } from '@/store/modules/service';
import { CODE_DICT } from '@/config/code';

const firstAidId = inject<Ref<string>>('firstAidId')
const serviceStore = useServiceStore()

const codeAidForm = reactive({
  'RYPG-HEIGHT': '',
  'RYPG-WEIGHT': '',
  'RYPG-GENDER': '',
  'RYPG-BMI': '',
  'RYPG-SYSTOLIC-PRESSURE': '',
  'RYPG-DIASTOLIC-PRESSURE': '',
  'RYPG-PULSE': '',
  'RYPG-MRS': '',
  'RYPG-NIHSS': '',
  'RYPG-BLOOD-REPORT-TIME': '',
  'RYPG-BLOOD-SUGAR': '',
  'RYPG-CT-LK-TIME': ''
})

const currentPatient = computed(() => serviceStore.currentPatient)

// code form item change
async function onChange(code: string, value: string) {
  // codeForm[code] = value
  try {
    if (!firstAidId?.value) {
      uni.$u.alertError('缺少急救id参数')
      return
    }
    const param = { codeAndAnswerList: [{ questionCode: code, answer: [value], time: '' }], firstAidId: firstAidId?.value, sourceId: '', sourceType: '' }
    await uni.$u.api.updateAidCode(param)
    uni.$u.toast('更新成功')
  } catch (error) {
    uni.$u.alertError(error)
  }
}


function init() {

  // 同步store里的急救信息
  if (!currentPatient.value) return
  // 同步code信息
  const { recordValDict, patientGender } = currentPatient.value
  if (!recordValDict) return

  for (const key in codeAidForm) {
    if (!recordValDict[key]) continue
    codeAidForm[key] = recordValDict[key][0]?.answer[0] || ''
  }

  codeAidForm['RYPG-GENDER'] = GET_GENDER_TEXT_BY_CODE[patientGender]
}


init()
</script>
