import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AssetHousesOrientationEnum,
  AssetHousesOwnershipTypeEnum,
  AssetHousesUsageEnum,
} 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.Asset.AssetHouses.Update>
      {...MyModalFormProps.props}
      title={props.title}
      wrapperCol={{ span: 24 }}
      width="800px"
      trigger={
        <MyButtons.Default title={props.title} size="small" type="primary" />
      }
      form={form}
      key={new Date().getTime()}
      onOpenChange={(open: any) => {
        if (open) {
          form.setFieldsValue(props?.item); // 编辑赋值
        }
      }}
      onFinish={async (values) =>
        Apis.Asset.AssetHouses.Update({
          ...values,
          asset_projects_id: props?.item?.asset_projects_id,
          asset_buildings_id:
            values.asset_buildings_id || props?.item?.asset_buildings_id,
          asset_units_id: values.asset_units_id || props?.item?.asset_units_id,
          id: props?.item?.id,
        })
          .then(() => {
            props.reload?.();
            message.success('房屋编辑成功');
            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?.asset_projects_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?.asset_projects_id,
                      asset_buildings_id: asset_buildings_id,
                    },
                    colProps: { span: 6 },
                    formItemProps: { ...rulesHelper.number },
                  }),
                  {
                    key: 'name',
                    title: '房号',
                    colProps: { span: 6 },
                    formItemProps: { ...rulesHelper.text },
                  },
                ],
              },
            ];
          },
        },
        MyFormItems.EnumRadio({
          key: 'ownership_type',
          title: '房屋属性',
          colProps: { span: 24 },
          valueEnum: AssetHousesOwnershipTypeEnum,
          // required: true,
        }),
        MyFormItems.EnumRadio({
          key: 'usage',
          title: '用途',
          colProps: { span: 24 },
          // valueEnum: AssetHousesUsageEnum,
          valueEnum: () => {
            let obj: any = JSON.parse(JSON.stringify(AssetHousesUsageEnum));
            delete obj.ParkingSpace;
            return obj;
          },
          required: true,
        }),
        {
          key: 'floor',
          title: '楼层',
          valueType: 'digit',
          fieldProps: {
            addonAfter: '楼',
            max: 99,
          },
          colProps: { span: 6 },
          formItemProps: { ...rulesHelper.number },
        },
        {
          key: 'built_area',
          title: '建筑面积',
          valueType: 'digit',
          fieldProps: {
            addonAfter: '㎡',
            max: 99999,
          },
          colProps: { span: 6 },
          formItemProps: { ...rulesHelper.number },
        },
        {
          key: 'inside_area',
          title: '套内面积',
          valueType: 'digit',
          fieldProps: {
            addonAfter: '㎡',
            max: 99999,
          },
          colProps: { span: 6 },
          formItemProps: { ...rulesHelper.number },
        },
        {
          key: 'chargeable_area',
          title: '计费面积',
          valueType: 'digit',
          fieldProps: {
            addonAfter: '㎡',
            max: 99999,
          },
          colProps: { span: 6 },
          formItemProps: { ...rulesHelper.number },
        },
        {
          valueType: 'group',
          columns: [
            {
              key: 'room',
              title: '房',
              valueType: 'digit',
              fieldProps: {
                addonAfter: '房',
              },
              colProps: { span: 5 },
            },
            {
              key: 'hall',
              title: '厅',
              valueType: 'digit',
              fieldProps: {
                addonAfter: '厅',
              },
              colProps: { span: 5 },
            },
            {
              key: 'bathroom',
              title: '卫',
              valueType: 'digit',
              fieldProps: {
                addonAfter: '卫',
              },
              colProps: { span: 5 },
            },
            {
              key: 'kitchen',
              title: '厨',
              valueType: 'digit',
              fieldProps: {
                addonAfter: '厨',
              },
              colProps: { span: 4 },
            },
            {
              key: 'balcony',
              title: '阳台',
              valueType: 'digit',
              fieldProps: {
                addonAfter: '阳台',
              },
              colProps: { span: 5 },
            },
          ],
        },
        MyFormItems.EnumRadio({
          key: 'orientation',
          title: '房屋朝向',
          colProps: { span: 24 },
          valueEnum: AssetHousesOrientationEnum,
          // required: true,
        }),
        // 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;
        //   },
        // }),
      ]}
    />
  );
}
