import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
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={`编辑`}
      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 },
          formItemProps: { ...rulesHelper.number },
          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: 'owner_name',
          title: '业主姓名',
          colProps: { span: 24 },
        },
        {
          key: 'owner_phone',
          title: '业主手机号',
          colProps: { span: 24 },
        },
        Selects?.AssetFullName({
          key: 'asset_houses_id',
          params: {
            asset_projects_id: props?.item?.asset_projects_id,
          },
          title: '关联房屋',
          request: async (params) => {
            const res = await Apis.Asset.AssetHouses.SelectFullName({
              keywords: params?.keyWords,
              asset_projects_id: props?.item?.asset_projects_id,
              ...params,
            });
            let list = res?.data || [];
            // 编辑模式下，确保当前选中的房屋在列表中，以正确显示 label
            const currentHouseId = props.item?.asset_houses_id;
            if (
              currentHouseId &&
              !list.some((item: any) => item.value === currentHouseId)
            ) {
              try {
                const showRes = await Apis.Asset.AssetHouses.Show({
                  id: currentHouseId,
                });
                if (showRes?.data) {
                  list = [
                    {
                      label: showRes.data.full_name,
                      value: showRes.data.id,
                    },
                    ...list,
                  ];
                }
              } catch {
                /* ignore */
              }
            }
            return list;
          },
        }),
        {
          key: 'car_number',
          title: '车牌号',
          colProps: { span: 24 },
        },
        {
          key: 'remark',
          title: '备注',
          valueType: 'textarea',
        },
      ]}
    />
  );
}
