import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AssetHousesOrientationEnum,
  AssetHousesOwnershipTypeEnum,
  AssetHousesStatusEnum,
  AssetHousesUsageEnum,
} from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  console.log(props?.item, 'item');
  return (
    <BetaSchemaForm<ApiTypes.Asset.AssetHouses.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="800px"
      trigger={<MyButtons.Create title={`添加${props.title}`} />}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Asset.AssetHouses.Store({
          ...values,
          asset_projects_id: props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          valueType: 'dependency',
          name: ['asset_buildings_id', 'asset_units_id'],
          columns: ({ asset_buildings_id }) => {
            return [
              {
                valueType: 'group',
                columns: [
                  Selects?.AssetBuildings({
                    key: 'asset_buildings_id',
                    title: '选择楼栋',
                    params: {
                      asset_projects_id: props?.item?.id,
                    },
                    colProps: { span: 6 },
                    formItemProps: { ...rulesHelper.number },
                    fieldProps: {
                      showSearch: true,
                      onChange: () => {
                        form.setFieldsValue({
                          asset_units_id: undefined,
                          asset_floors_id: undefined,
                        });
                      },
                    },
                  }),
                  Selects?.AssetUnits({
                    key: 'asset_units_id',
                    title: '选择单元',
                    params: {
                      asset_projects_id: props?.item?.id,
                      asset_buildings_id: asset_buildings_id,
                    },
                    colProps: { span: 6 },
                    formItemProps: { ...rulesHelper.number },
                  }),
                  {
                    key: 'name',
                    title: '房号',
                    colProps: { span: 6 },
                    formItemProps: { ...rulesHelper.text },
                  },
                  {
                    key: 'floor',
                    title: '楼层',
                    valueType: 'digit',
                    fieldProps: {
                      addonAfter: '楼',
                      max: 99,
                    },
                    colProps: { span: 6 },
                    formItemProps: { ...rulesHelper.number },
                  },
                ],
              },
            ];
          },
        },

        MyFormItems.EnumRadio({
          key: 'usage',
          title: '用途',
          colProps: { span: 18 },
          valueEnum: AssetHousesUsageEnum,
          required: true,
        }),
        MyFormItems.EnumSelect({
          key: 'ownership_type',
          title: '产权性质',
          colProps: { span: 6 },
          valueEnum: AssetHousesOwnershipTypeEnum,
          // required: true,
        }),
        // {
        //   key: 'ownership_term',
        //   title: '产权年限',
        //   fieldProps: {
        //     addonAfter: '年',
        //   },
        //   colProps: { span: 8 },
        // },

        {
          key: 'built_area',
          title: '建筑面积',
          valueType: 'digit',
          fieldProps: {
            addonAfter: '㎡',
            max: 99999,
          },
          colProps: { span: 6 },
        },
        {
          key: 'inside_area',
          title: '套内面积',
          valueType: 'digit',
          fieldProps: {
            addonAfter: '㎡',
            max: 99999,
          },
          colProps: { span: 6 },
        },
        {
          key: 'chargeable_area',
          title: '计费面积',
          valueType: 'digit',
          fieldProps: {
            addonAfter: '㎡',
            max: 99999,
          },
          colProps: { span: 6 },
        },
        MyFormItems.EnumRadio({
          key: 'status',
          title: '房屋状态',
          colProps: { span: 6 },
          // valueEnum: AssetHousesStatusEnum,
          // required: true,
          valueEnum: () => {
            let obj: any = JSON.parse(JSON.stringify(AssetHousesStatusEnum));
            delete obj.SelfOccupied;
            delete obj.Rented;
            delete obj.Vacant;
            return obj;
          },
        }),
        // {
        //   key: 'full_name',
        //   title: '房屋全称',
        //   colProps: { span: 12 },
        // },
        MyFormItems.EnumRadio({
          key: 'orientation',
          title: '房屋状态',
          colProps: { span: 6 },
          valueEnum: AssetHousesOrientationEnum,
          // required: true,
        }),
        {
          valueType: 'group',
          columns: [
            {
              key: 'room',
              title: '房',
              valueType: 'digit',
              fieldProps: {
                addonAfter: '房',
              },
              colProps: { span: 4 },
            },
            {
              key: 'hall',
              title: '厅',
              valueType: 'digit',
              fieldProps: {
                addonAfter: '厅',
              },
              colProps: { span: 4 },
            },
            {
              key: 'bathroom',
              title: '卫',
              valueType: 'digit',
              fieldProps: {
                addonAfter: '卫',
              },
              colProps: { span: 4 },
            },
            {
              key: 'kitchen',
              title: '厨',
              valueType: 'digit',
              fieldProps: {
                addonAfter: '厨',
              },
              colProps: { span: 4 },
            },
            {
              key: 'balcony',
              title: '阳台',
              valueType: 'digit',
              fieldProps: {
                addonAfter: '阳台',
              },
              colProps: { span: 4 },
            },
          ],
        },
      ]}
    />
  );
}
