import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
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 Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const actionRef = useRef<any>();

  return (
    <BetaSchemaForm<ApiTypes.Common.ConvenienceServices.Update>
      {...MyModalFormProps.props}
      title={`编辑便民服务`}
      trigger={<MyButtons.Edit />}
      wrapperCol={{ span: 24 }}
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values: any) =>
        Apis.Common.ConvenienceServices.Update({
          ...values,
          id: props.item?.id ?? 0,
          asset_projects_id: props.item?.asset_projects_id ?? 0,
        })
          .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: 24 },
          fieldProps: {
            addonBefore: '主服务名称',
          },
          formItemProps: { ...rulesHelper.text },
        },
        {
          valueType: 'formList',
          dataIndex: 'content',
          formItemProps: {
            ...rulesHelper.array,
            style: {
              borderRadius: '6px',
              marginBottom: 0,
            },
          },
          initialValue: [
            {
              name: null,
              phone: null,
            },
          ],
          fieldProps: {
            copyIconProps: false,
            creatorButtonProps: {
              creatorButtonText: '添加主服务',
              style: {
                marginTop: '8px',
              },
            },
          },
          columns: [
            {
              valueType: 'group',
              colProps: { span: 24 },
              columns: [
                {
                  key: 'name',
                  colProps: { span: 12 },
                  fieldProps: {
                    addonBefore: '子服务名称',
                  },
                  formItemProps: { ...rulesHelper.text },
                },
                {
                  key: 'phone',
                  colProps: { span: 12 },
                  fieldProps: {
                    addonBefore: '联系电话',
                  },
                  formItemProps: { ...rulesHelper.text },
                },
              ],
            },
          ],
        },
      ]}
    />
  );
}
