import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseOccupantsCardTypeEnum,
  HouseOccupantsHouseRelationEnum,
  HouseOccupantsRelationWithOwnerEnum,
  HouseOccupantsResidentialRelationEnum,
  HouseRegistersTypeEnum,
} from '@/gen/Enums';
import { BetaSchemaForm, ProCard } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function AddOccupant(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Archive.HouseRegisters.Store>
      {...MyModalFormProps.props}
      title={`${props.title}`}
      wrapperCol={{ span: 24 }}
      width="1000px"
      trigger={
        <MyButtons.Default title={props.title} size="middle" type="primary" />
      }
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values: any) =>
        Apis.Archive.HouseRegisters.Store({
          ...values,
          asset_houses_id: props?.item?.id,
          type: HouseRegistersTypeEnum.AddOccupant.value,
          customer_info: values.customer_info?.map((res: any) => {
            return {
              ...res,
              house_relation: HouseOccupantsHouseRelationEnum.NonOwner.value,
              relation_with_owner:
                values?.residential_relation ===
                HouseOccupantsResidentialRelationEnum.PrimaryTenant.value
                  ? values?.relation_with_owner
                  : values?.residential_relation,
            };
          }),
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          valueType: 'formList',
          dataIndex: 'customer_info',
          colProps: { span: 24 },
          initialValue: [''],
          fieldProps: {
            copyIconProps: false,
            creatorButtonProps: {
              creatorButtonText: '添加客户',
            },
            itemRender: (
              { listDom, action }: any,
              { index }: { index: number },
            ) => {
              return (
                <ProCard
                  bordered
                  style={{ marginBlockEnd: 0 }}
                  title={`客户${index + 1}`}
                  extra={action}
                  bodyStyle={{ paddingBlockEnd: 0 }}
                >
                  {listDom}
                </ProCard>
              );
            },
          },
          columns: [
            {
              valueType: 'group',
              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.Tenant;
                      //   return obj;
                      // },
                      required: true,
                    }),
                    {
                      name: ['residential_relation'],
                      valueType: 'dependency',
                      columns: ({ residential_relation }: any) => {
                        return residential_relation ===
                          HouseOccupantsResidentialRelationEnum.Resident.value
                          ? [
                              MyFormItems.EnumRadio({
                                key: 'relation_with_owner',
                                title: '与产权人关系',
                                // title:
                                //   residential_relation === 'Resident'
                                //     ? '与产权人关系'
                                //     : '与主租人关系',
                                valueEnum: HouseOccupantsRelationWithOwnerEnum,
                                colProps: { span: 16 },
                                required: true,
                              }),
                            ]
                          : [];
                      },
                    },
                  ],
                },
                {
                  title: '姓名',
                  dataIndex: 'name',
                  colProps: { span: 6 },
                  formItemProps: { ...rulesHelper.text },
                },
                {
                  title: '手机号',
                  dataIndex: 'phone',
                  colProps: { span: 6 },
                  fieldProps: {
                    maxLength: 11,
                  },
                  formItemProps: { ...rulesHelper.phone },
                },

                {
                  name: ['residential_relation'],
                  valueType: 'dependency',
                  columns: ({ residential_relation }: any) => {
                    return residential_relation ===
                      HouseOccupantsResidentialRelationEnum.PrimaryTenant.value
                      ? [
                          MyFormItems.EnumSelect({
                            key: 'card_type',
                            title: '证件类型',
                            colProps: { span: 6 },
                            valueEnum: HouseOccupantsCardTypeEnum,
                            required: true,
                          }),
                          {
                            title: '证件号码',
                            dataIndex: 'id_card',
                            colProps: { span: 6 },
                            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 },
                          }),
                          {
                            title: '是否入住',
                            dataIndex: 'is_live_in',
                            colProps: { span: 6 },
                            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: 6 },
                                      fieldProps: {
                                        style: { width: '100%' },
                                      },
                                      formItemProps: { ...rulesHelper.text },
                                    },
                                  ]
                                : [];
                            },
                          },
                        ]
                      : [
                          MyFormItems.EnumSelect({
                            key: 'card_type',
                            title: '证件类型',
                            colProps: { span: 6 },
                            valueEnum: HouseOccupantsCardTypeEnum,
                          }),
                          {
                            title: '证件号码',
                            dataIndex: 'id_card',
                            colProps: { span: 6 },
                            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: 'is_live_in',
                            colProps: { span: 4 },
                            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: 6 },
                                      fieldProps: {
                                        style: { width: '100%' },
                                      },
                                      formItemProps: { ...rulesHelper.text },
                                    },
                                  ]
                                : [];
                            },
                          },
                          {
                            title: '备注',
                            dataIndex: 'remark',
                            colProps: { span: 24 },
                          },
                        ];
                  },
                },
              ],
            },
          ],
        },
      ]}
    />
  );
}
