<template>
  <view>
    <!-- input -->
    <uni-easyinput :value="value" type="text" placeholder="请输入" @change="$emit('on-change', $event)" @confirm="$emit('on-change', $event)"
                   v-if="config.type === 'input'" />

    <!-- datetime -->
    <uni-datetime-picker type="datetime" :value="value" :modelValue="value" @change="$emit('on-change', $event)"
                         v-else-if="config.type === 'datetime'" />

    <!-- radio -->
    <uni-data-checkbox :value="value" :localdata="config.range" @change="$emit('on-change', $event.detail.value)"
                       v-else-if="config.type === 'radio'" />


    <!-- checkbox -->
    <uni-data-checkbox :value="value" :multiple="true" :localdata="config.range" @change="$emit('on-change', $event.detail.value)"
                       v-else-if="config.type === 'checkbox'" />

    <!-- select -->
    <uni-data-select :value="value" :modelValue="value" :localdata="config.range" @change="$emit('on-change', $event)"
                     v-else-if="config.type === 'select'"></uni-data-select>

    <!-- text -->
    <view v-else-if="config.type === 'text'" class="flex-1 flex justify-end uni-secondary-color u-font-sm" style="height: 35px;"
          @click="$emit('on-click', code)">
      {{ value }}
      <uni-icons type="forward" v-if="config.rightArrow" size="16" />
    </view>

    <!-- 跳页面的 page -->
    <view v-else-if="config.type === 'page'" class="flex-1 flex justify-end uni-secondary-color u-font-sm" style="height: 35px;"
          @click="onOpenPage">
      <text class="u-m-r-20">{{ value || config.rightText }}</text>
      <uni-icons type="forward" v-if="config.rightArrow" size="16" />
    </view>
  </view>
</template>

<script lang="ts" setup>
import { CODE_DICT } from '@/config/code'
import type { IConfig } from '@/config/code'
import { computed, inject } from 'vue'
import type { Ref } from 'vue'

interface IProps {
  code: string
  value: string | Array<string>
}

const firstAidId = inject<Ref<string>>('firstAidId')
const props = defineProps<IProps>()
defineEmits(['on-change', 'on-click'])

const config = computed<IConfig>(() => CODE_DICT[props.code])

// mrs nihss 等打开页面
// 并把code传过去
function onOpenPage() {
  const name = CODE_DICT[props.code].page?.name
  uni.$u.openPage(name, true, `code=${props.code}&firstAidId=${firstAidId?.value}`)
}
</script>

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