import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseOccupantsCardTypeEnum,
  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="600px"
      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.UpdateInfo.value,
          house_relation: props?.item?.house_relation,
          update_info: {
            name: values.name || props?.item,
            card_type: values.card_type,
            id_card: values.id_card,
            card_front_image: values.card_front_image,
            card_back_image: values.card_back_image,
          },
        };
        return Apis.Archive.HouseRegisters.ChangeOccupant(requestData)
          .then(() => {
            props.reload?.();
            message.success('信息编辑成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        {
          valueType: 'group',
          columns: [
            {
              title: '名称',
              dataIndex: 'name',
              colProps: { span: 6 },
              formItemProps: { ...rulesHelper.text },
            },
            MyFormItems.EnumSelect({
              key: 'card_type',
              title: '证件类型',
              colProps: { span: 10 },
              valueEnum: HouseOccupantsCardTypeEnum,
              required: true,
            }),
            {
              title: '证件号码',
              dataIndex: 'id_card',
              colProps: { span: 8 },
              fieldProps: {
                maxLength: 18,
              },
              formItemProps: { ...rulesHelper.text },
            },
            {
              valueType: 'group',
              columns: [
                MyFormItems.UploadImages({
                  key: 'card_front_image',
                  title: '证件正面',
                  // uploadType: 'file',
                  required: true,
                  max: 1,
                  colProps: { span: 6 },
                }),
                MyFormItems.UploadImages({
                  key: 'card_back_image',
                  title: '证件反面',
                  // uploadType: 'file',
                  required: true,
                  max: 1,
                  colProps: { span: 6 },
                }),
              ],
            },
          ],
        },
      ]}
    />
  );
}
