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

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Banner.Banners.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(); // 清空表单数据
          form.setFieldsValue({
            banner_spaces_id: props.item?.banner_spaces_id,
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Banner.Banners.Store(values)
          .then(() => {
            props.reload?.();
            message.success('添加广告内容成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.GetBannerSpace({
          key: 'banner_spaces_id',
          title: '选择广告位',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            showSearch: true,
            optionFilterProp: 'label',
          },
        }),
        {
          key: 'name',
          title: '内容名称',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          valueType: 'group',
          columns: [
            MyFormItems.EnumRadio({
              key: 'type',
              title: '展示类型',
              colProps: { span: 12 },
              valueEnum: BannersTypeEnum,
              required: true,
            }),
            {
              name: ['type'],
              valueType: 'dependency',
              columns: ({ type }: any) => {
                return type === 'Text'
                  ? [
                      {
                        title: '文本内容',
                        key: ['source', 0, 'text'],
                        colProps: { span: 12 },
                        formItemProps: { ...rulesHelper.text },
                      },
                    ]
                  : type === 'Image'
                  ? [
                      MyFormItems.UploadImages({
                        key: 'source',
                        title: '广告图片',
                        uploadType: 'file',
                        max: 1,
                        colProps: { span: 12 },
                        formItemProps: { ...rulesHelper.text },
                      }),
                    ]
                  : type === 'Video'
                  ? [
                      MyFormItems.UploadImages({
                        key: 'source',
                        title: '广告视频',
                        uploadType: 'file',
                        max: 1,
                        colProps: { span: 12 },
                        formItemProps: { ...rulesHelper.text },
                      }),
                    ]
                  : [];
              },
            },
          ],
        },
        MyFormItems.EnumRadio({
          key: 'redirect_type',
          title: '跳转类型',
          colProps: { span: 24 },
          valueEnum: BannersRedirectTypeEnum,
          required: false,
        }),
        {
          key: 'redirect_path',
          title: '跳转链接',
          colProps: { span: 24 },
          fieldProps: {
            placeholder: '请输入跳转链接',
          },
        },
        {
          name: ['redirect_type'],
          valueType: 'dependency',
          columns: ({ redirect_type }: any) => {
            return redirect_type === 'AnotherMiniProgram'
              ? [
                  {
                    title: '小程序AppID',
                    dataIndex: 'app_id',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  },
                ]
              : [];
          },
        },

        {
          key: 'start_time',
          title: '开始时间',
          valueType: 'dateTime',
          colProps: { span: 8 },
        },
        {
          key: 'end_time',
          title: '结束时间',
          valueType: 'dateTime',
          colProps: { span: 8 },
        },
        {
          key: 'sort',
          title: '排序',
          valueType: 'digit',
          colProps: { span: 8 },
          fieldProps: {
            placeholder: '数值越大越靠前',
            min: 0,
            style: { width: '100%' },
          },
          initialValue: 0,
        },
      ]}
    />
  );
}
