import { useAliOssUploadCo } from "@/common/components/upload/AliOssUploadCo";
import type { IDynamicFormOptions, IDynamicFormRef } from "@/components/dynamic";
import type { PickerIdFieldProps } from "@/components/dynamic/wrappers/PickerIdField";
import type { FieldProps } from "@/components/form/Field.vue";
import type { UploaderFieldProps } from "@/components/form/UploaderField.vue";
import type { Ref } from "vue";

export function villageCommonContent (ref: Ref<IDynamicFormRef>, options: { 
  title: string,
  showTitle?: boolean,
  showContent?: boolean,
  noType?: boolean,
  noCloseExtra?: boolean,
  contentKey?: string,
} = {
  title: '文章',
  showTitle: true,
}) : IDynamicFormOptions {
  options.title = '';
  return {
    formItems: [
      {
        name: 'a',
        type: 'flat-group',
        childrenColProps: { span: 24 },
        children: [
          ...(options.showTitle ? [{
            label: `${options.title}标题`,
            name: 'name',
            type: 'text',
            defaultValue: '',
            additionalProps: {
              placeholder: `请输入${options.title}标题`,
            },
            rules: [{
              required: true,
              message: `请输入${options.title}标题`,
            }]
          }]: []),
          ...(options.showContent !== false ? [{
            label: `${options.title}内容`,
            name: options.contentKey || 'content',
            type: 'richtext',
            defaultValue: '',
            additionalProps: {
              placeholder: '请输入介绍内容正文',
              maxLength: 1000,
            },
            rules: [{
              required: true,
              message: '请输入介绍内容',
            }]
          },
          ...(options.noType !== true ? [{
            label: `${options.title}类型`,
            name: 'type',
            type: 'select-id',
            defaultValue: 1,
            additionalProps: {
              loadData: async () => [
                { text: '文章', value: 1 },
                { text: '图片', value: 2 },
                { text: '视频', value: 3 },
                { text: '相册', value: 4 },
                { text: '数字档案', value: 5 },
              ],
            } as PickerIdFieldProps,
            formProps: { showRightArrow: true },
            rules: [{
              required: true,
              message: '请选择类型',
            }]
          },
          {
            label: '来源(可选)',
            name: 'source',
            type: 'text',
            defaultValue: '',
            additionalProps: {
              placeholder: '如果是转载文章可以输入来源',
            },
          }] : []),
          ]: []),
        ],
      },
      {
        name: 'b',
        type: 'flat-group',
        childrenColProps: { span: 24 },
        children: [
            
          {
            label: `${options.title}相关图片(可选)`,
            name: 'images',
            type: 'uploader',
            defaultValue: '',
            additionalProps: {
              upload: useAliOssUploadCo('xiangyuan/common'),
              maxFileSize: 1024 * 1024 * 20, // 20MB
              maxUploadCount: 20,
            } as UploaderFieldProps,
            rules: [],
            formProps: {
              extraMessage: '建议分辨率：1920*1080以上',
            },
          },
          {
            label: `${options.title}介绍视频(可选)`,
            name: 'video',
            type: 'uploader',
            defaultValue: '',
            additionalProps: {
              upload: useAliOssUploadCo('xiangyuan/video'),
              maxFileSize: 1024 * 1024 * 1000, // 1000MB
              single: true,
              chooseType: 'video',
            } as UploaderFieldProps,
            formProps: {
              extraMessage: '您还可以上传介绍视频，建议使用MP4格式1080P分辨率',
            },
            rules: []
          },

        ],
      },
      {
        name: 'c',
        type: 'flat-group',
        label: '其他附件(可选)',
        formProps: {
          showLabel: false,
        },
        additionalProps: {
          collapsible: options.noCloseExtra !== true,
          collapsed: options.noCloseExtra !== true,
        },
        childrenColProps: { span: 24 },
        children: [
          {
            label: `${options.title}数字档案(可选)`,
            name: 'archives',
            type: 'uploader',
            defaultValue: '',
            additionalProps: {
              upload: useAliOssUploadCo('xiangyuan/archives'),
              maxFileSize: 1024 * 1024 * 200, // 200MB
              chooseType: '',
              single: true,
            } as UploaderFieldProps,
            rules: [],
            formProps: {
              extraMessage: '建议上传项目相关文件，如项目合同、项目计划等',
            },
          },
          {
            label: `${options.title}其他附件(可选)`,
            name: 'annex',
            type: 'uploader',
            defaultValue: '',
            additionalProps: {
              upload: useAliOssUploadCo('xiangyuan/annex'),
              maxFileSize: 1024 * 1024 * 1000, // 1000MB
              maxUploadCount: 20,
              chooseType: '',
            } as UploaderFieldProps,
            rules: [],
            formProps: {
              extraMessage: '建议上传项目相关文件，如项目合同、项目计划等',
            },
          },
          {
            label: `关键字`,
            name: 'keywords',
            type: 'text-tag',
            defaultValue: '',
            rules: [],
            additionalProps: {
              placeholder: '可以输入关键字',
              tagJoinType: ';',
            },
            formProps: {
              extraMessage: '用于采集系统内部处理优化关键字搜索，可以不填写',
            },
          },
        ],
      }
    ],
  }
}