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 QuestionsCreate(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();

  return (
    <BetaSchemaForm<ApiTypes.Survey.Questions.Store>
      {...MyModalFormProps.props}
      form={form}
      title={`添加${props.title}`}
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      labelAlign="left"
      key={new Date().getTime()}
      width="600px"
      trigger={<MyButtons.Create title={`添加${props.title}`} />}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values) => {
        console.log(values);
        return Apis.Survey.Questions.Store({
          ...values,
          is_enabled: true,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        Selects?.QuestionCategories({
          colProps: { span: 24 },
          required: true,
        }),
        {
          key: 'title',
          title: '题目',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.EnumRadio({
          key: 'type',
          title: '类型',
          valueEnum: QuestionsTypeEnum,
          colProps: { span: 24 },
          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: 24 },
        },
        {
          key: 'required',
          title: '是否必答',
          valueType: 'switch',
          colProps: { span: 24 },
          fieldProps: {
            style: { width: '100%' },
            checkedChildren: '是',
            unCheckedChildren: '否',
          },
        },
        // {
        //   key: 'is_enabled',
        //   title: '是否启用',
        //   valueType: 'switch',
        //   colProps: { span: 24 },
        // },
        // MyFormItems.ColorPicker(),
      ]}
    />
  );
}
