<template>
  <basicModal
    @register="modalRegister"
    ref="modalRef"
    class="basicModal basicFormModal"
    @on-ok="handleSubmit"
    @on-close="handleClose"
  >
    <template #default>
      <n-form
        class="ls-form"
        ref="formRef"
        :model="formData"
        label-placement="left"
        label-width="110px"
      >
        <n-form-item
          label="任务名称"
          path="jobName"
          :rule="{ required: true, message: '请输入任务名称', trigger: 'blur' }"
        >
          <n-input
            class="ls-input"
            v-model:value="formData.jobName"
            placeholder="请输入任务名称"
            clearable
          />
        </n-form-item>
        <n-form-item
          label="任务别名"
          path="jobAlias"
          :rule="{ required: true, message: '请输入任务别名', trigger: 'blur' }"
        >
          <n-input
            class="ls-input"
            v-model:value="formData.jobAlias"
            placeholder="请输入任务别名"
            clearable
          />
        </n-form-item>
        <n-form-item
          label="任务分组"
          path="jobGroup"
          :rule="{ required: true, message: '请选择任务分组', trigger: 'change' }"
        >
          <n-select
            v-model:value="formData.jobGroup"
            placeholder="请选择任务分组"
            :options="jobGroupList"
          />
        </n-form-item>
        <n-form-item
          label="任务触发器"
          path="jobTrigger"
          :rule="{ required: true, message: '请输入任务触发器', trigger: 'blur' }"
        >
          <n-input
            class="ls-input"
            v-model:value="formData.jobTrigger"
            placeholder="请输入任务触发器"
            clearable
          />
        </n-form-item>
        <n-form-item
          label="任务状态"
          path="status"
          :rule="{ type: 'number', required: true, message: '请选择任务状态', trigger: 'change' }"
        >
          <n-select
            v-model:value="formData.status"
            placeholder="请选择任务状态"
            :options="statusList"
          />
        </n-form-item>
        <n-form-item
          label="正则表达式"
          path="cronExpression"
          :rule="{ required: true, message: '请输入正则表达式', trigger: 'blur' }"
        >
          <n-input
            class="ls-input"
            v-model:value="formData.cronExpression"
            placeholder="请输入正则表达式"
            clearable
          />
        </n-form-item>
        <n-form-item
          label="执行策略"
          path="executePolicy"
          :rule="{ type: 'number', required: true, message: '请选择执行策略', trigger: 'change' }"
        >
          <n-select
            v-model:value="formData.executePolicy"
            placeholder="请选择执行策略"
            :options="executePolicyList"
          />
        </n-form-item>
        <n-form-item label="是否同步任务" path="isSync" class="flex-1">
          <n-radio-group v-model:value="formData.isSync" name="isSync">
            <n-radio :value="1">是</n-radio>
            <n-radio :value="0">否</n-radio>
          </n-radio-group>
        </n-form-item>
        <n-form-item
          label="任务URL"
          path="url"
          :rule="{ required: true, message: '请输入任务URL', trigger: 'blur' }"
        >
          <n-input
            class="ls-input"
            v-model:value="formData.url"
            placeholder="请输入任务URL"
            clearable
          />
        </n-form-item>
        <n-form-item
          label="任务备注"
          path="note"
          :rule="{ required: true, message: '请输入任务备注', trigger: 'blur' }"
        >
          <n-input v-model:value="formData.note" type="textarea" />
        </n-form-item>
      </n-form>
    </template>
  </basicModal>
</template>
<script lang="ts" setup>
  import { getJobDetail, jobAdd, jobUpdate } from '@/api/monitor/job';
  import { onMounted, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { useModal } from '@/components/Modal';

  const emit = defineEmits(['success', 'update:visible']);
  const formRef = ref();

  /**
   * 定义表单参数
   */
  const message = useMessage();
  const formData = reactive({
    id: '',
    jobName: '',
    jobAlias: '',
    jobGroup: undefined,
    jobTrigger: '',
    status: undefined,
    cronExpression: '',
    executePolicy: undefined,
    isSync: 0,
    url: '',
    note: '',
  });

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    jobId: {
      type: Number,
      required: true,
      default: 0,
    },
  });
  const [modalRegister, { openModal, setSubLoading }] = useModal({
    title: props.jobId ? '编辑任务' : '添加任务',
    subBtuText: '确定',
    width: 600,
  });
  const jobGroupList = [{ label: 'DEFAULT', value: 'DEFAULT' }];
  const statusList = [
    { label: '未发布', value: 0 },
    { label: '运行中', value: 1 },
    { label: '暂停', value: 2 },
    { label: '删除', value: 3 },
  ];
  const executePolicyList = [
    { label: '立即执行', value: 1 },
    { label: '执行一次', value: 2 },
    { label: '放弃执行', value: 3 },
  ];
  /**
   * 执行提交
   */
  const handleSubmit = async () => {
    formRef.value
      .validate()
      .then(async () => {
        props.jobId ? await jobUpdate(formData) : await jobAdd(formData);
        message.success('操作成功');
        setSubLoading(false);
        emit('update:visible', false);
        emit('success');
      })
      .catch((error) => {
        setSubLoading(false);
      });
  };

  /**
   * 关闭窗体
   */
  const handleClose = () => {
    emit('update:visible', false);
  };

  /**
   * 设置表单数据
   */
  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();
    }
  });
  //导出方法
  defineExpose({
    openModal,
  });
</script>
