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 Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Asset.AssetUnits.Update>
      {...MyModalFormProps.props}
      title={`编辑`}
      wrapperCol={{ span: 24 }}
      width="700px"
      trigger={<MyButtons.Default title={`编辑`} size="small" type="link" />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.setFieldsValue(props?.item); // 编辑赋值
        }
      }}
      onFinish={async (values) =>
        Apis.Asset.AssetUnits.Update({
          ...values,
          asset_projects_id: props?.item?.asset_projects_id,
          id: props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success('单元编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.AssetBuildings({
          key: 'asset_buildings_id',
          title: '选择楼栋',
          params: {
            asset_projects_id: props?.item?.asset_projects_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 },
        },
      ]}
    />
  );
}
