<template>
  <el-dialog
      v-model="props.visible"
      :title="props.jobId?'编辑':'新增'"
      width="600"
      :close-on-click-modal="false"
      :before-close="dialogClose"
  >
    <el-form
        class="ls-form"
        ref="formRef"
        :model="formData"
        label-width="110px"
    >
      <el-form-item
          label="任务名称"
          prop="jobName"
          :rules="{ required: true, message: '请输入任务名称', trigger: 'blur' }"
      >
        <el-input
            class="ls-input"
            v-model="formData.jobName"
            placeholder="请输入任务名称"
            clearable
        />
      </el-form-item>
      <el-form-item
          label="任务别名"
          prop="jobAlias"
          :rules="{ required: true, message: '请输入任务别名', trigger: 'blur' }"
      >
        <el-input
            class="ls-input"
            v-model="formData.jobAlias"
            placeholder="请输入任务别名"
            clearable
        />
      </el-form-item>
      <el-form-item
          label="任务分组"
          prop="jobGroup"
          :rules="{ required: true, message: '请输入任务分组', trigger: 'blur' }"
      >
        <el-select v-model="formData.jobGroup" placeholder="请选择任务分组">
            <el-option label="DEFAULT" value="DEFAULT"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item
          label="任务触发器"
          prop="jobTrigger"
          :rules="{ required: true, message: '请输入任务触发器', trigger: 'blur' }"
      >
        <el-input
            class="ls-input"
            v-model="formData.jobTrigger"
            placeholder="请输入任务触发器"
            clearable
        />
      </el-form-item>
      <el-form-item
          label="任务状态"
          prop="status"
          :rules="{ required: true, message: '请选择任务状态', trigger: 'blur' }"
      >
      <el-select v-model="formData.status" placeholder="请选择任务状态">
            <el-option label="未发布" :value="0"></el-option>
            <el-option label="运行中" :value="1"></el-option>
            <el-option label="暂停" :value="2"></el-option>
            <el-option label="删除" :value="3"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item
          label="正则表达式"
          prop="cronExpression"
          :rules="{ required: true, message: '请输入正则表达式', trigger: 'blur' }"
      >
        <el-input
            class="ls-input"
            v-model="formData.cronExpression"
            placeholder="请输入正则表达式"
            clearable
        />
      </el-form-item>
      <el-form-item
          label="执行策略"
          prop="executePolicy"
          :rules="{ required: true, message: '请选择执行策略', trigger: 'blur' }"
      >
      <el-select v-model="formData.executePolicy" placeholder="请选择执行策略">
            <el-option label="立即执行" :value="1"></el-option>
            <el-option label="执行一次" :value="2"></el-option>
            <el-option label="放弃执行" :value="3"></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="是否同步任务" prop="isSync" class="flex-1">
          <el-radio-group v-model="formData.isSync" name="isSync">
            <el-radio :value="1">是</el-radio>
            <el-radio :value="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      <el-form-item
          label="任务URL"
          prop="url"
          :rules="{ required: true, message: '请输入任务URL', trigger: 'blur' }"
      >
      <el-input
            class="ls-input"
            v-model="formData.url"
            placeholder="请输入任务URL"
            clearable
        />
      </el-form-item>
      <el-form-item label="任务备注" prop="note" :rules="{ required: true, message: '请输入任务备注', trigger: 'blur' }">
        <el-input v-model="formData.note" type="textarea"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogClose">取消</el-button>
        <el-button :loading="subLoading" type="primary" @click="submit">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import type {FormInstance} from "element-plus";
import {getJobDetail,jobAdd,jobUpdate} from "@/api/monitor/job";
import {onMounted, reactive, shallowRef} from "vue";
import {message} from "@/utils/auth";
import {useLockFn} from "@/utils/useLockFn";

const emit = defineEmits(["success", "update:visible"]);
const formRef = shallowRef<FormInstance>();
const formData = reactive({
  id: "",
  jobName: "",
  jobAlias: "",
  jobGroup: "",
  jobTrigger: "",
  status: "",
  cronExpression: "",
  executePolicy: "",
  isSync: 0,
  url:"",
  note: ""
});

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false
  },
  jobId: {
    type: Number,
    required: true,
    default: 0
  }
});

const handleSubmit = async () => {
    await formRef.value?.validate();
    props.jobId ? await jobUpdate(formData) : await jobAdd(formData);
    message("操作成功");
    emit("update:visible", false);
    emit("success");
};

const dialogClose = () => {
  emit("update:visible", false);
};

const { isLock:subLoading,lockFn: submit } = useLockFn(handleSubmit);


const setFormData = async () => {
  const data = await getJobDetail(props.jobId);
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

onMounted(() => {
  if (props.jobId) {
    setFormData();
  }
});

</script>
