import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';

import { Selects } from '@/components/Select';
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 Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Archive.HouseRegisters.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      width="900px"
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            ...props.item,
            asset_houses_id: props.item?.asset_house?.id,
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Archive.HouseRegisters.Update({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.AssetProjects({
          title: '选择项目',
          key: 'asset_projects_id',
          colProps: { span: 8 },
          formItemProps: { ...rulesHelper.text },
        }),

        {
          valueType: 'dependency',
          name: ['asset_projects_id'],
          columns: ({ asset_projects_id }) => [
            Selects?.AssetHouses({
              title: '选择房屋',
              key: 'asset_houses_id',
              params: { asset_projects_id: asset_projects_id },
              formItemProps: { ...rulesHelper.text },
              colProps: { span: 8 },
            }),
          ],
        },
        MyFormItems.EnumRadio({
          key: 'type',
          title: '类型',
          colProps: { span: 8 },
          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,
        }),
      ]}
    />
  );
}
