<template>
  <div style="overflow-y: auto; background-color: #ffffff;">
    <CusTab :tabs="[
          {name: 'list布局', value: '1'},
          {name: 'card布局', value: '2'},
          {name: '自定义上传', value: '3'},
      ]" v-model:param="tab"/>
    <CusForm ref="ref_form">
      <template v-if="tab === '1'">
        <h1>upload（默认layout="list",type="all"）</h1>
        <el-button @click="onSubmit">校验</el-button>
        <div style="width: 100%;">{{form.fileList1}}</div>
        <CusFormColumn
            :span="12"
            link="upload"
            label="默认有值"
            v-model:param="form.fileList1"
            :limit="3"
            nameKey="fName"
            urlKey="fUrl"
        />
        <CusFormColumn
            :span="12"
            link="upload"
            label="预览的时候，不显示按钮，view='true'"
            v-model:param="form.fileList1"
            :view="true"
            nameKey="fName"
            urlKey="fUrl"
        />
        <div style="width: 100%;">为了避免不做校验，而删除了外部数据，默认不可删除，通过delRule传入校验方法，默认的话也需要传入:delRule="() => true"</div>
        <div style="width: 100%;">{{form.fileList5}}</div>
        <CusFormColumn
            :span="12"
            link="upload"
            label="只能删除source=席位"
            v-model:param="form.fileList5"
            :delRule="(file) => file.source === '席位'"
            nameKey="fName"
            urlKey="fUrl"
        />
        <CusFormColumn
            :span="12"
            link="upload"
            label="默认删除"
            v-model:param="form.fileList5"
            :delRule="(file) => true"
            nameKey="fName"
            urlKey="fUrl"
        />
        <div style="width: 100%;">{{form.fileList2}}</div>
        <CusFormColumn
            required
            :span="12"
            link="upload"
            label="校验"
            v-model:param="form.fileList2"
            nameKey="fName"
            urlKey="fUrl"
        />
        <div style="width: 100%;">{{form.fileList3}}</div>
        <CusFormColumn
            :span="12"
            link="upload"
            label="type=img,纯图片"
            type="img"
            v-model:param="form.fileList3"
            nameKey="fName"
            urlKey="fUrl"
        />
        <div style="width: 100%;">{{form.fileList4}}</div>
        <CusFormColumn
            :span="12"
            link="upload"
            label="type=img,纯文件"
            type="file"
            v-model:param="form.fileList4"
            nameKey="fName"
            urlKey="fUrl"
        />
      </template>
      <template v-else-if="tab === '2'">
        <h1 style="width: 100%;">upload（layout="card"）</h1>
        <el-button @click="onSubmit">校验</el-button>
        <div style="width: 100%;margin-bottom: 40px">{{form.fileList1}}</div>
        <CusFormColumn
            :span="12"
            link="upload"
            label="默认有值"
            layout="card"
            v-model:param="form.fileList1"
            :limit="3"
            nameKey="fName"
            urlKey="fUrl"
        />
        <CusFormColumn
            :span="12"
            link="upload"
            label="limitNoUpload，达到limit限制后不显示上传图标"
            layout="card"
            v-model:param="form.fileList1"
            :limit="3"
            nameKey="fName"
            urlKey="fUrl"
            :delRule="(file) => true"
            limitNoUpload
        />
        <CusFormColumn
            :span="12"
            link="upload"
            label="预览的时候，不显示按钮，view='true'"
            layout="card"
            v-model:param="form.fileList1"
            :view="true"
            nameKey="fName"
            urlKey="fUrl"
        />
        <div style="width: 100%;">为了避免不做校验，而删除了外部数据，默认不可删除，通过delRule传入校验方法，默认的话也需要传入:delRule="() => true"</div>
        <div style="width: 100%;">{{form.fileList5}}</div>
        <CusFormColumn
            :span="12"
            link="upload"
            label="只能删除source=席位"
            layout="card"
            v-model:param="form.fileList5"
            :delRule="(file) => file.source === '席位'"
            nameKey="fName"
            urlKey="fUrl"
        />
        <CusFormColumn
            :span="12"
            link="upload"
            label="默认删除"
            layout="card"
            v-model:param="form.fileList5"
            :delRule="(file) => true"
            nameKey="fName"
            urlKey="fUrl"
        />
        <div style="width: 100%;">{{form.fileList2}}</div>
        <CusFormColumn
            required
            :span="12"
            link="upload"
            label="校验"
            layout="card"
            v-model:param="form.fileList2"
            nameKey="fName"
            urlKey="fUrl"
        />
        <div style="width: 100%;">{{form.fileList3}}</div>
        <CusFormColumn
            :span="12"
            link="upload"
            label="type=img,纯图片"
            type="img"
            layout="card"
            v-model:param="form.fileList3"
            nameKey="fName"
            urlKey="fUrl"
        />
        <div style="width: 100%;">{{form.fileList4}}</div>
        <CusFormColumn
            :span="12"
            link="upload"
            label="type=img,纯文件"
            type="file"
            layout="card"
            v-model:param="form.fileList4"
            nameKey="fName"
            urlKey="fUrl"
        />
      </template>
      <template v-else>
        <h1>upload（自定义上传acceptType、acceptMax、acceptFunc(return {})）</h1>
        <div style="width: 100%;">{{form.cus}}</div>
        <CusFormColumn
            :span="12"
            link="upload"
            label="自定义上传"
            v-model:param="form.cus"
            acceptType=".xls,.png"
            :acceptMax="10"
            :acceptFunc="cusUpload"
            nameKey="fName"
            urlKey="fUrl"
        />
      </template>
    </CusForm>
  </div>
</template>

<script lang="ts">
import {
  defineComponent,
  computed,
  onMounted,
  ref,
  reactive,
  watch,
  getCurrentInstance,
  ComponentInternalInstance,
  toRefs,
  nextTick
} from 'vue'
import {useStore} from 'vuex'
import {useRouter, useRoute} from 'vue-router'

export default defineComponent({
  name: '',
  components: {},
  setup() {
    const store = useStore();
    const router = useRouter();
    const route = useRoute();
    const that = (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties
    const state = reactive({
      tab: '1',
      form: {
        fileList1: [
            { "fUrl": "http://8.140.240.182:18085/profile/upload/2023/10/23/6b7bf3b4-592a-4e2f-ba92-11923671ce3b.doc", "fName": "迁移案例.doc"},
          { "fUrl": "http://8.140.240.182:18085/profile/images/2023/10/23/297caaa4-b7ab-4f05-aaea-08ea70d4729e.png", "fName": "Snipaste_2023-09-22_19-30-15.png"},
          { "fUrl": "http://8.140.240.182:18085/profile/images/2023/10/25/032d3c49-d7b0-41af-b737-001a5ee3d51a.mp4", "fName": "login-video.mp4"}
        ],
        fileList2: [],
        fileList3: [],
        fileList4: [],
        fileList5: [
          { "fUrl": "http://8.140.240.182:18085/profile/images/2023/10/23/468ff222-5de1-4850-a09f-0a79530487d8.png", "fName": "head-com-avatar.png", source: '席位' },
          { "fUrl": "http://8.140.240.182:18085/profile/upload/2023/10/23/6b7bf3b4-592a-4e2f-ba92-11923671ce3b.doc", "fName": "迁移案例.doc", source: '一标三识' }
        ],
        cus: []
      }
    })
    const ref_form = ref()
    const onSubmit = () => {
      ref_form.value.submit().then(() => {
        console.log('校验通过')
      }).catch((e: any) => {
        console.log('校验失败')
        console.log(e)
      })
    }
    const cusUpload = async (options) => {
      //  假装有接口
      const formData = new FormData();
      formData.append("file", options.file);
      let result = {}
      await that.$api.uploadFile(formData).then(res => {
        if (res?.code === 200) {
          result = {
            fUrl: res.url,
            fName: res.fileName
          }
        }
      }).catch(() => {
        return null
      })
      return result
    }
    return {
      ...toRefs(state),
      onSubmit,
      ref_form,
      cusUpload
    }
  },
})
</script>

<style scoped lang="scss">
</style>
