<template>
  <!-- 传承人基础表单 -->
  <Form 
    :formModel="formModel"
    :formOptions="formOptions"
    :load="loadData"
    :model="InheritorInfo"
    pushExamine
  />
</template>

<script setup lang="ts">
import { ref, type Ref } from 'vue';
import Form from './form.vue';
import InheritorContent, { InheritorInfo } from '@/api/collect/InheritorContent';
import CommonContent from '@/api/CommonContent';
import type { IDynamicFormOptions } from '@/components/dynamic';

const formModel = ref(new InheritorInfo()) as Ref<InheritorInfo>;
const formOptions = ref<IDynamicFormOptions>({
  formItems: [
    {
      type: 'flat-group', label: '传承人基础档案', name: 'ichInfo',
      childrenColProps: { span: 24 },
      children: [
        { 
          label: '姓名', name: 'title', type: 'text',
          additionalProps: { placeholder: '请输入标题' },
        },
        { 
          label: '性别', name: 'gender', type: 'select',
          additionalProps: {
            placeholder: '请选择性别',
            columns: [[
              { text: '女', value: 0 },
              { text: '男', value: 1 },
              { text: '未知', value: 2 }
            ]],
          },
        },
        { 
          label: '传承人等级', name: 'level', type: 'select-id',
          additionalProps: {
            placeholder: '请选择传承人等级',
            loadData: async () => (await CommonContent.getCategoryList(2)).map(p => ({ text: p.title, value: p.id, raw: p }))
          },
        },
        { 
          label: '传承人批次', name: 'batch', type: 'select-id',
          additionalProps: {
            placeholder: '请选择传承人批次',
            loadData: async () => (await CommonContent.getCategoryList(289)).map(p => ({ text: p.title, value: p.id, raw: p }))
          },
        },
        //{ label: '别称', name: 'alsoName', type: 'text', additionalProps: { placeholder: '请输入别称' } },
        //{ label: '时代', name: 'age', type: 'text', additionalProps: { placeholder: '请输入时代' } },
        { label: '联系电话', name: 'mobile', type: 'text', additionalProps: { placeholder: '请输入联系电话' } },
        { label: '籍贯', name: 'birthplace', type: 'text', additionalProps: { placeholder: '请输入籍贯' } },
        { label: '民族', name: 'nation', type: 'text', additionalProps: { placeholder: '请输入民族' } },
        { label: '出生日期', name: 'dateBirth', type: 'text', additionalProps: { placeholder: '请选择出生日期' } },
        //{ label: '逝世日期', name: 'deathBirth', type: 'text', additionalProps: { placeholder: '请选择逝世日期' } },
        { label: '保护单位', name: 'unit', type: 'text', additionalProps: { placeholder: '请输入单位' } },
        { label: '传承人简介', name: 'intro', type: 'richtext', additionalProps: { placeholder: '请输入简介' } },
        //{ label: '详情', name: 'content', type: 'richtext', additionalProps: { placeholder: '请输入详情' } },
        { label: '奖项/成就', name: 'prize', type: 'richtext', additionalProps: { placeholder: '请输入奖项-成就' } },
        { label: '传承人谱系', name: 'pedigree', type: 'richtext', additionalProps: { placeholder: '请输入传承谱系' } },
        { 
          label: '传承人照片', name: 'images', type: 'uploader',
          //hidden: { callback: (_, model) => (model as IchInfo).type !== 4 },
          formProps: {
            extraMessage: '建议分辨率：1920*1080以上，请上传传承人证件照、工作照、生活照、实践活动照',
          },
          additionalProps: {
            placeholder: '请上传图片',
            maxCount: 100,
            name: 'file',
            accept: 'image/*',
            // beforeUpload: useBeforeUploadImageChecker(),
            // upload: useAliOssUploadCo('inheritor/images'),
          },
        },
        /* { 
          type: 'array-object', label: '传承人照片(建议分辨率：1920*1080以上，请上传传承人证件照、工作照、生活照、实践活动照)', 
          name: 'typicalImages',
          formProps: {
            center: false,
          },
          additionalProps: {
            direction: 'horizontal'
          },
          newChildrenObject: (arrayNow) => ({
            desc: `代表性图片${arrayNow.length+1}`,
            url: '',
            from: '',
            mobile: '',
          }),
          children: [
            { type: 'text', label: '照片来源', name: 'from', additionalProps: { placeholder: '请输入来源' } },
            //{ type: 'text', label: '联系方式', name: 'mobile', additionalProps: { placeholder: '请输入联系方式' } },
            { type: 'text', label: '照片说明', name: 'desc', additionalProps: { placeholder: '请输入说明' } },
            { 
              label: '照片', name: 'url', type: 'uploader',
              additionalProps: {
                name: 'file',
                placeholder: '请上传图片',
                upload: useImageSimpleUploadCo(),
              } as UploaderFormItemProps,
            },
          ]
        }, */
        { 
          label: '传承人相关视频', name: 'video', type: 'uploader',
          //hidden: { callback: (_, model) => (model as InheritorInfo).type !== 3 },
          additionalProps: {
            placeholder: '请上传视频',
            name: 'file',
            accept: 'video/*',
            // beforeUpload: useBeforeUploadVideoChecker(),
            // upload: useAliOssUploadCo('inheritor/video'),
          },  
        },
      ]
    },
  ],
  formRules: {
    title: [{ required: true, message: '请输入标题' }],
    region: [{ required: true, message: '请选 择地区' }],
    type: [{ required: true, message: '请选择类型' }],
    image: [{ required: true, message: '请上传图片' }],
    gender: [{ required: true, message: '请选择性别' }],
    level: [{ required: true, message: '请选择级别' }],
    batch: [{ required: true, message: '请输入批次' }],
    ichType: [{ required: true, message: '请选择非遗类型' }],
  }
});

async function loadData(id: number|undefined) {
  formModel.value = await InheritorContent.getInheritorInfo(id);
}
</script>