import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
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}`}
          // disabled={!props?.item?.asset_buildings_id}
        />
      }
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Asset.AssetUnits.Store({
          ...values,
          asset_projects_id: props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.AssetBuildings({
          key: 'asset_buildings_id',
          title: '选择楼栋',
          params:{
            asset_projects_id: props?.item?.id,
          },
          colProps: { span:24 },
          formItemProps: { ...rulesHelper.number },
        }),
        {
          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 },
        },
      ]}
    />
  );
}
