import { DescItem } from '/@/components/Description/index';
import { BasicColumn, FormSchema } from '/@/components/Table';
import moment from 'moment';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';

///任务组名Enum
export enum EJobGroup {
  DEFAULT = 'DEFAULT',
  SYSTEM = 'SYSTEM',
  REPORT = 'REPORT',
}

//任务详细配置
export const personSchema: DescItem[] = [
  {
    field: 'jobGroup',
    label: '任务分组：',
    render: (_, data) => {
      return data.jobGroup == EJobGroup.DEFAULT
        ? '默认'
        : data.jobGroup == EJobGroup.SYSTEM
        ? '系统'
        : '报表';
    },
  },
  {
    field: 'jobName',
    label: '任务名称：',
  },
  {
    field: 'createTime',
    label: '创建时间：',
  },
  {
    field: 'cronExpression',
    label: 'cron表达式：',
  },
  {
    field: 'updateTime',
    label: '下次执行时间：',
  },
  {
    field: 'invokeTarget',
    label: '调用目标方法：',
  },
  {
    field: 'status',
    label: '任务状态：',
    render: (_, data) => {
      return data.status == 1 ? '启用' : '禁用';
    },
  },
  {
    field: 'concurrent',
    label: '是否并发：',
    render: (_, data) => {
      return data.concurrent == 0 ? '允许' : '禁止';
    },
  },
  {
    field: 'misfirePolicy',
    label: '执行策略：',
    render: (_, data) => {
      return data.misfirePolicy == 0
        ? '立即执行'
        : data.misfirePolicy == 1
        ? '执行一次'
        : '放弃执行';
    },
  },
];

// 调度日志表格配置
export const columnSchedue: BasicColumn[] = [
  {
    title: '任务名称',
    dataIndex: 'jobName',
    width: 120,
  },
  {
    title: '任务组名',
    dataIndex: 'jobGroup',
    width: 120,
    format: (_text: string, record: Recordable) => {
      return record.jobGroup === EJobGroup.DEFAULT
        ? '默认'
        : record.jobGroup === EJobGroup.SYSTEM
        ? '系统'
        : '报表';
    },
  },
  {
    title: '调用目标字符串',
    dataIndex: 'invokeTarget',
    width: 120,
  },
  {
    title: '日志信息',
    dataIndex: 'jobMessage',
    width: 160,
  },
  {
    title: '执行状态',
    dataIndex: 'status',
    width: 160,
    customRender: ({ record }) => {
      const status = record.status;
      const success = status === 1;
      const color = success ? 'green' : 'red';
      const successText: string = '成功';
      const failedText: string = '失败';
      const text = success ? successText : failedText;
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '执行时间',
    dataIndex: 'startTime',
    width: 180,
  },
];

// 调度日志表格查询配置
export const searchSchedueFormSchema: FormSchema[] = [
  {
    field: 'jobName',
    label: '任务名称',
    component: 'Input',
    colProps: { span: 4 },
    componentProps: {
      maxLength: 36,
      placeholder: '请输入任务名称',
    },
  },
  {
    field: 'jobGroup',
    label: '任务组名',
    component: 'Select',
    colProps: { span: 4 },
    componentProps: {
      options: [
        {
          label: '默认',
          value: EJobGroup.DEFAULT,
        },
        {
          label: '系统',
          value: EJobGroup.SYSTEM,
        },
        {
          label: '报表',
          value: EJobGroup.REPORT,
        },
      ],
      placeholder: '请选择任务组名',
    },
  },
  {
    field: 'status',
    label: '执行状态',
    component: 'Select',
    colProps: { span: 4 },
    componentProps: {
      options: [
        {
          label: '成功',
          value: 1,
        },
        {
          label: '失败',
          value: 0,
        },
      ],
      placeholder: '请选择执行状态',
    },
  },
  {
    field: 'sendTime',
    label: '执行时间',
    component: 'RangePicker',
    componentProps: {
      showTime: {
        defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')],
      },
    },
    colProps: { span: 4 },
  },
];
// 调度日志详情
export const scheduleLogDetailSchema: DescItem[] = [
  {
    field: 'jobName',
    label: '任务名称：',
  },
  {
    field: 'jobGroup',
    label: '任务组名：',
    render: (_, data) => {
      return data.jobGroup == EJobGroup.DEFAULT
        ? '默认'
        : data.jobGroup == EJobGroup.SYSTEM
        ? '系统'
        : '报表';
    },
  },
  {
    field: 'invokeTarget',
    label: '调用目标字符串：',
  },
  {
    field: 'jobMessage',
    label: '日志信息：',
  },
  {
    field: 'status',
    label: '执行状态：',
    render: (_, data) => {
      return data.status == 1 ? '成功' : '失败';
    },
  },
  {
    field: 'startTime',
    label: '执行时间：',
  },
];
