<script lang="ts" setup name="ApprovalDialog">
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'
import useUserStore from '@/store/modules/user'
import { submitApproval } from '@/api/approval'
/**
 * 审批弹窗
 */
defineProps({
  /**
   * 提示文字
   */
  title: {
    type: String,
    default: '审批操作',
  },
})
const emit = defineEmits(['onSuccess'])
// 弹窗显示状态
const dialogVisible = ref(false)
// 默认表单
const defaultFormData = {
  type: '', // 审批类型
  taskId: '', // 任务id
  comments: '', // 备注
  user: '', // 当前用户
  approvalTime: '', // 当前时间
}
// 弹窗类型 agree同意 refuse拒绝 reject驳回 revoke取消
const type = ref('agree')
// 表单数据对象
const formData = reactive({ ...defaultFormData })

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

// agree同意 refuse拒绝 reject驳回 revoke取消
const userInfo = useUserStore()

// ---------------表单提交--------------------------------
// 表单对象
const dataFormRef = ref<FormInstance>()
// 校验规则
const rules: FormRules = reactive({
  comments: [{ required: true, message: '审批结论必填', trigger: ['blur', 'change'] }],
})
/**
 * 初始化审批弹窗
 * @param type 审批类型
 * @param taskId 任务id
 */
function initDialog(type: string, taskId: string) {
  formData.type = type
  formData.taskId = taskId
  formData.user = userInfo.name
  formData.approvalTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
  formData.comments = ''
  dialogVisible.value = true
}
// 提交表单
function submitForm() {
  if (dataFormRef) {
    dataFormRef.value?.validate((valid: boolean) => {
      if (valid) {
        btnLoading.value = true
        if (formData.type) {
          submitApproval(formData.type, { taskId: formData.taskId, comments: formData.comments }).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="left"
      label-width="80px"
      :model="formData"
      :rules="rules"
    >
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="审批意见:">
            <el-radio-group v-model="formData.type" disabled>
              <el-radio label="agree" size="large">
                同意
              </el-radio>
              <el-radio label="refuse" size="large">
                驳回
              </el-radio>
              <el-radio label="reject" size="large">
                拒绝
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="审批结论:" props="comments">
            <el-input
              v-model="formData.comments"
              :rows="4"
              show-word-limit
              type="textarea"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="审批人:">
            <el-input
              v-model="formData.user"
              placeholder="审批人"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审批时间:">
            <el-input
              v-model="formData.approvalTime"
              style="width: 100%;"
              placeholder="审批时间"
              disabled
            />
          </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>
