<!-- 补送、退回 -->
<script lang="ts" setup name="teminateDialog">
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import { reactive, ref } from 'vue'
import { replenishOrBack } from '@/api/business/manager/order'
const emit = defineEmits(['onSuccess']) // 退回成功方法
const dialogVisible = ref(false) // 弹窗显示状态
const formData = ref({ // 默认表单
  sampleIdList: [], // 样品id列表
  orderId: '', // 委托单id
  remark: '', // 补送详情或退回原因
  type: '', // 类型1补送、2退回
})
const btnLoading = ref(false) // 保存按钮加载状态
const title = ref('')
const dialogTitle = ref('')

// ---------------------------------表单提交---------------------------------------------
const dataFormRef = ref<FormInstance>() // 表单对象
const rules = reactive<FormRules>({ // 校验规则
  remark: [{ required: true, message: '不能为空', trigger: ['blur'] }],
})

// 提交表单
function submitForm(formEl: FormInstance | undefined) {
  if (!formEl) { return }
  formEl.validate((valid) => {
    if (valid) {
      replenishOrBack(formData.value).then((res) => {
        ElMessage.success('操作成功')
        btnLoading.value = false
        dialogVisible.value = false
        emit('onSuccess')
      }).catch((_) => {
        btnLoading.value = false
      })
    }
  })
}

// ------------------------------------------弹窗操作------------------------------------
/**
 * 初始化审批弹窗
 * @param list 选中的设备id集合
 * @param getOrderId 任务单id
 * @param title 标题
 * @param getType 类型 1补送、2退回
 */
function initDialog(list: any, getOrderId: string, getTitle: string, getType: string) {
  // dataFormRef.value!.clearValidate()
  formData.value.remark = '' // 将补送详情或者退回原因置空
  formData.value.orderId = getOrderId // 委托单id
  formData.value.sampleIdList = list.map((item: { sampleId: string }) => item.sampleId) // 选中的设备id集合
  formData.value.type = getType // 操作类型
  title.value = getTitle // 标题
  dialogTitle.value = getTitle.slice(0, 2)
  dialogVisible.value = true
}

// 关闭弹窗
function handleClose() {
  dialogVisible.value = false
}

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

<template>
  <el-dialog
    v-model="dialogVisible"
    :title="dialogTitle"
    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="`请输入${title}`" prop="remark">
            <el-input
              v-model="formData.remark"
              :rows="4"
              show-word-limit
              type="textarea"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :loading="btnLoading" @click="submitForm(dataFormRef)">
        提交
      </el-button>
      <el-button type="info" @click="handleClose">
        取消
      </el-button>
    </template>
  </el-dialog>
</template>

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