<!-- 退检操作 -->
<script lang="ts" setup name="SendBackDialog">
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import { reactive, ref } from 'vue'
import type { ISendBack, IlabMeasureList } from '../../schedule/task/task-interface'
import { confirmSendBack, sendBack } from '@/api/business/schedule/task'

// 对外暴露的方法： 退回成功
const emit = defineEmits(['onSuccess'])
// 弹窗显示状态
const dialogVisible = ref(false)
// 表单数据对象
const formData = reactive({
  id: '',
  operateReason: '', // 退回原因
  measurePersonId: '',
  orderId: '',
  requireCertifications: '',
  sampleId: '',
})
const type = ref('sendBack') // sendBack退检、confirmSendBack 确认退检

// 保存按钮加载状态
const btnLoading = ref(false)

// ---------------表单提交--------------------------------
// 表单对象
const dataFormRef = ref<FormInstance>()
// 校验规则
const rules: FormRules = reactive({
  operateReason: [{ required: true, message: '退检原因必填', trigger: ['blur', 'change'] }],
})
/**
 * 初始化审批弹窗
 * @param type 审批类型
 * @param taskId 任务id
 */
function initDialog(row: IlabMeasureList, typeParam = 'sendBack') {
  type.value = typeParam
  formData.id = row.id
  formData.operateReason = ''
  if (typeParam === 'confirmSendBack') {
    formData.measurePersonId = row.measurePersonId
    formData.orderId = row.orderId
    formData.requireCertifications = `${row.requireCertifications}`
    formData.sampleId = row.sampleId
    formData.operateReason = row.remark
  }
  else {
    formData.operateReason = ''
  }
  dialogVisible.value = true
}
// 提交表单
function submitForm() {
  if (dataFormRef.value) {
    dataFormRef.value?.validate((valid: boolean) => {
      if (valid) {
        ElMessageBox.confirm(
          '确认退检吗？',
          '提示',
          { confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning' },
        ).then(() => {
          btnLoading.value = true
          if (type.value === 'sendBack') {
            const params = {
              id: formData.id,
              operateReason: formData.operateReason, // 退回原因
            }
            sendBack(params).then((res: { code: number }) => {
              if (res.code == 200) {
                ElMessage.success('退检成功')
                btnLoading.value = false
                dialogVisible.value = false
                emit('onSuccess')
              }
            }).catch((_) => {
              btnLoading.value = false
            })
          }
          else {
            const params = [{
              measurePersonId: formData.measurePersonId,
              orderId: formData.orderId,
              requireCertifications: formData.requireCertifications,
              sampleId: formData.sampleId,
            }]
            confirmSendBack(params).then(() => {
              ElMessage.success('确认退检成功')
              btnLoading.value = false
              dialogVisible.value = false
              emit('onSuccess')
            })
          }
        })
      }
    })
  }
}
// 关闭弹窗
function handleClose() {
  dialogVisible.value = false
}

// ----------------------- 以下是暴露的方法内容 ----------------------------
defineExpose({ initDialog })
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title="type === 'confirmSendBack' ? '确认退检' : '退检'"
    width="600"
    :before-close="handleClose"
  >
    <el-form
      ref="dataFormRef"
      label-position="top"
      label-width="80px"
      :model="formData"
      :rules="rules"
    >
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item :label="type === 'confirmSendBack' ? '退检原因' : '请输入退检原因'" prop="operateReason">
            <el-input
              v-model="formData.operateReason"
              :rows="4"
              show-word-limit
              type="textarea"
              :maxlength="50"
              :disabled="type === 'confirmSendBack'"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :loading="btnLoading" @click="submitForm">
        提交
      </el-button>
      <el-button type="info" @click="handleClose">
        取消
      </el-button>
    </template>
  </el-dialog>
</template>

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