<script setup lang="ts">
import {ref} from "vue";
import type {IQuartzTableData} from "@/model/table/IQuartzTableData.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import QuartzApi from "@/api/quartz.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const props = defineProps(["formData"]);
const emit = defineEmits(['reLoadingTableData']);
const close = () => {
  modelValue.value = false;
}
// 删除任务
const deleteData = () => {
  MessageUtils.confirmMessageBox('确定删除此条记录？', '删除确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    QuartzApi.deleteQuartzConfig(props.formData.className).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(()=>{})
}

// 启动定时任务
const startJob = () => {
  MessageUtils.confirmMessageBox('确定开启此任务？', '启动确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    QuartzApi.startQuartzConfig(props.formData.className).then((res) => {
      const responseString = JSON.stringify(res.data);
      const response = JSON.parse(responseString) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('启动成功');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    })
  }).catch(()=>{})
}

// 停止定时任务
const stopJob = () => {
  MessageUtils.confirmMessageBox('确定停止此任务？', '停止确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    QuartzApi.stopQuartzConfig(props.formData.className).then((res) => {
      const responseString = JSON.stringify(res.data);
      const response = JSON.parse(responseString) as AppServeResponse;
      if (response && response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('停止成功');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg, '警告');
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    })
  }).catch(()=>{})
}

// 保存数据
const save = () => {
  MessageUtils.confirmMessageBox('确定更新数据？', '更新确认').then(() => {
    const loadingInstance = MessageUtils.loading();
    QuartzApi.updateQuartzConfig(props.formData).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-scrollbar style="height:40vh">
        <el-form :model="formData" label-position="right" label-width="130px" require-asterisk-position="right">
          <el-form-item label="类名称键:" required>
            <el-text type="primary">{{formData.className}}</el-text>
          </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-text type="warning">提醒：您在此处启动的任务将在系统重新启动时失效，如需要自动启动，请将状态设置为启用</el-text>
      </el-scrollbar>
      <template #footer>
        <el-row>
          <el-col :span="16">
            <el-button-group>
              <el-button type="success" @click="startJob">启动任务</el-button>
              <el-button type="warning" @click="stopJob">停止任务</el-button>
            </el-button-group>
          </el-col>
          <el-col :span="8">
            <el-button-group>
              <el-button type="primary" @click="save">保存修改</el-button>
              <el-button type="warning" @click="deleteData">删除数据</el-button>
              <el-button type="danger" @click="close">关闭窗口</el-button>
            </el-button-group>
          </el-col>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>