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

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

  return (
    <BetaSchemaForm<ApiTypes.Survey.QuestionCategories.Update>
      {...MyModalFormProps.props}
      form={form}
      title={`编辑${props.title}`}
      wrapperCol={{ span: 24 }}
      key={new Date().getTime()}
      width="500px"
      trigger={<MyButtons.Edit title="编辑" />}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Survey.QuestionCategories.Update({
          ...values,
          is_enabled: values?.is_enabled ? true : false,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        { key: 'name', title: '名称', formItemProps: { ...rulesHelper.text } },
        {
          key: 'description',
          title: '描述',
          valueType: 'textarea',
        },
        {
          key: 'is_enabled',
          title: '是否启用',
          valueType: 'switch',
        },
        // MyFormItems.ColorPicker(),
      ]}
    />
  );
}
