<template>
  <view class="u-p-30">

    <NameGenderAge :right-text="`得分: ${score}`" />

    <uni-card :title="data[index].text" :border="false">

      <uni-data-checkbox mode="list" v-model="data[index].value" :localdata="data[index].range"></uni-data-checkbox>

      <view class="flex justify-center u-p-30">
        <u-button type="primary" size="medium" @click="onNext" class="bg-main" shape="circle" :disabled="index === 0">上一题</u-button>
        <u-button type="primary" size="medium" @click="onPrev" class="bg-main" shape="circle"
                  v-show="index !== data.length - 1">下一题</u-button>
        <u-button type="primary" size="medium" @click="onSubmit" class="bg-main" shape="circle"
                  v-show="index === data.length - 1">提交</u-button>
      </view>
    </uni-card>
  </view>
</template>

<script lang="ts" setup>
import { computed, reactive, ref } from 'vue';
import { NIHSS_LIST } from '@/config/service'
import { useGetFirstAidId } from '@/hooks/useGetFirstAidId';

const { firstAidId, code } = useGetFirstAidId()

const data = reactive([...JSON.parse(JSON.stringify(NIHSS_LIST))])
const index = ref(0)

const score = computed(() => {
  return data.reduce((prev, current) => current.value + prev, 0)
})


async function onSubmit() {
  try {
    const answer = [score.value]
    const param = { codeAndAnswerList: [{ questionCode: code.value, answer, time: '' }], firstAidId: firstAidId?.value, sourceId: '', sourceType: '' }
    await uni.$u.api.updateAidCode(param)
    uni.$u.toast('更新成功')

    // 更新数据
    uni.$u.openPage('detail2', true, `firstAidId=${firstAidId.value}`)
    // uni.navigateBack()
  } catch (error) {
    uni.$u.alertError(error)
  }
}

function onNext() {
  index.value -= 1
}

function onPrev() {
  index.value += 1
}

</script>
