<template>
  <div class="img-row" :style="{width: config.itemWidth, height: config.itemHeight}">
    <!--    升级img展示组件-->
    <Media class="img-row__img-item" :type="config.mediaType" :src="realUrl"></Media>
    <!--    <img class="img-row__img-item" :src="file.url">-->
    <!--进度条仅在当前页面上传的文件中显示-->
    <template v-if="file.status === 'ready'">
      <!-- <el-progress style="flex-grow: 2" type="circle" :percentage="file.percentage" :status="file.status" :width="60"></el-progress> -->
      <div class="progress-cancel">
        ({{parseFileSize(file.loaded)}}/{{parseFileSize(file.total)}})
        <cip-button-text size="small" @click="cancelUpload(file)">取消</cip-button-text>
      </div>
      <el-progress :stroke-width="4" style="top:50%;width:80%" indeterminate :percentage="72" />
    </template>
    <template v-else>
      <div class="handle-button-wrapper">
        <template v-if="file.status!=='error'">
          <i class="el-icon-view" @click="preview"></i>
          <i class="el-icon-download" @click="download"></i>
        </template>
        <!-- <cip-table-button size="small" @click="preview" >
          <i class="el-icon-zoom-in"></i>
        </cip-table-button>
        <cip-table-button size="small" @click="download">
          <i class="el-icon-download"></i>
        </cip-table-button>
        <cip-table-button size="small" @click="remove" :loading="removeLoading" v-if="removable">
          <i class="el-icon-delete"></i>
        </cip-table-button> -->
      </div>
      <div v-if="removable" class="upload--close">
        <ElIcon @click="remove"><CircleCloseFilled /></ElIcon>
      </div>
    </template>
  </div>
</template>
<script>
import { computed, ref } from 'vue'
import { ElMessageBox, ElProgress, ElIcon } from 'element-plus'
import Media from './media'
import { Dialog as VanDialog } from 'vant'
import { store } from '@cip/request'
import { getValueByTemplate, getEquipmentType } from '@cip/utils/util'
import { CircleCloseFilled } from '@element-plus/icons-vue'
import CipButtonText from '@cip/components/cip-button-text'

export default {
  components: { ElProgress, Media, ElIcon, CircleCloseFilled, CipButtonText },
  props: {
    file: {
      type: Object,
      default: () => { return {} }
    },
    config: Object,
    removable: Boolean
  },
  setup (props, { emit }) {
    // 根据文件大小 显示 B kb mb
    const parseFileSize = (size) => {
      let unit = 'k'
      if (size > 1024) {
        size = size / 1024
        unit = 'kb'
      }
      if (size > 1024) {
        size = size / 1024
        unit = 'mb'
      }
      return `${Math.floor(size)}${unit}`
    }
    const isMobile = getEquipmentType() === 'mobile'
    const realUrl = computed(() => {
      const url = props.file.url
      if (props.config.imgTemplate) {
        return props.config.imgTemplate(url)
      } else {
        return url
      }
    })
    const removeLoading = ref(false)
    const fileValue = computed(() => {
      return props.file
    })
    const cancelUpload = (file) => {
      file.abort()
    }
    const preview = () => {
      emit('preview', fileValue.value)
    }
    const download = () => {
      const download = document.createElement('a')
      download.setAttribute('href', getValueByTemplate(realUrl.value, store.apiConfig) + '?response-content-type=application/octet-stream')
      const fileName = fileValue.value.name || fileValue.value.url.split('?')[0].split('/').pop()
      download.setAttribute('download', fileName)
      download.click()
    }
    const remove = async () => {
      removeLoading.value = true
      const name = fileValue.value.name
      const message = name ? `确认删除《${name}》` : '确认删除'
      try {
        isMobile
          ? await VanDialog.confirm({
            title: '警告',
            message: `确认删除《${fileValue.value.name}》`
          })
          : await ElMessageBox.confirm(message, '警告', {
            type: 'warning'
          })
        emit('after-remove', fileValue.value)
      } catch (error) {
        console.info('用户取消删除')
      } finally {
        removeLoading.value = false
      }
    }
    return {
      realUrl,
      cancelUpload,
      preview,
      download,
      remove,
      removeLoading,
      parseFileSize
    }
  }
}
</script>
<style lang="less">
.img-row {
  box-sizing: border-box;
  width: 80px;
  height: 80px;
  margin: 0 8px 8px 0;
  padding: 0;
  display: inline-flex;
  overflow: visible;
  border-radius: 2px;
  border: 0;
  background-color: var(~'--@{elNamespace}-fill-color-lighter');
  position: relative;
  .@{elNamespace}-progress__text {
      display: none;
  }
  .upload--close {
    line-height: 0;
    display: none;
    cursor: pointer;
    position: absolute;
    right: -6px;
    top: -6px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background-color: #fff;
    z-index: 1001;
    i{
      font-size: 16px;
      color: var(~'--@{elNamespace}-color-danger');
      position: relative;
      top: -1px;
      left: -1px;
      width: 16px;
      height: 16px;
    }
  }
  &:hover {
    .handle-button-wrapper {
      opacity: 1;
      z-index: 1000;
    }
    .upload--close {
      display: block;
    }
  }

  &__img-item {
    width: 100%;
    height: 100%;
  }

  &__process {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .handle-button-wrapper {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    justify-content: center;
    color: #fff;
    opacity: 0;
    font-size: 20px;
    transition: opacity .3s;
    background-color: rgba(0, 0, 0, .3);
    display: flex;
    align-items: center;
    i {
      font-size: 22px;
      color: #fff;
      margin: 0 4px;
      cursor: pointer;
    }
  }

  &__button {
    cursor: pointer;
    flex-shrink: 0;

    &+.file-row__button {
      margin-left: 4px;
    }
  }

  .@{elNamespace}-progress {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .progress-cancel {
    position: absolute;
    top: 50%;
    transform: translate(0, 10px);
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;

    .@{elNamespace}-button {
      padding-top: 60px;
    }
  }
}
</style>
