<template>
  <el-dialog
    :title="title"
    visible
    :before-close="closeDialog"
    :close-on-click-modal="false"
    width="800px"
  >
    <div v-for="item in fileList" :key="item.id" class="file-list">
      <span>{{ getFileName(item.url) }}</span>
      <div v-if="disabled" @click="deleteFile(item.id)">{{ $t('common.delete') }}</div>
      <div @click="downloadFile(item.url)">{{ $t('printLadingBill.download') }}</div>
    </div>
    <div style="margin-top: 30px;display:flex;">
      <!--      <el-button type="primary" @click="closeDialog">{{ $t('common.newfile') }}</el-button> -->
      <el-upload v-if="disabled" action="#" accept="png" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUploadimg1">
        <el-button size="small">
          {{ $t('common.newfile') }}
          <i class="el-icon-upload el-icon--right" />
        </el-button>
      </el-upload>
      <el-button style="margin-left: 60px;" @click="closeDialog">{{ $t('release.close') }}</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { warehousePictureList, warehousePictureCreate, warehousePictureDelete } from '@/api/order'
import { upNamefile } from '@/api/user'

export default {
  props: {
    orderId: [String, Number],
    orderNo: [String, Number],
    disabled: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      show: false,
      detail: null,
      fileList: []
    }
  },
  computed: {
    title() {
      let t = this.$t('prepayDeal.customsData')
      if (this.orderNo) {
        t += '-' + this.orderNo
      }
      return t
    }
  },
  created() {
    this.show = true
    this.loadData()
  },
  methods: {

    loadData() {
      warehousePictureList({ bizId: this.orderId, type: 6 }).then((res) => {
        this.fileList = res.data
      })
    },
    getFileName(url) {
    	if (!url) return '/'
    	var file = url.split('/')
    	return file[file.length - 1]
    },
    closeDialog() {
      this.show = false
      this.$emit('close')
    },
    // 覆盖默认的上传行为
    requestUpload() {
    },
    // 上传预处理
    beforeUploadimg1(file) {
      if (this.fileList.length >= 10) {
        this.$message.error(this.$t('文件上传个数超出十条'))
        return
      }
      console.log(file)
      var type = file.name.split('.')

      if (['xls', 'doc', 'ppt', 'txt', 'pdf', 'jpg', 'png', 'jpeg', 'docx', 'xlsx'].indexOf(type[type.length - 1]) === -1) {
        this.$message.error(this.$t('setting.fileType'))
      } else {
      // 上传
        const formData = new FormData()
        formData.append('file', file)
        formData.append('path', this.uuid())
        upNamefile(formData).then(response => {
          if (response.data) {
            this.createImage(response.data)
          }
        })
      }
    },
    createImage(url) {
      warehousePictureCreate({ bizId: this.orderId, type: 6, url: url }).then(res => {
        this.$message.success(this.$t('上传成功'))
        this.loadData()
      })
    },
    downloadFile(url) {
      window.open(url, '_self')
    },
    deleteFile(id) {
      this.$confirm(this.$t('common.deleteDeclaration')).then(function() {
        return warehousePictureDelete(id)
      }).then(() => {
        this.loadData()
        this.$message({
          message: this.$t('common.delete') + this.$t('common.success'),
          type: 'success'
        })
      }).catch(() => {})
    },
    uuid() {
      var s = []
      var hexDigits = '0123456789abcdef'
      for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
      }
      s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
      s[8] = s[13] = s[18] = s[23] = '-'

      var uuid = s.join('')
      return uuid
    }
  }
}
</script>
<style lang="scss" scoped>
.title {
  font-size: 16px;
  margin: 20px 0;
  display: flex;
  align-items: center;
  &:before {
    content: '';
    width: 5px;
    height: 15px;
    background: #666;
    margin-right: 10px;
  }
}
  .file-list{
  	display: flex;
  	align-items: center;
  	color: #297CE7;
    margin-left: 30px;
    font-size:16px;
    margin-bottom:10px;
    div{
      cursor: pointer;
      margin-left: 30px;
    }
  }
</style>
