<template>
  <el-dialog  
    v-model="dialogVisible" 
    title="申请返修"
    width="720px" 
    style="height: 490px"
    >
    <div class="ApplyforRepair">
      <!-- 患者基础信息 -->
      <div class="info-section">
        <div class="section-title"><span style="margin-left: 5px;" >患者基础信息</span></div>
        <el-form label-width="80px">
          <el-row :gutter="24">
            <el-col :span="9">
              <el-form-item label="检查日期:" label-width="80">
                <el-input
                  style="width: 160px"
                  :value="formatDate(rowinfo.examDate, 'YYYY-MM-DD')"
                  disabled
                />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="姓名:" label-width="60">
                <el-input :value="rowinfo.pname" style="width: 100px" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="性别:" label-width="50">
                <el-input :value="rowinfo.gender" style="width: 100px" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="检查编号:">
                <el-input :value="rowinfo.examId" style="width: 140px" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <el-form-item label="检查机构:" label-width="30">
                <el-input :value="rowinfo.orgName" style="width: 180px" disabled />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!-- 申请返修信息 -->
      <div class="info-section">
        <div class="section-title"><span style="margin-left: 5px;">申请返修信息</span></div>
        <el-form label-width="100px" :model="formData" :rules="rules" ref="formRef">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="申请医生:" prop="applyDoctor">
                <el-input v-model="formData.applyDoctor" style="width: 180px" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请时间:" prop="applyDateTime">
                <el-date-picker
                  v-model="formData.applyDateTime"
                  type="datetime"
                  placeholder="请选择申请时间"
                  style="width: 180px"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="申请原因:" prop="processContent">
            <el-input 
              v-model="formData.processContent" 
              type="textarea" 
              rows="4"
              placeholder="请输入申请原因"
            />
          </el-form-item>
        </el-form>
      </div>
      <el-form-item>
        <el-button type="primary" @click="save" :disabled="issaveshow">确认</el-button>
        <el-button @click="() => (dialogVisible = false)">取消</el-button>
      </el-form-item>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { getUserProfile, ProfileVO } from '@/api/system/user/profile'
import { formatDate } from '@/utils/formatTime'
import { ProcessManageApi,ProcessManageVO } from '@/api/ECG/processManage'
const Profilevo = ref<ProfileVO>({} as ProfileVO) //当前登录人信息
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const issaveshow = ref(false)
const rowinfo = ref() //当前患者数据行
const formData = ref({
  id: '',
  userId: '',
  orgId: '',
  examId: '',
  regId: '',
  processOrgId: '',
  processorgName: '',
  processDoctor: '',
  processDate: '',
  processContent: '',
  applyDoctor: '',
  applyDateTime: '',
  remark: '',
  processstats:'',
  refuseremark: '',
  pname: ''
})
const formRef = ref()
const rules = {
  applyDateTime: [
    { required: true, message: '请选择申请时间', trigger: 'change' }
  ],
  processContent: [
    { required: true, message: '请输入申请原因', trigger: 'blur' }
  ]
}

const resetForm = () => {
  formData.value = {
    id: '',
    userId: '',
    orgId: '',
    examId: '',
    regId: '',
    processOrgId: '',
    processorgName: '',
    processDoctor: '',
    processDate: '',
    processContent: '',
    applyDoctor: '',
    applyDateTime: '',
    remark: '',
    processstats:'',
    refuseremark: '',
    pname: ''
  }
}



const opendiag = async (type: string, row: any, orgId: string, doctorname: string,id:string) => {
  resetForm()
  const data = await ProcessManageApi.getPage({
    examId: row.examId
  })
  
  // 检查是否存在 processstats === 0 的记录
  if (data?.list?.[0]?.processstats === 0) {
    dialogVisible.value = false
    message.alertError('已申请')
    return
  }
  
  rowinfo.value = row
  // 设置基础数据
  formData.value.examId = row.examId
  formData.value.orgId = orgId
  formData.value.regId = row.regId
  formData.value.pname = row.pname
  formData.value.applyDoctor = doctorname
  formData.value.userId = id
  dialogTitle.value = t('action.' + type)
  if(data?.list?.[0]?.processstats === 0){
    dialogVisible.value = false
    message.alertError('已申请')
    return
  }
  else{
    dialogVisible.value = true
  }
}
async function save() {
  if (!formRef.value) 
  return
  await formRef.value.validate((valid: boolean) => {
    if (valid) {
      formData.value.id = rowinfo.value.id
      formData.value.userId = formData.value.userId
      formData.value.examId = rowinfo.value.examId
      formData.value.regId = rowinfo.value.regId
      formData.value.orgId = formData.value.orgId
      formData.value.applyDoctor = formData.value.applyDoctor
      formData.value.pname = rowinfo.value.pname
      formData.value.applyDateTime = new Date().toLocaleString().replace(/\//g, '-')
      const savedata = formData.value as unknown as ProcessManageVO
      ProcessManageApi.create(savedata).then((data) => {
        if (data) {
          message.alertSuccess('申请成功')
          dialogVisible.value = false
          emit('success')
        } else {
          message.alertError('申请失败')
        }
      })
    }
  })
}
defineExpose({ opendiag }) // 提供 open 方法，用于打开弹窗
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
defineOptions({ name: 'ECGApplyforRepair' })

</script>
<style lang="css" scoped>
.ApplyforRepair {
  width: 680px;
  background-color: #fff;
}

.info-section {
  margin-bottom: 10px;
  padding: 2px;
  border: 1px solid #d7d7d7;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #d7d7d7;
  text-align: center;
}
</style>
