<template>
  <el-form :label-position="labelAlign">
    <el-form-item :label="title" v-if="fileListShow === 'READ' || fileListShow === 'EDIT'" :label-position="labelAlign"  :style="{marginTop: '10px',marginLeft: tag!=='需求上报'?'15px':'0'}">
      <file-upload @getFile="getOtherFile" v-if="fileListShow === 'EDIT'"/>
<!--      :style="{width:isOpenPrint?'610px': '100%'}"  table-layout="auto"  id="printTable"-->
      <fvTable style="width:100%;max-height: 160px;" v-if="processViewer"  height="160" :tableConfig="tableConfig"
              :data="_value" :isSettingCol="false" :pagination="false">
        <template #empty>
          <el-empty :image-size="55" description="暂无数据" style="padding: 0"/>
        </template>
      </fvTable>
    </el-form-item>
  </el-form>
  <file-preview ref="filePreviewRef" :fullscreen="fullscreen" v-if="filePreviewShow" :fileName="filePreviewParam.fileName" :fileUrl="filePreviewParam.fileUrl"
                :fileType="filePreviewParam.fileType"/>
</template>

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

const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  tag: {
    type: String,
    default: ''
  },
  fileNameTableWidth: {
    type: String,
    default: '400'
  },
  fileListShow: {
    type: String,
    default: 'READ'
  },
  value: {
    type: Array,
    default: []
  },
  processViewer: {
    type: Boolean,
    default: false
  },
  labelAlign: {
    type: String,
    default: 'right'
  },
  //弹窗是否铺满全屏
  fullscreen: {
    type: Boolean,
    default: false
  },
  //是否开始打印
  isOpenPrint: {
    type: Boolean,
    default: false
  },
})
const emit = defineEmits(['update:value'])
const tableConfig = reactive({
  columns: [
    {
      prop: 'index',
      type: 'index',
      label: '序号',
      align: 'center',
      width: 85,
    },
    {
      prop: 'originalFileName',
      label: '文件名',
      align: 'center',
      width: props.fileNameTableWidth,
      showOverflowTooltip: false,
      currentRender: ({row, index}) => (<div style="color: #2a99ff;cursor: pointer;" onClick={()=>clickToPreview(row)}>{row.originalFileName}</div>)
    },
    {
      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'}
                                              onDelete={() => handleDelete(row)}/>
                    : ''
              }
            </div>
        )
      }
    }
  ]
})
const filePreviewParam = ref({
  fileUrl: '',
  fileName: '',
  fileType: 'pdf'
})
const filePreviewShow = ref(false)
const _value = computed({
  get() {
    return props.value;
  },
  set(val) {
    emit("update:value", val);
  }
})
const clickToPreview=(row)=>{
  filePreviewShow.value = false
  filePreviewParam.value = {
    fileUrl: row.url,
    fileName: row.originalFileName,
    fileType: row.fileType
  }
  nextTick(()=>{
    filePreviewShow.value = true
  })
}
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 lang="scss">
:deep(.el-table--fit ) {
  height: 160px !important;
}
@media print {
  //#printTable{
  //  width: 400px!important;
  //}

}
</style>
