import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseOccupantsCardTypeEnum,
  HouseOccupantsRelationWithOwnerEnum,
  HouseOccupantsResidentialRelationEnum,
} 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.HouseOccupants.Update>
      {...MyModalFormProps.props}
      title={`编辑客户信息`}
      wrapperCol={{ span: 24 }}
      width="920px"
      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.Archive.HouseOccupants.Update({
          ...values,
          id: props?.item?.id,
          phone: props?.item?.phone,
        })
          .then(() => {
            props.reload?.();
            message.success('客户信息编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          valueType: 'group',
          columns: [
            MyFormItems.EnumRadio({
              key: 'residential_relation',
              title: '关联身份',
              colProps: { span: 8 },
              // valueEnum: HouseOccupantsResidentialRelationEnum,
              valueEnum: () => {
                let obj: any = JSON.parse(
                  JSON.stringify(HouseOccupantsResidentialRelationEnum),
                );
                delete obj.PropertyOwner;
                return obj;
              },
              required: true,
              fieldProps: {
                disabled:
                  props?.item?.residential_relation ===
                  HouseOccupantsResidentialRelationEnum.PropertyOwner.value,
              },
            }),
            {
              name: ['residential_relation'],
              valueType: 'dependency',
              columns: ({ residential_relation }: any) => {
                return residential_relation ===
                  HouseOccupantsResidentialRelationEnum.Resident.value
                  ? [
                      MyFormItems.EnumRadio({
                        key: 'relation_with_owner',
                        title: '与业主关系',
                        // valueEnum: HouseOccupantsRelationWithOwnerEnum,
                        valueEnum: () => {
                          let obj: any = JSON.parse(
                            JSON.stringify(HouseOccupantsRelationWithOwnerEnum),
                          );
                          delete obj.Self;
                          delete obj.PrimaryTenant;
                          delete obj.Tenant;
                          return obj;
                        },
                        colProps: { span: 16 },
                        required: true,
                      }),
                    ]
                  : [];
              },
            },
          ],
        },
        {
          title: '姓名',
          dataIndex: 'name',
          colProps: { span: 8 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          name: ['residential_relation'],
          valueType: 'dependency',
          columns: ({ residential_relation }: any) => {
            return residential_relation ===
              HouseOccupantsResidentialRelationEnum.PrimaryTenant.value
              ? [
                  MyFormItems.EnumSelect({
                    key: 'card_type',
                    title: '证件类型',
                    colProps: { span: 8 },
                    valueEnum: HouseOccupantsCardTypeEnum,
                    required: true,
                  }),
                  {
                    title: '证件号码',
                    dataIndex: 'id_card',
                    colProps: { span: 8 },
                    required: true,
                    fieldProps: {
                      maxLength: 18,
                    },
                  },
                  MyFormItems.UploadImages({
                    key: 'card_front_image',
                    title: '证件正面',
                    // uploadType: 'file',
                    max: 1,
                    colProps: { span: 6 },
                    formItemProps: { ...rulesHelper.text },
                  }),
                  MyFormItems.UploadImages({
                    key: 'card_back_image',
                    title: '证件反面',
                    // uploadType: 'file',
                    max: 1,
                    colProps: { span: 6 },
                    formItemProps: { ...rulesHelper.text },
                  }),
                ]
              : [
                  MyFormItems.EnumSelect({
                    key: 'card_type',
                    title: '证件类型',
                    colProps: { span: 8 },
                    valueEnum: HouseOccupantsCardTypeEnum,
                  }),
                  {
                    title: '证件号码',
                    dataIndex: 'id_card',
                    colProps: { span: 8 },
                    fieldProps: {
                      maxLength: 18,
                    },
                  },
                  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 },
                  }),

                  {
                    title: '客户备注',
                    dataIndex: 'remark',
                    colProps: { span: 24 },
                  },
                ];
          },
        },
      ]}
    />
  );
}
