import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { HouseMeterReadingsOperationTypeEnum } from '@/gen/Enums';
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.HouseMeterReadings.Store>
      {...MyModalFormProps.props}
      title={`添加抄表数据`}
      layout="horizontal"
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 18 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      trigger={<MyButtons.Default title={`抄表`} type="primary" />}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.resetFields(); // 清空表单数据
          form.setFieldsValue({
            ...props.item,
            latest_value:
              props.item?.latest_value === '0.00'
                ? props.item?.initial_value
                : props.item?.latest_value,
            latest_time: props.item?.latest_time || props.item?.initial_time,
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Meter.HouseMeterReadings.Store({
          ...values,
          operation_type:
            HouseMeterReadingsOperationTypeEnum.NormalReading.value,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'latest_value',
          title: '上次抄表读数',
          colProps: { span: 24 },
          fieldProps: { disabled: true },
        },
        {
          key: 'latest_time',
          title: '上次抄表时间',
          valueType: 'date',
          colProps: { span: 24 },
          fieldProps: { disabled: true },
        },

        {
          key: 'current_reading',
          title: '本次抄表读数',
          valueType: 'number',
          colProps: { span: 24 },
          formItemProps: {
            ...rulesHelper.number,
            rules: [
              ...rulesHelper.number.rules,
              {
                validator: (_, value) => {
                  const latestValue = form.getFieldValue('latest_value');
                  if (
                    value &&
                    latestValue &&
                    Number(value) <= Number(latestValue)
                  ) {
                    return Promise.reject(
                      new Error('本次读数必须大于上次抄表读数'),
                    );
                  }
                  return Promise.resolve();
                },
              },
            ],
          },
        },
        {
          key: 'reading_time',
          title: '本次抄表时间',
          valueType: 'date',
          colProps: { span: 24 },
          formItemProps: {
            ...rulesHelper.text,
            rules: [
              ...rulesHelper.text.rules,
              {
                validator: (_, value) => {
                  const latestTime = form.getFieldValue('latest_time');
                  if (value && latestTime) {
                    const currentTime = new Date(value);
                    const lastTime = new Date(latestTime);
                    if (currentTime <= lastTime) {
                      return Promise.reject(
                        new Error('本次抄表时间必须大于上次抄表时间'),
                      );
                    }
                  }
                  return Promise.resolve();
                },
              },
            ],
          },
        },
        Selects?.Employees({
          key: 'company_employees_id',
          title: '抄表人',
          params: {
            companies_id: props.item?.companies_id,
          },
          colProps: { span: 24 },
          required: true,
          fieldProps: {
            showSearch: true,
            fieldNames: {
              label: 'label',
              value: 'value',
            },
          },
        }),

        {
          key: 'remark',
          title: '备注',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
