import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Address } from '@/components/Address';
import { Apis } from '@/gen/Apis';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message, Modal } from 'antd';
import { useNavigate } from 'umi';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const navigate = useNavigate();

  return (
    <BetaSchemaForm<ApiTypes.Company.Companies.Store>
      {...MyModalFormProps.props}
      title={`添加机构`}
      wrapperCol={{ span: 24 }}
      width="600px"
      trigger={<MyButtons.Create title={`添加机构`} />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) =>
        Apis.Company.Companies.Store({
          ...values,
          merchant_type: 'PropertyManagement',
        })
          .then((response) => {
            const companyId = response?.data?.id;
            props.reload?.();
            message.success(props.title + '成功');

            // 询问用户是否立即配置
            Modal.confirm({
              title: '配置提示',
              content: '是否立即配置该机构？',
              okText: '是',
              cancelText: '否',
              onOk: () => {
                // 跳转到配置页面
                navigate(`/company/list/show/${companyId}`);
              },
            });

            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'name',
          title: '机构名称',
          colProps: { span: 14 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'short_name',
          title: '机构简称',
          colProps: { span: 10 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'business_license_number',
          title: '营业执照号',
          colProps: { span: 24 },
          tooltip: '限制18位',
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            maxLength: 18,
          },
        },

        {
          key: 'contact_name',
          title: '联系人姓名',
          colProps: { span: 6 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'contact_phone',
          title: '联系人手机',
          colProps: { span: 8 },
          valueType: 'number',
          fieldProps: {
            maxLength: 11,
          },
          formItemProps: { ...rulesHelper.phone },
        },
        {
          key: 'contact_email',
          title: '联系人邮箱',
          valueType: 'text',
          colProps: { span: 10 },
          fieldProps: {
            placeholder: '139@mail.com',
            type: 'email',
          },
          formItemProps: {
            rules: [
              {
                pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
                message: '请输入正确的邮箱，如 139@email.com 格式输入',
              },
            ],
          },
        },
        Address.Cascader({
          key: 'casacader',
          title: '所在地区',
          colProps: { span: 14 },
          keys: ['province', 'city', 'area', 'street'],
          required: true,
        }),
        {
          key: 'address',
          title: '详细地址',
          colProps: { span: 10 },
        },
        MyFormItems.UploadImages({
          key: 'business_license_image',
          title: '营业执照',
          max: 1,
        }),
      ]}
    />
  );
}
