<template>
  <ul :class="['el-upload-list', 'el-upload-list--' + listType, { 'is-disabled': disabled }]">
    <li
      v-for="(file, index) in files"
      :class="['el-upload-list__item', 'is-' + file.status, focusing ? 'focusing' : '']"
      :key="`${file.name}-${index}`"
      tabindex="0"
      @keydown.delete="!disabled && $emit('remove', file)"
      @focus="focusing = true"
      @blur="focusing = false"
      @click.stop="focusing = false"
    >
      <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.stop="handleClick(file)">
        <fa-icon
          :icon="['far', 'file-alt']"
          class="u-mr2 u-text--light">
        </fa-icon>{{file.name}}
      </a>
      <label class="el-upload-list__item-status-label">
        <fa-icon
          :icon="listSuccessIcon"
          :class="statusIconInPicture ? 'el-upload-list__picture-icon' : 'u-text--success'"
          fixed-width>
        </fa-icon>
      </label>
      <fa-icon
        :icon="['fal', 'times']"
        v-if="!disabled"
        @click.stop="$emit('remove', file)"
        fixed-width>
      </fa-icon>
      <i class="el-upload-list__close-tip" v-if="!disabled">{{ t('el.upload.deleteTip') }}</i>
      <el-progress
        v-if="file.status === 'uploading'"
        :type="listType === 'picture-card' ? 'circle' : 'bar'"
        :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.stop="handlePreview(file)"
        >
          <fa-icon :icon="['fal', 'search-plus']"></fa-icon>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click.stop="$emit('remove', file)"
        >
          <fa-icon :icon="['fal', 'trash-alt']"></fa-icon>
        </span>
      </span>
    </li>
  </ul>
</template>
<script>
  import Locale from 'element-ui/src/mixins/locale';
  import ElProgress from 'element-ui/packages/progress';

  export default {
    mixins: [Locale],
  
    name: 'ElUploadList',

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

    props: {
      files: {
        type: Array,
        default() {
          return [];
        }
      },
      disabled: {
        type: Boolean,
        default: false
      },
      handlePreview: Function,
      listType: String
    },

    computed: {
      statusIconInPicture() {
        return ['picture-card', 'picture'].indexOf(this.listType) > -1;
      },
      listSuccessIcon() {
        if (this.listType === 'text') return 'check-circle';
        if (this.statusIconInPicture) return 'check';
        return '';
      }
    },

    methods: {
      parsePercentage(val) {
        return parseInt(val, 10);
      },
      handleClick(file) {
        this.handlePreview && this.handlePreview(file);
      }
    }
  };
</script>
