import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { ConvenienceServicesTypeEnum } from '@/gen/Enums';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';
import { useRef } from 'react';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const actionRef = useRef<any>();

  return (
    <BetaSchemaForm<ApiTypes.Common.ConvenienceServices.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.Common.ConvenienceServices.Store(values)
          .then(() => {
            props.reload?.();
            message.success('添加便民服务成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'type',
          title: '类型',
          colProps: { span: 24 },
          valueEnum: ConvenienceServicesTypeEnum,
          required: true,
        }),
        {
          key: 'name',
          title: '名称',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        Selects?.AssetProjects({
          key: 'asset_projects_id',
          title: '项目',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          valueType: 'formList',
          dataIndex: 'content',
          title: '服务内容',
          formItemProps: { ...rulesHelper.array },
          initialValue: [
            {
              name: null,
              phone: null,
            },
          ],
          fieldProps: {
            actionRef: actionRef,
            copyIconProps: false,
            // deleteIconProps: false,
          },
          columns: [
            {
              valueType: 'group',
              colProps: { span: 24 },
              columns: [
                {
                  key: 'name',
                  colProps: { span: 12 },
                  // title: '服务名称',
                  fieldProps: {
                    addonBefore: '联系对象',
                  },
                  width: '100%',
                  formItemProps: { ...rulesHelper.text },
                },
                {
                  key: 'phone',
                  colProps: { span: 12 },
                  // title: '阶梯单价',
                  fieldProps: {
                    addonBefore: '联系电话',
                  },
                  formItemProps: { ...rulesHelper.text },
                },
              ],
            },
          ],
        },
      ]}
    />
  );
}
