import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  HouseMetersMeterTypeEnum,
  HouseMetersUsageTypeEnum,
} 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.HouseMeters.Store>
      {...MyModalFormProps.props}
      title={`添加仪表`}
      layout="horizontal"
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 18 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      trigger={<MyButtons.Create title={`添加仪表`} />}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Meter.HouseMeters.Store({
          ...values,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        ...(props?.item?.id
          ? []
          : [
              Selects?.AssetProjects({
                key: 'asset_projects_id',
                title: '选择项目',
                colProps: { span: 24 },
                required: true,
              }),
            ]),

        MyFormItems.EnumRadio({
          key: 'meter_type',
          title: '仪表类型',
          colProps: { span: 24 },
          valueEnum: HouseMetersMeterTypeEnum,
          required: true,
        }),
        MyFormItems.EnumRadio({
          key: 'usage_type',
          title: '使用类型',
          colProps: { span: 24 },
          valueEnum: HouseMetersUsageTypeEnum,
          required: true,
        }),
        {
          key: 'name',
          title: '仪表名称',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'multiple',
          title: '倍率',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'initial_value',
          title: '初始抄表读数',
          valueType: 'number',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.number },
        },
        {
          key: 'initial_time',
          title: '初始抄表时间',
          valueType: 'date',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },

        {
          valueType: 'dependency',
          name: ['asset_projects_id'],
          columns: ({ asset_projects_id }) => {
            return [
              Selects?.ChargeStandard({
                title: '选择收费标准',
                key: 'charge_standards_id',
                params: {
                  asset_projects_id: asset_projects_id || props?.item?.id || 0,
                },
                colProps: { span: 24 },
                formItemProps: { ...rulesHelper.text },
              }),
            ];
          },
        },
        {
          key: 'remark',
          title: '备注',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
