<template>
  <DialogLayout
    title="上传资源"
    width="680px"
    class="upload_dialog"
    :visible="$store.upload_dialog.visible"
    :actionBarOption="{ noCencel: true, right: true, confirmTxt: '关闭' }"
    @onConfirm="closeDialog"
    @onCancel="closeDialog"
  >
    <div class="upload_item">
      <div class="upload_label">上传说明：</div>
      <div class="upload_ctx ml-12">
        <p>文本文件：.doc、.docx、.pdf</p>
        <p>音频文件：.mp3、.acc、.flac等</p>
        <p>表格文件：.xls、.xlsx</p>
        <p>视频文件：.mp4、.avi、.mkv、.wmv等</p>
        <p>演示文稿：.ppt、.pptx</p>
        <p>图片文件：.jpg、.jepg、.bmp、.png、.gif等</p>
        <p>音频文件、视频文件不得超过500MB，其他文件不得超过200MB；</p>
      </div>
    </div>

    <div class="upload_item flex-center">
      <div class="upload_label">上传文件：</div>
      <div class="upload_ctx ml-12">
        <button class="upload_button el-icon-plus" @click="toUpload">
          添加文件
        </button>
      </div>
    </div>

    <div class="upload_label mt-12 mb-12">上传详情：</div>
    <TableLayout
      height="300"
      :column="column"
      :data="$store.upload_dialog.tasks"
    >
      <template v-slot:size="{ row }">{{ row.size?.formatFileSize() }}</template>
      <template v-slot:progress="{ row }">
        <div class="flex-h flex-center" :class="'status_' + row.status">
          <div
            class="upload_progress"
            :style="`--progress:${row.progress}%`"
          ></div>
          <div
            class="ml-12 mr-12 progress_status"
            :class="[,,'el-icon-success','el-icon-error'][row.status]"
            :data-progress="row.progress+'%'"
          />
        </div>
      </template>
    </TableLayout>
  </DialogLayout>
</template>

<script>
import { DialogLayout, TableLayout } from '@/components/layout'

export default {
  name: 'UploadDialog',

  components: { DialogLayout, TableLayout },

  data: () => ({ res_list: [{ progress: 30 }] }),

  created () {
    this.column = [
      { prop: 'name', label: '文件名' },
      { prop: 'type', label: '文件类型' },
      { prop: 'size', label: '文件大小' },
      { prop: 'progress', label: '上传进度', width: '200px' }
    ]
  },

  methods: {
    toUpload () {
      this.$store.upload()
    },

    closeDialog () {
      this.$store.upload_dialog.visible = false
      this.$store.upload_dialog.form_data = undefined
      this.$emit('onCloseUploadDialog')
    }
  }
}
</script>

<style lang="scss">
.upload_dialog {
  .upload_item {
    padding: 16px 0;
    display: flex;
  }

  .upload_label {
    color: $--color-text-primary;
    font-weight: bold;
  }

  .upload_ctx {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    color: $--color-text-secondary;

    p {
      min-width: 38%;
      margin-bottom: 16px;
    }
  }

  .upload_progress {
    width: 120px;
    height: 8px;
    background-color: #e5e5e5;
    border-radius: 12px;
    overflow: hidden;

    &::after {
      content: '';
      display: block;
      width: var(--progress);
      height: 100%;
      border-radius: inherit;
      background-color: $--color-primary;
    }
  }

  .status_3 .upload_progress::after {
    background-color: $--color-danger;
  }

  .progress_status {
    color: $--color-primary;

    &::before {
      font-size: 16px;
    }

    &.el-icon-error {
      color: $--color-danger;
    }
  }

  .status_1 .progress_status::before {
    content: attr(data-progress);
    font-size: 12px;
  }

  .upload_button {
    padding: 0 8px;
    line-height: 30px;
    border: 1px dashed $--color-primary;
    color: $--color-primary;
    background-color: transparent;
    border-radius: 5px;
    transition: 0.2s transform;
    cursor: pointer;

    &::before {
      font-weight: bold;
    }

    &:hover {
      transform: scale(1.06);
    }
  }
}
</style>
