<template>
  <transition-group
    tag="ul"
    :class="[
      'hn-upload-list',
      'hn-upload-list--' + listType,
      { 'is-disabled': disabled }
    ]"
    name="hn-list"
  >
    <li
      v-for="file in files"
      :class="['hn-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"
    >
      <img
        class="hn-upload-list__item-thumbnail"
        v-if="file.status !== 'uploading' && ['picture-card', 'picture'].indexOf(listType) > -1"
        :src="file.url" alt=""
      >
      <a class="hn-upload-list__item-name" @click="handleClick(file)">
        <!-- 添加文件附加信息 -->
        <p class="hn-upload-list__item-name__wrap">
          <i class="hn-icon-document"></i>{{file.name}}
        </p>
        <slot :file="file"></slot>
      </a>
      <label class="hn-upload-list__item-status-label">
        <i :class="{
          'hn-icon-upload-success': true,
          'hn-icon-circle-check': listType === 'text',
          'hn-icon-check': ['picture-card', 'picture'].indexOf(listType) > -1
        }"></i>
      </label>
      <i class="hn-icon-close" v-if="!disabled" @click="$emit('remove', file)"></i>
      <i class="hn-icon-close-tip" v-if="!disabled">{{ t('hn.upload.deleteTip') }}</i> <!--因为close按钮只在li:focus的时候 display, li blur后就不存在了，所以键盘导航时永远无法 focus到 close按钮上-->
      <hn-progress
        v-if="file.status === 'uploading'"
        :type="listType === 'picture-card' ? 'circle' : 'line'"
        :stroke-width="listType === 'picture-card' ? 6 : 2"
        :percentage="parsePercentage(file.percentage)">
      </hn-progress>
      <span class="hn-upload-list__item-actions" v-if="listType === 'picture-card'">
        <span
          class="hn-upload-list__item-preview"
          v-if="handlePreview && listType === 'picture-card'"
          @click="handlePreview(file)"
        >
          <i class="hn-icon-zoom-in"></i>
        </span>
        <span
          v-if="!disabled"
          class="hn-upload-list__item-delete"
          @click="$emit('remove', file)"
        >
          <i class="hn-icon-delete"></i>
        </span>
      </span>
    </li>
  </transition-group>
</template>
<script>
  import Locale from 'heiner-ui/src/mixins/locale';
  import HnProgress from 'heiner-ui/packages/progress';

  export default {

    name: 'HnUploadList',

    mixins: [Locale],

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

    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>
