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

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

  return (
    <BetaSchemaForm<ApiTypes.Survey.Questions.Update>
      {...MyModalFormProps.props}
      form={form}
      title={`编辑${props.title}`}
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      labelAlign="left"
      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.Questions.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={[
        Selects?.QuestionCategories({
          colProps: { span: 12 },
          required: true,
        }),

        {
          key: 'title',
          title: '题目',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.EnumRadio({
          key: 'type',
          title: '类型',
          valueEnum: QuestionsTypeEnum,
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          valueType: 'dependency',
          name: ['type'],
          columns: ({ type }) => {
            return type === QuestionsTypeEnum.Fill.value
              ? [
                  {
                    key: 'max_length',
                    title: '答题字数',
                    valueType: 'digit',
                    colProps: { span: 24 },
                    fieldProps: {
                      addonAfter: '个字内',
                    },
                    formItemProps: { ...rulesHelper.number },
                  },
                ]
              : [];
          },
        },
        {
          key: 'placeholder',
          title: '提示',
          colProps: { span: 12 },
        },
        {
          key: 'required',
          title: '是否必答',
          valueType: 'switch',
          colProps: { span: 12 },
        },
        {
          key: 'is_enabled',
          title: '是否启用',
          valueType: 'switch',
          colProps: { span: 12 },
        },
        // MyFormItems.ColorPicker(),
      ]}
    />
  );
}
