<template>
  <el-dialog v-model="show" title="编辑" width="40%" @close="handleClose">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="任务类名">
        <el-input v-model="form.className" disabled />
      </el-form-item>
      <el-form-item prop="cronExpression" label="cron表达式">
        <el-input v-model="form.cronExpression" />
      </el-form-item>
      <el-form-item label="状态">
        <el-tag :type="form.status ? 'success' : ('error' as any)" disable-transitions>{{
          form.status ? '成功' : '失败'
        }}</el-tag>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="onComfirm">确认</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
const emtis = defineEmits(['comfirm'])
const show = ref(false)

const form = ref()
const formRef = ref()

const rules = { cronExpression: [{ required: true, message: '请输入cron表达式', trigger: 'blur' }] }

const onComfirm = () => {
  formRef.value?.validate((valid: Boolean, fields: any) => {
    if (valid) {
      emtis('comfirm', form.value)
      handleClose()
    } else {
      console.log('error submit!', fields)
    }
  })
}

const handleClose = () => {
  formRef.value?.resetFields()
  show.value = false
}

defineExpose({
  form,
  show
})
</script>

<style scoped></style>
