<template>
  <el-drawer
    v-model="dialogVisible"
    :title="title"
    resizable
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
    size="70%"
    :before-close="handleClosed"
  >

    <div class="attack-risk-form">
      <h1 class="form-title">外出情况确认书</h1>
      <!-- 基本信息 -->
      <el-row :gutter="40">
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>长者姓名</text>
          <search-the-elderly ref="selectElderRef" :disabled="isDetail" @update_elder="elderUp" v-model="dataForm.elderName" :tId="dataForm.tenantId"/>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>档案号</text>
          <el-input v-if="!isDetail" v-model="dataForm.contractNumber" disabled />
          <el-text v-else disabled="">{{dataForm.contractNumber}}</el-text>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>入院日期</text>
          <el-input v-if="!isDetail" :model-value="dayjs(dataForm.checkInTime).format('YYYY-MM-DD')=='Invalid Date'?'':dayjs(dataForm.checkInTime).format('YYYY-MM-DD')" disabled />
          <el-text v-else disabled="">{{dayjs(dataForm.checkInTime).format('YYYY-MM-DD')}}</el-text>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>床位号</text>
          <el-input v-if="!isDetail" v-model="dataForm.bedName" disabled />
          <el-text v-else disabled="">{{dataForm.bedName}}</el-text>
        </el-col>
      </el-row>

      <el-row :gutter="40">
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>记录人</text>
          <el-input :disabled="isDetail" v-model="dataForm.assessor"  />
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="row">
          <text>记录日期</text>
          <el-date-picker :disabled="isDetail" v-model="dataForm.assessDate" type="date" style="width: 100%;"/>

        </el-col>
      </el-row>

      <!-- 内容区域 -->
      <div class="form-body">
        <OutingConfirmationBody
          v-model="outingConfirmationForm"
          :is-text-mode="isDetail"
        />
      </div>

    </div>



    <template #footer>
      <el-button @click="handleClosed">关闭</el-button>
    
      <el-button style="margin-left: 22px;margin-right: 30px" v-loading="formLoading" type="primary" v-show="!isDetail" @click="submitForm">确定</el-button>
    </template>

  </el-drawer>
</template>

<script lang="ts" setup>
import { computed, ref, watch, reactive } from 'vue'

import dayjs from 'dayjs'
import OutingConfirmationBody from '../components/OutingConfirmationBody.vue'

import { attackRiskCreate, attackRiskUpdate } from "@/api/social-work";
import { deleteForm } from '@/api/bpm/form/index.js';
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const title = ref('')
const dialogVisible = ref(false) // 弹窗
const formRef = ref() // 表单 Ref
const outingConfirmationForm = ref({
  elderName: '',
  outingAbility: '',
  elderSign: '',
  guardianSign: '',
  signDate: ''
}) // 外出情况确认书表单数据
const isDetail = ref(false) // 是否详情打开
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
let dataForm = ref({
  // 表单字段
  id: undefined,
  idCard: '',
  contractNumber: '', //档案号
  elderName: '',//长者姓名
  bedName: '', //床位号
  elderAge: '', //年龄
  elderSex: '', //性别
  checkInTime: '', //入院日期
  elderId: '',
  tenantId: undefined
})




const elderUp = (e) => {
  dataForm.value.elderName = e.elderName
  dataForm.value.elderId = e.id
  dataForm.value.elderSex = e.elderSex === 1 ? '男' : '女'
  dataForm.value.bedName = e.bedName || ''
  dataForm.value.checkInTime = e.checkInTime
  dataForm.value.contractNumber = e.contractNumber||e.fileNumber
  dataForm.value.elderAge = e.elderAge
  // 同步长者姓名到外出情况确认书
  outingConfirmationForm.value.elderName = e.elderName
}

// ========== 攻击风险因素评估量表 表单序列化方法 ==========

const open = (params1, params2, params3) => {
  dialogVisible.value = true
  title.value = params.title
  isDetail.value = params.isDetail
  dataForm.value = params.data
}


defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  if (formLoading.value) {
    return
  }
  formLoading.value = true
  // 提交请求
  try {
   
    const tempParams = {
      ...dataForm.value,
      assessData: JSON.stringify({
        outingConfirmation: outingConfirmationForm.value
      }),
      assessor: dataForm.value.assessor,
      assessDate: dataForm.value.assessDate ? dayjs(dataForm.value.assessDate).format('YYYY-MM-DD') : ''
    }

    if (dataForm.value.id) {
      const res = await attackRiskUpdate(tempParams)
      if (res) {
        message.success(t('common.updateSuccess'))
        dialogVisible.value = false
        // 发送操作成功的事件
        emit('success')
      }
    } else {
      const res = await attackRiskCreate(tempParams)
      if (res) {
        message.success(t('common.createSuccess'))
        dialogVisible.value = false
        // 发送操作成功的事件
        emit('success')
      }
    }
  } finally {
    setTimeout(() => {
      formLoading.value = false
    }, 500)
  }
}



/** 重置表单 */
const resetForm = () => {

  formRef.value?.resetFields()

 
}

// 关闭表单
const handleClosed = () => {
  dialogVisible.value = false
  resetForm()
}


</script>

<style scoped lang="scss">

.form-title {
  text-align: center;
  font-size: 20px;
  width: 100%;
  margin-bottom: 20px;
}

.attack-risk-form {
  max-width: 1200px;
  margin: 0 auto;
  background: #fff;





  .info-row {
    display: flex;
    gap: 40px;
    width: 100%;
    flex-direction: row;
    align-items: center;
    margin-bottom: 10px;

    .info-item {
      display: flex;
      align-items: center;
      gap: 8px;

      .label {
        width: 82px;
        text-align: right;
        margin-right: 4px;
        white-space: nowrap;
      }
    }
  }


  .form-body {
    border: 1px solid #333;
    padding: 15px;
  }

  // 调整 Element Plus 组件样式
  :deep(.el-input__inner) {
    height: 28px;
    line-height: 28px;
    border-top: none;
    border-left: none;
    border-right: none;
    border-radius: 0;
    padding: 0 4px;

    &:focus {
      border-color: #409eff;
    }
  }

  :deep(.el-checkbox__label),
  :deep(.el-radio__label) {
    padding-left: 4px;
  }
}
.row{
  margin-bottom: 12px;
  display: flex;
  flex-direction: row;
  align-items: center;
  text{
    text-align: right;
    margin-right: 4px;
    width: 82px;
  }
}

// NGASR自杀风险评估量表 特有样式
.total-score-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
  margin-bottom: 20px;

  .total-score {
    display: flex;
    align-items: baseline;
    gap: 4px;

    .score-label {
      font-size: 16px;
      font-weight: bold;
    }

    .score-value {
      font-size: 28px;
      font-weight: bold;
      color: #409eff;
    }

    .score-max {
      font-size: 14px;
      color: #909399;
    }
  }

  .risk-level {
    font-size: 16px;
    font-weight: bold;
    padding: 8px 16px;
    border-radius: 4px;

    &.risk-none {
      color: #67c23a;
      background: #f0f9eb;
    }

    &.risk-low {
      color: #e6a23c;
      background: #fdf6ec;
    }

    &.risk-medium {
      color: #f56c6c;
      background: #fef0f0;
    }

    &.risk-high {
      color: #f56c6c;
      background: #fef0f0;
      border: 1px solid #f56c6c;
    }
  }
}

.assessment-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;

  table {
    width: 100%;
    border: 1px solid #333;

    th, td {
      border: 1px solid #333;
      padding: 12px;
      text-align: left;
      vertical-align: top;
    }

    th {
      background: #f5f7fa;
      font-weight: bold;
      text-align: center;
    }

    .content-col {
      width: 65%;
    }

    .level-col {
      width: 10%;
      text-align: center;
    }

    .select-col {
      width: 25%;
      text-align: center;
    }

    .content {
      text-align: left;

      .content-title {
        font-weight: bold;
        margin-bottom: 8px;
      }

      .content-item {
        margin-left: 15px;
        margin-bottom: 4px;
        font-size: 13px;
      }
    }

    .level {
      text-align: center;
      font-weight: bold;
    }

    .select {
      text-align: center;

      :deep(.el-radio) {
        margin-right: 0;
      }
    }
  }
}

.risk-judgment-section {
  margin: 20px 0;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
  border: 1px solid #e4e7ed;

  .risk-judgment-title {
    font-weight: bold;
    font-size: 16px;
    margin-bottom: 12px;
    color: #303133;
  }

  .risk-options {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;

    :deep(.el-radio) {
      margin-right: 0;
    }
  }
}

.preventive-section {
  margin: 20px 0;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
  border: 1px solid #e4e7ed;

  .preventive-title {
    font-weight: bold;
    font-size: 16px;
    margin-bottom: 12px;
    color: #303133;
  }

  .preventive-options {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;

    :deep(.el-checkbox) {
      margin-right: 20px;
      margin-bottom: 8px;
    }

    .other-input {
      width: 200px;
      margin-left: 10px;
    }
  }
}

.signature-section {
  margin: 20px 0;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
  border: 1px solid #e4e7ed;

  .signature-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 20px;
  }

  .signature-item {
    display: flex;
    align-items: center;
    gap: 10px;

    &.date-item {
      :deep(.el-date-picker) {
        width: 150px;
      }
    }
  }

  .signature-label {
    font-weight: bold;
    white-space: nowrap;
  }

  .signature-input {
    width: 150px;
  }

  .date-picker {
    width: 150px;
  }
}

.note-section {
  margin-top: 20px;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
  border: 1px solid #e4e7ed;

  .note-title {
    font-weight: bold;
    font-size: 16px;
    margin-bottom: 12px;
    color: #303133;
  }

  .note-content {
    font-size: 13px;
    line-height: 1.8;
    color: #606266;

    p {
      margin: 5px 0;
    }

    .indent {
      margin-left: 20px;
    }
  }
}
</style>
