<!-- 删除原因 -->
<script lang="ts" setup name="DelReasonDialog">
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { reactive, ref } from 'vue'
import { SCHEDULE } from '@/utils/scheduleDict'
import { submit } from '@/api/equipment/standard/book'

// 对外暴露的方法： 退回成功
const emit = defineEmits(['onSuccess'])
// 弹窗显示状态
const dialogVisible = ref(false)
// 默认表单
const form = ref({
  approvalType: '3', // 删除传3
  formId: SCHEDULE.EQUIPMENT_BOOK_APPROVAL,
  id: '',
  reason: '', // 删除原因
})

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

// ---------------表单提交--------------------------------
// 表单对象
const dataFormRef = ref<FormInstance>()
// 校验规则
const rules = reactive<FormRules>({
  reason: [{ required: true, message: '删除原因必填', trigger: ['blur', 'change'] }],
})
/**
 * 初始化审批弹窗
 */
function initDialog(row: any) {
  form.value.id = row.id
  form.value.reason = ''
  dialogVisible.value = true
}
// 提交表单
function submitForm(formEl: FormInstance | undefined) {
  if (!formEl) { return }
  formEl.validate((valid) => {
    if (valid) {
      btnLoading.value = true
      submit(form.value).then((res) => {
        ElMessage.success('删除审批已提交')
        btnLoading.value = false
        dialogVisible.value = false
        emit('onSuccess')
      }).catch((_) => {
        btnLoading.value = false
      })
    }
  })
}
// 关闭弹窗
function handleClose() {
  dialogVisible.value = false
}

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

<template>
  <el-dialog
    v-model="dialogVisible"
    title="删除"
    width="600"
    :before-close="handleClose"
  >
    <el-form
      ref="dataFormRef"
      label-position="top"
      label-width="80px"
      :model="form"
      :rules="rules"
    >
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="请输入删除原因" prop="reason">
            <el-input
              v-model="form.reason"
              :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>
