<script setup lang="ts">
import { ref } from 'vue';
import type { SelectOption } from 'naive-ui';
import OperateModel from '@/components/advanced/operate-model.vue';
import { useFormRules } from '@/hooks/common/form';

defineOptions({
  name: 'JobOperateModel',
  inheritAttrs: false
});

withDefaults(
  defineProps<{
    statusOptions?: Api.SystemManage.DictInfo[];
    jobGroupOptions: Array<SelectOption>;
    misfirePolicyOptions: Array<SelectOption>;
  }>(),
  {
    statusOptions: () => []
  }
);

const { defaultRequiredRule } = useFormRules();

type Model = Omit<Api.SystemManage.Config, 'jobId' | 'nextValidTime'>;
type RuleKey = keyof Pick<Model, 'jobName' | 'invokeTarget' | 'cronExpression'>;

const model = ref<Model>({
  jobName: '',
  jobGroup: null,
  invokeTarget: '',
  cronExpression: '',
  misfirePolicy: '1',
  concurrent: '1',
  status: '0'
});

const rules: Record<RuleKey, App.Global.FormRule> = {
  jobName: defaultRequiredRule,
  invokeTarget: defaultRequiredRule,
  cronExpression: defaultRequiredRule
};
</script>

<template>
  <OperateModel v-model:model="model" :rules="rules" title="任务" v-bind="$attrs">
    <template #formItem>
      <NFormItemGi span="24 m:12" label="任务名称" path="jobName">
        <NInput v-model:value="model.jobName" placeholder="请输入任务名称" />
      </NFormItemGi>
      <NFormItemGi span="24 m:12" label="任务组名" path="jobGroup">
        <NSelect v-model:value="model.jobGroup" placeholder="请选择任务组名" :options="jobGroupOptions" clearable />
      </NFormItemGi>
      <NFormItemGi span="24" label="调用方法" path="invokeTarget">
        <NInput v-model:value="model.invokeTarget" placeholder="请输入调用方法" />
      </NFormItemGi>
      <NFormItemGi span="24 m:12" label="cron表达式" path="cronExpression">
        <NInput v-model:value="model.cronExpression" placeholder="请输入cron表达式" />
      </NFormItemGi>
      <NFormItemGi span="24 m:12" label="是否并发" path="concurrent">
        <NRadioGroup v-model:value="model.concurrent" name="concurrent">
          <NRadioButton value="0" label="允许" />
          <NRadioButton value="1" label="禁止" />
        </NRadioGroup>
      </NFormItemGi>
      <NFormItemGi span="24 m:12" label="错误策略" path="misfirePolicy">
        <NRadioGroup v-model:value="model.misfirePolicy" name="misfirePolicy">
          <NRadioButton
            v-for="item in misfirePolicyOptions"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          />
        </NRadioGroup>
      </NFormItemGi>
      <NFormItemGi span="24" label="状态" path="status">
        <NRadioGroup v-model:value="model.status" name="status">
          <NRadio v-for="(item, index) in statusOptions" :key="index" :value="item.dictValue" :label="item.dictLabel" />
        </NRadioGroup>
      </NFormItemGi>
    </template>
  </OperateModel>
</template>

<style scoped></style>
