<template>
  <baseTitle title="标签名称"></baseTitle>
  <el-form :model="formData" ref="tagForm" label-width="auto" :rules="rules">
    <el-form-item label="标签名称" prop="tagName">
      <el-input v-model="formData.tagName" placeholder="请输入标签名称" style="width: 300px" v-if="showInput"/>
      <el-select v-model="formData.tagName" placeholder="请选择标签" remote clearable filterable style="width: 300px" v-else>
        <el-option
            v-for="item in tagsOption"
            :key="item.label"
            :label="item.value"
            :value="item.label"
        />
      </el-select>
    </el-form-item>
  </el-form>
  <baseTitle title="其他文件"></baseTitle>
  <el-card style="width: 100%;margin: 15px 0">
    <file-upload @getFile="getFiles"/>
    <fvTable style="width: 100%;max-height: 160px;height: 160px"  height="160"  v-if="showTable" :tableConfig="tableConfig"
             :data="fileList" :isSettingCol="false" :pagination="false">
      <template #empty>
        <el-empty :image-size="55" description="暂无数据" style="padding: 0"/>
      </template>
    </fvTable>
  </el-card>
  <div class="oper-page-btn">
    <el-button color="#DED0B2" @click="handleSubmit(tagForm)">提交</el-button>
  </div>
</template>

<script setup lang="jsx">
const props = defineProps({
  tagsOption: {
    type: Array,
    default: []
  }, formData: {
    type: Array,
    default: []
  }, showInput: {
    type: Boolean,
    default: true
  },
})
const rules = reactive({
  tagName: [{required: true, message: '请输入标签名称', trigger: ['blur', 'change']}],
})
const tagForm = ref()
const showTable = ref(true)
const emits = defineEmits(['getFile'])
const fileList = ref([])
const tableConfig = reactive({
  columns: [
    {
      prop: 'index',
      type: 'index',
      label: '序号',
      align: 'center',
      width: '80',
    },
    {
      prop: 'originalFileName',
      label: '文件名',
      align: 'center',
    },
    {
      prop: 'tag',
      label: '标签',
      align: 'center'
    },
    {
      prop: 'tag',
      label: '文件大小',
      align: 'center',
      currentRender: ({row, index}) => (parseInt(row.size / 1024) + 'KB')
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      currentRender: ({row, index}) => {
        return (
            <div>
              <el-button type="primary" link onClick={() => handleDownload(row)}>下载</el-button>
            </div>
        )
      }
    }
  ]
})
const compositeParam = (item) => {
  return {
    fileId: item.id,
    size: item.size,
    originalFileName: item.originalFilename,
    fileType: item.fileType,
    url: item.url,
    newFile: true,
    tag: formData.value.tagName,
  }
}
const getFiles = (val) => {
  showTable.value = false
  let fileObj = compositeParam(val)
  // emit("getFile", fileObj)
  fileList.value.push(fileObj)
  nextTick(() => {
    showTable.value = true
  })
}
watch(() => fileList.value, (val) => {
  fileList.value = val
})
defineExpose({
  fileList
})
</script>

<style scoped>

</style>
