<template>
  <view class="uni-px-10 ">
    <uni-section title="患者信息" type="line"> </uni-section>
    <uni-forms :modelValue="baseInfo" :label-width="100" class="white uni-radius-lg uni-pa-6">
      <uni-forms-item label="扫描身份证" name="" @click="onScan">
        <view class="flex full-height justify-end">
          <uni-icons type="scan" color="#70798C" size="24" />
        </view>
      </uni-forms-item>
      <uni-forms-item label="姓名" name="patientName">
        <uni-easyinput type="text" v-model="baseInfo.patientName" placeholder="请输入姓名" />
      </uni-forms-item>
      <uni-forms-item name="patientGender" label="性别">
        <uni-data-checkbox v-model="baseInfo.patientGender" :localdata="GENDER_LIST" />
      </uni-forms-item>
      <uni-forms-item label="民族" name="patientNation">
        <uni-data-select :localdata="nationList" v-model="baseInfo.patientNation" :clear="false" />
      </uni-forms-item>
      <uni-forms-item label="身份证号" name="patientIdCardNo">
        <uni-easyinput type="idcard" v-model="baseInfo.patientIdCardNo" placeholder="请输入身份证号" />
      </uni-forms-item>
    </uni-forms>

    <uni-section title="疑似诊断" type="line"> </uni-section>
    <uni-data-checkbox mode="list" v-model="baseInfo.firstAidZlType" :localdata="FIRST_AID_ZL_TYPE" class="white uni-radius-lg uni-pa-6">
    </uni-data-checkbox>

    <view class="flex uni-px-6 uni-py-10">
      <u-button @click="onSubmit" shape="circle" type="primary" class="bg-main" size="medium">完成创建患者</u-button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { useUserStore } from '@/store/modules/user'
import { GENDER_LIST, FIRST_AID_ZL_TYPE } from '@/config/service'
import { computed } from 'vue';

const baseInfo = reactive({
  patientName: '张三',
  patientGender: 0,
  patientNation: '汉族',
  patientIdCardNo: '142222188901120112',
  firstAidZlType: 0
})
const nationList = ref([])

const userStore = useUserStore()

const token = computed(() => userStore.token)

// 提交创建急救
async function onSubmit() {
  const param = { ...baseInfo, }
  try {
    const { firstAidId, firstAidStatus } = await uni.$u.api.createAid(param)
    uni.$u.toast('创建成功')
    uni.$u.openPage('detail2', false, `firstAidId=${firstAidId}&firstAidStatus=${firstAidStatus}`)
  } catch (error) {
    uni.$u.alertError(error)
  }
}

// 扫描身份证
function onScan() {
  uni.chooseImage({
    count: 1,
    sizeType: ['original', 'compressed'],
    sourceType: ['album', 'camera'],
    success(res) {
      upload(res.tempFilePaths[0])
    }
  });
}

// 上传
function upload(tempPath: string) {
  const host = uni.getStorageSync(uni.$u.LOCAL_KEY.HOST)
  if (!host) {
    uni.showModal({ title: '提示', content: '请先配置服务器地址' })
    return
  }
  uni.uploadFile({
    url: `${host}/sys/ocr/idcardInfo`, //仅为示例，非真实的接口地址
    filePath: tempPath,
    name: 'file',
    header: { Authorization: `Bearer ${token.value}` },
    success: (uploadFileRes) => {
      if (!uploadFileRes?.data) {
        uni.$u.toast('请求失败')
        return
      }
      const { msg, code, data } = JSON.parse(uploadFileRes.data) as any

      if (code === 200) {
        const { name, sex, idCardNo, nation } = data
        baseInfo.patientName = name || ''
        baseInfo.patientGender = sex || ''
        baseInfo.patientNation = nation || ''
        baseInfo.patientIdCardNo = idCardNo || ''
        // handleScan(url)
      } else if (code === 401) {
        uni.$u.api.login().then(upload(tempPath))
      } else {
        uni.$u.toast(msg)
      }
    }
  });
}

// 查民族列表
async function getNationList() {
  try {
    const data = await uni.$u.api.getNationList()
    nationList.value = data.map((item: string) => ({ value: item, text: item }))
  } catch (error) {
    uni.$u.alertError(error)
  }
}
// 初始获取民族
getNationList()
</script>
