<script setup lang="ts">

import type {IQuartzTableData} from "@/model/table/IQuartzTableData.ts";
import {ref} from "vue";
import StringUtils from "@/utils/StringUtils.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import QuartzApi from "@/api/quartz.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const close = () => {
  modelValue.value = false;
}
const formData = ref<IQuartzTableData>({
  status: 1
});

const add = () => {
  if(StringUtils.isNullOrEmpty(formData.value.className) || StringUtils.isNullOrEmpty(formData.value.jobDesc)
      || StringUtils.isNullOrEmpty(formData.value.cron) || formData.value.status == undefined) {
    MessageUtils.warningMessageBox('有必填数据未填');
    return;
  }
  MessageUtils.confirmMessageBox('确定添加输入的数据？','添加确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    QuartzApi.addQuartzConfig(formData.value).then((res) => {
      const responseString = JSON.stringify(res.data);
      const response = JSON.parse(responseString) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('添加成功');
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    })
  }).catch(() => {});
}


</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" title="添加定时任务" @close="close" :close-on-click-modal="false">
      <el-text type="success">注意：添加任务之后需要手动启动，否则启用的定时任务将在系统重新启动时启动</el-text>
      <el-scrollbar style="height:30vh">
        <el-form :model="formData" label-position="right" label-width="100px" require-asterisk-position="right">
          <el-form-item label="类名称键:" required>
            <el-input v-model="formData.className" placeholder="请填写类名称键"></el-input>
          </el-form-item>
          <el-form-item label="任务描述:" required>
            <el-input v-model="formData.jobDesc" placeholder="请填写任务描述"></el-input>
          </el-form-item>
          <el-form-item label="状态:" required>
            <el-switch v-model="formData.status" inline-prompt active-text="启用" :active-value="1" inactive-text="停用" :inactive-value="0"></el-switch>
          </el-form-item>
          <el-form-item label="cron 表达式:" required>
            <el-input v-model="formData.cron" placeholder="请填写cron 表达式"></el-input>
          </el-form-item>
          <el-form-item label="备注:">
            <el-input v-model="formData.remark" placeholder=""></el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <el-button type="primary" @click="add">确定添加</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>