import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Address } from '@/components/Address';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AssetProjectsChargeEnum,
  AssetProjectsEntrustTypeEnum,
  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="600px"
      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: 12 },
        },

        ...(props?.item?.id
          ? [
              Selects?.Companies({
                key: 'companies_id',
                title: '所属机构',
                colProps: { span: 12 },
                formItemProps: { ...rulesHelper.number },
                fieldProps: {
                  disabled: true,
                },
              }),
            ]
          : [
              Selects?.Companies({
                key: 'companies_id',
                title: '所属机构',
                colProps: { span: 12 },
                formItemProps: { ...rulesHelper.number },
              }),
            ]),

        {
          valueType: 'dependency',
          name: ['companies_id'],
          columns: ({ companies_id }) => {
            return [
              Selects?.PropertyBrands({
                title: '选择物业品牌',
                key: 'company_property_brands_id',
                params: {
                  companies_id: companies_id,
                },
                colProps: { span: 12 },
                formItemProps: { ...rulesHelper.text },
                fieldProps: {
                  showSearch: true,
                },
              }),
            ];
          },
        },
        Address.Cascader({
          key: 'casacader',
          title: '选择地址',
          colProps: { span: 16 },
          keys: ['province', 'city', 'area', 'street'],
          required: true,
        }),
        {
          key: 'address',
          title: '详细地址',
          colProps: { span: 8 },
          formItemProps: { ...rulesHelper.text },
        },

        MyFormItems.EnumSelect({
          key: 'property_type',
          title: '项目类型',
          colProps: { span: 8 },
          valueEnum: AssetProjectsPropertyTypeEnum,
          // required: true,
        }),
        MyFormItems.EnumSelect({
          key: 'status',
          title: '项目状态',
          colProps: { span: 8 },
          valueEnum: AssetProjectsStatusEnum,
          // required: true,
        }),
        MyFormItems.EnumSelect({
          key: 'entrust_type',
          title: '委托类型',
          colProps: { span: 8 },
          valueEnum: AssetProjectsEntrustTypeEnum,
          // required: true,
        }),
        MyFormItems.EnumRadio({
          key: 'charge',
          title: '收费方式',
          colProps: { span: 8 },
          valueEnum: AssetProjectsChargeEnum,
          // required: true,
        }),
        {
          key: 'takeover_date',
          title: '接管日期',
          valueType: 'date',
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          colProps: { span: 8 },
        },
        {
          key: 'closure_date',
          title: '封园日期',
          valueType: 'date',
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          colProps: { span: 8 },
        },
        {
          title: '项目经纬度',
          dataIndex: 'location',
          colProps: { span: 24 },
          fieldProps: {
            addonAfter: (
              <MyButtons.View
                title="获取经纬度"
                onClick={() => {
                  window.open(
                    'https://api.map.baidu.com/lbsapi/getpoint/index.html',
                  );
                }}
              />
            ),
          },
        },
      ]}
    />
  );
}
