<template>
  <transition-group
    tag="ul"
    :class="[
      drag ? 'is-drag' : '',
      'xb-upload-list',
      'xb-upload-list--' + listType,
      { 'is-disabled': disabled}
    ]"
    name="xb-list"
  >
    <li
      v-for="(file, index) in files"
      :class="['xb-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="xb-upload-list__item-thumbnail"
          v-if="file.status !== 'uploading' && ['picture-card', 'picture'].indexOf(listType) > -1"
          :src="file.url"
          alt
        />
        <i :class="['xb_icon_'+docIcon(file), 'xb-icon-drag']" v-if="drag"></i>
        <a class="xb-upload-list__item-name" @click="handleClick(file)">
          <i class="xb_icon_pin"></i>
          <span>{{file.name}}</span>
          <i
            class="xb_icon_refresh"
            v-if="file.status === 'fail'"
            @click="$emit('refresh', file.raw, index)"
          ></i>
        </a>
        <label class="xb-upload-list__item-status-label">
          <i
            :class="{
            'xb-icon-upload-success': true,
            'xb_icon_correct_facet': listType === 'text',
            'xb_icon_correct_linear': ['picture-card', 'picture'].indexOf(listType) > -1
          }"
          ></i>
        </label>
        <i class="xb_icon_delete" v-if="!disabled" @click="$emit('remove', file)"></i>
        <i class="xb-icon-close-tip" v-if="!disabled">{{ t('lang.upload.deleteTip') }}</i>
        <xb-progress
          v-if="file.status === 'uploading' || file.status === 'fail'"
          :type="listType === 'picture-card' ? 'circle' : 'line'"
          :stroke-width="listType === 'picture-card' ? 6 : 4"
          :color="file.status === 'uploading' ? '#67C23A':''"
          :status="file.status === 'fail' ? 'exception':null"
          :percentage="file.status === 'fail' ? 100 : parsePercentage(file.percentage)"
        ></xb-progress>
        <span class="xb-upload-list__item-actions" v-if="listType === 'picture-card'">
          <span
            class="xb-upload-list__item-preview"
            v-if="handlePreview && listType === 'picture-card'"
            @click="handlePreview(file)"
          >
            <i class="xb_icon_doubt"></i>
          </span>
          <span v-if="!disabled" class="xb-upload-list__item-delete" @click="$emit('remove', file)">
            <i class="xb_icon_delete"></i>
          </span>
        </span>
      </slot>
    </li>
  </transition-group>
</template>
<script>
import Locale from 'xinbao-ui/src/mixins/locale';
import XbProgress from 'xinbao-ui/packages/progress';

export default {

  name: 'XbUploadList',

  mixins: [Locale],

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

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

  },
  methods: {
    parsePercentage(val) {
      return parseInt(val, 10);
    },
    handleClick(file) {
      this.handlePreview && this.handlePreview(file);
    },
    docIcon(file) {
      let docSuffix = file.name.split('.').slice(-1)[0];
      let icon = '';
      switch (docSuffix) {
        case 'doc':
          icon = 'word';
          break;
        case 'docx':
          icon = 'word';
          break;
        case 'xls':
          icon = 'excel';
          break;
        case 'xlsx':
          icon = 'excel';
          break;
        case 'pdf':
          icon = 'pdf';
          break;
        default:
          icon = 'file';
      }
      return icon;
    }
  }
};
</script>
