<template>
  <transition-group
    tag="ul"
    :class="[
      'el-upload-list',
      'el-upload-list--' + listType,
      { 'is-disabled': disabled },
    ]"
    name="el-list"
  >
    <li
      v-for="file in files"
      :class="[
        'el-upload-list__item',
        'is-' + file.status,
        focusing ? 'focusing' : '',
      ]"
      :key="file.uid"
      tabindex="0"
      @keydown.delete="!disabled && $emit('remove', file)"
      @focus="focusing = true"
      @blur="focusing = false"
      @click="focusing = false"
    >
      <slot :file="file">
        <img
          class="el-upload-list__item-thumbnail"
          v-if="
            file.status !== 'uploading' &&
            ['picture-card', 'picture'].indexOf(listType) > -1
          "
          :src="file.url"
          alt=""
        />
        <a class="el-upload-list__item-name" @click="handleClick(file)">
          <i class="bi-iconfont-attach-2"></i><span>{{ file.name }}</span>
        </a>
        <label class="el-upload-list__item-status-label">
          <i
            :class="{
              'bi-iconfont-upload-success': true,
              'bi-iconfont-check-circle-fill':
                listType === 'text' && file.status === 'success',
              'bi-iconfont-check':
                ['picture-card', 'picture'].indexOf(listType) > -1,
            }"
          ></i>
          <i
            :class="{
              'bi-iconfont-upload-fail': true,
              'bi-iconfont-refresh':
                listType === 'text' && file.status === 'fail',
              'bi-iconfont-close': ['picture-card', 'picture'].indexOf(listType) > -1
            }"
            @click="$emit('refresh', file.file)"
          ></i>
          <i
            :class="{
              'bi-iconfont-upload-fail': true,
              'bi-iconfont-close':
                listType === 'text' && file.status === 'fail',
            }"
            @click="$emit('remove', file)"
          ></i>
        </label>
        <i
          class="bi-iconfont-close"
          v-if="!disabled && file.status !== 'fail'"
          @click="$emit('remove', file)"
        ></i>
        <i class="el-icon-close-tip" v-if="!disabled && file.status !== 'fail'">{{
          t("el.upload.deleteTip")
        }}</i>
        <!--因为close按钮只在li:focus的时候 display, li blur后就不存在了，所以键盘导航时永远无法 focus到 close按钮上-->
        <el-progress
          v-if="file.status === 'uploading'"
          :width="70"
          :type="listType === 'picture-card' ? 'circle' : 'line'"
          :stroke-width="listType === 'picture-card' ? 6 : 2"
          :percentage="parsePercentage(file.percentage)"
        >
        </el-progress>
        <span
          class="el-upload-list__item-actions"
          v-if="listType === 'picture-card'"
        >
          <span
            class="el-upload-list__item-preview"
            v-if="handlePreview && listType === 'picture-card'"
            @click="handlePreview(file)"
          >
            <i class="el-icon-zoom-in"></i>
          </span>
          <span
            v-if="!disabled"
            class="el-upload-list__item-delete"
            @click="$emit('remove', file)"
          >
            <i class="el-icon-delete"></i>
          </span>
        </span>
      </slot>
    </li>
  </transition-group>
</template>
<script>
import Locale from 'bi-element/src/mixins/locale';
import ElProgress from 'bi-element/packages/progress';

export default {
  name: 'ElUploadList',

  mixins: [Locale],

  data() {
    return {
      focusing: false
    };
  },
  components: { ElProgress },

  props: {
    files: {
      type: Array,
      default() {
        return [];
      }
    },
    disabled: {
      type: Boolean,
      default: false
    },
    handlePreview: Function,
    listType: String
  },
  methods: {
    parsePercentage(val) {
      return parseInt(val, 10);
    },
    handleClick(file) {
      this.handlePreview && this.handlePreview(file);
    }
  }
};
</script>
