import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { HouseWorkOrdersLevelEnum } from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';
import dayjs from 'dayjs';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Patrol.PatrolTasks.Store>
      {...MyModalFormProps.props}
      title={`添加临时任务`}
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      trigger={<MyButtons.Create title={`临时任务`} />}
      onFinish={async (values) =>
        Apis.Patrol.PatrolTasks.Store({
          ...values,
        })
          .then(() => {
            props.reload?.();
            message.success('添加成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.AssetProjects({
          title: '选择项目',
          key: 'asset_projects_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          valueType: 'dependency',
          name: ['asset_projects_id'],
          columns: ({ asset_projects_id }) => {
            return asset_projects_id
              ? [
                  Selects?.PatrolRoutesSelect({
                    title: '选择路线',
                    key: 'patrol_routes_id',
                    params: {
                      asset_projects_id: asset_projects_id,
                    },
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  }),
                ]
              : [
                  Selects?.PatrolRoutesSelect({
                    title: '选择路线',
                    key: 'patrol_routes_id',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                    fieldProps: { disabled: true },
                  }),
                ];
          },
        },
        {
          title: '开始时间',
          key: 'start_time',
          valueType: 'dateTime',
          colProps: { span: 24 },
          fieldProps: {
            style: { width: '100%' },
            placeholder: '请设置开始时间',
            disabledDate: (current: any) => {
              // 只能选择当天及之后的时间
              return current && current < dayjs().startOf('day');
            },
          },
          formItemProps: {
            ...rulesHelper.text,
          },
        },
        {
          title: '结束时间',
          key: 'end_time',
          valueType: 'dateTime',
          colProps: { span: 24 },
          fieldProps: {
            style: { width: '100%' },
            placeholder: '请设置结束日期',
            disabledDate: (current: any) => {
              //选择时间必须在开始时间之后，且为同一天
              return (
                current && current < dayjs(form.getFieldValue('start_time'))
              );
            },
          },
          formItemProps: {
            ...rulesHelper.text,
          },
        },
        MyFormItems.EnumRadio({
          key: 'level',
          title: '任务等级',
          colProps: { span: 24 },
          valueEnum: HouseWorkOrdersLevelEnum,
          required: true,
        }),
      ]}
    />
  );
}
