<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' : '', file[bottomTextKey] ? 'hasfoot' : '']"
      :key="file.uid"
      tabindex="0"
      @keydown.delete="!disabled && $emit('remove', file)"
      @focus="focusing = true"
      @blur="focusing = false"
      @click="focusing = false"
    >
      <slot :file="file">
        <div :class="{'el-upload-list__item-hover-flex': renderFileName, 'el-upload-list__item-hover': true}">
          <img
              class="el-upload-list__item-thumbnail"
              v-if="file.status !== 'uploading' && ['picture-card', 'picture'].indexOf(listType) > -1"
              :src="file.url" alt=""
          >
          <el-tooltip effect="light" :content="file.errorText || '上传失败'" placement="top" :disabled="file.status !== 'fail'">
            <a :class="{'el-upload-list__item-name': true, 'el-upload-list__item-name-hasRender': renderFileName}" @click="handleClick(file)">
              <i v-if="file.status === 'uploading' && listType === 'text'" class="el-icon-loading"></i>
              <svg-dom class="el-upload-list__item-svg" :svgName="fileIcon(file.name)"></svg-dom>
              <template v-if="file.status !== 'fail'">
                <tooltip-over
                    :content="file.name"
                >
                </tooltip-over>
              </template>
              <template v-else>
                {{file.name}}
              </template>
            </a>
          </el-tooltip>
          <template v-if="renderFileName">
            <span class="el-upload-list__item-render">{{renderFileName ? renderFileName(file) : null}}</span>
          </template>
          <label class="el-upload-list__item-status-label">
            <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 v-if="showDownloadIcon && listType === 'text'" class="el-icon-download" @click="handleClickDownload(file)"></i>
          <i :class="{'el-icon-delete-text': listType === 'text', 'el-icon-close': ['picture-card', 'picture'].indexOf(listType) > -1}" v-if="!disabled" @click="$emit('remove', file)">{{listType === 'text' ? '删除' : ''}}</i>
          <el-progress
              v-if="file.status === 'uploading'"
              :class="{'el-progress-texttype': listType === 'text'}"
              :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)"
          >
            <el-button type="text">删除</el-button>
          </span>
        </span>
        </div>

      </slot>
      <div v-if="file[bottomTextKey]" class="el-upload-list__item-footer"><span>{{file[bottomTextKey]}}</span></div>
    </li>
  </transition-group>
</template>
<script>
  import Locale from 'element-ui-for-gov/src/mixins/locale';
  import ElProgress from 'element-ui-for-gov/packages/progress';
  import ElTooltip from 'element-ui-for-gov/packages/tooltip';
  import ElButton from 'element-ui-for-gov/packages/button';
  import TooltipOver from './tooltip-over';
  import SvgDom from './svg.js';
  export default {

    name: 'ElUploadList',

    mixins: [Locale],

    data() {
      return {
        focusing: false
      };
    },
    components: { ElProgress, SvgDom, ElTooltip, ElButton, TooltipOver },

    props: {
      files: {
        type: Array,
        default() {
          return [];
        }
      },
      disabled: {
        type: Boolean,
        default: false
      },
      renderFileName: Function,
      handlePreview: Function,
      listType: String,
      showDownloadIcon: Boolean,
      handleDownload: Function,
      bottomTextKey: String
    },
    methods: {
      parsePercentage(val) {
        return parseInt(val, 10);
      },
      handleClick(file) {
        this.handlePreview && this.handlePreview(file);
      },
      fileIcon(name) {
        let suffix = name.substr(name.lastIndexOf('.') + 1);
        return {
          'jpeg': 'picture',
          'jpg': 'picture',
          'png': 'picture'
        }[suffix] || suffix;
      },
      handleClickDownload(file) {
        this.handleDownload && this.handleDownload(file);
      }
    }
  };
</script>
