<template>
  <el-dialog title="修改定时任务" v-model="show" width="400px" append-to-body draggable>
    <el-form label-width="120px">
      <el-form-item label="任务名称" prop="dataFlag">
        <el-select v-model="form.taskId">
          <el-option v-for="item in files" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="!form.pid" label="定时表达式" prop="fiveValue">
        <w-cron-input v-model="form.cron" />
      </el-form-item>
      <template v-if="(files.find(a => a.id == form.taskId)?.params || []).length > 0">
        <el-divider>参数：SpEL表达式</el-divider>
        <el-alert style="margin-bottom: 1rem;">#root,#act：ApplicationContext<br />#bean: 执行任务的方法所在的bean</el-alert>
        <el-form-item v-for="(item, index) in files.find(a => a.id == form.taskId)?.params || []" :key="index"
          :label="item">
          <el-input v-model="form.paramELs[index]" />
        </el-form-item>

      </template>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="show = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, getCurrentInstance } from 'vue';
import { ElLoading, ElMessage } from 'element-plus';
import * as api from '../cron';

const emit = defineEmits(["success"]);

const { proxy } = getCurrentInstance();
const form = ref({
});
const show = ref(false);

const files = ref([])

const open = async (taskList, obj = {}) => {
  form.value = {...obj };
  show.value = true;
  files.value = taskList;
}

const submitForm = async () => {
  let loading = ElLoading.service({ fullscreen: true, lock: true, background: '#0001' });
  try {
    await api.update(form.value);
    ElMessage.success("添加成功");
    show.value = false;
    emit("success", form.value);
  } finally {
    loading.close();
  }
}


defineExpose({ open });
</script>

<style lang="scss" scoped></style>