<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model=formData
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
    
    <el-form-item label="当前患者" v-if="false" >
        <el-input v-model="name"  />
      </el-form-item>

    <el-form-item label="分诊设备" >
        <el-select
          v-model="fordevicemValue"
          placeholder="请选择分诊设备"
          clearable
          class="!w-300px"
         >
          <el-option v-for="item in fordevicemData" :key="item.deviceId" :label="item.deviceName" :value="item.deviceName+'|'+item.deviceId" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ApplyformApi, ApplyformVO,UPFJApplyformVO } from '@/api/applyregistration/applyform'

/** 申请登记记录 表单 */
defineOptions({ name: 'ApplyformForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const upFJApplyformVO = ref<UPFJApplyformVO[]>([]);
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  regId: undefined,
  regSource: undefined,
  examId: undefined,
  pname: undefined,
  gender: undefined,
  birthday: undefined,
  examDate: undefined,
  examItemName: undefined,
  deviceId: undefined,
  deviceName: undefined,
  contactTel: undefined,
  regDate: undefined,
  sortDate: undefined,
  billgDoctor: undefined,
  examStatus: undefined,
  billDoctorDepartment: undefined,
  createDate: undefined,
  examItemCode: undefined,
  orgId: undefined,
  sortDoctor: undefined,
})



const fordevicemValue = ref('')
const name = ref('')
let ID:number=0//表单ID
let Username:string=""//分检医生
const fordevicemData = ref<any[]>([])
const selectedRows = ref<any[]>([]); // 存储当前选中的行
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, orgId: string,pname:string,id:number,username:string,list:any[]) => {
  dialogVisible.value = true
 
  dialogTitle.value = type
 // formType.value = type
  name.value=pname
  ID=id
  Username=username
  resetForm()
  if(type=="批量")
{
  selectedRows.value=list
  console.log("批量"+list.length)
}
  

  // 修改时，设置数据
 
    formLoading.value = true
    try {
    
      
    fordevicemData.value = await ApplyformApi.getDevicelist(orgId)
    } finally {
      formLoading.value = false
    }
  
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {

  
  // 校验表单
  if(!fordevicemValue.value)
  {
    await message.alert("请选择分检设备")
  }
  else
  {
    // 提交请求
     formLoading.value = true
     try {
      if(dialogTitle.value=="批量")
     {
 /*         // 批量分检的二次确     
       await message.delConfirm("是否批量分检选择内容") */
      selectedRows.value.forEach((item) => {
        console.log(`selectedRows ${Username} was unselected.`);
        upFJApplyformVO.value.push({ id: item.id, device:item.deviceName+'|'+item.deviceId,sortDoctor: Username})
      });
        await ApplyformApi.batchupdateFJApplyform(upFJApplyformVO.value)
     }
     else{
      await ApplyformApi.updateFJApplyform({ id: ID.toString(), device:fordevicemValue.value.toString(),sortDoctor: Username})
     }
      
       message.success("分检成功")
       dialogVisible.value = false
       // 发送操作成功的事件
       emit('success')
  } finally {
    formLoading.value = false
  }

  }

 
}

/** 重置表单 */
const resetForm = () => {
  fordevicemData.value =[]
  fordevicemValue.value=''
  upFJApplyformVO.value=[]
  selectedRows.value=[]
  formRef.value?.resetFields()
}
</script>