import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseOccupantsHouseRelationEnum,
  HouseOccupantsRelationWithOwnerEnum,
} 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}
      form={form}
      title={`编辑`}
      wrapperCol={{ span: 24 }}
      width="600px"
      trigger={<MyButtons.Default title={`编辑`} size="small" type="link" />}
      onOpenChange={(open: any) => {
        if (open) {
          form.setFieldsValue(props?.item); // 编辑赋值
        }
      }}
      onFinish={async (values) =>
        Apis.Archive.HouseOccupants.Update({
          ...values,
          id: props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success('楼栋编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          valueType: 'group',
          columns: [
            MyFormItems.EnumRadio({
              key: 'house_relation',
              title: '住户类型',
              colProps: { span: 6 },
              valueEnum: () => {
                let obj: any = JSON.parse(
                  JSON.stringify(HouseOccupantsHouseRelationEnum),
                );
                delete obj.Owner;
                return obj;
              },
              required: true,
            }),
            {
              name: ['house_relation'],
              valueType: 'dependency',
              columns: ({ house_relation }: any) => {
                return house_relation === 'Resident'
                  ? [
                      MyFormItems.EnumRadio({
                        key: 'relation_with_owner',
                        title: '与产权人关系',
                        colProps: { span: 18 },
                        valueEnum: () => {
                          let obj: any = JSON.parse(
                            JSON.stringify(HouseOccupantsRelationWithOwnerEnum),
                          );
                          delete obj.Self;
                          return obj;
                        },
                        required: true,
                      }),
                    ]
                  : [
                      MyFormItems.EnumRadio({
                        key: 'relation_with_owner',
                        title: '与租客关系',
                        colProps: { span: 18 },
                        valueEnum: () => {
                          let obj: any = JSON.parse(
                            JSON.stringify(HouseOccupantsRelationWithOwnerEnum),
                          );
                          delete obj.ContactPerson;
                          return obj;
                        },
                        required: true,
                      }),
                    ];
              },
            },

            {
              title: '姓名',
              dataIndex: 'name',
              colProps: { span: 12 },
              formItemProps: { ...rulesHelper.text },
            },
            {
              title: '手机号',
              dataIndex: 'phone',
              colProps: { span: 12 },
              fieldProps: {
                maxLength: 11,
              },
              formItemProps: { ...rulesHelper.phone },
            },
            {
              title: '是否入住',
              dataIndex: 'is_live_in',
              colProps: { span: 12 },
              valueType: 'switch',
            },
            {
              name: ['is_live_in'],
              valueType: 'dependency',
              columns: ({ is_live_in }: any) => {
                return is_live_in
                  ? [
                      {
                        title: '入住日期',
                        dataIndex: 'move_in_date',
                        valueType: 'date',
                        colProps: { span: 12 },
                        fieldProps: {
                          style: { width: '100%' },
                        },
                        formItemProps: { ...rulesHelper.text },
                      },
                    ]
                  : [,];
              },
            },
          ],
        },
      ]}
    />
  );
}
