<template>
  <el-form :label-position="labelAlign">
    <el-form-item :label="title?'其他文件':''" v-if="fileListShow === 'READ' || fileListShow === 'EDIT'" :label-position="labelAlign" :label-width="title?95:''">
      <file-upload @getFile="getOtherFile" v-if="fileListShow === 'EDIT'"/>
      <fvTable style="width: 100%;max-height: 300px;" v-if="processViewer" :tableConfig="tableConfig"
              :data="_value" :isSettingCol="false" :pagination="false">
        <template #empty>
          <el-empty :image-size="90" description="暂无数据" style="padding: 0"/>
        </template>
      </fvTable>
    </el-form-item>
  </el-form>
</template>

<script setup lang="jsx">
import {downloadFile, deleteFile} from "@/api/project-demand";
import {ElNotification} from "element-plus";

const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  tag: {
    type: String,
    default: ''
  },
  fileListShow: {
    type: String,
    default: 'READ'
  },
  value: {
    type: Array,
    default: []
  },
  processViewer: {
    type: Boolean,
    default: false
  },
  labelAlign: {
    type: String,
    default: 'right'
  }
})
const emit = defineEmits(['update:value'])
const tableConfig = reactive({
  columns: [
    {
      prop: 'index',
      type: 'index',
      label: '序号',
      align: 'center',
      width: 85,
      fixed:true
    },
    {
      prop: 'originalFileName',
      label: '文件名',
      align: 'center',
    },
    {
      prop: 'tag',
      label: '标签',
      align: 'center'
    },
    {
      prop: 'size',
      label: '文件大小',
      align: 'center',
      width: 150,
      currentRender: ({row, index}) => (parseInt(row.size / 1024) + 'KB')
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        let btn = []
        btn.push({label: '下载', func: () => handleDownload(row), type: 'primary'})
        // if (row.newFile) {
        //   btn.push({label: '删除', func: () => handleDelete(row), type: 'primary'})
        // }
        return (
            <div style={{width: '100%'}}>
              {
                btn.map(item => (
                    <el-button
                        type={item.type}
                        onClick={() => item.func()}
                        link>
                      {item.label}
                    </el-button>
                ))
              }
              {
                row.newFile ? <popover-delete name={row.originalFileName} type={'文件'} btnType={'danger'}
                                              perm={['mosr:requirement:del']}
                                              onDelete={() => handleDelete(row)}/>
                    : ''
              }
            </div>
        )
      }
    }
  ]
})

const _value = computed({
  get() {
    return props.value;
  },
  set(val) {
    emit("update:value", val);
  }
})

const getOtherFile = (val) => {
  props.processViewer = false
  let fileObj = compositeParam(val)
  _value.value.push(fileObj)
  nextTick(() => {
    props.processViewer = true
  })
}


const compositeParam = (item, type) => {
  return {
    fileId: item.id,
    size: item.size,
    originalFileName: item.originalFilename,
    fileType: item.fileType,
    url: item.url,
    newFile: true,
    tag: props.tag
  }
}
const handleDownload = (row) => {
  downloadFile(row.fileId).then(res => {
    const blob = new Blob([res])
    let a = document.createElement('a')
    a.href = URL.createObjectURL(blob)
    a.download = row.originalFileName
    a.click()
  })
}
const handleDelete = (row) => {
  deleteFile(row.fileId).then(res => {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      _value.value.splice(_value.value.findIndex((item) => item.fileId === row.fileId), 1);
    }
  });
}

watch(() => props.processViewer, (newVal) => {
  props.processViewer = newVal
}, {deep: true})
</script>

<style scoped>
:deep(.el-table--fit ) {
  height: 300px !important;
}
</style>
