import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { HouseRegistersTypeEnum } 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.Archive.HouseRegisters.ChangeOccupant>
      {...MyModalFormProps.props}
      title={`修改电话`}
      wrapperCol={{ span: 24 }}
      width="500px"
      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: any) => {
        const requestData: any = {
          house_occupants_id: props?.item?.id,
          type: HouseRegistersTypeEnum.UpdatePhone.value,
          house_relation: props?.item?.house_relation,
          update_info: {
            phone: values.phone,
          },
        };
        return Apis.Archive.HouseRegisters.ChangeOccupant(requestData)
          .then(() => {
            props.reload?.();
            message.success('电话修改成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        {
          title: '手机号',
          dataIndex: 'phone',
          colProps: { span: 24 },
          fieldProps: {
            maxLength: 11,
          },
          formItemProps: { ...rulesHelper.phone },
        },
      ]}
    />
  );
}
