<template>
  <view class="">
    <uni-section type="line" title="患者急救记录" titleFontSize="16px"> </uni-section>
    <uni-forms :modelValue="codeAidForm" :label-width="160" err-show-type="toast" class="white uni-radius-lg uni-pa-10">
      <uni-forms-item :name="key" v-for="key in part1" :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="codeAidForm[key]" @on-change="onChange(key, $event)" />
      </uni-forms-item>
    </uni-forms>

    <uni-section type="line" title="初步判断" titleFontSize="16px"> </uni-section>
    <view class="white">
      <uni-data-checkbox mode="list" v-model="codeAidForm['RYPG-ZDJG']" :localdata="CHU_BU_PAN_DUAN" class="white uni-radius-lg uni-pa-6"
                         @change="onChange('RYPG-ZDJG', $event.detail.value)">
      </uni-data-checkbox>
      <view class="u-p-b-20 u-p-l-30 u-p-r-30" v-if="codeAidForm['RYPG-ZDJG'] === '其他'">
        <uni-easyinput v-model="codeAidForm['RYPG-ZDJG-ELSE']" type="text" placeholder="请输入其他" @change="onChange('RYPG-ZDJG-ELSE', $event)" />
      </view>
    </view>

    <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 label="是否进行静脉溶栓" name="JMRS-Y">
        <uni-data-checkbox v-model="codeAidForm['JMRS-Y']" :localdata="CODE_DICT['JMRS-Y'].range"
                           @change="onChange('JMRS-Y', $event.detail.value)">
        </uni-data-checkbox>
      </uni-forms-item>

      <!-- 未溶栓原因 是否静脉溶栓=否的时候显示-->
      <view class="white" v-if="codeAidForm['JMRS-Y'] === '否'">
        <view>未溶栓原因</view>
        <uni-data-checkbox mode="list" :multiple="true" v-model="codeAidForm['JMRS-WRSYY']" :localdata="CODE_DICT['JMRS-WRSYY'].range"
                           class="white uni-radius-lg uni-pa-6" @change="onChange('JMRS-WRSYY', $event.detail.value)">
        </uni-data-checkbox>
        <view class="u-p-b-20 u-p-l-30 u-p-r-30" v-if="codeAidForm['JMRS-WRSYY'].includes('其他')">
          <uni-easyinput v-model="codeAidForm['JMRS-WRSYY-ELSE']" type="text" placeholder="请输入其他"
                         @change="onChange('JMRS-WRSYY-ELSE', $event)" />
        </view>
      </view>


      <!--  是否静脉溶栓=是的时候显示-->
      <view class="white" v-else>
        <JMRS @on-change="onChange" />

      </view>
    </uni-forms>
  </view>
</template>

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

defineExpose({ init })
const firstAidId = inject<Ref<string>>('firstAidId')
const serviceStore = useServiceStore()

const part1 = ['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 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': '',

  'RYPG-ZDJG': '', // 初步判断
  'RYPG-ZDJG-ELSE': '', // 初步判断 - 其他

  'JMRS-Y': '',
  'JMRS-WRSYY': [''],
  'JMRS-WRSYY-ELSE': ''
})

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

// code form item change
async function onChange(code: string, value: string) {
  // codeForm[code] = value
  try {
    if (!firstAidId?.value) {
      uni.$u.alertError('缺少急救id参数')
      return
    }
    if (code === 'RYPG-HEIGHT') {
      //  身高变了 计算BMI
      codeAidForm['RYPG-BMI'] = computeBMI(value, codeAidForm['RYPG-WEIGHT'])
    }

    if (code.includes('_ELSE')) {
      // TODO: 其他
      return
    }
    const answer = typeof value === 'string' ? [value] : value
    const param = { codeAndAnswerList: [{ questionCode: code, answer, 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
  // console.log('aid record init', currentPatient.value.recordValDict['RYPG-NIHSS'][0]);
  // 同步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) || ''
  codeAidForm['RYPG-WEIGHT'] = currentCar.value?.sensorWeight?.toString() || ''
  codeAidForm['RYPG-BMI'] = computeBMI(codeAidForm['RYPG-HEIGHT'], codeAidForm['RYPG-WEIGHT'])
}


init()
</script>
