import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  AssetUnitsBuildingStructureEnum,
  AssetUnitsBuildingTypeEnum,
} 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.Asset.AssetUnits.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="700px"
      trigger={
        <MyButtons.Create
          title={`${props.title}`}
          size={props?.item?.size || 'small'}
        />
      }
      form={form}
      key={new Date().getTime()}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Asset.AssetUnits.Store({
          ...values,
          asset_projects_id: props?.item?.id,
          asset_buildings_id: props?.item?.asset_buildings_id,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'name',
          title: '单元名称',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'alias_name',
          title: '单元别名',
          colProps: { span: 12 },
        },
        MyFormItems.EnumRadio({
          key: 'building_structure',
          title: '建筑结构',
          colProps: { span: 12 },
          valueEnum: AssetUnitsBuildingStructureEnum,
          // required: true,
        }),
        MyFormItems.EnumRadio({
          key: 'building_type',
          title: '建筑类型',
          colProps: { span: 12 },
          valueEnum: AssetUnitsBuildingTypeEnum,
          // required: true,
        }),
        {
          key: 'elevator_count',
          title: '电梯数量',
          colProps: { span: 6 },
        },
        {
          key: 'units_per_building',
          title: '单元总户数',
          colProps: { span: 6 },
        },
        {
          key: 'highest_floor',
          title: '最高楼层',
          colProps: { span: 6 },
        },
      ]}
    />
  );
}
