import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { AssetHousesStatusEnum } 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.AssetHouses.Update>
      {...MyModalFormProps.props}
      title={props.title}
      wrapperCol={{ span: 24 }}
      width="380px"
      trigger={
        <MyButtons.Default title={props.title} size="small" type="link" />
      }
      form={form}
      key={new Date().getTime()}
      onOpenChange={(open: any) => {
        if (open) {
          form.setFieldsValue(props?.item); // 编辑赋值
        }
      }}
      onFinish={async (values) =>
        Apis.Asset.AssetHouses.Update({
          ...values,
          id: props?.item?.id,
          asset_projects_id: props?.item?.asset_projects_id,
          asset_buildings_id: props?.item?.asset_buildings_id,
          asset_units_id: props?.item?.asset_units_id,
          name: props?.item?.name,
        })
          .then(() => {
            props.reload?.();
            message.success('房屋编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        // MyFormItems.EnumRadio({
        //   key: 'status',
        //   title: '房屋状态',
        //   colProps: { span: 6 },
        //   // valueEnum: AssetHousesStatusEnum,
        //   // required: true,
        //   valueEnum: () => {
        //     let obj: any = JSON.parse(JSON.stringify(AssetHousesStatusEnum));
        //     delete obj.SelfOccupied;
        //     delete obj.Rented;
        //     delete obj.Vacant;
        //     return obj;
        //   },
        // }),
        MyFormItems.EnumRadio({
          key: 'status',
          title: '请选择新的房屋状态',
          colProps: { span: 24 },
          // 删除当前状态
          valueEnum: () => {
            let obj: any = JSON.parse(JSON.stringify(AssetHousesStatusEnum));
            delete obj[props.item?.status];
            return obj;
          },
          required: true,
        }),
      ]}
    />
  );
}
