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 } from 'antd';

export default function Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Company.Companies.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Default title="编辑" type="primary" size="small" />}
      wrapperCol={{ span: 24 }}
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            ...props.item,
            casacader: [
              props.item?.province_id || '',
              props.item?.city_id || '',
              props.item?.area_id || '',
              props.item?.street_id || '',
            ],
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Company.Companies.Update({ ...values, id: props.item?.id ?? 0 })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            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: '营业执照号',
          tooltip: '限制18位',
          colProps: { span: 24 },
          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,
        }),
      ]}
    />
  );
}
