import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Address } from '@/components/Address';
import MyModalsMapLeaflet from '@/components/ModalsMapLeaflet';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AssetProjectsChargeEnum,
  AssetProjectsEntrustTypeEnum,
  AssetProjectsOwnershipTypeEnum,
  AssetProjectsPropertyTypeEnum,
  AssetProjectsStatusEnum,
} 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();

  return (
    <BetaSchemaForm<ApiTypes.Asset.AssetProjects.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      wrapperCol={{ span: 24 }}
      width="800px"
      trigger={<MyButtons.Create title={`${props.title}`} />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
          form.setFieldsValue({
            companies_id: props?.item?.id,
          });
        }
      }}
      onFinish={async (values: any) =>
        Apis.Asset.AssetProjects.Store({
          ...values,
          longitude: values?.location ? values.location.split(',')[0] : '',
          latitude: values?.location ? values.location.split(',')[1] : '',
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'name',
          title: '项目名称',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'alias_name',
          title: '项目别名',
          colProps: { span: 6 },
        },
        Selects?.PropertyBrands({
          title: '选择物业品牌',
          key: 'company_property_brands_id',
          colProps: { span: 6 },
          fieldProps: {
            showSearch: true,
          },
        }),
        Address.Cascader({
          key: 'casacader',
          title: '选择地址',
          colProps: { span: 12 },
          keys: ['province', 'city', 'area', 'street'],
          required: true,
        }),
        {
          key: 'address',
          title: '详细地址',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },

        MyFormItems.EnumSelect({
          key: 'property_type',
          title: '项目类型',
          colProps: { span: 6 },
          valueEnum: AssetProjectsPropertyTypeEnum,
          // required: true,
        }),
        MyFormItems.EnumSelect({
          key: 'status',
          title: '项目状态',
          colProps: { span: 6 },
          //valueEnum: AssetProjectsStatusEnum,
          valueEnum: () => {
            let obj: any = JSON.parse(JSON.stringify(AssetProjectsStatusEnum));
            delete obj.Exited;
            return obj;
          },
          // required: true,
        }),
        MyFormItems.EnumSelect({
          key: 'entrust_type',
          title: '委托类型',
          colProps: { span: 6 },
          valueEnum: AssetProjectsEntrustTypeEnum,
          // required: true,
        }),
        MyFormItems.EnumRadio({
          key: 'charge',
          title: '收费方式',
          colProps: { span: 6 },
          valueEnum: AssetProjectsChargeEnum,
          // required: true,
        }),
        {
          key: 'takeover_date',
          title: '接管日期',
          valueType: 'date',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },
        {
          key: 'closure_date',
          title: '封园日期',
          valueType: 'date',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },

        {
          key: 'plot_ratio',
          title: '容积率',
          valueType: 'number',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },
        {
          key: 'project_phase_number',
          title: '项目总期数',
          valueType: 'digit',
          fieldProps: { style: { width: '100%' }, max: 99 },
          colProps: { span: 6 },
        },

        {
          key: 'total_households',
          title: '规划总户数',
          valueType: 'number',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },
        {
          key: 'takeover_households',
          title: '接管总户数',
          valueType: 'number',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },
        {
          key: 'total_parking_spaces',
          title: '规划车位数',
          valueType: 'number',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },

        {
          key: 'takeover_parking_spaces',
          title: '接管车位数',
          valueType: 'number',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },
        {
          key: 'contract_building_area',
          title: '合同建筑面积(㎡)',
          valueType: 'number',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },
        {
          key: 'land_area',
          title: '占地面积(㎡)',
          valueType: 'number',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },

        {
          key: 'takeover_building_area',
          title: '接管建筑面积(㎡)',
          valueType: 'number',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },
        {
          key: 'property_area',
          title: '物业面积(㎡)',
          valueType: 'number',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },
        {
          key: 'green_area',
          title: '绿化面积(㎡)',
          valueType: 'number',
          fieldProps: { style: { width: '100%' } },
          colProps: { span: 6 },
        },
        MyFormItems.EnumRadio({
          key: 'ownership_type',
          title: '业权类型',
          colProps: { span: 6 },
          valueEnum: AssetProjectsOwnershipTypeEnum,
        }),

        {
          key: 'is_owners_committee',
          title: '成立业委会',
          valueType: 'switch',
          fieldProps: {
            checkedChildren: '是',
            unCheckedChildren: '否',
          },
          colProps: { span: 6 },
        },
        {
          name: ['is_owners_committee'],
          valueType: 'dependency',
          columns: ({ is_owners_committee }: any) => {
            return is_owners_committee
              ? [
                  {
                    valueType: 'group',
                    columns: [
                      {
                        key: 'owners_committee_number',
                        title: '第几届业委会',
                        valueType: 'number',
                        fieldProps: { style: { width: '100%' } },
                        colProps: { span: 6 },
                      },
                      {
                        key: 'owners_committee_start_date',
                        title: '业委会开始日期',
                        valueType: 'date',
                        fieldProps: { style: { width: '100%' } },
                        colProps: { span: 6 },
                      },
                      {
                        key: 'owners_committee_end_date',
                        title: '业委会结束日期',
                        valueType: 'date',
                        fieldProps: { style: { width: '100%' } },
                        colProps: { span: 6 },
                      },
                    ],
                  },
                ]
              : [];
          },
        },
        {
          valueType: 'group',
          columns: [
            {
              key: 'longitude',
              title: '经度',
              dataIndex: 'longitude',
              colProps: { span: 6 },
            },
            {
              key: 'latitude',
              title: '纬度',
              dataIndex: 'latitude',
              colProps: { span: 6 },
            },
            {
              title: ' ',
              colProps: { span: 6 },
              renderFormItem: () => {
                return (
                  <MyModalsMapLeaflet
                    onChange={(e) => {
                      form.setFieldsValue({
                        latitude: e?.lat?.toFixed(6),
                        longitude: e?.lng?.toFixed(6),
                      });
                      console.log(e, '3333');
                    }}
                  />
                );
              },
            },
          ],
        },
      ]}
    />
  );
}
