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

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Meter.HouseMeterTasks.Store>
      {...MyModalFormProps.props}
      title={`创建${props.title}`}
      width="480px"
      layout="horizontal"
      labelCol={{ span: 8 }}
      wrapperCol={{ span: 16 }}
      labelAlign="left"
      trigger={<MyButtons.Create title={`${props.title}`} />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values: any) =>
        Apis.Meter.HouseMeterTasks.Store({
          ...values,
          data: [props?.item?.id],
          asset_projects_id: props?.item?.asset_projects_id,
          meter_type: props?.item?.meter_type,
          generation_method: 'Meter',
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '账单任务创建成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'month',
          title: '选择生成月份',
          valueType: 'date',
          colProps: { span: 24 },
          fieldProps: {
            picker: 'month',
            format: 'YYYY-MM',
            valueFormat: 'YYYY-MM',
            style: {
              width: '100%',
            },
            onChange: (e: any, dateString: string) => {
              form.setFieldsValue({
                start_date: rulesHelper.getMonthStartDate(dateString),
                end_date: rulesHelper.getMonthEndDate(dateString),
              });
            },
          },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'start_date',
          title: '计费开始日期',
          valueType: 'date',
          colProps: { span: 24 },
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'end_date',
          title: '计费结束日期',
          valueType: 'date',
          colProps: { span: 24 },
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          formItemProps: { ...rulesHelper.text },
        },
      ]}
    />
  );
}
