<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"
      :style="listType!=='file-list' ? {width:width,height: height} : ''"
      tabindex="0"
      @keydown.delete="!disabled && !readonly && $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','file-card'].indexOf(listType) > -1 && !file.icon"
          :src="file.url" alt=""
        >
        <span class="el-upload-list__item-icon" v-if="listType === 'file-card' && file.icon">
          <i :class="['thumbnail-icon',file.icon]" v-if="!disabled"></i>
        </span>
        <a class="el-upload-list__item-name" @click="handleClick(file)">
          <i :class="[file.icon ? file.icon :'el-icon-document']"></i>{{file.name}}
        </a>
        <label class="el-upload-list__item-status-label" v-if="!readonly && previewable">
          <i :class="{
            'el-icon-upload-success': true,
            'el-icon-circle-check': listType === 'text',
            'el-icon-check': ['picture-card', 'picture'].indexOf(listType) > -1
          }"></i>
        </label>
        <i class="el-icon-close" v-if="!disabled && !readonly" @click="$emit('remove', file)"></i>
        <i class="el-icon-close-tip" v-if="!disabled && !readonly">{{ t('el.upload.deleteTip') }}</i>
        <bml-progress
          v-if="file.status === 'uploading'"
          :type="listType === 'picture-card' ? 'circle' : 'line'"
          :stroke-width="listType === 'picture-card' ? 6 : 2"
          :percentage="parsePercentage(file.percentage)">
        </bml-progress>
        <span class="el-upload-list__item-actions" v-if="viewActions">
          <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="editable && !readonly"
            class="el-upload-list__item-delete"
            @click="handleEdit(file)"
          >
            <i class="el-icon-full-screen"></i>
          </span>
          -->
          <span
            v-if="!disabled && !readonly"
            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 'bml-workstand-ui/src/mixins/locale';
  import BmlProgress from 'bml-workstand-ui/packages/progress';

  export default {

    name: 'BmlUploadList',

    mixins: [Locale],

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

    props: {
      files: {
        type: Array,
        default() {
          return [];
        }
      },
      editable: {
        type: Boolean,
        default: true
      },
      viewActions:{
        type: Boolean,
        default: true
      },
      previewable: {
        type: Boolean,
        default: true
      },
      readonly: {
        type: Boolean,
        default: false
      },
      disabled: {
        type: Boolean,
        default: false
      },
      limit:{
        type: Number,
        default: 0
      },
      width: {
        type: String,
        default: '196px'
      },
      height: {
        type: String,
        default: '196px'
      },
      handlePreview: Function,
      handleEdit: Function,
      listType: String
    },
    methods: {
      parsePercentage(val) {
        return parseInt(val, 10);
      },
      handleClick(file) {
        this.handlePreview && this.handlePreview(file);
      }
    }
  };
</script>
