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

export default function Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Asset.AssetCarPorts.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      layout="horizontal"
      labelCol={{ span: 4 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Asset.AssetCarPorts.Update({
          ...values,
          id: props.item?.id ?? 0,
          asset_projects_id: props.item?.asset_projects_id ?? 0,
          asset_parking_places_id: props.item?.asset_parking_places_id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'name',
          title: '车位名称',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 24 },
        },
        {
          key: 'built_area',
          title: '建筑面积',
          valueType: 'digit',
          colProps: { span: 24 },
          fieldProps: {
            addonAfter: 'm²',
          },
          width: '100%',
        },
        MyFormItems.EnumRadio({
          key: 'type',
          title: '车位类型',
          valueEnum: AssetCarPortsTypeEnum,
          required: true,
        }),
        MyFormItems.EnumRadio({
          key: 'property_type',
          title: '产权类型',
          valueEnum: AssetCarPortsPropertyTypeEnum,
          required: true,
        }),
        MyFormItems.EnumRadio({
          key: 'status',
          title: '状态',
          valueEnum: AssetCarPortsStatusEnum,
          required: true,
        }),
        {
          key: 'remark',
          title: '备注',
          valueType: 'textarea',
        },
      ]}
    />
  );
}
