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

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Archive.HouseRegisters.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="900px"
      form={form}
      trigger={
        <MyButtons.Default title={props.title} size="small" type="link" />
      }
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Archive.HouseRegisters.Store({
          ...values,
          asset_houses_id: props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'type',
          title: '类型',
          colProps: { span: 24 },
          valueEnum: HouseRegistersTypeEnum,
          required: true,
        }),
        {
          valueType: 'formList',
          dataIndex: 'customer_info',
          colProps: { span: 24 },
          fieldProps: {
            copyIconProps: false,
            creatorButtonProps: {
              creatorButtonText: '添加客户',
            },
            itemRender: (
              { listDom, action }: any,
              { index }: { index: number },
            ) => {
              return (
                <ProCard
                  bordered
                  style={{ marginBlockEnd: 8 }}
                  title={`客户${index + 1}`}
                  extra={action}
                  bodyStyle={{ paddingBlockEnd: 0 }}
                >
                  {listDom}
                </ProCard>
              );
            },
          },
          columns: [
            {
              valueType: 'group',
              columns: [
                {
                  title: '姓名',
                  dataIndex: 'name',
                  colProps: { span: 8 },
                  formItemProps: { ...rulesHelper.text },
                },
                {
                  title: '手机号',
                  dataIndex: 'phone',
                  colProps: { span: 8 },
                  fieldProps: {
                    maxLength: 11,
                  },
                  formItemProps: { ...rulesHelper.text },
                },
                MyFormItems.EnumSelect({
                  key: 'house_relation',
                  title: '房客关系',
                  colProps: { span: 8 },
                  valueEnum: HouseOccupantsHouseRelationEnum,
                  required: true,
                }),

                {
                  title: '客户地址',
                  dataIndex: 'address',
                  colProps: { span: 8 },
                  formItemProps: { ...rulesHelper.text },
                },
                {
                  title: '解除时间',
                  dataIndex: 'unbound_time',
                  colProps: { span: 8 },
                  valueType: 'date',
                  fieldProps: {
                    style: { width: '100%' },
                  },
                  formItemProps: { ...rulesHelper.text },
                },
                MyFormItems.EnumSelect({
                  key: 'relation_with_owner',
                  title: '与产权人关系',
                  colProps: { span: 8 },
                  valueEnum: HouseOccupantsRelationWithOwnerEnum,
                  required: true,
                }),
                MyFormItems.EnumSelect({
                  key: 'card_type',
                  title: '证件类型',
                  colProps: { span: 8 },
                  valueEnum: HouseOccupantsCardTypeEnum,
                  required: true,
                }),
                {
                  title: '证件号码',
                  dataIndex: 'id_card',
                  colProps: { span: 8 },
                  fieldProps: {
                    maxLength: 18,
                  },
                  formItemProps: { ...rulesHelper.text },
                },
                {
                  title: '是否在居住中',
                  dataIndex: 'is_live_in',
                  colProps: { span: 8 },
                  valueType: 'switch',
                },
                {
                  valueType: 'group',
                  columns: [
                    MyFormItems.UploadImages({
                      key: 'card_front_image',
                      title: '身份证正面',
                      max: 1,
                      colProps: { span: 8 },
                      formItemProps: { ...rulesHelper.text },
                    }),
                    MyFormItems.UploadImages({
                      key: 'card_back_image',
                      title: '身份证反面',
                      max: 1,
                      colProps: { span: 8 },
                      formItemProps: { ...rulesHelper.text },
                    }),
                  ],
                },
                MyFormItems.UploadImages({
                  key: 'ownership_info',
                  title: '产权信息',
                  max: 100,
                  colProps: { span: 24 },
                  formItemProps: { ...rulesHelper.array },
                }),
              ],
            },
          ],
        },

        MyFormItems.EnumRadio({
          key: 'customer_type',
          title: '客户类型',
          colProps: { span: 24 },
          valueEnum: HouseRegistersCustomerTypeEnum,
        }),
        MyFormItems.EnumRadio({
          key: 'usage_plan',
          title: '使用计划',
          colProps: { span: 24 },
          valueEnum: HouseRegistersUsagePlanEnum,
        }),
        // MyFormItems.EnumRadio({
        //   key: 'status',
        //   title: '状态',
        //   colProps: { span: 24 },
        //   valueEnum: HouseRegistersStatusEnum,
        // }),
        MyFormItems.EnumRadio({
          key: 'house_status',
          title: '房屋状态',
          colProps: { span: 24 },
          valueEnum: HouseRegistersHouseStatusEnum,
        }),
      ]}
    />
  );
}
